mc_rtc  2.14.0
logging.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_rtc/utils_api.h>
8 
10 
11 #include <spdlog/fmt/fmt.h>
12 #include <spdlog/fmt/ostr.h>
13 #include <spdlog/logger.h>
14 
15 #define BOOST_STACKTRACE_LINK
16 #include <boost/stacktrace.hpp>
17 
18 namespace mc_rtc
19 {
20 
21 namespace log
22 {
23 
24 namespace details
25 {
26 
27 MC_RTC_UTILS_DLLAPI spdlog::logger & success();
28 
29 MC_RTC_UTILS_DLLAPI spdlog::logger & info();
30 
31 MC_RTC_UTILS_DLLAPI spdlog::logger & cerr();
32 
33 MC_RTC_UTILS_DLLAPI void notify(const std::string & message);
34 
36 
37 } // namespace details
38 
39 template<typename ExceptionT = std::runtime_error, typename... Args>
40 void error_and_throw [[noreturn]] (Args &&... args)
41 {
42  auto message = fmt::format(std::forward<Args>(args)...);
43  details::notify(message);
44  details::cerr().critical(message);
45  details::cerr().critical("=== Backtrace ===\n{}", MC_FMT_STREAMED(boost::stacktrace::stacktrace()));
46  throw ExceptionT(message);
47 }
48 
49 template<typename... Args>
50 void critical(Args &&... args)
51 {
52  details::cerr().critical(std::forward<Args>(args)...);
53 }
54 
55 template<typename... Args>
56 void error(Args &&... args)
57 {
58  details::cerr().error(std::forward<Args>(args)...);
59 }
60 
61 template<typename... Args>
62 void warning(Args &&... args)
63 {
64  details::cerr().warn(std::forward<Args>(args)...);
65 }
66 
67 template<typename... Args>
68 void info(Args &&... args)
69 {
70  details::info().info(std::forward<Args>(args)...);
71 }
72 
73 template<typename... Args>
74 void success(Args &&... args)
75 {
76  details::success().info(std::forward<Args>(args)...);
77 }
78 
79 template<typename... Args>
80 void notify(Args &&... args)
81 {
82  details::notify(fmt::format(std::forward<Args>(args)...));
83 }
84 
85 } // namespace log
86 
87 } // namespace mc_rtc
88 
89 #ifndef WIN32
90 
91 namespace mc_rtc
92 {
93 
94 constexpr auto OUT_NONE = "\033[00m";
95 constexpr auto OUT_BLUE = "\033[01;34m";
96 constexpr auto OUT_GREEN = "\033[01;32m";
97 constexpr auto OUT_PURPLE = "\033[01;35m";
98 constexpr auto OUT_RED = "\033[01;31m";
99 
100 } // namespace mc_rtc
101 
102 # define LOG_ERROR(args) \
103  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::error instead\""); \
104  std::cerr << mc_rtc::OUT_RED << args << mc_rtc::OUT_NONE << "\n";
105 # define LOG_WARNING(args) \
106  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::warning instead\""); \
107  std::cerr << mc_rtc::OUT_PURPLE << args << mc_rtc::OUT_NONE << "\n";
108 # define LOG_INFO(args) \
109  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::info instead\""); \
110  std::cout << mc_rtc::OUT_BLUE << args << mc_rtc::OUT_NONE << "\n";
111 # define LOG_SUCCESS(args) \
112  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::success instead\""); \
113  std::cout << mc_rtc::OUT_GREEN << args << mc_rtc::OUT_NONE << "\n";
114 
115 # define LOG_ERROR_AND_THROW(exception_type, args) \
116  { \
117  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::error_and_throw<exception_type> instead\""); \
118  std::stringstream strstrm; \
119  strstrm << args; \
120  LOG_ERROR(strstrm.str()) \
121  throw exception_type(strstrm.str()); \
122  }
123 
124 #else
125 
126 # include <windows.h>
127 namespace mc_rtc
128 {
129 static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
130 constexpr auto OUT_NONE = 15;
131 constexpr auto OUT_BLUE = 11;
132 constexpr auto OUT_GREEN = 10;
133 constexpr auto OUT_PURPLE = 13;
134 constexpr auto OUT_RED = 12;
135 } // namespace mc_rtc
136 
137 # define __MC_RTC_STR2__(x) #x
138 # define __MC_RTC_STR1__(x) __MC_RTC_STR2__(x)
139 # define __MC_RTC_PRAGMA_LOC__ __FILE__ "("__MC_RTC_STR1__(__LINE__) ") "
140 
141 # define LOG_ERROR(args) \
142  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::error instead")); \
143  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_RED); \
144  std::cerr << args << std::endl; \
145  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
146 
147 # define LOG_WARNING(args) \
148  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::warning instead")); \
149  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_PURPLE); \
150  std::cerr << args << std::endl; \
151  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
152 
153 # define LOG_INFO(args) \
154  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::info instead")); \
155  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_BLUE); \
156  std::cout << args << std::endl; \
157  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
158 
159 # define LOG_SUCCESS(args) \
160  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::success instead")); \
161  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_GREEN); \
162  std::cout << args << std::endl; \
163  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
164 
165 # define LOG_ERROR_AND_THROW(exception_type, args) \
166  { \
167  __pragma( \
168  message(__MC_RTC_PRAGMA_LOC__ \
169  ": warning: this macro is deprecated, use mc_rtc::log::error_and_throw<exception_type> instead")); \
170  std::stringstream strstrm; \
171  strstrm << args; \
172  LOG_ERROR(strstrm.str()) \
173  throw exception_type(strstrm.str()); \
174  }
175 
176 #endif
#define MC_FMT_STREAMED(X)
Definition: fmt_formatters.h:164
MC_RTC_UTILS_DLLAPI spdlog::logger & info()
MC_RTC_UTILS_DLLAPI spdlog::logger & success()
MC_RTC_UTILS_DLLAPI void disable_notifications()
MC_RTC_UTILS_DLLAPI void notify(const std::string &message)
MC_RTC_UTILS_DLLAPI spdlog::logger & cerr()
void info(Args &&... args)
Definition: logging.h:68
void error_and_throw(Args &&... args)
Definition: logging.h:40
void success(Args &&... args)
Definition: logging.h:74
void notify(Args &&... args)
Definition: logging.h:80
void critical(Args &&... args)
Definition: logging.h:50
void warning(Args &&... args)
Definition: logging.h:62
void error(Args &&... args)
Definition: logging.h:56
Definition: Contact.h:88
constexpr auto OUT_BLUE
Definition: logging.h:95
constexpr auto OUT_GREEN
Definition: logging.h:96
constexpr auto OUT_RED
Definition: logging.h:98
constexpr auto OUT_NONE
Definition: logging.h:94
constexpr auto OUT_PURPLE
Definition: logging.h:97
#define MC_RTC_UTILS_DLLAPI
Definition: utils_api.h:50