Skip to content

Commit

Permalink
Add API to override log outut handler
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.
  • Loading branch information
torarnv committed Apr 14, 2017
1 parent a6d08c3 commit 53c8faf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Source/KSCrash/Recording/Tools/KSLogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,25 @@ bool kslog_clearLogFile()
#pragma mark - C -
// ===========================================================================

static KSLogFunction g_logFunction = NULL;

void kslog_setOutputFunction(KSLogFunction function)
{
g_logFunction = 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_logFunction)
{
g_logFunction(level, file, line, function, fmt, args);
return;
}

if(level && file && line >= 0 && function)
{
writeFmtToLog("%s: %s (%u): %s: ", level, lastPathEntry(file), line, function);
Expand Down
15 changes: 15 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 @@ -259,6 +260,20 @@ bool kslog_setLogFilename(const char* filename, bool overwrite);
/** Clear the log file. */
bool kslog_clearLogFile();

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

/** Set the function responsible for logging.
* When set, will deliver all logging to this function
* instead of the default handler, so stdout output and file
* output will be disabled unless the handler has its own
* mechanism for dealing with those.
*
* @param function The function that should handle logging.
*/
void kslog_setOutputFunction(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 53c8faf

Please sign in to comment.