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 
34 {
35  return m_minimumLogLevel;
36 }
37 
39 {
40  m_minimumLogLevel = level;
41 }
42 
43 const char * Log::getIdent() const
44 {
45  return (m_ident);
46 }
47 
48 void Log::setIdent(const char *ident)
49 {
50  m_ident = ident;
51 }
52 
53 void 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 
72 void Log::flush(const bool force)
73 {
74  if (m_outputBufferWritten > 0 && (m_outputBuffer[m_outputBufferWritten-1] == '\n' || force))
75  {
79  }
80 }
81 
82 void Log::terminate() const
83 {
84  for (;;);
85 }
86 
87 Log & operator << (Log &log, const char *str)
88 {
89  log.append(str);
90  return log;
91 }
92 
93 Log & operator << (Log &log, int number)
94 {
95  String s = number;
96  log.append(*s);
97  return log;
98 }
99 
100 Log & operator << (Log &log, const char character)
101 {
102  const char tmp[2] = { character, 0 };
103  log.append(tmp);
104  return log;
105 }
106 
107 Log & operator << (Log &log, unsigned number)
108 {
109  String s = number;
110  log.append(*s);
111  return log;
112 }
113 
114 Log & operator << (Log &log, unsigned long number)
115 {
116  String s = number;
117  log.append(*s);
118  return log;
119 }
120 
121 Log & operator << (Log &log, void *ptr)
122 {
123  String s;
124  s << Number::Hex << ptr;
125  log.append(*s);
126  return log;
127 }
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::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
String
Abstraction of strings.
Definition: String.h:41
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.h
Log
Logging class.
Definition: Log.h:96
Log::append
void append(const char *str)
Append to buffered output.
Definition: Log.cpp:53
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.
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::~Log
virtual ~Log()
Destructor.
Definition: Log.cpp:29
Log::Log
Log()
Constructor.
Definition: Log.cpp:21
Log::getMinimumLogLevel
Level getMinimumLogLevel()
Get the minimum logging level.
Definition: Log.cpp:33
String.h
Log::setIdent
void setIdent(const char *ident)
Set log identity.
Definition: Log.cpp:48
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Number::Hex
@ Hex
Decimal: 0-10.
Definition: Types.h:171
Log::Level
Level
Logging level values.
Definition: Log.h:106