@@ -31,6 +31,8 @@ namespace fmt = std;
3131#include " fmt/format.h"
3232#endif
3333
34+ #include < CDoc.h>
35+
3436#define FORMAT fmt::format
3537
3638namespace libcdoc
@@ -42,62 +44,19 @@ namespace libcdoc
4244class CDOC_EXPORT ILogger
4345{
4446public:
45- /* *
46- * @brief Log-level enumeration to indicate severity of the log message.
47- */
48- enum LogLevel
49- {
50- /* *
51- * @brief Most critical level. Application is about to abort.
52- */
53- LEVEL_FATAL,
54-
55- /* *
56- * @brief Errors where functionality has failed or an exception have been caught.
57- */
58- LEVEL_ERROR,
59-
60- /* *
61- * @brief Warnings about validation issues or temporary failures that can be recovered.
62- */
63- LEVEL_WARNING,
64-
65- /* *
66- * @brief Information that highlights progress or application lifetime events.
67- */
68- LEVEL_INFO,
69-
70- /* *
71- * @brief Debugging the application behavior from internal events of interest.
72- */
73- LEVEL_DEBUG,
74-
75- /* *
76- * @brief Most verbose level. Used for development, NOP in production code.
77- */
78- LEVEL_TRACE
79- };
80-
81- ILogger () : minLogLevel(LEVEL_WARNING) {}
82- virtual ~ILogger () {}
83-
8447 /* *
8548 * @brief Logs given message with given severity, file name and line number.
49+ *
50+ * It tests the log level and if <= min_level invokes logMessage
51+ *
8652 * @param level Severity of the log message.
8753 * @param file File name where the log message was recorded.
8854 * @param line Line number in the file where the log message was recorded.
89- * @param message The log message.
90- *
91- * Every class implementing the ILogger interface must implement the member function.
92- * Default implementation does nothing.
55+ * @param msg The log message.
9356 */
94- virtual void LogMessage (LogLevel level, std::string_view file, int line, std::string_view message) {}
95-
96- /* *
97- * @brief Returns current minimum log level of the logger.
98- * @return Minimum log level.
99- */
100- LogLevel GetMinLogLevel () const noexcept { return minLogLevel; }
57+ void log (LogLevel level, std::string_view file, int line, std::string_view msg) {
58+ if (level <= min_level) logMessage (level, file, line, msg);
59+ }
10160
10261 /* *
10362 * @brief Sets minimum log level for the logger.
@@ -107,68 +66,56 @@ class CDOC_EXPORT ILogger
10766 * to LogLevelInfo (default), then LogLevelFatal, LogLevelError, LogLevelWarning and LogLevelInfo
10867 * messages are logged, but not LogLevelDebug or LogLevelTrace messages.
10968 */
110- void SetMinLogLevel (LogLevel level) noexcept { minLogLevel = level; }
111-
69+ void setMinLogLevel (LogLevel level) noexcept { min_level = level; }
70+ protected:
11271 /* *
113- * @brief Adds ILogger implementation to logging queue .
72+ * @brief Logs given message with given severity, file name and line number .
11473 *
115- * This function does not take ownership of the logger's instance .
116- * It is up to the caller to free the resources of the logger's instance and
117- * keep it alive until removed from the queue.
74+ * Every class implementing the ILogger interface must implement this member function .
75+ * The efault implementation does nothing.
76+ * The level should be checked by caller, thus the implementation should expect that level <= min_level
11877 *
119- * @param logger Logger's instance to be added.
120- * @return Unique cookie identifying the logger's instance in the logging queue.
121- */
122- static int addLogger (ILogger* logger);
123-
124- /* *
125- * @brief Removes logger's instance from the logging queue.
126- * @param cookie Unique cookie returned by the add_logger function when the logger was added.
127- * @return Pointer to ILogger object that is removed. It's up to user to free the resources.
128- */
129- static ILogger* removeLogger (int cookie);
130-
131- /* *
132- * @brief Returns global logger's instance.
133- * @return Global logger's instance.
78+ * @param level Severity of the log message.
79+ * @param file File name where the log message was recorded.
80+ * @param line Line number in the file where the log message was recorded.
81+ * @param msg The log message.
13482 */
135- static ILogger* getLogger ();
83+ virtual void logMessage (LogLevel level, std::string_view file, int line, std::string_view msg) {}
13684
137- static void setLogger (ILogger *logger);
138-
139- protected:
14085 /* *
14186 * @brief Minimum level of log messages to log.
14287 */
143- LogLevel minLogLevel ;
88+ LogLevel min_level = LEVEL_WARNING ;
14489};
14590
91+ typedef ILogger Logger;
92+
14693#ifndef SWIG
14794template <typename ... Args>
148- static inline void LogFormat (ILogger:: LogLevel level, std::string_view file, int line, fmt::format_string<Args...> fmt, Args&&... args)
95+ static inline void LogFormat (LogLevel level, std::string_view file, int line, fmt::format_string<Args...> fmt, Args&&... args)
14996{
15097 auto msg = fmt::format (fmt, std::forward<Args>(args)...);
151- ILogger::getLogger ()-> LogMessage (level, file, line, msg);
98+ libcdoc::log (level, file, line, msg);
15299}
153100
154- static inline void LogFormat (ILogger:: LogLevel level, std::string_view file, int line, std::string_view msg)
101+ static inline void LogFormat (LogLevel level, std::string_view file, int line, std::string_view msg)
155102{
156- ILogger::getLogger ()-> LogMessage (level, file, line, msg);
103+ libcdoc::log (level, file, line, msg);
157104}
158105#endif
159106
160107#define LOG (l,...) LogFormat((l), __FILE__, __LINE__, __VA_ARGS__)
161- #define LOG_ERROR (...) LogFormat(libcdoc::ILogger:: LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
162- #define LOG_WARN (...) LogFormat(libcdoc::ILogger:: LEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
163- #define LOG_INFO (...) LogFormat(libcdoc::ILogger:: LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
164- #define LOG_DBG (...) LogFormat(libcdoc::ILogger:: LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
108+ #define LOG_ERROR (...) LogFormat(libcdoc::LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
109+ #define LOG_WARN (...) LogFormat(libcdoc::LEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
110+ #define LOG_INFO (...) LogFormat(libcdoc::LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
111+ #define LOG_DBG (...) LogFormat(libcdoc::LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
165112
166113#ifdef NDEBUG
167114#define LOG_TRACE (...)
168115#define LOG_TRACE_KEY (MSG, KEY )
169116#else
170- #define LOG_TRACE (...) LogFormat(libcdoc::ILogger:: LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__)
171- #define LOG_TRACE_KEY (MSG, KEY ) LogFormat(libcdoc::ILogger:: LEVEL_TRACE, __FILE__, __LINE__, MSG, toHex(KEY))
117+ #define LOG_TRACE (...) LogFormat(libcdoc::LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__)
118+ #define LOG_TRACE_KEY (MSG, KEY ) LogFormat(libcdoc::LEVEL_TRACE, __FILE__, __LINE__, MSG, toHex(KEY))
172119#endif
173120
174121}
0 commit comments