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 S, typename... Args>
40 void error_and_throw [[noreturn]] (const S & format, Args &&... args)
41 {
42  std::string message;
43  if constexpr(sizeof...(Args) == 0) { message = fmt::format("{}", format); }
44  else
45  {
46  message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
47  }
49  details::cerr().critical(message);
50  details::cerr().critical("=== Backtrace ===\n{}", MC_FMT_STREAMED(boost::stacktrace::stacktrace()));
51  throw ExceptionT(message);
52 }
53 
54 template<typename S, typename... Args>
55 void critical(const S & format, Args &&... args)
56 {
57  if constexpr(sizeof...(Args) == 0) { details::cerr().critical(format); }
58  else
59  {
60  details::cerr().critical(fmt::runtime(format), std::forward<Args>(args)...);
61  }
62 }
63 
64 template<typename S, typename... Args>
65 void error(const S & format, Args &&... args)
66 {
67  if constexpr(sizeof...(Args) == 0) { details::cerr().error(format); }
68  else
69  {
70  details::cerr().error(fmt::runtime(format), std::forward<Args>(args)...);
71  }
72 }
73 
74 template<typename S, typename... Args>
75 void warning(const S & format, Args &&... args)
76 {
77  if constexpr(sizeof...(Args) == 0) { details::cerr().warn(format); }
78  else
79  {
80  details::cerr().warn(fmt::runtime(format), std::forward<Args>(args)...);
81  }
82 }
83 
84 template<typename S, typename... Args>
85 void info(const S & format, Args &&... args)
86 {
87  if constexpr(sizeof...(Args) == 0) { details::info().info(format); }
88  else
89  {
90  details::info().info(fmt::runtime(format), std::forward<Args>(args)...);
91  }
92 }
93 
94 template<typename S, typename... Args>
95 void success(const S & format, Args &&... args)
96 {
97  if constexpr(sizeof...(Args) == 0) { details::success().info(format); }
98  else
99  {
100  details::success().info(fmt::runtime(format), std::forward<Args>(args)...);
101  }
102 }
103 
104 template<typename S, typename... Args>
105 void notify(const S & format, Args &&... args)
106 {
107  if constexpr(sizeof...(Args) == 0) { details::notify(fmt::format("{}", format)); }
108  else
109  {
110  details::notify(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
111  }
112 }
113 
114 } // namespace log
115 
116 } // namespace mc_rtc
117 
118 #ifndef WIN32
119 
120 namespace mc_rtc
121 {
122 
123 constexpr auto OUT_NONE = "\033[00m";
124 constexpr auto OUT_BLUE = "\033[01;34m";
125 constexpr auto OUT_GREEN = "\033[01;32m";
126 constexpr auto OUT_PURPLE = "\033[01;35m";
127 constexpr auto OUT_RED = "\033[01;31m";
128 
129 } // namespace mc_rtc
130 
131 # define LOG_ERROR(args) \
132  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::error instead\""); \
133  std::cerr << mc_rtc::OUT_RED << args << mc_rtc::OUT_NONE << "\n";
134 # define LOG_WARNING(args) \
135  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::warning instead\""); \
136  std::cerr << mc_rtc::OUT_PURPLE << args << mc_rtc::OUT_NONE << "\n";
137 # define LOG_INFO(args) \
138  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::info instead\""); \
139  std::cout << mc_rtc::OUT_BLUE << args << mc_rtc::OUT_NONE << "\n";
140 # define LOG_SUCCESS(args) \
141  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::success instead\""); \
142  std::cout << mc_rtc::OUT_GREEN << args << mc_rtc::OUT_NONE << "\n";
143 
144 # define LOG_ERROR_AND_THROW(exception_type, args) \
145  { \
146  _Pragma("GCC warning \"This macro is deprecated, use mc_rtc::log::error_and_throw<exception_type> instead\""); \
147  std::stringstream strstrm; \
148  strstrm << args; \
149  LOG_ERROR(strstrm.str()) \
150  throw exception_type(strstrm.str()); \
151  }
152 
153 #else
154 
155 # include <windows.h>
156 namespace mc_rtc
157 {
158 static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
159 constexpr auto OUT_NONE = 15;
160 constexpr auto OUT_BLUE = 11;
161 constexpr auto OUT_GREEN = 10;
162 constexpr auto OUT_PURPLE = 13;
163 constexpr auto OUT_RED = 12;
164 } // namespace mc_rtc
165 
166 # define __MC_RTC_STR2__(x) #x
167 # define __MC_RTC_STR1__(x) __MC_RTC_STR2__(x)
168 # define __MC_RTC_PRAGMA_LOC__ __FILE__ "("__MC_RTC_STR1__(__LINE__) ") "
169 
170 # define LOG_ERROR(args) \
171  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::error instead")); \
172  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_RED); \
173  std::cerr << args << std::endl; \
174  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
175 
176 # define LOG_WARNING(args) \
177  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::warning instead")); \
178  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_PURPLE); \
179  std::cerr << args << std::endl; \
180  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
181 
182 # define LOG_INFO(args) \
183  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::info instead")); \
184  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_BLUE); \
185  std::cout << args << std::endl; \
186  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
187 
188 # define LOG_SUCCESS(args) \
189  __pragma(message(__MC_RTC_PRAGMA_LOC__ ": warning: this macro is deprecated, use mc_rtc::log::success instead")); \
190  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_GREEN); \
191  std::cout << args << std::endl; \
192  SetConsoleTextAttribute(mc_rtc::hConsole, mc_rtc::OUT_NONE);
193 
194 # define LOG_ERROR_AND_THROW(exception_type, args) \
195  { \
196  __pragma( \
197  message(__MC_RTC_PRAGMA_LOC__ \
198  ": warning: this macro is deprecated, use mc_rtc::log::error_and_throw<exception_type> instead")); \
199  std::stringstream strstrm; \
200  strstrm << args; \
201  LOG_ERROR(strstrm.str()) \
202  throw exception_type(strstrm.str()); \
203  }
204 
205 #endif
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 notify(const S &format, Args &&... args)
Definition: logging.h:105
void error_and_throw(const S &format, Args &&... args)
Definition: logging.h:40
void critical(const S &format, Args &&... args)
Definition: logging.h:55
void warning(const S &format, Args &&... args)
Definition: logging.h:75
void info(const S &format, Args &&... args)
Definition: logging.h:85
void error(const S &format, Args &&... args)
Definition: logging.h:65
void success(const S &format, Args &&... args)
Definition: logging.h:95
Definition: Contact.h:88
constexpr auto OUT_BLUE
Definition: logging.h:124
constexpr auto OUT_GREEN
Definition: logging.h:125
constexpr auto OUT_RED
Definition: logging.h:127
constexpr auto OUT_NONE
Definition: logging.h:123
constexpr auto OUT_PURPLE
Definition: logging.h:126
message
#define MC_RTC_UTILS_DLLAPI
Definition: utils_api.h:50