Moby
Log.h
1 /****************************************************************************
2  * Copyright 2009 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef _MOBY_LOG_H_
8 #define _MOBY_LOG_H_
9 
10 #include <iostream>
11 #include <ctime>
12 #include <limits>
13 #include <sstream>
14 #include <fstream>
15 #include <boost/shared_ptr.hpp>
16 
17 namespace Moby {
18 
19 #ifdef NDEBUG
20 #define FILE_LOG(level) if (false) Log<OutputToFile>().get(level)
21 #define LOGGING(level) false
22 #else
23 #define FILE_LOG(level) if ((level & Log<OutputToFile>::reporting_level) > 0) Log<OutputToFile>().get(level)
24 #define LOGGING(level) ((level & Log<OutputToFile>::reporting_level) > 0)
25 #endif
26 
28 {
29  static std::ofstream stream;
30 
31  static void output(const std::string& msg);
32 };
33 
35 template <typename OutputPolicy>
36 class Log
37 {
38  public:
39  Log()
40  {
41  #ifdef THREADSAFE
42  std::cerr << "Log() warning: this class is not thread-safe!" << std::endl;
43  #endif
44  }
45 
46  std::ostringstream& get(unsigned level = 0)
47  {
48  time_t rawtime;
49  std::time(&rawtime);
50  tm* ptm = std::gmtime(&rawtime);
51  os << "- " << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec;
52  os << " " << level << ": ";
53  message_level = level;
54  return os;
55  }
56 
57  ~Log()
58  {
59  if ((message_level & reporting_level) > 0)
60  OutputPolicy::output(os.str());
61  }
62 
63  static unsigned reporting_level;
64 
65  private:
66  std::ostringstream os;
67  unsigned message_level;
68 }; // end class
69 
70 // make the default warning level zero (no reporting)
71 template <class T>
72 unsigned Log<T>::reporting_level = 0;
73 
74 } // end namespace
75 
76 #endif
77 
Moby's logging utility.
Definition: Log.h:36
Definition: Log.h:27