Advertisement
natli

Untitled

Dec 9th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include "myLog.h"
  2.  
  3. const enum myLog::logLevels DEFAULT_LOG_LEVEL = myLog::LEVEL_1;
  4.  
  5. myLog::myLog()
  6. {
  7.    initVars();
  8.    init();
  9. }
  10.  
  11. myLog::myLog(const string& fileName)
  12. {
  13.     initVars();
  14.     init(fileName);
  15. }
  16.  
  17. myLog::myLog(const string& fileName,enum logLevels levelIn)
  18. {
  19.     initVars();
  20.     logLevel = levelIn;
  21.     init(fileName);
  22. }
  23.  
  24. myLog::~myLog()
  25. {
  26.    if ( logLevel<QUIET_MODE )
  27.    {
  28.       clear(ios::goodbit);
  29.       *this << endl;
  30.       printHeader(1);    // add ending time to log file
  31.    }
  32.    close();
  33. }
  34.  
  35. void myLog::init(const string& fileName)
  36. {
  37.    if ( (fileName.c_str())[0] )
  38.       openLog(fileName,LOG_WRITE);
  39.    else
  40.       openLog("syslog.log",LOG_WRITE);
  41. }
  42.  
  43. void myLog::init(const string& fileName, int mode)
  44. {
  45.    if ( (fileName.c_str())[0] )
  46.       openLog(fileName,mode);
  47.    else
  48.       openLog("syslog.log", mode);
  49. }
  50.  
  51. void myLog::init()
  52. {
  53.    openLog("syslog.log",LOG_WRITE);
  54. }
  55.  
  56. void myLog::openLog(const string& fileName, int mode)
  57. {
  58.    if (logLevel < QUIET_MODE)
  59.    {
  60.       open(fileName.c_str(),mode);
  61.  
  62.       // fail() returns a null zero when operation fails
  63.       // rc = (*this).fail();
  64.  
  65.       if ( fail() == 0 )
  66.       {
  67.          logName = fileName;
  68.          printHeader(0);         // insert start time into top of log file
  69.       }
  70.       else
  71.       {
  72.          cout << "ERROR: Log file " << fileName.c_str()
  73.               << " could not be opened for write access." << endl;
  74.          logLevel = QUIET_MODE;
  75.       }
  76.    }
  77.    else
  78.    {
  79.       cout << "Logging disabled (QUIET_MODE set)" << endl;
  80.    }
  81. }
  82.  
  83. void myLog::initVars()
  84. {
  85.    time(&startTime);
  86.    logLevel = DEFAULT_LOG_LEVEL;
  87. }
  88.  
  89. void myLog::printHeader(int theEnd)
  90. {
  91.    if ( logLevel < QUIET_MODE )
  92.    {
  93.       clear(ios::goodbit);
  94.  
  95.       // setup time
  96.       time_t sttime;
  97.       time(&sttime);
  98.  
  99.       // convert to gm time
  100.       struct tm * tim = gmtime(&sttime);
  101.  
  102.       // set data items
  103.       int sec  = tim->tm_sec;           // second (0-61, allows for leap seconds)
  104.       int min  = tim->tm_min;           // minute (0-59)
  105.       int hour = tim->tm_hour;          // hour (0-23)
  106.  
  107.       int mon  = tim->tm_mon + 1;       // month (0-11)
  108.       int mday = tim->tm_mday;          // day of the month (1-31)
  109.       int year = tim->tm_year % 100;    // years since 1900
  110.  
  111.       char cur_time[9];
  112.       char cur_date[9];
  113.  
  114.       sprintf(cur_time,"%02d:%02d:%02d",hour,min,sec);
  115.       sprintf(cur_date,"%02d/%02d/%02d",mon,mday,year);
  116.  
  117.       char line_1[61];
  118.       sprintf(line_1,"A yuChen Technology Development Work Log File");
  119.  
  120.       char line_2[61];
  121.       sprintf(line_2,"DATE: %s - %s%30s",cur_date,cur_time,logName.c_str());
  122.  
  123.       *this << line_1 << endl;
  124.       *this << line_2 << endl << endl;
  125.  
  126.       if (theEnd) *this << getExecTime() << endl;
  127.    }
  128.  
  129. }
  130.  
  131. void myLog::getExecTime(int* min, int* sec)
  132. {
  133.    time_t endTime;
  134.    time(&endTime);
  135.    *min = (int)((endTime - startTime)/60);
  136.    *sec = (int)((endTime - startTime)%60);
  137. }
  138.  
  139. char* myLog::getExecTime()
  140. {
  141.     int min = 0;
  142.     int sec = 0;
  143.     getExecTime(&min, &sec);
  144.     static char execTime[128];
  145.     sprintf(execTime, "Execution time: %d minutes %d seconds", min, sec);
  146.     return execTime;
  147. }
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement