Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Logging.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Logging.h
17  * Header file for the logging
18  * Copyright (C) 2005 Simon Newton
19  */
44 #ifndef INCLUDE_OLA_LOGGING_H_
45 #define INCLUDE_OLA_LOGGING_H_
46 
47 #include <ostream>
48 #include <string>
49 #include <sstream>
50 
59 #define OLA_LOG(level) (level <= ola::LogLevel()) && \
60  ola::LogLine(__FILE__, __LINE__, level).stream()
61 
67 #define OLA_FATAL OLA_LOG(ola::OLA_LOG_FATAL)
68 
75 #define OLA_WARN OLA_LOG(ola::OLA_LOG_WARN)
76 
83 #define OLA_INFO OLA_LOG(ola::OLA_LOG_INFO)
84 
91 #define OLA_DEBUG OLA_LOG(ola::OLA_LOG_DEBUG)
92 
93 namespace ola {
94 
100 enum log_level {
106  OLA_LOG_MAX,
107 };
108 
113 extern log_level logging_level;
114 
118 typedef enum {
121  OLA_LOG_NULL,
122 } log_output;
123 
129  public:
133  virtual ~LogDestination() {}
134 
140  virtual void Write(log_level level, const std::string &log_line) = 0;
141 };
142 
147  public:
151  void Write(log_level level, const std::string &log_line);
152 };
153 
158  public:
162  virtual ~SyslogDestination() {}
163 
167  virtual bool Init() = 0;
168 
173  virtual void Write(log_level level, const std::string &log_line) = 0;
174 };
175 
176 #ifdef _WIN32
177 
180 class WindowsSyslogDestination : public SyslogDestination {
181  public:
185  bool Init();
186 
190  void Write(log_level level, const std::string &log_line);
191  private:
192  typedef void* WindowsLogHandle;
193  WindowsLogHandle m_eventlog;
194 };
195 #else
196 
200  public:
204  bool Init();
205 
209  void Write(log_level level, const std::string &log_line);
210 };
211 #endif // _WIN32
212 
220 class LogLine {
221  public:
222  LogLine(const char *file, int line, log_level level);
223  ~LogLine();
224  void Write();
225 
226  std::ostream &stream() { return m_stream; }
227  private:
228  log_level m_level;
229  std::ostringstream m_stream;
230  unsigned int m_prefix_length;
231 };
243 void SetLogLevel(log_level level);
244 
249 inline log_level LogLevel() { return logging_level; }
250 
254 void IncrementLogLevel();
255 
261 bool InitLoggingFromFlags();
262 
269 bool InitLogging(log_level level, log_output output);
270 
276 void InitLogging(log_level level, LogDestination *destination);
277 /***/
278 } // namespace ola
280 #endif // INCLUDE_OLA_LOGGING_H_
log_output
The destination to write log messages to.
Definition: Logging.h:118
log_level
The OLA log levels. This controls the verbosity of logging. Each level also includes those below it...
Definition: Logging.h:100
Definition: Logging.h:105
void Write(log_level level, const std::string &log_line)
Write a line to syslog.
Definition: Logging.cpp:237
Definition: Logging.h:104
virtual void Write(log_level level, const std::string &log_line)=0
Write a line to the system logger.
void SetLogLevel(log_level level)
Set the logging level.
Definition: Logging.cpp:71
log_level LogLevel()
Fetch the current level of logging.
Definition: Logging.h:249
void IncrementLogLevel()
Increment the log level by one. The log level wraps to OLA_LOG_NONE.
Definition: Logging.cpp:76
A SyslogDestination that writes to Unix syslog.
Definition: Logging.h:199
Definition: Logging.h:119
An abstract base of LogDestination that writes to syslog.
Definition: Logging.h:157
virtual void Write(log_level level, const std::string &log_line)=0
An abstract function for writing to your log destination.
Definition: Logging.h:101
virtual ~SyslogDestination()
Destructor.
Definition: Logging.h:162
Definition: Logging.h:102
virtual ~LogDestination()
Destructor.
Definition: Logging.h:133
bool InitLogging(log_level level, log_output output)
Initialize the OLA logging system.
Definition: Logging.cpp:118
Definition: Logging.h:103
A LogDestination that writes to stderr.
Definition: Logging.h:146
virtual bool Init()=0
Initialize the SyslogDestination.
log_level logging_level
Application global logging level.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool InitLoggingFromFlags()
Initialize the OLA logging system from flags.
Definition: Logging.cpp:83
Definition: Logging.h:120
bool Init()
Initialize the UnixSyslogDestination.
Definition: Logging.cpp:233
The base class for log destinations.
Definition: Logging.h:128
void Write(log_level level, const std::string &log_line)
Writes a messages out to stderr.
Definition: Logging.cpp:185