Blame view

src/log.h 13.6 KB
13df26c3   Steven   Static Logger cla...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /* ****************************************************************************
   * Copyright 2019 Open Systems Development BV                                 *
   *                                                                            *
   * Permission is hereby granted, free of charge, to any person obtaining a    *
   * copy of this software and associated documentation files (the "Software"), *
   * to deal in the Software without restriction, including without limitation  *
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
   * and/or sell copies of the Software, and to permit persons to whom the      *
   * Software is furnished to do so, subject to the following conditions:       *
   *                                                                            *
   * The above copyright notice and this permission notice shall be included in *
   * all copies or substantial portions of the Software.                        *
   *                                                                            *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
   * DEALINGS IN THE SOFTWARE.                                                  *
   * ***************************************************************************/
  #pragma once
  
  #include <string>
13df26c3   Steven   Static Logger cla...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <syslog.h>
  
  namespace osdev      {
  namespace components {
  namespace log        {
  
  #define LogEmergency(context, text)                                                         \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Emergency)\
      {                                                                                       \
          osdev::components::log::Log::emergency(__FILE__, __LINE__, context, text);          \
      }
  
  #define LogAlert(context, text)                                                             \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Alert)    \
      {                                                                                       \
          osdev::components::log::Log::alert(__FILE__, __LINE__, context, text);              \
      }
  
  #define LogCritical(context, text)                                                          \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Critical) \
      {                                                                                       \
          osdev::components::log::Log::critical(__FILE__, __LINE__, context, text);           \
      }
  
  #define LogError(context, text)                                                             \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Error)    \
      {                                                                                       \
          osdev::components::log::Log::error(__FILE__, __LINE__, context, text);              \
      }
  
  #define LogWarning(context, text)                                                           \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Warning)  \
      {                                                                                       \
          osdev::components::log::Log::warning(__FILE__, __LINE__, context, text);            \
      }
  
  #define LogNotice(context, text)                                                            \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Notice)   \
      {                                                                                       \
          osdev::components::log::Log::notice(__FILE__, __LINE__, context, text);             \
      }
  
  #define LogInfo(context, text)                                                              \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Info)     \
      {                                                                                       \
          osdev::components::log::Log::info(__FILE__, __LINE__, context, text);               \
      }
  
  #define LogDebug(context, text)                                                             \
      if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Debug)    \
      {                                                                                       \
          osdev::components::log::Log::debug(__FILE__, __LINE__, context, text);              \
      }
  
c60cc4ce   Steven   added documentation
79
80
81
82
83
  /*!
   * \brief The LogLevel enum
   * Enumeration class dealing with LogLevels.
   * Used to convert to syslog macro's.
   */
13df26c3   Steven   Static Logger cla...
84
85
86
87
88
89
90
91
92
93
94
95
  enum class LogLevel
  {
      Emergency = 0,
      Alert,
      Critical,
      Error,
      Warning,
      Notice,
      Info,
      Debug
  };
  
c60cc4ce   Steven   added documentation
96
97
98
99
100
  /*!
   * \brief The LogMask enum
   * Enumeration class dealing with LogMask.
   * Used to convert to syslog macro's.
   */
13df26c3   Steven   Static Logger cla...
101
102
103
104
105
106
107
108
109
110
111
112
113
  enum class LogMask
  {
      Mask = 0,
      Upto,
      None
  };
  
  /*! \class Log
      \brief Basic logging mechanism.
  */
  class Log
  {
  public:
c60cc4ce   Steven   added documentation
114
115
116
117
118
119
      /**
       *  @brief  Initialize the logging mechanism
       *  @param  context     - The main context
       *  @param  logFile     - Logfile if available
       *  @param  logDepth    - Initial log-depth
       */
13df26c3   Steven   Static Logger cla...
120
121
122
123
      static void init( const std::string& context,
                        const std::string& logFile = std::string(),
                        LogLevel logDepth = LogLevel::Info );
  
c60cc4ce   Steven   added documentation
124
      //! Shutdown the logging mechanism
13df26c3   Steven   Static Logger cla...
125
126
      static void terminate();
  
c60cc4ce   Steven   added documentation
127
128
129
130
131
132
      /*!
       * \brief Write to syslog
       * \param priority      - priority level [from debug up to emergency]
       * \param message       - The string to print
       * \param context       - The context name. default name is the name of the executable.
       */
13df26c3   Steven   Static Logger cla...
133
134
      static void write( const int &priority, const std::string &message, const std::string &context = std::string() );
  
c60cc4ce   Steven   added documentation
135
136
137
138
139
140
141
      /**
       *  @brief  Log an emergency message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
142
      static void emergency( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
143
144
145
146
147
148
149
      /**
       *  @brief  Log an alert message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
150
      static void alert    ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
151
152
153
154
155
156
157
      /**
       *  @brief  Log a critical message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
158
      static void critical ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
159
160
161
162
163
164
165
      /**
       *  @brief  Log an error message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
166
      static void error    ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
167
168
169
170
171
172
173
      /**
       *  @brief  Log a warning message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
174
      static void warning  ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
175
176
177
178
179
180
181
      /**
       *  @brief  Log a notice message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
182
      static void notice   ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
183
184
185
186
187
188
189
      /**
       *  @brief  Log an info message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
190
      static void info     ( const char* file, int line, const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
191
192
193
194
195
196
197
      /**
       *  @brief  Log a debug message in a category.
       *  @param  file        - Name of the source-file
       *  @param  line        - The line number in the source-file
       *  @param  category    - The category of the message.
       *  @param  message     - The string to print
       */
13df26c3   Steven   Static Logger cla...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
      static void debug    ( const char* file, int line, const std::string& category, const std::string& message );
  
      /**
       *  @return The current log level.
       */
      static LogLevel level()
      {
          if( s_logMask == LogMask::None )
          {
              return LogLevel::Debug;
          }
          return s_logLevel;
      }
  
c60cc4ce   Steven   added documentation
212
213
214
215
      /*!
       * \brief setMask update the current logMask
       * \param logMask   - Enum defining the logmask used.
       */
13df26c3   Steven   Static Logger cla...
216
      static void setMask    ( LogMask logMask )     { s_logMask = logMask;   }
c60cc4ce   Steven   added documentation
217
218
219
220
      /*!
       * \brief setLogLevel update the current logLevel
       * \param logLevel  - Enum defining the logLevel used, in combination with Mask.
       */
13df26c3   Steven   Static Logger cla...
221
      static void setLogLevel( LogLevel logLevel )   { s_logLevel = logLevel; }
c60cc4ce   Steven   added documentation
222
223
224
225
      /*!
       * \brief setContext update the current context
       * \param context   - String containing the new context name.
       */
13df26c3   Steven   Static Logger cla...
226
227
228
      static void setContext ( std::string context ) { s_context = context;   }
  
  protected:
c60cc4ce   Steven   added documentation
229
230
231
232
233
      /**
       * @brief Log an emergency message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
234
      static void emergency( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
235
236
237
238
239
      /**
       * @brief Log an alert message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
240
      static void alert    ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
241
242
243
244
245
      /**
       * @brief Log a critical message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
246
      static void critical ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
247
248
249
250
251
      /**
       * @brief Log an error message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
252
      static void error    ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
253
254
255
256
257
      /**
       * @brief Log a warning message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
258
      static void warning  ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
259
260
261
262
263
      /**
       * @brief Log a notice message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
264
      static void notice   ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
265
266
267
268
269
      /**
       * @brief Log an info message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
270
      static void info     ( const std::string& category, const std::string& message );
c60cc4ce   Steven   added documentation
271
272
273
274
275
      /**
       * @brief Log an debug message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
13df26c3   Steven   Static Logger cla...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
      static void debug    ( const std::string& category, const std::string& message );
  
      /**
       * @brief Log an error message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       * @param level Selected log level
       */
      static void log( const std::string &category,
          const std::string &message,
          LogLevel level );
  
  private:
      /**
       * @brief Replace the characters in a std::string with other characters.
       * @param strToReplace  The string we want to replace characters in
       * @param from_chars    The characters we want to replace
       * @param to_chars      The characters we want to replace with.
       * @return strToReplace This is the original string with the replaced characters.
       */
      static void ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars );
  
      /**
       * @brief Put filename, line-number and message in one string
       * @param file Source-file
       * @param line Line in source-file
       * @param message The string to print
       * @return Formatted string with file, line and message
       */
      static std::string fileinfoMessage(const char* file, int line,
                                     const std::string& message);
  
      //! The main context.
      static std::string  s_context;
  
      //! The name of the LogFile.
      static std::string  s_fileName;
  
      //! The amount of logging
      static LogLevel     s_logLevel;
  
      //! The mask
      static LogMask      s_logMask;
13df26c3   Steven   Static Logger cla...
319
320
321
322
323
  };
  
  } // End namespace log
  } // End namespace components
  } // End namespace osdev