Open Lighting Architecture: include/ola/Logging.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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 */
43
44#ifndef INCLUDE_OLA_LOGGING_H_
45#define INCLUDE_OLA_LOGGING_H_
46
47#include <ostream>
48#include <string>
49#include <sstream>
50
57#define OLA_LOG(level) (level <= ola::LogLevel()) && \
58 ola::LogLine(__FILE__, __LINE__, level).stream()
59
65#define OLA_FATAL OLA_LOG(ola::OLA_LOG_FATAL)
66
73#define OLA_WARN OLA_LOG(ola::OLA_LOG_WARN)
74
81#define OLA_INFO OLA_LOG(ola::OLA_LOG_INFO)
82
89#define OLA_DEBUG OLA_LOG(ola::OLA_LOG_DEBUG)
90
91namespace ola {
92
98 enum log_level {
99 OLA_LOG_NONE,
100 OLA_LOG_FATAL,
101 OLA_LOG_WARN,
102 OLA_LOG_INFO,
103 OLA_LOG_DEBUG,
104 OLA_LOG_MAX,
105};
106
111extern log_level logging_level;
112
116 typedef enum {
117 OLA_LOG_STDERR,
118 OLA_LOG_SYSLOG,
119 OLA_LOG_NULL,
120} log_output;
121
126 class LogDestination {
127 public:
131 virtual ~LogDestination() {}
132
138 virtual void Write(log_level level, const std::string &log_line) = 0;
139};
140
144 class StdErrorLogDestination: public LogDestination {
145 public:
149 void Write(log_level level, const std::string &log_line);
150};
151
155 class SyslogDestination: public LogDestination {
156 public:
160 virtual ~SyslogDestination() {}
161
165 virtual bool Init() = 0;
166
171 virtual void Write(log_level level, const std::string &log_line) = 0;
172};
173
174#ifdef _WIN32
178class WindowsSyslogDestination : public SyslogDestination {
179 public:
183 bool Init();
184
188 void Write(log_level level, const std::string &log_line);
189 private:
190 typedef void* WindowsLogHandle;
191 WindowsLogHandle m_eventlog;
192};
193#else
197 class UnixSyslogDestination : public SyslogDestination {
198 public:
202 bool Init();
203
207 void Write(log_level level, const std::string &log_line);
208};
209#endif // _WIN32
210
212
218class LogLine {
219 public:
220 LogLine(const char *file, int line, log_level level);
221 ~LogLine();
222 void Write();
223
224 std::ostream &stream() { return m_stream; }
225 private:
226 log_level m_level;
227 std::ostringstream m_stream;
228 unsigned int m_prefix_length;
229};
231
236
241void SetLogLevel(log_level level);
242
247 inline log_level LogLevel() { return logging_level; }
248
252void IncrementLogLevel();
253
259bool InitLoggingFromFlags();
260
267bool InitLogging(log_level level, log_output output);
268
274void InitLogging(log_level level, LogDestination *destination);
275/***/
276} // namespace ola
278#endif // INCLUDE_OLA_LOGGING_H_
ola::LogDestination
The base class for log destinations.
Definition Logging.h:126
ola::StdErrorLogDestination
A LogDestination that writes to stderr.
Definition Logging.h:144
ola::SyslogDestination
An abstract base of LogDestination that writes to syslog.
Definition Logging.h:155
ola::UnixSyslogDestination
A SyslogDestination that writes to Unix syslog.
Definition Logging.h:197
ola::StdErrorLogDestination::Write
void Write(log_level level, const std::string &log_line)
Writes a messages out to stderr.
Definition Logging.cpp:185
ola::UnixSyslogDestination::Init
bool Init()
Initialize the UnixSyslogDestination.
Definition Logging.cpp:233
ola::OLA_LOG_FATAL
@ OLA_LOG_FATAL
Definition Logging.h:100
ola::OLA_LOG_DEBUG
@ OLA_LOG_DEBUG
Definition Logging.h:103
ola::OLA_LOG_INFO
@ OLA_LOG_INFO
Definition Logging.h:102
ola::OLA_LOG_WARN
@ OLA_LOG_WARN
Definition Logging.h:101
ola::OLA_LOG_NONE
@ OLA_LOG_NONE
Definition Logging.h:99
ola::InitLoggingFromFlags
bool InitLoggingFromFlags()
Initialize the OLA logging system from flags.
Definition Logging.cpp:83
ola::IncrementLogLevel
void IncrementLogLevel()
Increment the log level by one. The log level wraps to OLA_LOG_NONE.
Definition Logging.cpp:76
ola::SyslogDestination::Init
virtual bool Init()=0
Initialize the SyslogDestination.
ola::SetLogLevel
void SetLogLevel(log_level level)
Set the logging level.
Definition Logging.cpp:71
ola::SyslogDestination::Write
virtual void Write(log_level level, const std::string &log_line)=0
Write a line to the system logger.
ola::UnixSyslogDestination::Write
void Write(log_level level, const std::string &log_line)
Write a line to syslog.
Definition Logging.cpp:237
ola::LogDestination::~LogDestination
virtual ~LogDestination()
Destructor.
Definition Logging.h:131
ola::LogLevel
log_level LogLevel()
Fetch the current level of logging.
Definition Logging.h:247
ola::LogDestination::Write
virtual void Write(log_level level, const std::string &log_line)=0
An abstract function for writing to your log destination.
ola::SyslogDestination::~SyslogDestination
virtual ~SyslogDestination()
Destructor.
Definition Logging.h:160
ola::OLA_LOG_STDERR
@ OLA_LOG_STDERR
Definition Logging.h:117
ola::OLA_LOG_SYSLOG
@ OLA_LOG_SYSLOG
Definition Logging.h:118
ola::InitLogging
bool InitLogging(log_level level, log_output output)
Initialize the OLA logging system.
Definition Logging.cpp:118
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::log_level
log_level
The OLA log levels. This controls the verbosity of logging. Each level also includes those below it.
Definition Logging.h:98
ola::log_output
log_output
The destination to write log messages to.
Definition Logging.h:116