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


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Fri Oct 2 2020 03:40:07