btftoolchain
logging.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 #ifdef __clang__
6 #include <experimental/source_location>
7 using std::experimental::source_location;
8 #else
9 #ifdef __GNUG__
10 #include <experimental/source_location>
11 using std::experimental::source_location;
12 #else
13 #include <source_location>
14 using std::source_location;
15 #endif
16 #endif
17 
18 #define FATAL_INTERNAL_ERROR_MSG(MSG) \
19  { \
20  const source_location location = source_location::current(); \
21  std::cout << "FATAL INTERNAL ERROR in <" << location.function_name() << "> at <" << location.line() << ">\n"; \
22  std::cout << MSG; \
23  std::terminate(); \
24  }
25 
26 namespace helper
27 {
28 namespace logging
29 {
30 
34 enum class LogLevel
35 {
37  none = 0,
39  error = 1,
41  warning = 2,
43  trace = 3
44 };
45 
52 void initLogging(LogLevel log_level, const std::string& log_file_path = "");
53 
57 void flush();
58 
65 std::ostream& printError(const source_location location = source_location::current());
66 
73 std::ostream& printWarning(const source_location location = source_location::current());
74 
81 std::ostream& printTrace(const source_location location = source_location::current());
82 
91 std::ostream& printLog(LogLevel log_level, const source_location location);
92 
99 std::string logLevelToString(LogLevel log_level);
100 
107 std::string stripSourceFileName(const std::string& file_name);
108 
109 } // namespace logging
110 } // namespace helper
void initLogging(LogLevel log_level, const std::string &log_file_path="")
Initalizes the logging feature.
Definition: logging.cpp:83
std::ostream & printTrace(const source_location location=source_location::current())
Writes a trace message to the log.
Definition: logging.cpp:102
std::ostream & printError(const source_location location=source_location::current())
Writes an error message to the log.
Definition: logging.cpp:92
std::string stripSourceFileName(const std::string &file_name)
Gets the source file name out of the path.
Definition: logging.cpp:123
LogLevel
Currently supported log levels for the logging feature.
Definition: logging.h:35
@ trace
Adds "Trace" before the log message.
@ none
No log level is selected.
@ warning
Adds "Warning" before the log message.
@ error
Adds "Error" before the log message.
std::string logLevelToString(LogLevel log_level)
Converts the enum LogLevel into string.
Definition: logging.cpp:107
void flush()
Flushes the log.
Definition: logging.cpp:145
std::ostream & printWarning(const source_location location=source_location::current())
Writes a warning message to the log.
Definition: logging.cpp:97
std::ostream & printLog(LogLevel log_level, const source_location location)
Prints a log message to the log.
Definition: logging.cpp:134
Definition: logging.h:27