FreeNOS
Log.cpp
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#include "Log.h"
19#include "String.h"
20
22 : WeakSingleton<Log>(this)
23 , m_minimumLogLevel(Notice)
24 , m_ident(ZERO)
25 , m_outputBufferWritten(0)
26{
27}
28
30{
31}
32
37
42
43const char * Log::getIdent() const
44{
45 return (m_ident);
46}
47
48void Log::setIdent(const char *ident)
49{
50 m_ident = ident;
51}
52
53void Log::append(const char *str)
54{
55 // Copy input. Note that we need to reserve 1 byte for the NULL-terminator
56 while (*str)
57 {
59 {
61 str++;
62 }
63 else
64 {
65 flush(true);
66 }
67 }
68
69 flush();
70}
71
72void Log::flush(const bool force)
73{
74 if (m_outputBufferWritten > 0 && (m_outputBuffer[m_outputBufferWritten-1] == '\n' || force))
75 {
79 }
80}
81
82void Log::terminate() const
83{
84 for (;;);
85}
86
87Log & operator << (Log &log, const char *str)
88{
89 log.append(str);
90 return log;
91}
92
93Log & operator << (Log &log, int number)
94{
95 String s = number;
96 log.append(*s);
97 return log;
98}
99
100Log & operator << (Log &log, const char character)
101{
102 const char tmp[2] = { character, 0 };
103 log.append(tmp);
104 return log;
105}
106
107Log & operator << (Log &log, unsigned number)
108{
109 String s = number;
110 log.append(*s);
111 return log;
112}
113
114Log & operator << (Log &log, unsigned long number)
115{
116 String s = number;
117 log.append(*s);
118 return log;
119}
120
121Log & operator << (Log &log, void *ptr)
122{
123 String s;
124 s << Number::Hex << ptr;
125 log.append(*s);
126 return log;
127}
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
const char * getIdent() const
Retrieve log identify.
Definition Log.cpp:43
Abstraction of strings.
Definition String.h:42
Singleton design pattern: only one instance is allowed.
Definition Singleton.h:70
Log & operator<<(Log &log, const char *str)
Definition Log.cpp:87
#define ZERO
Zero value.
Definition Macros.h:43
@ Hex
Decimal: 0-10.
Definition Types.h:171