Logger.cpp
Go to the documentation of this file.
1 
48 #include <iostream>
49 #include <fstream>
50 #include <signal.h>
51 #include <unistd.h>
52 
53 using namespace std;
54 
55 #include "roch_base/core/Logger.h"
56 
57 namespace sawyer
58 {
59 
60  const char *Logger::levelNames[] = {"ERROR", "EXCEPTION", "WARNING", "INFO", "DETAIL"};
61 
62  void loggerTermHandler(int signum)
63  {
64  Logger::instance().close();
65 
66  if ((signum == SIGABRT) || (signum == SIGSEGV))
67  {
68  /* We need to catch these so we can flush out any last messages.
69  * having done this, probably the most consistent thing to do
70  * is re-raise the signal. (We certainly don't want to just
71  * ignore these) */
72  signal(signum, SIG_DFL);
73  kill(getpid(), signum);
74  }
75  }
76 
77  Logger &Logger::instance()
78  {
79  static Logger instance;
80  return instance;
81  }
82 
83  Logger::Logger() :
84  enabled(true),
85  level(WARNING),
86  stream(&cerr)
87  {
88  nullStream = new ofstream("/dev/null");
89  }
90 
92  {
93  close();
94  }
95 
97  {
98  // The actual output stream is owned by somebody else, we only need to flush it
99  stream->flush();
100 
101  nullStream->close();
102  delete nullStream;
103  nullStream = 0;
104  }
105 
106  std::ostream &Logger::entry(enum logLevels msg_level, const char *file, int line)
107  {
108  if (!enabled) { return *nullStream; }
109  if (msg_level > this->level) { return *nullStream; }
110 
111  /* Construct the log entry tag */
112  // Always the level of the message
113  *stream << levelNames[msg_level];
114  // If file/line information is provided, need to print it with brackets:
115  if (file || (line >= 0))
116  {
117  *stream << " (";
118  if (file) { *stream << file; }
119  // Only want a comma if we have both items
120  if (file && (line >= 0)) { *stream << ","; }
121  if (line >= 0) { *stream << line; }
122  *stream << ")";
123  }
124  *stream << ": ";
125  return *stream;
126  }
127 
128  void Logger::setEnabled(bool en)
129  {
130  enabled = en;
131  }
132 
133  void Logger::setLevel(enum logLevels newLevel)
134  {
135  level = newLevel;
136  }
137 
138  void Logger::setStream(ostream *newStream)
139  {
140  stream->flush();
141  stream = newStream;
142  }
143 
145  {
146  signal(SIGINT, loggerTermHandler);
147  signal(SIGTERM, loggerTermHandler);
148  /* If there's an abort or segfault in Logger.close(), well...
149  * we're pretty much totally screwed anyway. */
150  signal(SIGABRT, loggerTermHandler);
151  signal(SIGSEGV, loggerTermHandler);
152  }
153 
154 }; // namespace sawyer
155 
std::ostream & entry(enum logLevels level, const char *file=0, int line=-1)
Definition: Logger.cpp:106
void setEnabled(bool enabled)
Definition: Logger.cpp:128
void setStream(std::ostream *stream)
Definition: Logger.cpp:138
static const char * levelNames[]
Definition: Logger.h:65
void hookFatalSignals()
Definition: Logger.cpp:144
bool enabled
Definition: Logger.h:49
void close()
Definition: Logger.cpp:96
std::ostream * stream
Definition: Logger.h:52
friend void loggerTermHandler(int signal)
Definition: Logger.cpp:62
void loggerTermHandler(int signal)
Definition: Logger.cpp:62
void setLevel(enum logLevels newLevel)
Definition: Logger.cpp:133
std::ofstream * nullStream
Definition: Logger.h:54


roch_base
Author(s): Mike Purvis , Paul Bovbel , Chen
autogenerated on Mon Jun 10 2019 14:41:13