ifm3d
log.h
1 // -*- c++ -*-
2 /*
3  * Copyright 2023-present ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_COMMON_LOGGING_LOG_H
8 #define IFM3D_COMMON_LOGGING_LOG_H
9 
10 #include <cstring>
11 
12 #include <ifm3d/common/logging/log_entry.h>
13 #include <ifm3d/common/logging/logger.h>
14 #include <ifm3d/common/logging/log_level.h>
15 
16 #include <fmt/format.h>
17 
18 #ifdef _MSC_VER
19 # define LOG_GET_FUNC() __FUNCTION__
20 #else
21 # define LOG_GET_FUNC() __PRETTY_FUNCTION__
22 #endif
23 
24 #ifndef IFM3D_COMMON_LOGGING_STRIP_PREFIX
25 # define IFM3D_COMMON_LOGGING_STRIP_PREFIX ""
26 #endif
27 
28 namespace ifm3d
29 {
30  constexpr const char*
31  LOG_STRIP_PREFIX(const char* ptr, const char* prefix)
32  {
33  return ::std::strncmp(ptr, prefix, ::std::strlen(prefix)) == 0 ?
34  (ptr + ::std::strlen(prefix)) :
35  ptr;
36  }
37 }
38 
39 #define LOG_GET_FILE() \
40  ::ifm3d::LOG_STRIP_PREFIX(__FILE__, IFM3D_COMMON_LOGGING_STRIP_PREFIX)
41 #define LOG_GET_LINE() __LINE__
42 
43 #define LOG_IF_(condition) \
44  if (!(condition)) \
45  { \
46  ; \
47  } \
48  else
49 
50 #define LOG(log_level, msg, ...) \
51  LOG_IF_(::ifm3d::Logger::Get().ShouldLog(log_level)) \
52  { \
53  ::ifm3d::Logger::Get().Write( \
54  ::ifm3d::LogEntry(::fmt::format(msg, ##__VA_ARGS__), \
55  log_level, \
56  LOG_GET_FILE(), \
57  LOG_GET_FUNC(), \
58  LOG_GET_LINE())); \
59  }
60 
61 #define LOG_VERBOSE(fmt, ...) \
62  LOG(::ifm3d::LogLevel::Verbose, fmt, ##__VA_ARGS__)
63 #define LOG_DEBUG(fmt, ...) LOG(::ifm3d::LogLevel::Debug, fmt, ##__VA_ARGS__)
64 #define LOG_INFO(fmt, ...) LOG(::ifm3d::LogLevel::Info, fmt, ##__VA_ARGS__)
65 #define LOG_WARNING(fmt, ...) \
66  LOG(::ifm3d::LogLevel::Warning, fmt, ##__VA_ARGS__)
67 #define LOG_ERROR(fmt, ...) LOG(::ifm3d::LogLevel::Error, fmt, ##__VA_ARGS__)
68 #define LOG_CRITICAL(fmt, ...) \
69  LOG(::ifm3d::LogLevel::Critical, fmt, ##__VA_ARGS__)
70 
71 #endif // IFM3D_COMMON_LOGGING_LOG_H