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