Ravelin
Log.h
1 /****************************************************************************
2  * Copyright 2015 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 _RAVELIN_LOG_H_
8 #define _RAVELIN_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 Ravelin {
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 
34 template <typename OutputPolicy>
35 class Log
36 {
37  public:
38 
39  std::ostringstream& get(unsigned level = 0)
40  {
41  time_t rawtime;
42  std::time(&rawtime);
43  tm* ptm = std::gmtime(&rawtime);
44  os << "- " << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec;
45  os << " " << level << ": ";
46  message_level = level;
47  return os;
48  }
49 
50  ~Log()
51  {
52  if ((message_level & reporting_level) > 0)
53  OutputPolicy::output(os.str());
54  }
55 
56  static unsigned reporting_level;
57 
58  private:
59  std::ostringstream os;
60  unsigned message_level;
61 }; // end class
62 
63 // make the default warning level zero (no reporting)
64 template <class T>
65 unsigned Log<T>::reporting_level = 0;
66 
67 } // end namespace
68 
69 #endif
70 
Definition: Log.h:35
Definition: Log.h:27