Skip to content

Commit

Permalink
Add API to get callbacks for log messages
Browse files Browse the repository at this point in the history
Useful when wrapping KSCrash in a framework that has other ways to
log than stdout/files.

Will not affect file/stdout logging, so that those can be toggled
independently.
  • Loading branch information
torarnv committed Apr 25, 2017
1 parent 0efe27e commit abfc7b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Source/KSCrash/Recording/Tools/KSLogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,23 @@ void kslog_setLogToStdout(bool enabled)
#pragma mark - C -
// ===========================================================================

static KSLogFunction g_logCallback = NULL;

void kslog_setLogCallback(KSLogFunction function)
{
g_logCallback = function;
}

static void i_kslog_log(const char* const level,
const char* const file,
const int line,
const char* const function,
const char* const fmt, va_list args)
{
if (g_logCallback)
{
g_logCallback(level, file, line, function, fmt, args);
}
if(level && file && line >= 0 && function)
{
writeFmtToLog("%s: %s (%u): %s: ", level, lastPathEntry(file), line, function);
Expand Down
17 changes: 17 additions & 0 deletions Source/KSCrash/Recording/Tools/KSLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ extern "C" {


#include <stdbool.h>
#include <stdarg.h>


#ifdef __OBJC__
Expand Down Expand Up @@ -267,6 +268,22 @@ bool kslog_clearLogFile();
*/
void kslog_setLogToStdout(bool enabled);

typedef void (*KSLogFunction)(const char* level, const char* file,
int line, const char* function,
const char* fmt, va_list args);

/** Set a function responsible for logging.
*
* When set, will deliver logging to this function
* in addition to file and stdout output (if enabled).
*
* WARNING: Only call async-safe functions from this function!
* DO NOT call Objective-C methods!!!
*
* @param function The function that should handle logging.
*/
void kslog_setLogCallback(KSLogFunction function);

/** Tests if the logger would print at the specified level.
*
* @param LEVEL The level to test for. One of:
Expand Down

0 comments on commit abfc7b0

Please sign in to comment.