The Pedigree Project  0.1
Log.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_LOG_H
21 #define KERNEL_LOG_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #ifdef THREADS
25 #include "pedigree/kernel/Spinlock.h"
26 #endif
27 #include "pedigree/kernel/processor/types.h"
28 #include "pedigree/kernel/utilities/StaticString.h"
29 #include "pedigree/kernel/utilities/StaticCord.h"
30 #include "pedigree/kernel/time/Time.h"
31 
32 class String;
33 class StringView;
34 
38 #define SHOW_FILE_IN_LOGS 0
39 
40 typedef StaticCord<8> LogCord;
41 
42 #if SHOW_FILE_IN_LOGS
43 #define FILE_LOG(entry, level) \
44  do \
45  { \
46  entry << level << __FILE__ << ":" << Dec << __LINE__ << Hex << " " \
47  << __FUNCTION__ << " -- "; \
48  } while (0)
49 #else
50 #define FILE_LOG(entry, level)
51 #endif
52 
53 #define LOG_AT_LEVEL(level, text, lock) \
54  do \
55  { \
56  Log::LogEntry __log_macro_logentry; \
57  FILE_LOG(__log_macro_logentry, level); \
58  __log_macro_logentry << level << text; \
59  Log::instance().addEntry(__log_macro_logentry, lock); \
60  } while (0)
61 
62 #ifndef NO_LOGGING
63 
65 #if DEBUG_LOGGING
66 #define DEBUG_LOG(text) LOG_AT_LEVEL(Log::Debug, text, 1)
67 #define DEBUG_LOG_NOLOCK(text) LOG_AT_LEVEL(Log::Debug, text, 0)
68 #else
69 #define DEBUG_LOG(text)
70 #define DEBUG_LOG_NOLOCK(text)
71 #endif
72 
74 #define NOTICE(text) LOG_AT_LEVEL(Log::Notice, text, 1)
75 #define NOTICE_NOLOCK(text) LOG_AT_LEVEL(Log::Notice, text, 0)
76 
78 #define WARNING(text) LOG_AT_LEVEL(Log::Warning, text, 1)
79 #define WARNING_NOLOCK(text) LOG_AT_LEVEL(Log::Warning, text, 0)
80 
82 #define ERROR(text) LOG_AT_LEVEL(Log::Error, text, 1)
83 #define ERROR_NOLOCK(text) LOG_AT_LEVEL(Log::Error, text, 0)
84 
89 #define FATAL(text) \
90  do \
91  { \
92  LOG_AT_LEVEL(Log::Fatal, text, 1); \
93  while (1) \
94  ; \
95  } while (0)
96 #define FATAL_NOLOCK(text) \
97  do \
98  { \
99  LOG_AT_LEVEL(Log::Fatal, text, 0); \
100  while (1) \
101  ; \
102  } while (0)
103 
104 #else // NO_LOGGING
105 
106 #define DEBUG_LOG(text)
107 #define DEBUG_LOG_NOLOCK(text)
108 #define NOTICE(text)
109 #define NOTICE_NOLOCK(text)
110 #define WARNING(text)
111 #define WARNING_NOLOCK(text)
112 #define ERROR(text)
113 #define ERROR_NOLOCK(text)
114 #define FATAL(text)
115 #define FATAL_NOLOCK(text)
116 
117 #endif
118 
120 #define LOG_LENGTH 128
121 
122 #ifdef HUGE_STATIC_LOG
123 // 2MB static log buffer
124 #define LOG_ENTRIES ((1 << 21) / sizeof(LogEntry))
125 #else
126 // 64K static log buffer
127 #define LOG_ENTRIES ((1 << 16) / sizeof(LogEntry))
128 #endif
129 
130 #define LOG_CALLBACK_COUNT 16
131 
134 {
141 };
142 
145 {
148 };
149 
150 // Function pointer to update boot progress -
151 // Description.
152 typedef void (*BootProgressUpdateFn)(const char *);
153 
154 extern size_t g_BootProgressCurrent;
155 extern size_t g_BootProgressTotal;
156 extern BootProgressUpdateFn g_BootProgressUpdate;
157 
158 extern void installSerialLogger();
159 
166 class Log
167 {
168  public:
169  struct LogEntry;
170 
173  {
174  public:
175  virtual void callback(const LogCord &cord) = 0;
176  virtual ~LogCallback();
177  };
178 
181  {
182  Debug = 0,
183  Notice,
184  Warning,
185  Error,
186  Fatal
187  };
188 
192 #ifdef THREADS
194 #endif
195 
198  EXPORTED_PUBLIC static Log &instance();
199 
201  void initialise1();
202 
204  void initialise2();
205 
207  EXPORTED_PUBLIC void
208  installCallback(LogCallback *pCallback, bool bSkipBacklog = false);
209 
211  EXPORTED_PUBLIC void removeCallback(LogCallback *pCallback);
212 
214  EXPORTED_PUBLIC Log &operator<<(const LogEntry &entry);
215 
218 
220  EXPORTED_PUBLIC void
221  addEntry(const LogEntry &entry, bool lock = true, bool flush = true);
222 
224  void flushEntry(bool lock = true);
225 
228  size_t getStaticEntryCount() const;
231  size_t getDynamicEntryCount() const;
232 
236  {
238  LogEntry();
239 
241  unsigned int timestamp;
248 
252  LogEntry &operator<<(const char *);
253  LogEntry &operator<<(const String &);
254  LogEntry &operator<<(const StringView &);
258  LogEntry &operator<<(char *append_str);
261  LogEntry &operator<<(bool b);
264  template <class T>
265  LogEntry &operator<<(T *p)
266  {
267  // Preserve the current number type but always print pointers as
268  // hex.
269  NumberType currentNumberType = numberType;
270  return (*this) << Hex << (reinterpret_cast<uintptr_t>(p))
271  << currentNumberType;
272  }
275  template <class T>
276  LogEntry &operator<<(T n);
277 
279  LogEntry &operator<<(SeverityLevel level);
281  LogEntry &operator<<(NumberType type);
282  };
283 
286  typedef LogEntry DynamicLogEntry;
287 
289  const StaticLogEntry &getStaticEntry(size_t n) const;
291  const DynamicLogEntry &getDynamicEntry(size_t n) const;
292 
293  bool echoToSerial();
294 
295  const LogEntry &getLatestEntry() const;
296 
297  void enableTimestamps();
298  void disableTimestamps();
299 
300  private:
302  Log();
304  ~Log();
307  Log(const Log &);
310  Log &operator=(const Log &);
311 
312  const NormalStaticString &getTimestamp();
313 
314  const TinyStaticString &severityToString(SeverityLevel level) const;
315 
317  StaticLogEntry m_StaticLog[LOG_ENTRIES];
319  // Vector<DynamicLogEntry*> m_DynamicLog;
322 
323  size_t m_StaticEntryStart, m_StaticEntryEnd;
324 
327  StaticLogEntry m_Buffer;
328 
331 
334  size_t m_nOutputCallbacks;
335 
338 
340  uint64_t m_LastEntryHash;
341 
344 
347 
350 
352  Time::Timestamp m_LastTime;
353 
356 
359  static TinyStaticString m_NoticeSeverityString;
360  static TinyStaticString m_WarningSeverityString;
361  static TinyStaticString m_ErrorSeverityString;
362  static TinyStaticString m_FatalSeverityString;
363 
366 
369  static TinyStaticString m_DedupeTail;
370 };
371 
374 #endif
StaticLogEntry m_Buffer
Definition: Log.h:327
size_t m_HashMatchedCount
Definition: Log.h:346
EXPORTED_PUBLIC void addEntry(const LogEntry &entry, bool lock=true, bool flush=true)
Definition: Log.cc:369
void initialise2()
Definition: Log.cc:138
bool m_Timestamps
Definition: Log.h:349
StaticString< LOG_LENGTH > str
Definition: Log.h:245
Time::Timestamp m_LastTime
Definition: Log.h:352
static TinyStaticString m_LineEnding
Definition: Log.h:365
the kernel&#39;s log
Definition: Log.h:166
size_t getStaticEntryCount() const
Definition: Log.cc:225
Definition: String.h:49
Log & operator=(const Log &)
SeverityLevel severity
Definition: Log.h:243
const StaticLogEntry & getStaticEntry(size_t n) const
Definition: Log.cc:235
StaticLogEntry m_StaticLog[LOG_ENTRIES]
Definition: Log.h:317
Log()
Definition: Log.cc:76
SeverityLevel m_LastEntrySeverity
Definition: Log.h:343
const DynamicLogEntry & getDynamicEntry(size_t n) const
Definition: Log.cc:240
LogEntry & operator<<(T *p)
Definition: Log.h:265
EXPORTED_PUBLIC Log & operator<<(const LogEntry &entry)
Definition: Log.cc:352
bool m_EchoToSerial
Definition: Log.h:330
unsigned int timestamp
Definition: Log.h:241
size_t m_StaticEntries
Definition: Log.h:321
NumberType
Definition: Log.h:133
Definition: Log.h:136
EXPORTED_PUBLIC void removeCallback(LogCallback *pCallback)
Definition: Log.cc:209
Spinlock m_Lock
Definition: Log.h:193
LogEntry StaticLogEntry
Definition: Log.h:285
size_t getDynamicEntryCount() const
Definition: Log.cc:230
#define LOG_ENTRIES
Definition: Log.h:127
SeverityLevel
Definition: Log.h:180
static EXPORTED_PUBLIC Log m_Instance
Definition: Log.h:337
LogCallback * m_OutputCallbacks[LOG_CALLBACK_COUNT]
Definition: Log.h:333
Definition: Log.h:140
static EXPORTED_PUBLIC Log & instance()
Definition: Log.cc:108
void initialise1()
Definition: Log.cc:113
Modifier
Definition: Log.h:144
Definition: Log.h:147
Definition: Log.h:138
uint64_t m_LastEntryHash
Definition: Log.h:340
void flushEntry(bool lock=true)
Definition: Log.cc:378
#define LOG_CALLBACK_COUNT
Definition: Log.h:130
Definition: errors.h:24
static TinyStaticString m_DebugSeverityString
Definition: Log.h:358
EXPORTED_PUBLIC void installCallback(LogCallback *pCallback, bool bSkipBacklog=false)
Definition: Log.cc:146
static NormalStaticString m_DedupeHead
Definition: Log.h:368
~Log()
Definition: Log.cc:101
NormalStaticString m_CachedTimestamp
Definition: Log.h:355
NumberType numberType
Definition: Log.h:247