Skip to content

Commit

Permalink
Added logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
haukeduden committed Nov 4, 2022
1 parent fef6112 commit 97ce197
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
20 changes: 20 additions & 0 deletions include/countly_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ int countly_c_flush();
int countly_c_end();


/** All possible logging levels, corresponding to the internal Countly class logging levels */
enum CountlyLogLevel
{
CountlyLogLevelDebug=0,
CountlyLogLevelInfo=1,
CountlyLogLevelWarning=2,
CountlyLogLevelError=3,
CountlyLogLevelFatal=4
};

/** Logging function prototype, to be passed to countly_c_setLogFunction
\param level log level
\param message text to log
*/
typedef void (*countly_log_function_t)(CountlyLogLevel level, const char* message);

/** Sets the logging function for all log levels */
int countly_c_setLogFunction(countly_log_function_t logFunction);


#ifdef __cplusplus
}
#endif
Expand Down
66 changes: 55 additions & 11 deletions src/countly_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

#include <countly_c.h>

void onException(std::exception& e) {
std::cerr << std::endl << "Countly internal exception: " << e.what() << std::endl;
std::cerr.flush();
#undef ERROR

CountlyLogLevel mapCountlyLogLevel(Countly::LogLevel level) {
switch(level) {
case Countly::LogLevel::DEBUG: return CountlyLogLevelDebug;
case Countly::LogLevel::INFO: return CountlyLogLevelInfo;
case Countly::LogLevel::WARNING: return CountlyLogLevelWarning;
case Countly::LogLevel::ERROR: return CountlyLogLevelError;
case Countly::LogLevel::FATAL: return CountlyLogLevelFatal;
default: return CountlyLogLevelInfo;
}
}


#define COUNTLY_C_GUARD_BEGIN try {
#define COUNTLY_C_GUARD_END } \
catch(std::exception& e) { \
onException(e); \
return COUNTLY_C_GENERIC_ERROR; \
} \
return COUNTLY_C_OK;


struct CountlyCContext {
std::string serverHost;
Expand All @@ -31,12 +31,46 @@ struct CountlyCContext {

bool started=false;

countly_log_function_t logFunction = nullptr;

static CountlyCContext& get() {
static CountlyCContext context;
return context;
}
};

/** Default function to relay internal Countly log messages to
* the user specific logging function
*/
void onCountlyLog(Countly::LogLevel level, const std::string& message) {
countly_log_function_t logFunction = CountlyCContext::get().logFunction;
if (logFunction != nullptr) {
logFunction(mapCountlyLogLevel(level), message.c_str());
}
}

void onException(std::exception& e) {
std::string logMessage = std::string("Countly internal exception: ") + e.what();
countly_log_function_t logFunction = CountlyCContext::get().logFunction;
if (logFunction != nullptr) {
logFunction(CountlyLogLevelError, logMessage.c_str());
}
else {
std::cerr << std::endl << logMessage << std::endl;
std::cerr.flush();
}
}


#define COUNTLY_C_GUARD_BEGIN try {
#define COUNTLY_C_GUARD_END } \
catch(std::exception& e) { \
onException(e); \
return COUNTLY_C_GENERIC_ERROR; \
} \
return COUNTLY_C_OK;




extern "C" int countly_c_init(
Expand All @@ -50,6 +84,7 @@ extern "C" int countly_c_init(

Countly& ct = Countly::getInstance();

ct.setLogger(onCountlyLog);
ct.SetMaxEventsPerMessage(40);
ct.SetPath(stateFilePath);

Expand Down Expand Up @@ -197,3 +232,12 @@ extern "C" int countly_c_end() {
}


extern "C" int countly_c_setLogFunction(countly_log_function_t logFunction) {
COUNTLY_C_GUARD_BEGIN

CountlyCContext::get().logFunction = logFunction;

COUNTLY_C_GUARD_END
}


2 changes: 1 addition & 1 deletion src/countly_c.def
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ EXPORTS
countly_c_recordEvent
countly_c_recordScreenView
countly_c_flush

countly_c_setLogFunction

0 comments on commit 97ce197

Please sign in to comment.