logstream.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
42 
43 #ifndef LOGSTREAMINCLUDE
44 #define LOGSTREAMINCLUDE
45 
46 #include <cstdio>
47 #include <sstream>
48 #include <string>
49 #include <iostream>
50 #include "gnsstk_export.h"
51 
52 namespace gnsstk
53 {
60 #ifndef FILELOG_MAX_LEVEL
61 #define FILELOG_MAX_LEVEL DEBUG7
62 #endif
63 
66 template <class T> class Log
67 {
68 public:
69  Log() {};
70  virtual ~Log();
72  std::ostringstream& Put(LogLevel level = INFO);
73 
75  static bool& ReportTimeTags();
77  static bool& ReportLevels();
79  static LogLevel& ReportingLevel();
81  static std::string ToString(LogLevel level);
83  static LogLevel FromString(const std::string& level);
84 
85 protected:
87  std::ostringstream os;
88 
89 #ifdef WIN32 // see kludge note below
90  GNSSTK_EXPORT static LogLevel reportingLevel;
91  GNSSTK_EXPORT static bool dumpTimeTags;
92  GNSSTK_EXPORT static bool dumpLevels;
93 #endif
94 
95 private:
96  Log(const Log&);
97  Log& operator=(const Log&);
98  std::string NowTime(void);
99 };
100 
101 template <class T> std::ostringstream& Log<T>::Put(LogLevel level)
102 {
103  if(Log<T>::ReportTimeTags()) os << NowTime() << " ";
104  if(Log<T>::ReportLevels()) {
105  os << ToString(level) << ": ";
106  // add indentation for deep debug levels
107  if(level > DEBUG) os << std::string(2*(level-DEBUG),' ');
108  }
109  return os;
110 }
111 
112 template <class T> Log<T>::~Log()
113 {
114  os << std::endl; // TD make optional?
115  T::Output(os.str());
116 }
117 
118 template <class T> bool& Log<T>::ReportLevels()
119 {
120 #ifndef WIN32 // see kludge note below
121  static bool dumpLevels = false;
122 #endif
123  return dumpLevels;
124 }
125 
126 template <class T> bool& Log<T>::ReportTimeTags()
127 {
128 #ifndef WIN32 // see kludge note below
129  static bool dumpTimeTags = false;
130 #endif
131  return dumpTimeTags;
132 }
133 
134 template <class T> LogLevel& Log<T>::ReportingLevel()
135 {
136 #ifndef WIN32 // see kludge note below
137  static LogLevel reportingLevel = INFO; // FILELOG_MAX_LEVEL;
138 #endif
139  return reportingLevel;
140 }
141 
142 template <class T> std::string Log<T>::ToString(LogLevel level)
143 {
144  // note enum LogLevel above
145  static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "VERBOSE",
146  "DEBUG","DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5", "DEBUG6", "DEBUG7" };
147  return buffer[level];
148 }
149 
150 template <class T> LogLevel Log<T>::FromString(const std::string& level)
151 {
152  // note enum LogLevel above
153  if(level == "DEBUG7") return DEBUG7;
154  if(level == "DEBUG6") return DEBUG6;
155  if(level == "DEBUG5") return DEBUG5;
156  if(level == "DEBUG4") return DEBUG4;
157  if(level == "DEBUG3") return DEBUG3;
158  if(level == "DEBUG2") return DEBUG2;
159  if(level == "DEBUG1") return DEBUG1;
160  if(level == "DEBUG") return DEBUG;
161  if(level == "VERBOSE") return VERBOSE;
162  if(level == "INFO") return INFO;
163  if(level == "WARNING") return WARNING;
164  if(level == "ERROR") return ERROR;
165  Log<T>().Put(WARNING)
166  << "Unknown logging level '" << level << "'. Using INFO level instead.";
167  return INFO;
168 }
169 
170 // time tag - platform dependent -----------------------------------------
171 //#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
172 #ifdef WIN32
173 #include <sys/timeb.h>
174 template <class T> inline std::string Log<T>::NowTime()
175 {
176  _timeb t;
177  _ftime(&t);
178  long sec=t.time;
179  double dt=double(sec)+t.millitm/1000.;
180  sec -= long(dt/86400)*86400;
181  dt = double(sec)+t.millitm/1000.;
182  char result[100] = {0};
183  int h=int(dt/3600.);
184  dt -= h*3600.;
185  int m=int(dt/60.);
186  dt -= m*60.;
187  int s=int(dt);
188  dt -= s;
189  std::sprintf(result,"%02d:%02d:%02d.%03d",h,m,s,int(dt*1000.));
190  return result;
191 }
192 
193 #else // not WIN32
194 
195 #include <sys/time.h>
196 template <class T> inline std::string Log<T>::NowTime()
197 {
198  char buffer[11];
199  time_t t;
200  time(&t);
201  tm r = {0};
202  strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r));
203  struct timeval tv;
204  gettimeofday(&tv, 0);
205  char result[100] = {0};
206  std::sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
207  return result;
208 }
209 
210 #endif //WIN32
211 
212 // ------- end class LOG
213 
275 {
276 public:
282  static std::ostream*& Stream();
283 
285  static void Output(const std::string& msg);
286 };
287 
288 inline std::ostream*& ConfigureLOGstream::Stream()
289 {
290  static std::ostream *pStream = &(std::cout);
291  return pStream;
292 }
293 
294 inline void ConfigureLOGstream::Output(const std::string& msg)
295 {
296  std::ostream *pStream = Stream();
297  if(!pStream) return;
298  *pStream << msg << std::flush;
299 }
300 
301 //----- end class ConfigureLOGstream
302 
304 class ConfigureLOG : public Log<ConfigureLOGstream> {
305 public:
306  static std::ostream*& Stream()
307  { return ConfigureLOGstream::Stream(); }
308  static LogLevel Level(const std::string& str)
309  { return FromString(str); }
310 };
311 
312 //----- end class ConfigureLOG
313 
315 #define LOG(level) \
316  if(level <= FILELOG_MAX_LEVEL && \
317  level <= ConfigureLOG::ReportingLevel() && \
318  ConfigureLOGstream::Stream()) ConfigureLOG().Put(level)
319 
320 // conveniences
321 #define pLOGstrm ConfigureLOGstream::Stream()
322 #define LOGstrm *(ConfigureLOGstream::Stream())
323 #define LOGlevel ConfigureLOG::ReportingLevel()
324 //#define showLOGlevel ConfigureLOG::ReportLevels()
325 //#define showLOGtime ConfigureLOG::ReportTimeTags()
326 
330 //#pragma optimize("g",off) // turn off global optimization
331 //#pragma optimize("",on) // turn on optimizations turned on by compiler switch
332 }
333 #endif //LOGSTREAMINCLUDE --- end of the include file. Test code follows in comments.
334 
335 /* Example:
336 // testls.cpp
337 #include "logstream.hpp"
338 #include <fstream>
339 
340 void func(void); // testls0.cpp
341 
342 int main(int argc, char **argv)
343 {
344  try {
345  std::ofstream oflog("testls.log",std::ios_base::out);
346  ConfigureLOG::Stream() = &oflog;
347  ConfigureLOG::ReportLevels() = true;
348  ConfigureLOG::ReportTimeTags() = true;
349  ConfigureLOG::ReportingLevel() = ConfigureLOG::Level("VERBOSE");
350 
351  LOG(INFO) << "In main(), level is "
352  << ConfigureLOG::ToString(ConfigureLOG::ReportingLevel());
353  LOG(INFO) << "This is an INFO message in main";
354  LOG(WARNING) << "This is a WARNING message in main";
355  LOG(ERROR) << "This is an ERROR message in main";
356 
357  func();
358  LOG(INFO) << "In main(), level is "
359  << ConfigureLOG::ToString(ConfigureLOG::ReportingLevel());
360 
361  LOG(INFO) << "Now write to testls.new.log";
362  std::ofstream oflognew("testls.new.log",std::ios_base::out);
363  ConfigureLOG::Stream() = &oflognew;
364  ConfigureLOG::ReportTimeTags() = false;
365  LOG(INFO) << "Change the level to DEBUG7 and turn off time tags";
366  ConfigureLOG::ReportingLevel() = ConfigureLOG::FromString("DEBUG7");
367 
368  LOG(INFO) << "This is file testls.new.log";
369  LOG(INFO) << "In main(), level is now "
370  << ConfigureLOG::ToString(ConfigureLOG::ReportingLevel());
371  LOG(DEBUG) << "DEBUG message";
372  LOG(DEBUG1) << "DEBUG1 message";
373  LOG(DEBUG2) << "DEBUG2 message";
374  LOG(DEBUG3) << "DEBUG3 message";
375  LOG(DEBUG4) << "DEBUG4 message";
376  LOG(DEBUG5) << "DEBUG5 message";
377  LOG(DEBUG6) << "DEBUG6 message";
378  LOG(DEBUG7) << "DEBUG7 message";
379 
380  LOG(INFO) << "Now go back to the original log file";
381  ConfigureLOG::Stream() = &oflog;
382 
383  LOG(INFO) << "This is file testls.log again";
384  LOG(INFO) << "In main(), level is now "
385  << ConfigureLOG::ToString(ConfigureLOG::ReportingLevel());
386 
387  LOG(INFO) << "Change the level to DEBUG3 and turn on time tags";
388  ConfigureLOG::ReportTimeTags() = true;
389  ConfigureLOG::ReportingLevel() = ConfigureLOG::FromString("DEBUG3");
390  LOG(DEBUG) << "DEBUG message";
391  LOG(DEBUG1) << "DEBUG1 message";
392  LOG(DEBUG2) << "DEBUG2 message";
393  LOG(DEBUG3) << "DEBUG3 message";
394  LOG(DEBUG4) << "DEBUG4 message";
395  LOG(DEBUG5) << "DEBUG5 message";
396  LOG(DEBUG6) << "DEBUG6 message";
397  LOG(DEBUG7) << "DEBUG7 message";
398 
399  return 0;
400  }
401  catch(std::exception& e) {
402  std::cerr << "Exception: " << e.what() << std::endl;
403  }
404  catch(...) {
405  std::cerr << "Unknown exception" << std::endl;
406  }
407  return 1;
408 }
409 // testls0.cpp
410 #include "logstream.hpp"
411 
412 void func(void)
413 {
414  LOG(INFO) << "Here in func(), level is "
415  << ConfigureLOG::ToString(ConfigureLOG::ReportingLevel());
416  LOG(DEBUG) << "This is a debug statement inside func()";
417 
418  LOG(INFO) << "Change the level to DEBUG7";
419  ConfigureLOG::ReportingLevel() = ConfigureLOG::FromString("DEBUG7");
420  LOG(DEBUG5) << "DEBUG5 message";
421  LOG(DEBUG6) << "DEBUG6 message";
422  LOG(DEBUG7) << "DEBUG7 message";
423  LOG(INFO) << "Change the level to DEBUG";
424  ConfigureLOG::ReportingLevel() = ConfigureLOG::FromString("DEBUG");
425  LOG(INFO) << "Leaving func()";
426 }
427 // execution:
428 C:>testls
429 
430 C:>cat testls.log
431 20:56:30.353 INFO: In main(), level is VERBOSE
432 20:56:30.353 INFO: This is an INFO message in main
433 20:56:30.353 WARNING: This is a WARNING message in main
434 20:56:30.353 ERROR: This is an ERROR message in main
435 20:56:30.353 INFO: Here in func(), level is VERBOSE
436 20:56:30.353 INFO: Change the level to DEBUG7
437 20:56:30.353 DEBUG5: DEBUG5 message
438 20:56:30.353 DEBUG6: DEBUG6 message
439 20:56:30.353 DEBUG7: DEBUG7 message
440 20:56:30.353 INFO: Change the level to DEBUG
441 20:56:30.353 INFO: Leaving func()
442 20:56:30.353 INFO: In main(), level is DEBUG
443 20:56:30.353 INFO: Now write to testls.new.log
444 INFO: This is file testls.log again
445 INFO: In main(), level is now DEBUG7
446 INFO: Change the level to DEBUG3 and turn on time tags
447 20:56:30.353 DEBUG: DEBUG message
448 20:56:30.353 DEBUG1: DEBUG1 message
449 20:56:30.353 DEBUG2: DEBUG2 message
450 20:56:30.353 DEBUG3: DEBUG3 message
451 
452 C:>cat testls.new.log
453 INFO: Change the level to DEBUG7 and turn off time tags
454 INFO: This is file testls.new.log
455 INFO: In main(), level is now DEBUG7
456 DEBUG: DEBUG message
457 DEBUG1: DEBUG1 message
458 DEBUG2: DEBUG2 message
459 DEBUG3: DEBUG3 message
460 DEBUG4: DEBUG4 message
461 DEBUG5: DEBUG5 message
462 DEBUG6: DEBUG6 message
463 DEBUG7: DEBUG7 message
464 INFO: Now go back to the original log file
465 */
gnsstk::DEBUG3
@ DEBUG3
Definition: logstream.hpp:58
gnsstk::DEBUG1
@ DEBUG1
Definition: logstream.hpp:58
gnsstk::Log::operator=
Log & operator=(const Log &)
do not implement
gnsstk::Log::~Log
virtual ~Log()
Definition: logstream.hpp:112
gnsstk::Log::os
std::ostringstream os
string stream to which output is written; destructor will dump to log stream.
Definition: logstream.hpp:87
gnsstk::WARNING
@ WARNING
Definition: logstream.hpp:57
gnsstk::DEBUG4
@ DEBUG4
Definition: logstream.hpp:58
gnsstk::ConfigureLOG::Level
static LogLevel Level(const std::string &str)
Definition: logstream.hpp:308
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::ConfigureLOGstream
Definition: logstream.hpp:274
gnsstk::DEBUG2
@ DEBUG2
Definition: logstream.hpp:58
gnsstk::Log::ToString
static std::string ToString(LogLevel level)
convert a level to a string corresponding to the LogLevel enum
Definition: logstream.hpp:142
gnsstk::Log::ReportingLevel
static LogLevel & ReportingLevel()
get/set the highest level that is actually output to log stream
Definition: logstream.hpp:134
example4.time
time
Definition: example4.py:103
gnsstk::Log::Put
std::ostringstream & Put(LogLevel level=INFO)
write out to log stream at level, default is INFO
Definition: logstream.hpp:101
gnsstk::ERROR
@ ERROR
Definition: logstream.hpp:57
gnsstk::ConfigureLOG
class ConfigureLOG - inherits class Log with type ConfigureLOGstream
Definition: logstream.hpp:304
gnsstk::VERBOSE
@ VERBOSE
Definition: logstream.hpp:57
gnsstk::Log::NowTime
std::string NowTime(void)
generate a timetag as string
Definition: logstream.hpp:196
gnsstk::ConfigureLOG::Stream
static std::ostream *& Stream()
Definition: logstream.hpp:306
gnsstk::Log::Log
Log()
Definition: logstream.hpp:69
gnsstk::ConfigureLOGstream::Output
static void Output(const std::string &msg)
used internally
Definition: logstream.hpp:294
gnsstk::Log::FromString
static LogLevel FromString(const std::string &level)
convert from a string (= names in the LogLevel enum) to a LogLevel
Definition: logstream.hpp:150
gnsstk::LogLevel
LogLevel
Definition: logstream.hpp:57
gnsstk::INFO
@ INFO
Definition: logstream.hpp:57
gnsstk::Log
Definition: logstream.hpp:66
gnsstk::DEBUG
@ DEBUG
Definition: logstream.hpp:57
gnsstk::DEBUG7
@ DEBUG7
Definition: logstream.hpp:58
gnsstk::DEBUG5
@ DEBUG5
Definition: logstream.hpp:58
gnsstk::DEBUG6
@ DEBUG6
Definition: logstream.hpp:58
gnsstk::Log::ReportLevels
static bool & ReportLevels()
get/set; if true, output level on each line, after the time tag
Definition: logstream.hpp:118
gnsstk::ConfigureLOGstream::Stream
static std::ostream *& Stream()
Definition: logstream.hpp:288
gnsstk::Log::ReportTimeTags
static bool & ReportTimeTags()
get/set; if true, output time tag on each line, first
Definition: logstream.hpp:126


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39