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
96class Log : public WeakSingleton<Log>
97{
98 private:
99
101 static const Size LogBufferSize = 512;
102
103 public:
104
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
211Log & operator << (Log &log, const char *str);
212
213Log & operator << (Log &log, int number);
214
215Log & operator << (Log &log, const char character);
216
217Log & operator << (Log &log, unsigned number);
218
219Log & operator << (Log &log, unsigned long number);
220
221Log & operator << (Log &log, void *ptr);
222
232#endif /* __LIB_LIBSTD_LOG_H */
Logging class.
Definition Log.h:97
Level m_minimumLogLevel
Minimum log level required to log.
Definition Log.h:194
virtual void terminate() const
Terminate the program immediately.
Definition Log.cpp:82
virtual ~Log()
Destructor.
Definition Log.cpp:29
char m_outputBuffer[LogBufferSize]
Output line is stored here until written using write()
Definition Log.h:200
void flush(const bool force=false)
Flush internal buffer.
Definition Log.cpp:72
Level getMinimumLogLevel()
Get the minimum logging level.
Definition Log.cpp:33
virtual void write(const char *str)=0
Write to the actual output device.
const char * m_ident
Identity.
Definition Log.h:197
Size m_outputBufferWritten
Number of characters written in the output buffer.
Definition Log.h:203
static const Size LogBufferSize
Size of the log buffer in bytes.
Definition Log.h:101
void setMinimumLogLevel(Level level)
Set the minimum logging level.
Definition Log.cpp:38
void setIdent(const char *ident)
Set log identity.
Definition Log.cpp:48
void append(const char *str)
Append to buffered output.
Definition Log.cpp:53
Log()
Constructor.
Definition Log.cpp:21
Level
Logging level values.
Definition Log.h:107
@ Error
Definition Log.h:111
@ Critical
Definition Log.h:110
@ Info
Definition Log.h:114
@ Alert
Definition Log.h:109
@ Warning
Definition Log.h:112
@ Debug
Definition Log.h:115
@ Emergency
Definition Log.h:108
@ Notice
Definition Log.h:113
const char * getIdent() const
Retrieve log identify.
Definition Log.cpp:43
Singleton design pattern: only one instance is allowed.
Definition Singleton.h:70
Log & operator<<(Log &log, const char *str)
Definition Log.cpp:87
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128