Skip to content

Commit 4dc229f

Browse files
author
Lauris Kaplinski
committed
Cleaned up logging
1 parent e9155eb commit 4dc229f

File tree

9 files changed

+207
-218
lines changed

9 files changed

+207
-218
lines changed

cdoc/CDoc.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "CDoc2Writer.h"
2222
#include "CDoc2Reader.h"
2323
#include "Configuration.h"
24-
#include "ILogger.h"
24+
#include "ConsoleLogger.h"
2525
#include "Io.h"
2626
#include "NetworkBackend.h"
2727

@@ -71,6 +71,28 @@ getVersion()
7171
return VERSION_STR;
7272
}
7373

74+
static Logger *
75+
getDefaultLogger()
76+
{
77+
static ConsoleLogger clogger;
78+
return &clogger;
79+
}
80+
81+
static Logger *sys_logger = nullptr;
82+
83+
void
84+
setLogger(Logger *logger)
85+
{
86+
sys_logger = logger;
87+
}
88+
89+
void
90+
log(LogLevel level, std::string_view file, int line, std::string_view msg)
91+
{
92+
Logger *logger = (sys_logger) ? sys_logger : getDefaultLogger();
93+
logger->log(level, file, line, msg);
94+
}
95+
7496
int
7597
libcdoc::CDocReader::getCDocFileVersion(DataSource *src)
7698
{

cdoc/CDoc.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,50 @@ CDOC_EXPORT std::string getErrorStr(int64_t code);
134134

135135
CDOC_EXPORT std::string getVersion();
136136

137+
// Logging interface
138+
139+
class ILogger;
140+
typedef ILogger Logger;
141+
142+
/**
143+
* @brief Log-level enumeration to indicate severity of the log message.
144+
*/
145+
enum LogLevel
146+
{
147+
/**
148+
* @brief Most critical level. Application is about to abort.
149+
*/
150+
LEVEL_FATAL,
151+
152+
/**
153+
* @brief Errors where functionality has failed or an exception have been caught.
154+
*/
155+
LEVEL_ERROR,
156+
157+
/**
158+
* @brief Warnings about validation issues or temporary failures that can be recovered.
159+
*/
160+
LEVEL_WARNING,
161+
162+
/**
163+
* @brief Information that highlights progress or application lifetime events.
164+
*/
165+
LEVEL_INFO,
166+
167+
/**
168+
* @brief Debugging the application behavior from internal events of interest.
169+
*/
170+
LEVEL_DEBUG,
171+
172+
/**
173+
* @brief Most verbose level. Used for development, NOP in production code.
174+
*/
175+
LEVEL_TRACE
176+
};
177+
178+
CDOC_EXPORT void setLogger(Logger *logger);
179+
CDOC_EXPORT void log(LogLevel level, std::string_view file, int line, std::string_view msg);
180+
137181
/**
138182
* @brief A simple container of file name and size
139183
*

cdoc/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ add_library(cdoc
3838
CryptoBackend.cpp
3939
NetworkBackend.cpp
4040
PKCS11Backend.cpp
41-
LogEngine.cpp
4241
$<$<PLATFORM_ID:Windows>:WinBackend.cpp>
4342
Certificate.cpp Certificate.h
4443
Crypto.cpp Crypto.h

cdoc/ConsoleLogger.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ namespace libcdoc
3535
class ConsoleLogger : public ILogger
3636
{
3737
public:
38-
virtual void LogMessage(LogLevel level, std::string_view file, int line, std::string_view message) override
38+
virtual void logMessage(LogLevel level, std::string_view file, int line, std::string_view message) override
3939
{
4040
// We ignore by default the file name and line number, and call LogMessage with the level and message.
41-
if (level <= minLogLevel)
42-
{
43-
std::ostream& ofs = (level == LEVEL_INFO) ? std::cout : std::cerr;
44-
ofs << message << '\n';
45-
}
41+
std::ostream& ofs = (level == LEVEL_INFO) ? std::cout : std::cerr;
42+
ofs << message << '\n';
4643
}
4744
};
4845

cdoc/Crypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void Crypto::LogSslError(const char* funcName, const char* file, int line)
426426
while (errorCode != 0)
427427
{
428428
ERR_error_string_n(errorCode, sslErrorStr, errorStrBufLen);
429-
ILogger::getLogger()->LogMessage(ILogger::LEVEL_ERROR, file, line, FORMAT("{} failed: {}", funcName, sslErrorStr));
429+
LOG_ERROR("{} failed: {}", funcName, sslErrorStr);
430430

431431
// Get next error code
432432
errorCode = ERR_get_error();

cdoc/ILogger.h

Lines changed: 33 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3638
namespace libcdoc
@@ -42,62 +44,19 @@ namespace libcdoc
4244
class CDOC_EXPORT ILogger
4345
{
4446
public:
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
14794
template<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

Comments
 (0)