Skip to content

Commit 1db00de

Browse files
authored
Merge branch 'open-eid:master' into master
2 parents e9155eb + 351fd66 commit 1db00de

31 files changed

+386
-517
lines changed

cdoc/CDoc.cpp

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
#include "CDoc2Writer.h"
2222
#include "CDoc2Reader.h"
2323
#include "Configuration.h"
24-
#include "ILogger.h"
2524
#include "Io.h"
2625
#include "NetworkBackend.h"
26+
#include "Utils.h"
27+
#include "Logger.h"
28+
29+
#include <iostream>
30+
#include <filesystem>
2731

2832
namespace libcdoc {
2933

@@ -71,6 +75,58 @@ getVersion()
7175
return VERSION_STR;
7276
}
7377

78+
/**
79+
* @brief Console logger
80+
*
81+
* An ILogger subclass that logs text to console.
82+
*
83+
* Info messages are logged to cout, all others to cerr.
84+
*/
85+
86+
class ConsoleLogger : public Logger
87+
{
88+
public:
89+
virtual void logMessage(LogLevel level, std::string_view file, int line, std::string_view message) override
90+
{
91+
// We ignore by default the file name and line number, and call LogMessage with the level and message.
92+
std::ostream& ofs = (level == LEVEL_INFO) ? std::cout : std::cerr;
93+
if (!file.empty()) {
94+
ofs << std::filesystem::path(file).filename().string() << ':' << line << " " << message << '\n';
95+
} else {
96+
ofs << message << '\n';
97+
}
98+
}
99+
};
100+
101+
static Logger *
102+
getDefaultLogger()
103+
{
104+
static ConsoleLogger clogger;
105+
return &clogger;
106+
}
107+
108+
static Logger *sys_logger = nullptr;
109+
110+
void
111+
setLogger(Logger *logger)
112+
{
113+
sys_logger = logger;
114+
}
115+
116+
void
117+
setLogLevel(LogLevel level)
118+
{
119+
Logger *logger = (sys_logger) ? sys_logger : getDefaultLogger();
120+
logger->setMinLogLevel(level);
121+
}
122+
123+
void
124+
log(LogLevel level, std::string_view file, int line, std::string_view msg)
125+
{
126+
Logger *logger = (sys_logger) ? sys_logger : getDefaultLogger();
127+
logger->log(level, file, line, msg);
128+
}
129+
74130
int
75131
libcdoc::CDocReader::getCDocFileVersion(DataSource *src)
76132
{
@@ -139,31 +195,6 @@ libcdoc::CDocReader::createReader(std::istream& ifs, Configuration *conf, Crypto
139195
return createReader(isrc.release(), true, conf, crypto, network);
140196
}
141197

142-
#if LIBCDOC_TESTING
143-
int64_t
144-
libcdoc::CDocReader::testConfig(std::vector<uint8_t>& dst)
145-
{
146-
LOG_TRACE("CDocReader::testConfig::Native superclass");
147-
if (conf) {
148-
LOG_DBG("CDocReader::testConfig this={} conf={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(conf));
149-
}
150-
LOG_ERROR("CDocReader::testConfig::conf is null");
151-
return WORKFLOW_ERROR;
152-
}
153-
154-
int64_t
155-
libcdoc::CDocReader::testNetwork(std::vector<std::vector<uint8_t>>& dst)
156-
{
157-
LOG_TRACE("CDocReader::testNetwork::Native superclass");
158-
if (network) {
159-
LOG_DBG("CDocReader::testNetwork this={} network={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(network));
160-
return network->test(dst);
161-
}
162-
LOG_ERROR("CDocReader::testNetwork::network is null");
163-
return WORKFLOW_ERROR;
164-
}
165-
#endif
166-
167198
libcdoc::CDocWriter::CDocWriter(int _version, DataConsumer *_dst, bool take_ownership)
168199
: version(_version), dst(_dst), owned(take_ownership)
169200
{

cdoc/CDoc.h

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
#include <string>
2525
#include <vector>
2626

27-
#ifndef LIBCDOC_TESTING
28-
// Remove this in production code
29-
#define LIBCDOC_TESTING 1
30-
#endif
31-
3227
namespace libcdoc {
3328

3429
/**
@@ -134,6 +129,50 @@ CDOC_EXPORT std::string getErrorStr(int64_t code);
134129

135130
CDOC_EXPORT std::string getVersion();
136131

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

cdoc/CDoc1Reader.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "Crypto.h"
2424
#include "CryptoBackend.h"
2525
#include "DDocReader.h"
26-
#include "ILogger.h"
2726
#include "Lock.h"
2827
#include "Utils.h"
2928
#include "XmlReader.h"

cdoc/CDoc1Writer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "Crypto.h"
2222
#include "DDocWriter.h"
23-
#include "ILogger.h"
2423
#include "Recipient.h"
2524
#include "Utils.h"
2625
#include "XmlWriter.h"

cdoc/CDoc2Reader.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "Configuration.h"
2323
#include "CryptoBackend.h"
2424
#include "CDoc2.h"
25-
#include "ILogger.h"
2625
#include "KeyShares.h"
2726
#include "Lock.h"
2827
#include "NetworkBackend.h"

cdoc/CDoc2Writer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Configuration.h"
2222
#include "Crypto.h"
2323
#include "CDoc2.h"
24-
#include "ILogger.h"
2524
#include "NetworkBackend.h"
2625
#include "Recipient.h"
2726
#include "Tar.h"

cdoc/CDocCipher.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "CDocCipher.h"
2020
#include "CDocReader.h"
2121
#include "CDoc2.h"
22-
#include "ILogger.h"
2322
#include "Lock.h"
2423
#include "NetworkBackend.h"
2524
#include "PKCS11Backend.h"

cdoc/CDocReader.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ class CDOC_EXPORT CDocReader {
200200
*/
201201
static CDocReader *createReader(std::istream& ifs, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network);
202202

203-
#if LIBCDOC_TESTING
204-
virtual int64_t testConfig(std::vector<uint8_t>& dst);
205-
virtual int64_t testNetwork(std::vector<std::vector<uint8_t>>& dst);
206-
#endif
207203
protected:
208204
explicit CDocReader(int _version) : version(_version) {};
209205

cdoc/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ set(PUBLIC_HEADERS
1515
CryptoBackend.h
1616
NetworkBackend.h
1717
PKCS11Backend.h
18-
ILogger.h
19-
ConsoleLogger.h
18+
Logger.h
2019
)
2120

2221
add_library(cdoc_ver INTERFACE)
@@ -38,7 +37,6 @@ add_library(cdoc
3837
CryptoBackend.cpp
3938
NetworkBackend.cpp
4039
PKCS11Backend.cpp
41-
LogEngine.cpp
4240
$<$<PLATFORM_ID:Windows>:WinBackend.cpp>
4341
Certificate.cpp Certificate.h
4442
Crypto.cpp Crypto.h

cdoc/Configuration.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "Configuration.h"
2222

23-
#include "ILogger.h"
2423
#include "Utils.h"
2524

2625
#include "json/picojson/picojson.h"

0 commit comments

Comments
 (0)