logging.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #ifndef PSEN_SCAN_V2_STANDALONE_LOGGING_H
17 #define PSEN_SCAN_V2_STANDALONE_LOGGING_H
18 
19 #include <chrono>
20 #include <console_bridge/console.h>
21 #include <sstream>
22 
23 #include <fmt/format.h>
24 #include <fmt/ostream.h>
25 
26 #define PSENSCAN_LOG(name, file, line, level, ...) \
27  do \
28  { \
29  console_bridge::log(file, line, level, fmt::format("{}: {}", name, fmt::format(__VA_ARGS__)).c_str()); \
30  } while (false) // https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block
31 
32 #define PSENSCAN_LOG_ONCE(name, file, line, level, ...) \
33  do \
34  { \
35  static bool already_logged = false; \
36  if (!already_logged) \
37  { \
38  console_bridge::log(file, line, level, fmt::format("{}: {}", name, fmt::format(__VA_ARGS__)).c_str()); \
39  already_logged = true; \
40  } \
41  } while (false)
42 
43 #define PSENSCAN_LOG_THROTTLE(period, name, file, line, level, ...) \
44  PSENSCAN_LOG_THROTTLE_INTERNAL(std::chrono::system_clock::now(), period, name, file, line, level, __VA_ARGS__)
45 
46 #define PSENSCAN_LOG_THROTTLE_INTERNAL(now, period, name, file, line, level, ...) \
47  do \
48  { \
49  static std::chrono::system_clock::time_point throttle_last_hit; \
50  auto throttle_now = now; \
51  if (throttle_last_hit + std::chrono::duration<double>(period) < throttle_now) \
52  { \
53  throttle_last_hit = throttle_now; \
54  PSENSCAN_LOG(name, file, line, level, __VA_ARGS__); \
55  } \
56  } while (false) // https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block
57 
58 using namespace console_bridge;
59 
60 #define PSENSCAN_ERROR(name, ...) PSENSCAN_LOG(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_ERROR, __VA_ARGS__)
61 #define PSENSCAN_INFO(name, ...) PSENSCAN_LOG(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_INFO, __VA_ARGS__)
62 #define PSENSCAN_WARN(name, ...) PSENSCAN_LOG(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_WARN, __VA_ARGS__)
63 #define PSENSCAN_DEBUG(name, ...) PSENSCAN_LOG(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_DEBUG, __VA_ARGS__)
64 
65 #define PSENSCAN_ERROR_THROTTLE_INTERNAL(now, period, name, ...) \
66  PSENSCAN_LOG_THROTTLE_INTERNAL(now, period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_ERROR, __VA_ARGS__)
67 #define PSENSCAN_INFO_THROTTLE_INTERNAL(now, period, name, ...) \
68  PSENSCAN_LOG_THROTTLE_INTERNAL(now, period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_INFO, __VA_ARGS__)
69 #define PSENSCAN_WARN_THROTTLE_INTERNAL(now, period, name, ...) \
70  PSENSCAN_LOG_THROTTLE_INTERNAL(now, period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_WARN, __VA_ARGS__)
71 #define PSENSCAN_DEBUG_THROTTLE_INTERNAL(now, period, name, ...) \
72  PSENSCAN_LOG_THROTTLE_INTERNAL(now, period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_DEBUG, __VA_ARGS__)
73 
74 #define PSENSCAN_ERROR_THROTTLE(period, name, ...) \
75  PSENSCAN_LOG_THROTTLE(period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_ERROR, __VA_ARGS__)
76 #define PSENSCAN_INFO_THROTTLE(period, name, ...) \
77  PSENSCAN_LOG_THROTTLE(period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_INFO, __VA_ARGS__)
78 #define PSENSCAN_WARN_THROTTLE(period, name, ...) \
79  PSENSCAN_LOG_THROTTLE(period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_WARN, __VA_ARGS__)
80 #define PSENSCAN_DEBUG_THROTTLE(period, name, ...) \
81  PSENSCAN_LOG_THROTTLE(period, name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_DEBUG, __VA_ARGS__)
82 
83 #define PSENSCAN_ERROR_ONCE(name, ...) \
84  PSENSCAN_LOG_ONCE(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_ERROR, __VA_ARGS__)
85 #define PSENSCAN_INFO_ONCE(name, ...) PSENSCAN_LOG_ONCE(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_INFO, __VA_ARGS__)
86 #define PSENSCAN_WARN_ONCE(name, ...) PSENSCAN_LOG_ONCE(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_WARN, __VA_ARGS__)
87 #define PSENSCAN_DEBUG_ONCE(name, ...) \
88  PSENSCAN_LOG_ONCE(name, __FILE__, __LINE__, CONSOLE_BRIDGE_LOG_DEBUG, __VA_ARGS__)
89 
90 #endif // PSEN_SCAN_V2_STANDALONE_LOGGING_H


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Nov 5 2022 02:13:36