Logging for C-Core SDK
How to enable logging
C-core has a simple and efficient, yet flexible logging system. There are several levels from which to choose. Also, the user can set the function that will do the actual writing of data to an output.
Log Level | Preprocessor Symbol | Description |
---|---|---|
none | PUBNUB_LOG_LEVEL_NONE | all checks disabled |
errors | PUBNUB_LOG_LEVEL_ERROR | only errors reported |
warnings | PUBNUB_LOG_LEVEL_WARNING | ignored invalid conditions are reported, too |
regular | PUBNUB_LOG_LEVEL_INFO | significant events reported, too |
debug | PUBNUB_LOG_LEVEL_DEBUG | various reports useful in debugging reported, too |
detail | PUBNUB_LOG_LEVEL_TRACE | all logs enabled (compiled in) |
To choose a level, define preprocessor symbol PUBNUB_LOG_LEVEL
to one of the symbols from the table above. If you don't define it, the default used is PUBNUB_LOG_LEVEL_INFO
.
You will see that some of our samples Makefiles
define this symbol and some don't, depending on their purpose.
Keep in mind that debug and detail levels produce a lot of output that is hard to decypher by the uninitiated. It's best to use them only when looking for causes of some bugs - for example, if you report a suspected issue to Pubnub support.
Even though C-core logging system is designed for its own purpose, you're free to use it in your code too, if it suits your needs. The interface is in the pubnub_log.h header
.
The actual writing of the log reports to an output is done by using the preprocessor macro PUBNUB_LOG_PRINTF
- define it to have printf-like semantics. If you don't define this symbol, the default used is printf
. For example, here's a simple way to define a function that will output (append) to a file:
int my_logger(char const *fmt, ...)
{
va_list args;
int result;
FILE *f = foppen("mylog.txt", "at");
if (NULL == f) {
return -1;
}
va_start(args, fmt);
result = vfprintf(f, fmt, args);
va_end(args);
fclose(f);
return result;
}
At some header or in the makefile
or project
define PUBNUB_LOG_PRINTF
to this function, for example:
#define PUBNUB_LOG_PRINTF my_logger