Blame view

include/log.h 7.55 KB
f23cf8e6   Peter M. Groen   Setting up the lo...
1
2
  #pragma once
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
3
  // std
f23cf8e6   Peter M. Groen   Setting up the lo...
4
5
  #include <syslog.h>
  #include <unistd.h>
25b0d2f8   Peter M. Groen   Setting up Loggin...
6
  #include <string>
f23cf8e6   Peter M. Groen   Setting up the lo...
7
8
9
10
11
  
  namespace osdev {
  namespace components {
  namespace mqtt {
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
12
13
14
15
  #define LogTrace(context, text)                                                                 \
      if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Trace ) {  \
           osdev::components::mqtt::Log::trace(__FILE__, __LINE__, context, text);                \
      }
f23cf8e6   Peter M. Groen   Setting up the lo...
16
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
17
18
19
20
  #define LogDebug(context, text)                                                                 \
      if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Debug ) {  \
           osdev::components::mqtt::Log::debug(__FILE__, __LINE__, context, text);                \
      }
f23cf8e6   Peter M. Groen   Setting up the lo...
21
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
22
23
24
25
  #define LogInfo(context, text)                                                                  \
      if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Info  ) {  \
           osdev::components::mqtt::Log::info(__FILE__, __LINE__, context, text);                 \
      }
f23cf8e6   Peter M. Groen   Setting up the lo...
26
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
27
28
29
30
  #define LogWarning(context, text)                                                               \
      if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Warning ) {\
           osdev::components::mqtt::Log::warning(__FILE__, __LINE__, context, text);              \
      }
f23cf8e6   Peter M. Groen   Setting up the lo...
31
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  #define LogError(context, text)                                                                 \
      if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Error ) {  \
           osdev::components::mqtt::Log::error(__FILE__, __LINE__, context, text);                \
      }
  
  enum class LogLevel
  {
      Trace,
      Debug,
      Info,
      Warning,
      Error
  };
  
  /*! \class  Log
   *  \brief  Basic logging mechanism.
   */
  class Log
  {
  public:
      /**
       *  @brief  String that identifies the start of the program in the logging.
       */
      static const std::string s_startMarker;
  
      /**
       *  @brief  Initialize the logging mechanism
       *  @param  context     - The main context
       *  @param  logFile     - Logfile if available
       *  @param  logDepth    - Initial log-depth
       */
      static void init( const std::string &context,
                        const std::string &logFile,
                        LogLevel logDepth = LogLevel::Info);
  
      //! Shutdown the logging mechanism
      static void terminate();
  
      /**
       *  @brief  Log a trace 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
       */
      static void trace( const char *file, int line,
                         const std::string &category, const std::string &message );
  
      /**
       *  @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
       */
      static void debug( const char *file, int line,
                         const std::string &category, const std::string &message );
  
      /**
       *  @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
       */
      static void info( const char *file, int line,
                         const std::string &category, const std::string &message );
  
      /**
       *  @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
       */
      static void warning( const char *file, int line,
                         const std::string &category, const std::string &message );
  
      /**
       *  @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
       */
      static void error( const char *file, int line,
                         const std::string &category, const std::string &message );
  
      /**
       *  @return The current log level.
       */
      static LogLevel level()
      {
          return s_logLevel;
      }
  
  protected:
      /**
       * @brief Log a debug message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
      static void trace(const std::string &category, const std::string &message);
  
      /**
       * @brief Log a debug message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
      static void debug(const std::string &category, const std::string &message);
  
      /**
       * @brief Log an info message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
      static void info(const std::string &category, const std::string &message);
  
      /**
       * @brief Log a warning message in a category.
       * @param category The category of the message.
       * @param message The string to print.
       */
      static void warning(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.
       */
      static void error(const std::string &category, const std::string &message);
  
      //! Create a convenient timestamp
      static std::string getDateTime();
  
      /**
       * @brief Write the message to the logfile
       * @param category The category of the message.
       * @param message The string to print.
       * @param level Selected log level
       */
      static void writeLog(const std::string &category,
          const std::string &message,
          LogLevel level);
  
      /**
       * @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 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);
  
84738f56   Peter M. Groen   Setting up the lo...
198
199
200
201
202
203
204
205
206
207
      /**
       * @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 );
  
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
208
209
210
211
212
213
214
215
216
      //! 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;
  };
f23cf8e6   Peter M. Groen   Setting up the lo...
217
218
219
220
  
  }   /* End namespace mqtt */
  }   /* End namespace components */
  }   /* End namespace osdev */