console.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_CONSOLE_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_CONSOLE_H_
27 
28 #include <corbo-core/text_style.h>
29 #include <string>
30 
31 // === Colors ===
32 
33 #define WARNING_COLOR corbo::TextColorCode::FG_LIGHT_YELLOW
34 #define ERROR_COLOR corbo::TextColorCode::FG_LIGHT_RED
35 #define DEFAULT_COLOR corbo::TextColorCode::FG_DEFAULT
36 
37 // ==== Function name ===
38 
39 #if defined(__GNUC__)
40 #define corbo_FUNCTION_NAME __PRETTY_FUNCTION__
41 #elif defined(_MSC_VER)
42 #define corbo_FUNCTION_NAME __FUNCSIG__
43 #else
44 #define corbo_FUNCTION_NAME __FUNCTION__
45 #endif
46 
47 #define corbo_FUNCTION_NAME_FORMATTED "[" << corbo_FUNCTION_NAME << "] "
48 
49 // === Debug messages ===
50 
51 #if defined(NDEBUG) || defined(DISABLE_IO)
52 
53 #define PRINT_DEBUG(msg)
54 #define PRINT_DEBUG_ONCE(msg)
55 #define PRINT_DEBUG_COND(cond, msg)
56 #define PRINT_DEBUG_COND_ONCE(cond, msg)
57 
58 #else
59 #define PRINT_DEBUG(msg) std::cout << "Debug: " << msg << std::endl;
61 
63 #define PRINT_DEBUG_ONCE(msg) \
64  { \
65  static const auto debugOnce = [&] { \
66  std::cout << "Debug: " << msg << std::endl; \
67  return true; \
68  }(); \
69  (void)debugOnce; \
70  } // void cast: avoid compiler warnings since it is unused later
71 
73 #define PRINT_DEBUG_COND(cond, msg) \
74  if (cond) std::cout << "Debug: " << msg << std::endl;
75 
77 #define PRINT_DEBUG_COND_ONCE(cond, msg) \
78  { \
79  static const auto debugOnce = [&] { \
80  if (cond) std::cout << "Debug: " << msg << std::endl; \
81  return true; \
82  }(); \
83  (void)debugOnce; \
84  }
85 
86 #endif
87 
88 #ifdef DISABLE_IO
89 
90 #define PRINT_INFO(msg)
91 #define PRINT_INFO_ONCE(msg)
92 #define PRINT_INFO_COND(cond, msg)
93 #define PRINT_INFO_COND_ONCE(cond, msg)
94 
95 #define PRINT_WARNING(msg)
96 #define PRINT_WARNING_ONCE(msg)
97 #define PRINT_WARNING_COND(cond, msg)
98 #define PRINT_WARNING_COND_ONCE(cond, msg)
99 
100 #define PRINT_ERROR(msg)
101 #define PRINT_ERROR_ONCE(msg)
102 #define PRINT_ERROR_COND(cond, msg)
103 #define PRINT_ERROR_COND_ONCE(cond, msg)
104 
105 #define PRINT_FATAL(msg)
106 
107 #define INPUT_STREAM(variable, default_val) variable = default_val;
108 #define INPUT_STREAM_MSG(msg, variable, default_val) INPUT_STREAM(variable, default_val);
109 #define INPUT_STREAM_DEFAULT(variable, default_val) variable = default_val;
110 #define INPUT_STREAM_DEFAULT_MSG(msg, variable, default_val) INPUT_STREAM_DEFAULT(variable, default_val);
111 
112 #else
113 
114 #include <iostream>
115 
117 #define PRINT_INFO(msg) std::cout << "Info: " << msg << std::endl
118 // #define PRINT_INFO_ARGS(msg, ...) std::printf(msg, ##__VA_ARGS__);
119 
121 #define PRINT_INFO_ONCE(msg) \
122  { \
123  static const auto infoOnce = [&] { \
124  std::cout << "Info: " << msg << std::endl; \
125  return true; \
126  }(); \
127  (void)infoOnce; \
128  } // void cast: avoid compiler warnings since it is unused later
129 
131 #define PRINT_INFO_COND(cond, msg) \
132  if (cond) std::cout << "Info: " << msg << std::endl
133 
135 #define PRINT_INFO_COND_ONCE(cond, msg) \
136  { \
137  static const auto infoOnce = [&] { \
138  if (cond) std::cout << "Info: " << msg << std::endl; \
139  return true; \
140  }(); \
141  (void)infoOnce; \
142  }
143 
145 #define PRINT_WARNING(msg) std::cout << WARNING_COLOR << "Warning: " << msg << DEFAULT_COLOR << std::endl
146 // #define PRINT_INFO_ARGS(msg, ...) std::printf(msg, ##__VA_ARGS__);
147 
149 #define PRINT_WARNING_ONCE(msg) \
150  { \
151  static const auto infoOnce = [&] { \
152  std::cout << WARNING_COLOR << "Warning: " << msg << DEFAULT_COLOR << std::endl; \
153  return true; \
154  }(); \
155  (void)infoOnce; \
156  } // void cast: avoid compiler warnings since it is unused later
157 
159 #define PRINT_WARNING_COND(cond, msg) \
160  if (cond) std::cout << WARNING_COLOR << "Warning: " << msg << DEFAULT_COLOR << std::endl
161 
163 #define PRINT_WARNING_COND_ONCE(cond, msg) \
164  { \
165  static const auto warningOnce = [&] { \
166  if (cond) std::cout << WARNING_COLOR << "Warning: " << msg << DEFAULT_COLOR << std::endl; \
167  return true; \
168  }(); \
169  (void)warningOnce; \
170  }
171 
173 #define PRINT_ERROR(msg) std::cerr << ERROR_COLOR << "Error: " << msg << DEFAULT_COLOR << std::endl
174 
176 #define PRINT_ERROR_ONCE(msg) \
177  { \
178  static const auto infoOnce = [&] { \
179  std::cerr << ERROR_COLOR << "Error: " << msg << DEFAULT_COLOR << std::endl; \
180  return true; \
181  }(); \
182  (void)infoOnce; \
183  } // void cast: avoid compiler warnings since it is unused later
184 
186 #define PRINT_ERROR_COND(cond, msg) \
187  if (cond) std::cerr << ERROR_COLOR << "Error: " << msg << DEFAULT_COLOR << std::endl
188 
190 #define PRINT_ERROR_COND_ONCE(cond, msg) \
191  { \
192  static const auto errorOnce = [&] { \
193  if (cond) std::cerr << ERROR_COLOR << "Error: " << msg << DEFAULT_COLOR << std::endl; \
194  return true; \
195  }(); \
196  (void)errorOnce; \
197  }
198 
200 #define PRINT_FATAL(msg) \
201  { \
202  std::cerr << ERROR_COLOR << "Fatal error: " << msg << " Stopping program execution." << DEFAULT_COLOR << std::endl; \
203  exit(1); \
204  }
205 
207 #define PRINT_DEBUG_WARN(msg) PRINT_DEBUG(WARNING_COLOR << msg << DEFAULT_COLOR)
208 
209 #define INPUT_STREAM(variable, default_val) std::cin >> variable
210 
211 #define INPUT_STREAM_MSG(msg, variable, default_val) \
212  std::cout << "Interaction: " << msg << ". "; \
213  std::cin >> variable
214 
215 // Never use unsigned variables
216 #define INPUT_STREAM_DEFAULT(variable, default_val) \
217  { \
218  std::cout << "[" << default_val << "] "; \
219  std::string name; \
220  std::getline(std::cin, name); \
221  if (name.empty()) \
222  variable = default_val; \
223  else \
224  { \
225  try \
226  { \
227  variable = static_cast<decltype(variable)>(std::stod(name)); \
228  } \
229  catch (const std::invalid_argument& ia) \
230  { \
231  std::cerr << "Invalid argument (no number found). Using default..." << std::endl; \
232  variable = default_val; \
233  } \
234  } \
235  }
236 
237 // Never use unsigned variables
238 #define INPUT_STREAM_DEFAULT_MSG(msg, variable, default_val) \
239  std::cout << "Interaction: " << msg << ". "; \
240  INPUT_STREAM_DEFAULT(variable, default_val)
241 
242 #endif
243 
244 // Extend print macros by named version (which also print the function signature)
245 #define PRINT_DEBUG_NAMED(msg) PRINT_DEBUG(corbo_FUNCTION_NAME_FORMATTED << msg)
246 #define PRINT_DEBUG_ONCE_NAMED(msg) PRINT_DEBUG_ONCE(corbo_FUNCTION_NAME_FORMATTED << msg)
247 #define PRINT_DEBUG_COND_NAMED(cond, msg) PRINT_DEBUG_COND(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
248 #define PRINT_DEBUG_COND_ONCE_NAMED(cond, msg) PRINT_DEBUG_COND_ONCE(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
249 
250 #define PRINT_INFO_NAMED(msg) PRINT_INFO(corbo_FUNCTION_NAME_FORMATTED << msg)
251 #define PRINT_INFO_ONCE_NAMED(msg) PRINT_INFO_ONCE(corbo_FUNCTION_NAME_FORMATTED << msg)
252 #define PRINT_INFO_COND_NAMED(cond, msg) PRINT_INFO_COND(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
253 #define PRINT_INFO_COND_ONCE_NAMED(cond, msg) PRINT_INFO_COND_ONCE(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
254 
255 #define PRINT_WARNING_NAMED(msg) PRINT_WARNING(corbo_FUNCTION_NAME_FORMATTED << msg)
256 #define PRINT_WARNING_ONCE_NAMED(msg) PRINT_WARNING_ONCE(corbo_FUNCTION_NAME_FORMATTED << msg)
257 #define PRINT_WARNING_COND_NAMED(cond, msg) PRINT_WARNING_COND(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
258 #define PRINT_WARNING_COND_ONCE_NAMED(cond, msg) PRINT_WARNING_COND_ONCE(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
259 
260 #define PRINT_ERROR_NAMED(msg) PRINT_ERROR(corbo_FUNCTION_NAME_FORMATTED << msg)
261 #define PRINT_ERROR_ONCE_NAMED(msg) PRINT_ERROR_ONCE(corbo_FUNCTION_NAME_FORMATTED << msg)
262 #define PRINT_ERROR_COND_NAMED(cond, msg) PRINT_ERROR_COND(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
263 #define PRINT_ERROR_COND_ONCE_NAMED(cond, msg) PRINT_ERROR_COND_ONCE(cond, corbo_FUNCTION_NAME_FORMATTED << msg)
264 
265 #define PRINT_FATAL_NAMED(msg) PRINT_FATAL(corbo_FUNCTION_NAME_FORMATTED << msg)
266 
267 #endif // SRC_CORE_INCLUDE_CORBO_CORE_CONSOLE_H_


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:47