FreeNOS
Log.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIB_LIBSTD_LOG_H
19 #define __LIB_LIBSTD_LOG_H
20 
21 #include "Singleton.h"
22 #include "Macros.h"
23 
39 #define MAKE_LOG(type, typestr, msg) \
40  {\
41  if (Log::instance() && type <= Log::instance()->getMinimumLogLevel()) \
42  (*Log::instance()) << "[" typestr "] " << __FILE__ ":" << __LINE__ << " " << __FUNCTION__ << " -- " << msg << "\r\n"; \
43  }
44 
50 #define FATAL(msg) \
51  { \
52  MAKE_LOG(Log::Emergency, "Emergency", msg); \
53  if (Log::instance()) Log::instance()->terminate(); \
54  }
55 
61 #define ERROR(msg) MAKE_LOG(Log::Error, "Error", msg)
62 
68 #define WARNING(msg) MAKE_LOG(Log::Warning, "Warning", msg)
69 
75 #define NOTICE(msg) MAKE_LOG(Log::Notice, "Notice", msg)
76 
82 #define INFO(msg) MAKE_LOG(Log::Info, "Info", msg)
83 
89 #define DEBUG(msg) MAKE_LOG(Log::Debug, "Debug", msg)
90 
96 class Log : public WeakSingleton<Log>
97 {
98  private:
99 
101  static const Size LogBufferSize = 512;
102 
103  public:
104 
106  enum Level
107  {
116  };
117 
118  public:
119 
123  Log();
124 
128  virtual ~Log();
129 
136 
140  void setMinimumLogLevel(Level level);
141 
147  void append(const char *str);
148 
154  void setIdent(const char *ident);
155 
161  const char * getIdent() const;
162 
166  virtual void terminate() const;
167 
168  protected:
169 
173  virtual void write(const char *str) = 0;
174 
175  private:
176 
189  void flush(const bool force = false);
190 
191  private:
192 
195 
197  const char *m_ident;
198 
201 
204 };
205 
211 Log & operator << (Log &log, const char *str);
212 
213 Log & operator << (Log &log, int number);
214 
215 Log & operator << (Log &log, const char character);
216 
217 Log & operator << (Log &log, unsigned number);
218 
219 Log & operator << (Log &log, unsigned long number);
220 
221 Log & operator << (Log &log, void *ptr);
222 
232 #endif /* __LIB_LIBSTD_LOG_H */
Log::getIdent
const char * getIdent() const
Retrieve log identify.
Definition: Log.cpp:43
Log::m_minimumLogLevel
Level m_minimumLogLevel
Minimum log level required to log.
Definition: Log.h:194
Log::terminate
virtual void terminate() const
Terminate the program immediately.
Definition: Log.cpp:82
Log::Debug
@ Debug
Definition: Log.h:115
Macros.h
Log::Emergency
@ Emergency
Definition: Log.h:108
Log::setMinimumLogLevel
void setMinimumLogLevel(Level level)
Set the minimum logging level.
Definition: Log.cpp:38
Log::flush
void flush(const bool force=false)
Flush internal buffer.
Definition: Log.cpp:72
Log::m_outputBufferWritten
Size m_outputBufferWritten
Number of characters written in the output buffer.
Definition: Log.h:203
operator<<
Log & operator<<(Log &log, const char *str)
Definition: Log.cpp:87
Log::m_ident
const char * m_ident
Identity.
Definition: Log.h:197
Log
Logging class.
Definition: Log.h:96
Log::append
void append(const char *str)
Append to buffered output.
Definition: Log.cpp:53
Log::Alert
@ Alert
Definition: Log.h:109
Log::Error
@ Error
Definition: Log.h:111
Log::LogBufferSize
static const Size LogBufferSize
Size of the log buffer in bytes.
Definition: Log.h:101
Log::write
virtual void write(const char *str)=0
Write to the actual output device.
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Log::Notice
@ Notice
Definition: Log.h:113
Log::m_outputBuffer
char m_outputBuffer[LogBufferSize]
Output line is stored here until written using write()
Definition: Log.h:200
WeakSingleton
Singleton design pattern: only one instance is allowed.
Definition: Singleton.h:69
Log::Critical
@ Critical
Definition: Log.h:110
Log::~Log
virtual ~Log()
Destructor.
Definition: Log.cpp:29
Log::Log
Log()
Constructor.
Definition: Log.cpp:21
Singleton.h
Log::getMinimumLogLevel
Level getMinimumLogLevel()
Get the minimum logging level.
Definition: Log.cpp:33
Log::Warning
@ Warning
Definition: Log.h:112
Log::setIdent
void setIdent(const char *ident)
Set log identity.
Definition: Log.cpp:48
Log::Info
@ Info
Definition: Log.h:114
Log::Level
Level
Logging level values.
Definition: Log.h:106