carl  24.04
Computer ARithmetic Library
logging.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "LogLevel.h"
4 
5 #include <sstream>
6 #include <string>
7 
8 namespace carl::logging {
9 
10 /**
11  * Additional information about a log message.
12  */
13 struct RecordInfo {
14  /// File name.
15  std::string filename;
16  /// Function name.
17  std::string func;
18  /// Line number.
19  std::size_t line;
20 };
21 
22 bool visible(LogLevel level, const std::string& channel) noexcept;
23 void log(LogLevel level, const std::string& channel, const std::stringstream& ss, const RecordInfo& info);
24 
25 }
26 
27 #ifdef __VS
28 #define __func__ __FUNCTION__
29 #endif
30 
31 /// Create a record info.
32 #define __CARL_LOG_RECORD ::carl::logging::RecordInfo{__FILE__, __func__, __LINE__}
33 /// Create a record info without function name.
34 #define __CARL_LOG_RECORD_NOFUNC ::carl::logging::RecordInfo{__FILE__, "", __LINE__}
35 /// Basic logging macro.
36 #define __CARL_LOG(level, channel, expr) { \
37  if (::carl::logging::visible(level, channel)) { \
38  std::stringstream __ss; __ss << expr; ::carl::logging::log(level, channel, __ss, __CARL_LOG_RECORD); \
39  }}
40 
41 /// Basic logging macro without function name.
42 #define __CARL_LOG_NOFUNC(level, channel, expr) { \
43  if (::carl::logging::visible(level, channel)) { \
44  std::stringstream __ss; __ss << expr; ::carl::logging::log(level, channel, __ss, __CARL_LOG_RECORD_NOFUNC); \
45  }}
46 
47 /// Intended to be called when entering a function. Format: `<function name>(<args>)`.
48 #define __CARL_LOG_FUNC(channel, args) __CARL_LOG_NOFUNC(::carl::logging::LogLevel::LVL_TRACE, channel, __func__ << "(" << args << ")");
49 
50 /// Log with level LVL_TRACE.
51 #define __CARL_LOG_TRACE(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_TRACE, channel, expr)
52 /// Log with level LVL_DEBUG.
53 #define __CARL_LOG_DEBUG(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_DEBUG, channel, expr)
54 /// Log with level LVL_INFO.
55 #define __CARL_LOG_INFO(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_INFO, channel, expr)
56 /// Log with level LVL_WARN.
57 #define __CARL_LOG_WARN(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_WARN, channel, expr)
58 /// Log with level LVL_ERROR.
59 #define __CARL_LOG_ERROR(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_ERROR, channel, expr)
60 /// Log with level LVL_FATAL.
61 #define __CARL_LOG_FATAL(channel, expr) __CARL_LOG(::carl::logging::LogLevel::LVL_FATAL, channel, expr)
62 
63 /// Log and assert the given condition, if the condition evaluates to false.
64 #define __CARL_LOG_ASSERT(channel, condition, expr) if (!(condition)) { __CARL_LOG_FATAL(channel, expr); assert(condition); }
Contains a custom logging facility.
Definition: carl-logging.cpp:6
LogLevel
Indicated which log messages should be forwarded to some sink.
Definition: LogLevel.h:12
void log(LogLevel level, const std::string &channel, const std::stringstream &ss, const RecordInfo &info)
Definition: logging.cpp:11
bool visible(LogLevel level, const std::string &channel) noexcept
Definition: logging.cpp:7
Additional information about a log message.
Definition: logging.h:13
std::string filename
File name.
Definition: logging.h:15
std::string func
Function name.
Definition: logging.h:17
std::size_t line
Line number.
Definition: logging.h:19