VSM C++ SDK
Vehicle Specific Modules SDK
log.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef LOG_H_
12 #define 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 
35 public:
36 
39 
41  enum class Level {
45  DEBUGGING,
47  INFO,
51  WARNING,
53  ERROR,
54 
56  MAX = ERROR
57  };
58 
61  public:
63  static std::unique_ptr<Platform_logger>
64  Create(Log &log);
65 
68  log(log)
69  {}
70 
71  virtual
73  {}
74 
76  virtual void
77  Write_message(Level level, const char *msg, std::va_list args)
78  __FORMAT(printf, 3, 0) = 0;
79 
80  protected:
81 
83  Log &log;
84  };
85 
87  Log();
88 
90  ~Log();
91 
93  static const char *
94  Get_level_str(Level level);
95 
97  static std::string
98  Get_basename(const char *path);
99 
103  static std::string
105 
111  static void
112  Write_message(Level level, const char *msg, ...) __FORMAT(printf, 2, 3);
113 
120  static void
121  Write_message_v(Level level, const char *msg, std::va_list args) __FORMAT(printf, 2, 0);
122 
128  void
129  Write_message_inst(Level level, const char *msg, ...) __FORMAT(printf, 3, 4);
130 
137  void
138  Write_message_v_inst(Level level, const char *msg, std::va_list args) __FORMAT(printf, 3, 0);
139 
143  static void
145  {
146  /* No need for separate lock because concurrent modification in any case
147  * will give unpredictable result.
148  */
149  Get_instance()->cur_level = level;
150  }
151 
157  static void
158  Set_level(const std::string &level);
159 
166  static void
167  Set_custom_log(const std::string &log_file)
168  {
169  Get_instance()->Set_custom_log_inst(log_file);
170  }
171 
173  void
174  Set_custom_log_inst(const std::string &log_file);
175 
188  static void
189  Set_max_custom_log_size(const std::string& size_str);
190 
196  static void
197  Set_max_custom_log_count(size_t count);
198 
203  void
204  Set_max_custom_log_size_inst(ssize_t size);
205 
208  void
209  Set_max_custom_log_count_inst(size_t count);
210 
211 private:
212  friend class Platform_logger;
213 
215  constexpr static ssize_t MIN_CUSTOM_LOG_FILE_SIZE = 16384;
216 
218  constexpr static ssize_t DEFAULT_MAX_CUSTOM_LOG_FILE_SIZE = 100 * 1024 * 1024;
219 
221  constexpr static const char* const LOG_FILE_ROTATOR_SUFFIX_FORMAT = "_%Y%m%d-%H%M%S";
222 
226  constexpr static const char* const LOG_FILE_ROTATOR_FIND_PATTERN = "_\?\?\?\?\?\?\?\?-\?\?\?\?\?\?*";
227 
229  Level cur_level =
230 # ifdef DEBUG
232 # else /* DEBUG */
233  Level::INFO;
234 # endif /* DEBUG */
235 
237  std::mutex mutex;
239  static volatile Log *log;
241  bool use_console = true;
243  std::unique_ptr<Platform_logger> plat_logger;
245  std::string custom_log_file_name;
247  FILE* custom_log_file = nullptr;
249  ssize_t max_custom_log = DEFAULT_MAX_CUSTOM_LOG_FILE_SIZE;
251  ssize_t custom_log_size;
252 
254  size_t custom_log_count = 1;
255 
257  static Log *
258  Get_instance();
259 
261  bool
262  Reopen_custom_log_file(const std::string &log_file);
263 
265  void
266  Write_custom_message(int thread_id, Log::Level level, const char *msg,
267  std::va_list args) __FORMAT(printf, 4, 0);
268 
275  void
276  Write_console_message_inst(int thread_id, Level level, const char *msg, ...) __FORMAT(printf, 4, 5);
277 
285  void
286  Write_console_message_v_inst(int thread_id, Level level, const char *msg,
287  std::va_list args) __FORMAT(printf, 4, 0);
288 
295  static int
296  Vprintf_utf8(const char *format, std::va_list args) __FORMAT(printf, 1, 0);
297 
303  static int
304  Printf_utf8(const char *format, ...) __FORMAT(printf, 1, 2);
305 
307  bool
308  Is_cleanup_needed();
309 
311  void
312  Do_cleanup(int thread_id);
313 
315  void
316  Remove_old_log_files();
317 };
318 
320 #define _LOG_WRITE_MSG(level, msg, ...) \
321  ::ugcs::vsm::Log::Write_message(level, "[%s:%d] " msg, \
322  ::ugcs::vsm::Log::Get_basename(__FILE__).c_str(), \
323  __LINE__, ## __VA_ARGS__)
324 
326 #define LOG_DEBUG(msg, ...) \
327  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::DEBUGGING, msg, ## __VA_ARGS__)
328 
330 #define LOG_DBG LOG_DEBUG
331 
332 #define LOG LOG_DEBUG
333 
335 #define LOG_INFO(msg, ...) \
336  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::INFO, msg, ## __VA_ARGS__)
337 
339 #define LOG_WARNING(msg, ...) \
340  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::WARNING, msg, ## __VA_ARGS__)
341 
343 #define LOG_WARN LOG_WARNING
344 
346 #define LOG_ERROR(msg, ...) \
347  _LOG_WRITE_MSG(::ugcs::vsm::Log::Level::ERROR, msg, ## __VA_ARGS__)
348 
350 #define LOG_ERR LOG_ERROR
351 
352 } /* namespace vsm */
353 } /* namespace ugcs */
354 
355 #endif /* LOG_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Informational messages.
static std::string Get_system_error()
Platform-dependent convenience method to get descriptive string for last system error.
Exception class with one parameter.
Definition: exception.h:88
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:67
Non-recoverable errors which should be noticed by an operator.
STL namespace.
Log & log
Reference to abstract logger.
Definition: log.h:83
static void Set_custom_log(const std::string &log_file)
Set log file path for a custom (i.e.
Definition: log.h:167
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:41
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.
#define VSM_DEFINE_EXCEPTION(__exc_class,...)
Define custom exception type.
Definition: exception.h:202
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:144
Platform-specific logging handler.
Definition: log.h:60
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.
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.