Skip to content

Commit fd0f73d

Browse files
[Sdk 884] Logger module added (#64)
* Logger module completed * Countly::log method added back * signature udpated * Method documentation added Unit tests added * MISRA rule: 14.4 appled * Update countly.cpp * Update countly.hpp Co-authored-by: ArtursKadikis <[email protected]>
1 parent c1cc1ca commit fd0f73d

File tree

8 files changed

+123
-19
lines changed

8 files changed

+123
-19
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ endif()
2020

2121
add_library(countly
2222
${CMAKE_CURRENT_SOURCE_DIR}/src/countly.cpp
23+
${CMAKE_CURRENT_SOURCE_DIR}/src/logger_module.cpp
2324
${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp)
2425

2526
target_include_directories(countly

examples/example_integration.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ int main() {
4040
ct.alwaysUsePost(true);
4141
ct.setDeviceID("test-device-id");
4242

43-
Countly::LoggerFunction logger_function;
44-
logger_function = printLog;
45-
ct.setLogger(logger_function);
43+
ct.setLogger(printLog);
4644
// OS, OS_version, device, resolution, carrier, app_version);
4745
ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
4846
// Server and port

include/countly.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ using json = nlohmann::json;
2222
#ifdef _WIN32
2323
#undef ERROR
2424
#endif
25-
26-
using namespace::countly_sdk;
25+
#include "countly/logger_module.hpp"
2726

2827
class Countly {
2928
public:
@@ -43,17 +42,22 @@ class Countly {
4342

4443
void setSalt(const std::string& value);
4544

46-
enum LogLevel {DEBUG, INFO, WARNING, ERROR, FATAL};
45+
enum LogLevel { DEBUG = 1, INFO = 2, WARNING = 3, ERROR = 4, FATAL = 5 };
4746

48-
using LoggerFunction = std::function<void(LogLevel, const std::string&)>;
49-
void setLogger(LoggerFunction fun);
47+
void setLogger(void (*fun)(LogLevel level, const std::string& message));
48+
49+
/*
50+
This function should not be used as it will be removed in a future release. It is
51+
currently added as a temporary workaround.
52+
*/
53+
inline std::function<void(LogLevel, const std::string&)> getLogger() { return logger_function; }
5054

5155
struct HTTPResponse {
5256
bool success;
5357
json data;
5458
};
5559

56-
void setSha256(SHA256Function fun);
60+
void setSha256(cly::SHA256Function fun);
5761

5862
using HTTPClientFunction = std::function<HTTPResponse(bool, const std::string&, const std::string&)>;
5963
void setHTTPClient(HTTPClientFunction fun);
@@ -252,9 +256,9 @@ class Countly {
252256

253257
void updateLoop();
254258

255-
SHA256Function sha256_function;
256-
LoggerFunction logger_function;
259+
cly::SHA256Function sha256_function;
257260
HTTPClientFunction http_client_function;
261+
void (*logger_function)(LogLevel level, const std::string& message) = nullptr;
258262

259263
std::string host;
260264

@@ -272,6 +276,8 @@ class Countly {
272276
std::string salt;
273277

274278
std::unique_ptr<std::thread> thread;
279+
std::unique_ptr<cly::LoggerModule> logger;
280+
275281
std::mutex mutex;
276282
bool stop_thread = false;
277283
bool running = false;

include/countly/constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define COUNTLY_KEEPALIVE_INTERVAL 3000
1212
#define COUNTLY_MAX_EVENTS_DEFAULT 200
1313

14-
namespace countly_sdk {
14+
namespace cly {
1515
using SHA256Function = std::function<std::string(const std::string&)>;
1616
}
1717

include/countly/logger_module.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef LOGGER_MODULE_HPP_
2+
#define LOGGER_MODULE_HPP_
3+
#include <string>
4+
#include <memory>
5+
#include <functional>
6+
7+
namespace cly {
8+
enum class LogLevel { DEBUG = 1, INFO = 2, WARNING = 3, ERROR = 4, FATAL = 5 };
9+
using LoggerFunction = std::function<void(LogLevel, const std::string&)>;
10+
11+
class LoggerModule {
12+
public:
13+
LoggerModule();
14+
~LoggerModule();
15+
16+
/**
17+
* Set custom logger function.
18+
*
19+
* @param logger pointer to function.
20+
*/
21+
void setLogger(LoggerFunction logger);
22+
23+
/**
24+
* Print important information.
25+
*
26+
* @param level importance and urgency of the message.
27+
* @param message description of log.
28+
*/
29+
void log(LogLevel level, const std::string& message);
30+
31+
private:
32+
class LoggerModuleImpl;
33+
std::unique_ptr<LoggerModuleImpl> impl;
34+
};
35+
36+
37+
}
38+
#endif
39+

src/countly.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ using json = nlohmann::json;
3131
#endif
3232

3333
Countly::Countly() {
34+
logger.reset(new cly::LoggerModule());
35+
3436
#if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP)
3537
curl_global_init(CURL_GLOBAL_ALL);
3638
#endif
@@ -39,6 +41,8 @@ Countly::Countly() {
3941
Countly::~Countly() {
4042
is_being_disposed = true;
4143
stop();
44+
logger.reset();
45+
4246
#if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP)
4347
curl_global_cleanup();
4448
#endif
@@ -61,9 +65,21 @@ void Countly::setSalt(const std::string& value) {
6165
mutex.unlock();
6266
}
6367

64-
void Countly::setLogger(LoggerFunction fun) {
68+
void temp_log(cly::LogLevel level, const std::string& msg) {
69+
Countly::getInstance().getLogger()(Countly::LogLevel(level), msg);
70+
}
71+
72+
void Countly::setLogger(void (*fun)(Countly::LogLevel level, const std::string& message)) {
6573
mutex.lock();
74+
6675
logger_function = fun;
76+
if (fun == nullptr) {
77+
logger->setLogger(nullptr);
78+
}
79+
else {
80+
logger->setLogger(temp_log);
81+
}
82+
6783
mutex.unlock();
6884
}
6985

@@ -73,7 +89,7 @@ void Countly::setHTTPClient(HTTPClientFunction fun) {
7389
mutex.unlock();
7490
}
7591

76-
void Countly::setSha256(SHA256Function fun) {
92+
void Countly::setSha256(cly::SHA256Function fun) {
7793
mutex.lock();
7894
sha256_function = fun;
7995
mutex.unlock();
@@ -788,10 +804,8 @@ void Countly::setDatabasePath(const std::string& path) {
788804
}
789805
#endif
790806

791-
void Countly::log(Countly::LogLevel level, const std::string& message) {
792-
if (logger_function) {
793-
logger_function(level, message);
794-
}
807+
void Countly::log(LogLevel level, const std::string& message) {
808+
logger->log(cly::LogLevel(level), message);
795809
}
796810

797811
static size_t countly_curl_write_callback(void *data, size_t byte_size, size_t n_bytes, std::string *body) {
@@ -804,7 +818,7 @@ std::string Countly::calculateChecksum(const std::string& salt, const std::strin
804818
std::string salted_data = data + salt;
805819
#ifdef COUNTLY_USE_CUSTOM_SHA256
806820
if (sha256_function == nullptr) {
807-
log(Countly::LogLevel::FATAL, "Missing SHA 256 function");
821+
log(LogLevel::FATAL, "Missing SHA 256 function");
808822
return {};
809823
}
810824

src/logger_module.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "countly/logger_module.hpp"
2+
#include <iostream>
3+
namespace cly {
4+
class LoggerModule::LoggerModuleImpl {
5+
public:
6+
LoggerFunction logger_function;
7+
8+
};
9+
10+
LoggerModule::LoggerModule()
11+
{
12+
impl.reset(new LoggerModuleImpl());
13+
}
14+
15+
LoggerModule::~LoggerModule() {
16+
}
17+
18+
void LoggerModule::setLogger(LoggerFunction logger) {
19+
impl->logger_function = logger;
20+
}
21+
22+
void LoggerModule::log(LogLevel level, const std::string& message) {
23+
if (impl->logger_function != nullptr) {
24+
impl->logger_function(level, message);
25+
}
26+
}
27+
}
28+

tests/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ std::string customChecksumCalculator(const std::string& data) {
135135
return result;
136136
}
137137

138+
void printLog(Countly::LogLevel level, const std::string& msg) {
139+
CHECK(msg == "message");
140+
CHECK(level == Countly::LogLevel::DEBUG);
141+
}
142+
143+
TEST_CASE("Logger function validation") {
144+
Countly& countly = Countly::getInstance();
145+
146+
CHECK(countly.getLogger() == nullptr);
147+
countly.setLogger(printLog);
148+
CHECK(countly.getLogger() != nullptr);
149+
150+
countly.getLogger()(Countly::LogLevel::DEBUG, "message");
151+
152+
countly.setLogger(nullptr);
153+
CHECK(countly.getLogger() == nullptr);
154+
}
155+
138156
TEST_CASE("custom sha256 function validation") {
139157
Countly& countly = Countly::getInstance();
140158

0 commit comments

Comments
 (0)