Xyris  0.5
Logger Class Reference

#include <Logger.hpp>

+ Collaboration diagram for Logger:

Public Types

enum  LogLevel {
  lTRACE, lDEBUG, lVERBOSE, lINFO,
  lWARNING, lERROR, lNone
}
 
typedef int(* LogWriter) (const char *fmt, va_list args)
 

Public Member Functions

 Logger (Logger const &)=delete
 
void operator= (Logger const &)=delete
 

Static Public Member Functions

static void Trace (const char *tag, const char *fmt,...)
 
static void Verbose (const char *tag, const char *fmt,...)
 
static void Debug (const char *tag, const char *fmt,...)
 
static void Info (const char *tag, const char *fmt,...)
 
static void Warning (const char *tag, const char *fmt,...)
 
static void Error (const char *tag, const char *fmt,...)
 
static void Print (const char *fmt,...)
 
static bool addWriter (LogWriter writer)
 
static bool removeWriter (LogWriter writer)
 
static void setLevel (LogLevel level)
 
static LogLevel getLevel ()
 
static Loggerthe ()
 

Private Member Functions

 Logger ()
 
const char * levelToString (LogLevel lvl)
 
void LogHelper (const char *tag, LogLevel lvl, const char *fmt, va_list args)
 
void LogHelperPrint (const char *fmt, va_list args)
 

Private Attributes

Mutex m_logBufferMutex
 
size_t m_writersIdx
 
LogLevel m_logLevel
 
LogWriter m_writers [m_maxWriterCount]
 
char m_logBuffer [m_maxBufferSize]
 

Static Private Attributes

static const uint8_t m_maxWriterCount = 2
 
static const uint32_t m_maxBufferSize = 1024
 

Detailed Description

Definition at line 16 of file Logger.hpp.

Member Typedef Documentation

◆ LogWriter

typedef int(* Logger::LogWriter) (const char *fmt, va_list args)

Definition at line 30 of file Logger.hpp.

Member Enumeration Documentation

◆ LogLevel

Enumerator
lTRACE 
lDEBUG 
lVERBOSE 
lINFO 
lWARNING 
lERROR 
lNone 

Definition at line 20 of file Logger.hpp.

20  {
21  lTRACE,
22  lDEBUG,
23  lVERBOSE,
24  lINFO,
25  lWARNING,
26  lERROR,
27  lNone,
28 };

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( Logger const &  )
delete

◆ Logger() [2/2]

Logger::Logger ( )
private

Definition at line 147 of file Logger.cpp.

148  : m_logBufferMutex("Logger")
149  , m_writersIdx(0)
150 #if defined(RELEASE)
151  , m_logLevel(lINFO)
152 #elif defined(DEBUG)
153  , m_logLevel(lDEBUG)
154 #endif
155 {
156  // Default constructor
157 }

Member Function Documentation

◆ addWriter()

bool Logger::addWriter ( LogWriter  writer)
static

Definition at line 119 of file Logger.cpp.

120 {
121  if (the().m_writersIdx < the().m_maxWriterCount) {
122  the().m_writers[the().m_writersIdx++] = writer;
123  return true;
124  }
125 
126  return false;
127 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Debug()

void Logger::Debug ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 71 of file Logger.cpp.

72 {
73  if (lDEBUG >= getLevel()) {
74  va_list ap;
75  va_start(ap, fmt);
76  the().LogHelper(tag, lDEBUG, fmt, ap);
77  va_end(ap);
78  }
79 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Error()

void Logger::Error ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 101 of file Logger.cpp.

102 {
103  if (lERROR >= getLevel()) {
104  va_list ap;
105  va_start(ap, fmt);
106  the().LogHelper(tag, lERROR, fmt, ap);
107  va_end(ap);
108  }
109 }
+ Here is the call graph for this function:

◆ getLevel()

static LogLevel Logger::getLevel ( )
inlinestatic

Definition at line 46 of file Logger.hpp.

46 { return the().m_logLevel; }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Info()

void Logger::Info ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 81 of file Logger.cpp.

82 {
83  if (lINFO >= getLevel()) {
84  va_list ap;
85  va_start(ap, fmt);
86  the().LogHelper(tag, lINFO, fmt, ap);
87  va_end(ap);
88  }
89 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ levelToString()

const char * Logger::levelToString ( LogLevel  lvl)
private

Definition at line 15 of file Logger.cpp.

16 {
17  switch (lvl) {
18  case lTRACE:
19  return "\033[36mTRACE\033[0m";
20  case lDEBUG:
21  return "\033[96mDEBUG\033[0m";
22  case lVERBOSE:
23  return "\033[92mVERBOSE\033[0m";
24  case lINFO:
25  return "\033[94mINFO\033[0m";
26  case lWARNING:
27  return "\033[93mWARNING\033[0m";
28  case lERROR:
29  return "\033[91mERROR\033[0m";
30  default:
31  return "UNKNOWN";
32  }
33 }
+ Here is the caller graph for this function:

◆ LogHelper()

void Logger::LogHelper ( const char *  tag,
LogLevel  lvl,
const char *  fmt,
va_list  args 
)
private

Definition at line 35 of file Logger.cpp.

36 {
38  ksprintf(the().m_logBuffer, "[%s] [%s] %s\n", tag, levelToString(lvl), fmt);
40 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ LogHelperPrint()

void Logger::LogHelperPrint ( const char *  fmt,
va_list  args 
)
private

Definition at line 42 of file Logger.cpp.

43 {
44  for (size_t i = 0; i < m_writersIdx; i++) {
45  if (the().m_writers[i] != nullptr) {
46  the().m_writers[i](fmt, ap);
47  }
48  }
49 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=()

void Logger::operator= ( Logger const &  )
delete

◆ Print()

void Logger::Print ( const char *  fmt,
  ... 
)
static

Definition at line 111 of file Logger.cpp.

112 {
113  va_list ap;
114  va_start(ap, fmt);
115  the().LogHelperPrint(fmt, ap);
116  va_end(ap);
117 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeWriter()

bool Logger::removeWriter ( LogWriter  writer)
static

Definition at line 129 of file Logger.cpp.

130 {
131  for (size_t idx = 0; idx < the().m_writersIdx; idx++) {
132  if (the().m_writers[idx] == writer) {
133  the().m_writers[idx] = nullptr;
134  return true;
135  }
136  }
137 
138  return false;
139 }
+ Here is the call graph for this function:

◆ setLevel()

static void Logger::setLevel ( LogLevel  level)
inlinestatic

Definition at line 45 of file Logger.hpp.

45 { the().m_logLevel = level; }
+ Here is the call graph for this function:

◆ the()

Logger & Logger::the ( )
static

Definition at line 141 of file Logger.cpp.

142 {
143  static Logger instance;
144  return instance;
145 }
+ Here is the caller graph for this function:

◆ Trace()

void Logger::Trace ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 51 of file Logger.cpp.

52 {
53  if (lTRACE >= getLevel()) {
54  va_list ap;
55  va_start(ap, fmt);
56  the().LogHelper(tag, lTRACE, fmt, ap);
57  va_end(ap);
58  }
59 }
+ Here is the call graph for this function:

◆ Verbose()

void Logger::Verbose ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 61 of file Logger.cpp.

62 {
63  if (lVERBOSE >= getLevel()) {
64  va_list ap;
65  va_start(ap, fmt);
66  the().LogHelper(tag, lVERBOSE, fmt, ap);
67  va_end(ap);
68  }
69 }
+ Here is the call graph for this function:

◆ Warning()

void Logger::Warning ( const char *  tag,
const char *  fmt,
  ... 
)
static

Definition at line 91 of file Logger.cpp.

92 {
93  if (lWARNING >= getLevel()) {
94  va_list ap;
95  va_start(ap, fmt);
96  the().LogHelper(tag, lWARNING, fmt, ap);
97  va_end(ap);
98  }
99 }
+ Here is the call graph for this function:

Field Documentation

◆ m_logBuffer

char Logger::m_logBuffer[m_maxBufferSize]
private

Definition at line 62 of file Logger.hpp.

◆ m_logBufferMutex

Mutex Logger::m_logBufferMutex
private

Definition at line 58 of file Logger.hpp.

◆ m_logLevel

LogLevel Logger::m_logLevel
private

Definition at line 60 of file Logger.hpp.

◆ m_maxBufferSize

const uint32_t Logger::m_maxBufferSize = 1024
staticprivate

Definition at line 57 of file Logger.hpp.

◆ m_maxWriterCount

const uint8_t Logger::m_maxWriterCount = 2
staticprivate

Definition at line 56 of file Logger.hpp.

◆ m_writers

LogWriter Logger::m_writers[m_maxWriterCount]
private

Definition at line 61 of file Logger.hpp.

◆ m_writersIdx

size_t Logger::m_writersIdx
private

Definition at line 59 of file Logger.hpp.


The documentation for this class was generated from the following files:
Logger::lVERBOSE
@ lVERBOSE
Definition: Logger.hpp:23
Logger::m_logBufferMutex
Mutex m_logBufferMutex
Definition: Logger.hpp:58
ksprintf
int ksprintf(char *buf, const char *fmt,...)
Sends formatted output to a string.
Definition: printf.cpp:342
Logger::getLevel
static LogLevel getLevel()
Definition: Logger.hpp:46
Logger::LogHelper
void LogHelper(const char *tag, LogLevel lvl, const char *fmt, va_list args)
Definition: Logger.cpp:35
Logger::LogHelperPrint
void LogHelperPrint(const char *fmt, va_list args)
Definition: Logger.cpp:42
Logger::m_logBuffer
char m_logBuffer[m_maxBufferSize]
Definition: Logger.hpp:62
Logger::lTRACE
@ lTRACE
Definition: Logger.hpp:21
Logger::lDEBUG
@ lDEBUG
Definition: Logger.hpp:22
Logger::m_writers
LogWriter m_writers[m_maxWriterCount]
Definition: Logger.hpp:61
Logger::m_logLevel
LogLevel m_logLevel
Definition: Logger.hpp:60
Logger::m_writersIdx
size_t m_writersIdx
Definition: Logger.hpp:59
Logger::lINFO
@ lINFO
Definition: Logger.hpp:24
Logger::levelToString
const char * levelToString(LogLevel lvl)
Definition: Logger.cpp:15
Logger::the
static Logger & the()
Definition: Logger.cpp:141
RAIIMutex
Definition: RAII.hpp:16
Logger::lNone
@ lNone
Definition: Logger.hpp:27
Logger
Definition: Logger.hpp:16
Logger::lWARNING
@ lWARNING
Definition: Logger.hpp:25
Logger::lERROR
@ lERROR
Definition: Logger.hpp:26
Logger::m_maxWriterCount
static const uint8_t m_maxWriterCount
Definition: Logger.hpp:56