Himalaya
Logger.hpp
Go to the documentation of this file.
1 // ====================================================================
2 // This file is part of Himalaya.
3 //
4 // Himalaya is licenced under the GNU General Public License (GNU GPL)
5 // version 3.
6 // ====================================================================
7 
8 #pragma once
9 
10 #include <iostream>
11 
12 /**
13  * @file Logger.hpp
14  * @brief Implementation of logging macros.
15  *
16  * The following message logger macros are available:
17  *
18  * VERBOSE_MSG(message) prints a verbose message
19  * DEBUG_MSG(message) prints a debug message
20  * INFO_MSG(message) prints information
21  * WARNING_MSG(message) prints a warning
22  * ERROR_MSG(message) prints an error
23  * FATAL_MSG(message) prints an error and throws an exception
24  *
25  * Verbose messages are only printed in ENABLE_VERBOSE is defined.
26  * Debug messages are only printed in ENABLE_DEBUG is defined.
27  * Colored output can be enabled if ENABLE_COLORS is defined.
28  * All output can be disabled if ENABLE_SILENT is defined.
29  */
30 
31 /**
32  * The following definitions may be put into a config.h file,
33  * generated by cmake.
34  */
35 #define ENABLE_COLORS
36 #define ENABLE_DEBUG
37 #define ENABLE_VERBOSE
38 #undef ENABLE_SILENT
39 
40 namespace himalaya {
41 
42 /**
43  * Enum representing the kind of output.
44  */
46 
47 } // namespace himalaya
48 
49 // All macros print to std::cerr by default in order to not interfere
50 // with normal Himalaya output
51 #define LOG_OUTPUT_STREAM std::cerr
52 
53 #ifdef ENABLE_VERBOSE
54  #define VERBOSE_MSG(message) LOG(himalaya::kVerbose, message)
55 #else
56  #define VERBOSE_MSG(message)
57 #endif
58 
59 #ifdef ENABLE_DEBUG
60  #define DEBUG_MSG(message) LOG(himalaya::kDebug, message)
61 #else
62  #define DEBUG_MSG(message)
63 #endif
64 
65 #define INFO_MSG(message) LOG(himalaya::kInfo, message)
66 #define WARNING_MSG(message) LOG(himalaya::kWarning, message)
67 #define ERROR_MSG(message) LOG(himalaya::kError, message)
68 
69 #ifdef ENABLE_SILENT
70  #define FATAL_MSG(message) \
71  do { \
72  exit(EXIT_FAILURE); \
73  throw std::runtime_error(message); \
74  } while (false)
75 #else
76  #define FATAL_MSG(message) \
77  do { \
78  LOG(himalaya::kFatal, message); \
79  throw std::runtime_error(message); \
80  } while (false)
81 #endif
82 
83 #ifdef ENABLE_SILENT
84  #define PRINT_PREFIX(level)
85 #else
86  #define PRINT_PREFIX(level) \
87  do { \
88  switch (level) { \
89  case himalaya::kVerbose: LOG_OUTPUT_STREAM << "Himalaya verbose: "; break; \
90  case himalaya::kDebug: LOG_OUTPUT_STREAM << "Himalaya debug: "; break; \
91  case himalaya::kInfo: LOG_OUTPUT_STREAM << "Himalaya info: "; break; \
92  case himalaya::kWarning: LOG_OUTPUT_STREAM << "Himalaya warning: "; break; \
93  case himalaya::kError: LOG_OUTPUT_STREAM << "Himalaya error: "; break; \
94  case himalaya::kFatal: LOG_OUTPUT_STREAM << "Himalaya fatal error: "; break; \
95  default: \
96  break; \
97  } \
98  } while (false)
99 #endif
100 
101 #ifdef ENABLE_SILENT
102  #define PRINT_FILE_LINE(level)
103 #else
104  #define PRINT_FILE_LINE(level) \
105  do { \
106  switch (level) { \
107  case himalaya::kFatal: \
108  LOG_OUTPUT_STREAM << "(file: " << __FILE__ \
109  << ", line: " << __LINE__ << ") "; \
110  break; \
111  default: \
112  break; \
113  } \
114  } while (false)
115 #endif
116 
117 #ifdef ENABLE_SILENT
118  #define PRINT_COLOR_CODE(level)
119 #else
120  #define PRINT_COLOR_CODE(level) \
121  do { \
122  switch (level) { \
123  case himalaya::kVerbose: LOG_OUTPUT_STREAM << "\033[0;36m"; break; \
124  case himalaya::kDebug: LOG_OUTPUT_STREAM << "\033[0;34m"; break; \
125  case himalaya::kInfo: LOG_OUTPUT_STREAM << "\033[1;34m"; break; \
126  case himalaya::kWarning: LOG_OUTPUT_STREAM << "\033[0;31m"; break; \
127  case himalaya::kError: LOG_OUTPUT_STREAM << "\033[1;31m"; break; \
128  case himalaya::kFatal: LOG_OUTPUT_STREAM << "\033[41;1;37m"; break; \
129  default: \
130  break; \
131  } \
132  } while (false)
133 #endif
134 
135 #ifdef ENABLE_SILENT
136  #define RESET_COLOR(level)
137 #else
138  #define RESET_COLOR(level) \
139  do { \
140  LOG_OUTPUT_STREAM << "\033[0m"; \
141  } while (false)
142 #endif
143 
144 #ifdef ENABLE_SILENT
145  #define PRINT_MESSAGE(level, message)
146 #else
147  #define PRINT_MESSAGE(level, message) \
148  do { \
149  LOG_OUTPUT_STREAM << message; \
150  } while (false)
151 #endif
152 
153 #ifdef ENABLE_SILENT
154  #define PRINT_ENDL(level)
155 #else
156  #define PRINT_ENDL(level) \
157  do { \
158  LOG_OUTPUT_STREAM << std::endl; \
159  } while (false)
160 #endif
161 
162 #ifdef ENABLE_SILENT
163  #define LOG(level, message)
164 #else
165  #ifdef ENABLE_COLORS
166  #define LOG(level, message) \
167  do { \
168  PRINT_COLOR_CODE(level); \
169  PRINT_PREFIX(level); \
170  PRINT_FILE_LINE(level); \
171  RESET_COLOR(level); \
172  PRINT_MESSAGE(level, message); \
173  PRINT_ENDL(level); \
174  } while (false)
175  #else
176  #define LOG(level, message) \
177  do { \
178  PRINT_PREFIX(level); \
179  PRINT_FILE_LINE(level); \
180  PRINT_MESSAGE(level, message); \
181  PRINT_ENDL(level); \
182  } while (false)
183  #endif
184 #endif
Definition: H3.cpp:14