00001 #if !defined(_TRACELOG_H_) 00002 #define _TRACELOG_H_ 00003 00004 #if _MSC_VER > 1000 00005 #pragma once 00006 #endif // _MSC_VER > 1000 00007 00008 #include <iostream> 00009 #include <fstream> 00010 #include <vector> 00011 #include <strstream> 00012 #include <sstream> 00013 #include <time.h> 00014 #include <string> 00015 using namespace std; 00016 00052 //static 00053 class TraceLog { 00054 protected: 00055 fstream logFile ; 00056 std::vector<string> messages ; 00057 int lastPrint ; 00058 time_t lasttime ; 00059 bool timing ; 00060 00061 public: 00062 TraceLog() { 00063 lastPrint = 0 ; 00064 time (&lasttime) ; 00065 00066 // *** EDIT HERE 00067 // set to true to enable output of timing 00068 timing = false ; 00069 00070 // *** EDIT HERE 00071 // uncomment the following line to enable this functionality 00072 //logFile.open ("glimsview_log.txt", fstream::out) ; 00073 } 00074 00075 virtual ~TraceLog() { 00076 writeLog() ; 00077 } 00078 00083 void log (char * msg) { 00084 if (logFile.is_open()) { 00085 time_t curtime ; 00086 time (&curtime) ; 00087 double dtime = difftime (curtime, lasttime) ; 00088 00089 std::stringstream msgstream ; 00090 if (timing) { 00091 msgstream << ctime(&curtime) << " (" 00092 << dtime << ") " 00093 << msg << endl ; 00094 } 00095 else { 00096 msgstream << msg ; 00097 } 00098 // messages.push_back (msgstream.str()) ; 00099 // writeLogNow () ; // enable for getting trace on crashing bugs 00100 writeLogRightNow (msgstream.str()) ; // enable for getting trace on crashing bugs w/o saving history in memory 00101 lasttime = curtime ; 00102 } 00103 } 00104 00109 void log (string msg) { 00110 if (logFile.is_open()) { 00111 time_t curtime ; 00112 time (&curtime) ; 00113 double dtime = difftime (curtime, lasttime) ; 00114 00115 std::stringstream msgstream ; 00116 if (timing) { 00117 msgstream << ctime(&curtime) << " (" 00118 << dtime << ")\n " 00119 << msg << endl ; 00120 } 00121 else { 00122 msgstream << msg ; 00123 } 00124 // messages.push_back (msgstream.str()) ; 00125 // writeLogNow () ; // enable for getting trace on crashing bugs 00126 writeLogRightNow (msgstream.str()) ; // enable for getting trace on crashing bugs w/o saving history in memory 00127 lasttime = curtime ; 00128 } 00129 } 00130 00134 void writeLog () { 00135 if (logFile.is_open()) { 00136 logFile << endl << "GLIMSView Log File" << endl ; 00137 logFile << "Num Msg: " << (int)messages.size() << endl ; 00138 for (int i = 0 ; i < (int)messages.size() ; i++ ) { 00139 logFile << i << " " << messages[i] << endl ; 00140 } 00141 logFile.close() ; 00142 } 00143 } 00144 00148 void writeLogNow () { 00149 if (logFile.is_open()) { 00150 int i = 0 ; 00151 for ( i = lastPrint ; i < (int)messages.size() ; i++ ) { 00152 logFile << i << " " << messages[i] << endl ; 00153 } 00154 logFile << flush ; 00155 lastPrint = i ; 00156 } 00157 } 00158 00162 void writeLogRightNow (string msg) { 00163 if (logFile.is_open()) { 00164 logFile << msg << endl ; 00165 logFile << flush ; 00166 } 00167 } 00168 00169 }; 00170 00171 00172 #endif // !defined(_TRACELOG_H_) 00173 00174 00175
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |