VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
log.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef _UGCS_VSM_LOG_H_
12 #define _UGCS_VSM_LOG_H_
13 
14 #include <ugcs/vsm/exception.h>
15 
16 #include <stdlib.h>
17 
18 #include <cstdio>
19 #include <cstdarg>
20 #include <string>
21 #include <memory>
22 #include <mutex>
23 #include <cinttypes>
24 
25 namespace ugcs {
26 namespace vsm {
27 
33 class Log {
34 public:
37 
39  enum class Level {
43  DEBUGGING,
45  INFO,
49  WARNING,
51  ERROR,
52 
54  MAX = ERROR
55  };
56 
59  public:
61  static std::unique_ptr<Platform_logger>
62  Create(Log &log);
63 
66  log(log)
67  {}
68 
69  virtual
71  {}
72 
74  virtual void
75  Write_message(Level level, const char *msg, std::va_list args)
76  __FORMAT(printf, 3, 0) = 0;
77 
78  protected:
80  Log &log;
81  };
82 
84  Log();
85 
87  ~Log();
88 
90  static const char *
91  Get_level_str(Level level);
92 
94  static std::string
95  Get_basename(const char *path);
96 
100  static std::string
102 
108  static void
109  Write_message(Level level, const char *msg, ...) __FORMAT(printf, 2, 3);
110 
117  static void
118  Write_message_v(Level level, const char *msg, std::va_list args) __FORMAT(printf, 2, 0);
119 
125  void
126  Write_message_inst(Level level, const char *msg, ...) __FORMAT(printf, 3, 4);
127 
134  void
135  Write_message_v_inst(Level level, const char *msg, std::va_list args) __FORMAT(printf, 3, 0);
136 
140  static void
142  {
143  /* No need for separate lock because concurrent modification in any case
144  * will give unpredictable result.
145  */
146  Get_instance()->cur_level = level;
147  }
148 
154  static void
155  Set_level(const std::string &level);
156 
163  static void
164  Set_custom_log(const std::string &log_file)
165  {
166  Get_instance()->Set_custom_log_inst(log_file);
167  }
168 
170  void
171  Set_custom_log_inst(const std::string &log_file);
172 
185  static void
186  Set_max_custom_log_size(const std::string& size_str);
187 
193  static void
194  Set_max_custom_log_count(size_t count);
195 
200  void
201  Set_max_custom_log_size_inst(ssize_t size);
202 
205  void
206  Set_max_custom_log_count_inst(size_t count);
207 
208 private:
209  friend class Platform_logger;
210 
212  constexpr static ssize_t MIN_CUSTOM_LOG_FILE_SIZE = 16384;
213 
215  constexpr static ssize_t DEFAULT_MAX_CUSTOM_LOG_FILE_SIZE = 100 * 1024 * 1024;
216 
218  constexpr static const char* const LOG_FILE_ROTATOR_SUFFIX_FORMAT = "_%Y%m%d-%H%M%S";
219 
223  constexpr static const char* const LOG_FILE_ROTATOR_FIND_PATTERN = "_\?\?\?\?\?\?\?\?-\?\?\?\?\?\?*";
224 
226  Level cur_level =
227 # ifdef DEBUG
229 # else /* DEBUG */
230  Level::INFO;
231 # endif /* DEBUG */
232 
234  std::mutex mutex;
236  static volatile Log *log;
238  bool use_console = true;
240  std::unique_ptr<Platform_logger> plat_logger;
242  std::string custom_log_file_name;
244  FILE* custom_log_file = nullptr;
246  ssize_t max_custom_log = DEFAULT_MAX_CUSTOM_LOG_FILE_SIZE;
248  ssize_t custom_log_size;
249 
251  size_t custom_log_count = 1;
252 
254  static Log *
255  Get_instance();
256 
258  bool
259  Reopen_custom_log_file(const std::string &log_file);
260 
262  void
263  Write_custom_message(int thread_id, Log::Level level, const char *msg,
264  std::va_list args) __FORMAT(printf, 4, 0);
265 
272  void
273  Write_console_message_inst(int thread_id, Level level, const char *msg, ...) __FORMAT(printf, 4, 5);
274 
282  void
283  Write_console_message_v_inst(int thread_id, Level level, const char *msg,
284  std::va_list args) __FORMAT(printf, 4, 0);
285 
292  static int
293  Vprintf_utf8(const char *format, std::va_list args) __FORMAT(printf, 1, 0);
294 
300  static int
301  Printf_utf8(const char *format, ...) __FORMAT(printf, 1, 2);
302 
304  bool
305  Is_cleanup_needed();
306 
308  void
309  Do_cleanup(int thread_id);
310 
312  void
313  Remove_old_log_files();
314 };
315 
317 #define _LOG_WRITE_MSG(level, msg, ...) \
318  ::ugcs::vsm::Log::Write_message(level, "[%s:%d] " msg, \
319  ::ugcs::vsm::Log::Get_basename(__FILE__).c_str(), \
320  __LINE__, ## __VA_ARGS__)
321 
323 #define LOG_DEBUG(msg, ...) \
324  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::DEBUGGING, msg, ## __VA_ARGS__)
325 
327 #define LOG_DBG LOG_DEBUG
328 
329 #define LOG LOG_DEBUG
330 
332 #define LOG_INFO(msg, ...) \
333  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::INFO, msg, ## __VA_ARGS__)
334 
336 #define LOG_WARNING(msg, ...) \
337  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::WARNING, msg, ## __VA_ARGS__)
338 
340 #define LOG_WARN LOG_WARNING
341 
343 #define LOG_ERROR(msg, ...) \
344  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::ERROR, msg, ## __VA_ARGS__)
345 
347 #define LOG_ERR LOG_ERROR
348 
349 } /* namespace vsm */
350 } /* namespace ugcs */
351 
352 #endif /* _UGCS_VSM_LOG_H_ */
Informational messages.
static std::string Get_system_error()
Platform-dependent convenience method to get descriptive string for last system error.
Most verbose debug level.
static void Set_max_custom_log_size(const std::string &size_str)
Set maximum size of a single custom log file.
~Log()
Destructor.
Class for handling log output.
Definition: log.h:33
Platform_logger(Log &log)
Construct platform logger to work with abstract logger.
Definition: log.h:65
Non-recoverable errors which should be noticed by an operator.
Log & log
Reference to abstract logger.
Definition: log.h:80
static void Set_custom_log(const std::string &log_file)
Set log file path for a custom (i.e.
Definition: log.h:164
static const char * Get_level_str(Level level)
Convert level value to readable string.
static std::string Get_basename(const char *path)
Get base name (without leading directories) of file path.
VSM exceptions definition.
Level
Available log levels.
Definition: log.h:39
Log()
Constructor.
static void Write_message_v(Level level, const char *msg, std::va_list args)
Write formatted message to the log.
void Set_max_custom_log_count_inst(size_t count)
static void Set_max_custom_log_count(size_t count)
Set maximum number of log files to keep.
#define __FORMAT(type, fmt_idx, arg_idx)
Specify that a function has format arguments (like printf or scanf).
Definition: defs.h:24
Maximal level code.
static void Write_message(Level level, const char *msg,...)
Write formatted message to the log.
void Write_message_v_inst(Level level, const char *msg, std::va_list args)
Write formatted message to the log.
static void Set_level(Level level)
Set current log level for the application.
Definition: log.h:141
static std::unique_ptr< Platform_logger > Create(Log &log)
Create unique instance.
virtual void Write_message(Level level, const char *msg, std::va_list args)=0
Write formatted message.
Platform-specific logging handler.
Definition: log.h:58
void Set_custom_log_inst(const std::string &log_file)
Set custom file path for the instance itself, behaves like Set_custom_log.
Warning messages which should be noticed by an operator.
VSM_DEFINE_EXCEPTION(Exception)
Logging related exception.
void Set_max_custom_log_size_inst(ssize_t size)
The same as Set_max_custom_log_size, but for the instance.
void Write_message_inst(Level level, const char *msg,...)
Write formatted message to the log.
Base class for all VSM exceptions.
Definition: exception.h:22