| [5284] | 1 | /// ----------------------------------------*- mode: C++; -*-- | 
|---|
|  | 2 | /// @file logfile.h | 
|---|
|  | 3 | /// Implementation of a logging stream | 
|---|
|  | 4 | /// ---------------------------------------------------------- | 
|---|
|  | 5 | /// $Id: logfile.h 2549 2007-04-02 22:17:37Z bless $ | 
|---|
|  | 6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/logfile.h $ | 
|---|
|  | 7 | // =========================================================== | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Copyright (C) 2005-2007, all rights reserved by | 
|---|
|  | 10 | // - Institute of Telematics, Universitaet Karlsruhe (TH) | 
|---|
|  | 11 | // | 
|---|
|  | 12 | // More information and contact: | 
|---|
|  | 13 | // https://projekte.tm.uka.de/trac/NSIS | 
|---|
|  | 14 | // | 
|---|
|  | 15 | // This program is free software; you can redistribute it and/or modify | 
|---|
|  | 16 | // it under the terms of the GNU General Public License as published by | 
|---|
|  | 17 | // the Free Software Foundation; version 2 of the License | 
|---|
|  | 18 | // | 
|---|
|  | 19 | // This program is distributed in the hope that it will be useful, | 
|---|
|  | 20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 22 | // GNU General Public License for more details. | 
|---|
|  | 23 | // | 
|---|
|  | 24 | // You should have received a copy of the GNU General Public License along | 
|---|
|  | 25 | // with this program; if not, write to the Free Software Foundation, Inc., | 
|---|
|  | 26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|---|
|  | 27 | // | 
|---|
|  | 28 | // =========================================================== | 
|---|
|  | 29 | #ifndef _logfile_h_ | 
|---|
|  | 30 | #define _logfile_h_ | 
|---|
|  | 31 |  | 
|---|
|  | 32 |  | 
|---|
| [10075] | 33 | #define _NO_LOGGING | 
|---|
| [5284] | 34 |  | 
|---|
|  | 35 | #include <fstream> // file stream | 
|---|
|  | 36 | #include <iostream> | 
|---|
|  | 37 |  | 
|---|
|  | 38 | #include <string.h> | 
|---|
|  | 39 | #include <pthread.h> | 
|---|
|  | 40 | #include <sys/time.h> | 
|---|
|  | 41 | #include <stdio.h> // sprintf | 
|---|
|  | 42 | #include <unistd.h> // getpid | 
|---|
|  | 43 |  | 
|---|
|  | 44 | #include "protlib_types.h" | 
|---|
|  | 45 |  | 
|---|
|  | 46 | namespace protlib { | 
|---|
|  | 47 |  | 
|---|
|  | 48 | using namespace std; | 
|---|
| [9686] | 49 | /** @addtogroup protlib | 
|---|
| [5284] | 50 | * @{ | 
|---|
|  | 51 | */ | 
|---|
|  | 52 |  | 
|---|
|  | 53 | namespace log { | 
|---|
|  | 54 |  | 
|---|
| [9991] | 55 | static const pthread_mutex_t initlogmutex= | 
|---|
|  | 56 | #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP | 
|---|
|  | 57 | PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; | 
|---|
|  | 58 | #else | 
|---|
|  | 59 | PTHREAD_RECURSIVE_MUTEX_INITIALIZER; | 
|---|
|  | 60 | #endif | 
|---|
| [5284] | 61 |  | 
|---|
|  | 62 | #undef ERROR_LOG | 
|---|
|  | 63 | #undef WARNING_LOG | 
|---|
|  | 64 | #undef EVENT_LOG | 
|---|
|  | 65 | #undef INFO_LOG | 
|---|
|  | 66 | #undef DEBUG_LOG | 
|---|
|  | 67 | #undef LOG_TYPES | 
|---|
|  | 68 |  | 
|---|
|  | 69 | #undef LOG_ALL | 
|---|
|  | 70 | #undef LOG_EMERG | 
|---|
|  | 71 | #undef LOG_ALERT | 
|---|
|  | 72 | #undef LOG_CRIT | 
|---|
|  | 73 | #undef LOG_NORMAL | 
|---|
|  | 74 | #undef LOG_UNIMP | 
|---|
|  | 75 |  | 
|---|
|  | 76 |  | 
|---|
|  | 77 | enum logclass_t | 
|---|
|  | 78 | { | 
|---|
|  | 79 | ERROR_LOG= 0x10, | 
|---|
|  | 80 | WARNING_LOG= 0x20, | 
|---|
|  | 81 | EVENT_LOG= 0x30, | 
|---|
|  | 82 | INFO_LOG=  0x40, | 
|---|
|  | 83 | DEBUG_LOG= 0x50, | 
|---|
|  | 84 | EVERY_LOG= 0xF0, | 
|---|
|  | 85 | LOG_TYPES= 5 | 
|---|
|  | 86 | }; | 
|---|
|  | 87 |  | 
|---|
|  | 88 |  | 
|---|
|  | 89 | enum loglevel_t | 
|---|
|  | 90 | { | 
|---|
|  | 91 | LOG_EMERG=  0, | 
|---|
|  | 92 | LOG_ALERT=  1, | 
|---|
|  | 93 | LOG_CRIT=   2, | 
|---|
|  | 94 | LOG_NORMAL= 4, | 
|---|
|  | 95 | LOG_UNIMP=  8, | 
|---|
|  | 96 | LOG_ALL=   15 | 
|---|
|  | 97 | }; | 
|---|
|  | 98 |  | 
|---|
|  | 99 | // colors | 
|---|
|  | 100 |  | 
|---|
|  | 101 | enum color_t | 
|---|
|  | 102 | { | 
|---|
|  | 103 | clear, | 
|---|
|  | 104 | bold_on, | 
|---|
|  | 105 | italics_on, | 
|---|
|  | 106 | underline_on, | 
|---|
|  | 107 | inverse_on, | 
|---|
|  | 108 | strikethrough_on, | 
|---|
|  | 109 | bold_off, | 
|---|
|  | 110 | italics_off, | 
|---|
|  | 111 | underline_off, | 
|---|
|  | 112 | inverse_off, | 
|---|
|  | 113 | strikethrough_off, | 
|---|
|  | 114 | black, | 
|---|
|  | 115 | red, | 
|---|
|  | 116 | green, | 
|---|
|  | 117 | yellow, | 
|---|
|  | 118 | blue, | 
|---|
|  | 119 | magenta, | 
|---|
|  | 120 | cyan, | 
|---|
|  | 121 | white, | 
|---|
|  | 122 | fg_default, | 
|---|
|  | 123 | bg_black, | 
|---|
|  | 124 | bg_red, | 
|---|
|  | 125 | bg_green, | 
|---|
|  | 126 | bg_yellow, | 
|---|
|  | 127 | bg_blue, | 
|---|
|  | 128 | bg_magenta, | 
|---|
|  | 129 | bg_cyan, | 
|---|
|  | 130 | bg_white, | 
|---|
|  | 131 | bg_default, | 
|---|
|  | 132 | num_colors, | 
|---|
|  | 133 | off=0}; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | extern const char* color[]; | 
|---|
|  | 136 | extern const char *const ANSIcolorcode[]; | 
|---|
|  | 137 |  | 
|---|
|  | 138 |  | 
|---|
|  | 139 | #define  ERRLog(mname, logstring) Log(ERROR_LOG, LOG_NORMAL, mname, logstring) | 
|---|
|  | 140 | #define  ERRCLog(mname, logstring) Log(ERROR_LOG, LOG_CRIT, mname, logstring) | 
|---|
|  | 141 | #define  EVLog(mname, logstring) Log(EVENT_LOG, LOG_NORMAL, mname, logstring) | 
|---|
|  | 142 | #define  WLog(mname, logstring) Log(WARNING_LOG, LOG_NORMAL, mname, logstring) | 
|---|
|  | 143 | #define  ILog(mname, logstring) Log(INFO_LOG, LOG_NORMAL, mname, logstring) | 
|---|
|  | 144 | #define  DLog(mname, logstring) Log(DEBUG_LOG, LOG_NORMAL, mname, logstring) | 
|---|
|  | 145 |  | 
|---|
|  | 146 |  | 
|---|
|  | 147 | #ifndef _NO_LOGGING | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // Log(lclass, llevel, mname, logstring) | 
|---|
|  | 150 | // lclass: logclass, llevel: severitylevel, mname: module or methodname, | 
|---|
|  | 151 | // logstring: things to log in stream notation | 
|---|
|  | 152 | #define  Log(lclass, llevel, mname, logstring) do {                     \ | 
|---|
|  | 153 | if ( protlib::log::DefaultLog.should_log(lclass, llevel) ) {          \ | 
|---|
|  | 154 | using protlib::log::DefaultLog;                                     \ | 
|---|
|  | 155 | DefaultLog.logstart(lclass, llevel, mname) << logstring;            \ | 
|---|
|  | 156 | DefaultLog.logend();                                                \ | 
|---|
|  | 157 | }                                                                     \ | 
|---|
|  | 158 | } while ( false ) | 
|---|
|  | 159 |  | 
|---|
|  | 160 | #define  LogS(lclass, llevel, mname, logstring) do {                    \ | 
|---|
|  | 161 | if ( protlib::log::DefaultLog.should_log(lclass, llevel) ) {          \ | 
|---|
|  | 162 | protlib::log::DefaultLog.logstart(lclass, llevel, mname,            \ | 
|---|
|  | 163 | __FILE__, __FUNCTION__, __LINE__) << logstring;                 \ | 
|---|
|  | 164 | protlib::log::DefaultLog.logend();                                  \ | 
|---|
|  | 165 | }                                                                     \ | 
|---|
|  | 166 | } while ( false ) | 
|---|
|  | 167 |  | 
|---|
|  | 168 | #else | 
|---|
|  | 169 |  | 
|---|
|  | 170 | #define  Log(logclass, loglevel, mname, logstring) | 
|---|
|  | 171 | #define  LogS(logclass, loglevel, mname, logstring) | 
|---|
|  | 172 |  | 
|---|
|  | 173 | #endif | 
|---|
|  | 174 |  | 
|---|
|  | 175 | class logfile | 
|---|
|  | 176 | { | 
|---|
|  | 177 | private: | 
|---|
|  | 178 | ostream* logstream; | 
|---|
|  | 179 | pthread_mutex_t logmutex; | 
|---|
|  | 180 |  | 
|---|
|  | 181 | unsigned char logfilter[LOG_TYPES]; | 
|---|
|  | 182 |  | 
|---|
|  | 183 | bool usecolors; | 
|---|
|  | 184 | bool quiet_start; | 
|---|
|  | 185 |  | 
|---|
|  | 186 | const char* timenow(); | 
|---|
|  | 187 |  | 
|---|
|  | 188 | public: | 
|---|
|  | 189 |  | 
|---|
|  | 190 | logfile(const char* filename="", bool usecolors= true, bool quietstart=false); | 
|---|
|  | 191 | ~logfile(); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | bool set_dest(const char* filename, bool quiet=false); | 
|---|
|  | 194 | void set_filter(logclass_t logclass, uint8 severitylevel); | 
|---|
|  | 195 | bool should_log(logclass_t logclass, loglevel_t severitylevel); | 
|---|
|  | 196 |  | 
|---|
|  | 197 |  | 
|---|
|  | 198 | ostream& logstart(logclass_t logclass, loglevel_t severity_level, | 
|---|
|  | 199 | const string& modname, | 
|---|
|  | 200 | const char* file="", | 
|---|
|  | 201 | const char* func="", | 
|---|
|  | 202 | int line=0); | 
|---|
|  | 203 | void coloron() { usecolors=true; for (int i= 0; i<num_colors; i++) {color[i]= ANSIcolorcode[i]; } } | 
|---|
|  | 204 | void coloroff() { usecolors=false; for (int i= 0; i<num_colors; i++) {color[i]= ""; } } | 
|---|
|  | 205 |  | 
|---|
|  | 206 | void logend(); | 
|---|
|  | 207 | }; // end class logfile | 
|---|
|  | 208 |  | 
|---|
|  | 209 | extern | 
|---|
|  | 210 |  | 
|---|
|  | 211 | inline | 
|---|
|  | 212 | logfile::logfile(const char* filename,  bool usecolors, bool quietstart) | 
|---|
|  | 213 | : logstream(0), | 
|---|
|  | 214 | logmutex(initlogmutex), | 
|---|
|  | 215 | usecolors(usecolors), | 
|---|
|  | 216 | quiet_start(quietstart) | 
|---|
|  | 217 | { | 
|---|
|  | 218 | for (int i= 0; i< LOG_TYPES; i++) | 
|---|
|  | 219 | logfilter[i]= LOG_ALL; | 
|---|
|  | 220 |  | 
|---|
|  | 221 | if (strlen(filename)) | 
|---|
|  | 222 | logstream= new(nothrow) ofstream(filename); | 
|---|
|  | 223 | else | 
|---|
|  | 224 | logstream= &cout; | 
|---|
|  | 225 |  | 
|---|
|  | 226 | if (!logstream) | 
|---|
|  | 227 | { | 
|---|
|  | 228 | cerr << "Could not open logfile " << filename << endl; | 
|---|
|  | 229 | } | 
|---|
|  | 230 | else | 
|---|
|  | 231 | { | 
|---|
|  | 232 | pthread_mutex_lock(&logmutex); | 
|---|
|  | 233 |  | 
|---|
|  | 234 | // if enable colors, use ANSI code, | 
|---|
|  | 235 | // if disable colors, so replace all strings with ""; | 
|---|
|  | 236 | for (int i= 0; i<num_colors; i++) | 
|---|
|  | 237 | { | 
|---|
|  | 238 | color[i]= (usecolors ? ANSIcolorcode[i] : ""); | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 | if (!quiet_start && logstream) | 
|---|
|  | 242 | { | 
|---|
|  | 243 | //       (*logstream) << color[blue] << timenow() | 
|---|
|  | 244 | //                 << '[' << getpid() << "] >>>>>>>>>>>>>>>>>>>>>>>> *** LOG START *** >>>>>>>>>>>>>>>>>>>>>>>>" | 
|---|
|  | 245 | //                 << color[off] << endl; | 
|---|
|  | 246 | } | 
|---|
|  | 247 | pthread_mutex_unlock(&logmutex); | 
|---|
|  | 248 | } | 
|---|
|  | 249 | } | 
|---|
|  | 250 |  | 
|---|
|  | 251 |  | 
|---|
|  | 252 | inline | 
|---|
|  | 253 | logfile::~logfile() | 
|---|
|  | 254 | { | 
|---|
|  | 255 | if (logstream) | 
|---|
|  | 256 | { | 
|---|
|  | 257 | pthread_mutex_lock(&logmutex); | 
|---|
|  | 258 |  | 
|---|
|  | 259 | if ( ! quiet_start ) | 
|---|
|  | 260 | //      (*logstream) << color[blue] << timenow() << '[' << getpid() | 
|---|
|  | 261 | //      << "] <<<<<<<<<<<<<<<<<<<<<<<< *** LOG  STOP *** <<<<<<<<<<<<<<<<<<<<<<<<" | 
|---|
|  | 262 | //      << color[off] << endl; | 
|---|
|  | 263 | pthread_mutex_unlock(&logmutex); | 
|---|
|  | 264 |  | 
|---|
|  | 265 | // destroy mutex | 
|---|
|  | 266 | pthread_mutex_destroy(&logmutex); | 
|---|
|  | 267 |  | 
|---|
|  | 268 | // delete if allocated stream | 
|---|
|  | 269 | if (logstream!= &cout) | 
|---|
|  | 270 | delete logstream; | 
|---|
|  | 271 |  | 
|---|
|  | 272 | logstream= 0; | 
|---|
|  | 273 | } | 
|---|
|  | 274 | } | 
|---|
|  | 275 |  | 
|---|
|  | 276 |  | 
|---|
|  | 277 |  | 
|---|
|  | 278 | inline | 
|---|
|  | 279 | void | 
|---|
|  | 280 | logfile::set_filter(logclass_t logclass, uint8 severity_level) | 
|---|
|  | 281 | { | 
|---|
|  | 282 | uint8 logclass_index= (logclass>>4)-1; | 
|---|
|  | 283 | if (logclass_index < LOG_TYPES) | 
|---|
|  | 284 | logfilter[logclass_index]=  severity_level; | 
|---|
|  | 285 | } | 
|---|
|  | 286 |  | 
|---|
|  | 287 | inline | 
|---|
|  | 288 | bool | 
|---|
|  | 289 | logfile::should_log(logclass_t logclass, loglevel_t severity_level) | 
|---|
|  | 290 | { | 
|---|
|  | 291 | return severity_level <= logfilter[(logclass>>4)-1]; | 
|---|
|  | 292 | } | 
|---|
|  | 293 |  | 
|---|
|  | 294 |  | 
|---|
|  | 295 | /** | 
|---|
|  | 296 | * returns current time in static char array | 
|---|
|  | 297 | * @return pointer to static character array that contains current time | 
|---|
|  | 298 | */ | 
|---|
|  | 299 | inline | 
|---|
|  | 300 | const char* logfile::timenow() | 
|---|
|  | 301 | { | 
|---|
|  | 302 | static time_t t; | 
|---|
|  | 303 | static struct timeval now; | 
|---|
|  | 304 | static char timestr[32]= ""; | 
|---|
|  | 305 | static char msecstr[6]; | 
|---|
|  | 306 |  | 
|---|
|  | 307 | gettimeofday(&now,NULL); | 
|---|
|  | 308 | t= now.tv_sec; | 
|---|
|  | 309 | strftime(timestr, sizeof(timestr)-sizeof(msecstr), "%Y-%m-%d %H:%M:%S.", localtime(&t)); | 
|---|
|  | 310 | snprintf(msecstr,sizeof(msecstr),"%03lu",now.tv_usec/1000UL); | 
|---|
|  | 311 | strcat(timestr,msecstr); | 
|---|
|  | 312 | return timestr; | 
|---|
|  | 313 | } | 
|---|
|  | 314 |  | 
|---|
|  | 315 | inline | 
|---|
|  | 316 | void logfile::logend() | 
|---|
|  | 317 | { | 
|---|
|  | 318 | if (logstream) | 
|---|
|  | 319 | { | 
|---|
|  | 320 | (*logstream) << color[off] << endl; | 
|---|
|  | 321 | } | 
|---|
|  | 322 |  | 
|---|
|  | 323 | pthread_mutex_unlock(&logmutex); | 
|---|
|  | 324 | } | 
|---|
|  | 325 |  | 
|---|
|  | 326 |  | 
|---|
|  | 327 | extern logfile& DefaultLog; | 
|---|
|  | 328 | //@} | 
|---|
|  | 329 |  | 
|---|
|  | 330 | } // end namespace log | 
|---|
|  | 331 |  | 
|---|
|  | 332 | } // end namespace protlib | 
|---|
|  | 333 |  | 
|---|
|  | 334 |  | 
|---|
|  | 335 |  | 
|---|
|  | 336 |  | 
|---|
|  | 337 |  | 
|---|
|  | 338 | /** | 
|---|
|  | 339 | * returns current time in static char array | 
|---|
|  | 340 | * @return pointer to static character array that contains current time | 
|---|
|  | 341 | */ | 
|---|
|  | 342 | inline | 
|---|
|  | 343 | const char* log_timenow(char* module, char* event) | 
|---|
|  | 344 | { | 
|---|
|  | 345 |  | 
|---|
|  | 346 | //static pthread_mutex_t timestampmutex= PTHREAD_MUTEX_INITIALIZER; | 
|---|
|  | 347 |  | 
|---|
|  | 348 |  | 
|---|
|  | 349 |  | 
|---|
|  | 350 | //pthread_mutex_lock(×tampmutex); | 
|---|
|  | 351 |  | 
|---|
|  | 352 |  | 
|---|
|  | 353 | static time_t t; | 
|---|
|  | 354 | //static struct timeval now; | 
|---|
|  | 355 | static char timestr[128]= ""; | 
|---|
|  | 356 | //static char msecstr[8]; | 
|---|
|  | 357 |  | 
|---|
|  | 358 | struct timespec tp; | 
|---|
|  | 359 |  | 
|---|
|  | 360 | clock_gettime(CLOCK_REALTIME, &tp); | 
|---|
|  | 361 |  | 
|---|
|  | 362 | //gettimeofday(&now,NULL); | 
|---|
|  | 363 | t= tp.tv_sec; | 
|---|
|  | 364 |  | 
|---|
|  | 365 | //bringing down the range to under 1 hour | 
|---|
|  | 366 | //write out microseconds | 
|---|
|  | 367 |  | 
|---|
|  | 368 | //printf("%ld\n", tp.tv_nsec); | 
|---|
|  | 369 | //printf("%ld\n", t % 3600); | 
|---|
|  | 370 |  | 
|---|
|  | 371 | sprintf(timestr, "%05ld%06ld - ", t % 3600, tp.tv_nsec/1000L); | 
|---|
|  | 372 | //snprintf(msecstr,sizeof(msecstr),"%06lu",tp.tv_nsec/1000000UL); | 
|---|
|  | 373 | strcat(timestr,module); | 
|---|
|  | 374 | strcat(timestr," - "); | 
|---|
|  | 375 | strcat(timestr,event); | 
|---|
|  | 376 |  | 
|---|
|  | 377 | //pthread_mutex_unlock(×tampmutex); | 
|---|
|  | 378 |  | 
|---|
|  | 379 | return timestr; | 
|---|
|  | 380 | } | 
|---|
|  | 381 |  | 
|---|
|  | 382 |  | 
|---|
|  | 383 |  | 
|---|
|  | 384 | #endif | 
|---|