SHARE
TWEET

Skitchin

a guest Dec 23rd, 2009 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************************************
  2.         DataLogger.h
  3.  
  4.         The data logger is used as a singleton to log various types of
  5.         information including general info, warnings, and errors.
  6. ***************************************************************************/
  7. #pragma once
  8.  
  9. #include <stdio.h>
  10. #include <Windows.h>
  11.  
  12. // Enums would be cleaner
  13. #define MESSAGE_TYPE_INFO       1
  14. #define MESSAGE_TYPE_WARING     2
  15. #define MESSAGE_TYPE_ERROR      3
  16.  
  17. class DataLogger
  18. {
  19. private:
  20.         FILE * pFile;
  21.         static DataLogger *dlLogger;
  22.         char m_chLevel;
  23.         bool m_bClearLog;
  24.  
  25. public:
  26.         DataLogger(bool bClearLog = false);
  27.         void init(bool bClearLog = false);
  28.         void setLevel(char chLevel);
  29.         static DataLogger * getInstance(bool bClearLog = false)
  30.         {
  31.                 if(!dlLogger)
  32.                 {
  33.                         dlLogger = new DataLogger(bClearLog);
  34.                 }
  35.  
  36.                 return dlLogger;
  37.         }
  38.         int addEntry( char *logdata, int iMessageType = 1 );
  39.         ~DataLogger( );
  40. };
  41.  
  42.  
  43. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  44.  
  45.  
  46.  
  47. /**************************************************************************
  48.         DataLogger.cpp
  49.  
  50.         The data logger is used as a singleton to log various types of
  51.         information including general info, warnings, and errors.
  52. ***************************************************************************
  53. ***************************************************************************/
  54.  
  55. #include "stdafx.h"
  56. #include "DataLogger.h"
  57.  
  58. DataLogger * DataLogger::dlLogger;
  59.  
  60. DataLogger::DataLogger(bool bClearLog)
  61. {
  62.         init(bClearLog);
  63. }
  64.  
  65. void DataLogger::init(bool bClearLog)
  66. {
  67.         dlLogger = NULL;
  68.         this->pFile = NULL;
  69.         CreateDirectory("../logs/", NULL);
  70.  
  71.         if(bClearLog)
  72.         {
  73.                 remove("../logs/log.txt");
  74.         }
  75.  
  76.         this->pFile = fopen( "../logs/log.txt", "a" );
  77. }
  78.  
  79. void DataLogger::setLevel(char chLevel)
  80. {
  81.         m_chLevel = chLevel;
  82. }
  83.  
  84. int DataLogger::addEntry( char *logdata, int iMessageType)
  85. {
  86.         char cEntryOut[1024];
  87.         SYSTEMTIME sysTime;
  88.         char strMessageType[10];
  89.  
  90.         // m_chLevel represents what level of filtering
  91.         // we want using flagged bits.
  92.  
  93.         if(m_chLevel == -1)
  94.         {
  95.                 return 0;
  96.         }
  97.  
  98.         if((m_chLevel >> (iMessageType-1)&0x1) == 1 || m_chLevel == 0)
  99.         {
  100.                 //MessageBox(NULL,"LOG","",MB_OK);
  101.                 if( this->pFile != NULL )
  102.                 {
  103.                         GetLocalTime( &sysTime );
  104.  
  105.                         switch(iMessageType)
  106.                         {
  107.                                 case MESSAGE_TYPE_INFO:
  108.                                         strncpy(strMessageType,"INFO",sizeof("INFO"));
  109.                                 break;
  110.  
  111.                                 case MESSAGE_TYPE_WARING:
  112.                                         strncpy(strMessageType,"WARNING",sizeof("WARNING"));
  113.                                 break;
  114.  
  115.                                 case MESSAGE_TYPE_ERROR:
  116.                                         strncpy(strMessageType,"ERROR",sizeof("ERROR"));
  117.                                 break;
  118.                         };
  119.  
  120.                         sprintf( cEntryOut, "%02d/%02d/%d %02d:%02d   %s:       %s\n", sysTime.wMonth, sysTime.wDay, sysTime.wYear, sysTime.wHour, sysTime.wMinute, strMessageType, logdata );
  121.                         this->pFile = fopen( "../logs/log.txt", "a" );
  122.                         fputs( cEntryOut, this->pFile );
  123.                         fclose( this->pFile );
  124.  
  125.                         return 0;
  126.                 }
  127.         }
  128.  
  129.         return -1;
  130. }
  131.  
  132. DataLogger::~DataLogger( )
  133. {
  134.         if(this->pFile)
  135.         {
  136.                 fclose( this->pFile );
  137.         }
  138. }
RAW Paste Data
Top