Skip to content

Commit

Permalink
Add API to disable stdout logging
Browse files Browse the repository at this point in the history
  • Loading branch information
torarnv committed Apr 15, 2017
1 parent 4251527 commit f2a7450
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
31 changes: 23 additions & 8 deletions Source/KSCrash/Recording/Tools/KSLogger.c
Expand Up @@ -61,6 +61,9 @@
/** Where console logs will be written */
static char g_logFilename[1024];

/** Whether logs should be written to stdout */
static bool g_logToStdout = true;

/** Write a formatted string to the log.
*
* @param fmt The format string, followed by its arguments.
Expand Down Expand Up @@ -117,7 +120,10 @@ static void writeToLog(const char* const str)
pos += bytesWritten;
}
}
write(STDOUT_FILENO, str, strlen(str));
if(g_logToStdout)
{
write(STDOUT_FILENO, str, strlen(str));
}
}

static inline void writeFmtArgsToLog(const char* fmt, va_list args)
Expand Down Expand Up @@ -193,23 +199,28 @@ void writeToLog(const char* const str)
{
fprintf(g_file, "%s", str);
}
fprintf(stdout, "%s", str);
if(g_logToStdout)
{
fprintf(stdout, "%s", str);
}
}

static inline void writeFmtArgsToLog(const char* fmt, va_list args)
{
unlikely_if(g_file == NULL)
{
g_file = stdout;
}

if(fmt == NULL)
{
writeToLog("(null)");
}
else
{
vfprintf(g_file, fmt, args);
if(g_file != NULL)
{
vfprintf(g_file, fmt, args);
}
if(g_logToStdout)
{
vfprintf(stdout, fmt, args);
}
}
}

Expand Down Expand Up @@ -252,6 +263,10 @@ bool kslog_clearLogFile()
return kslog_setLogFilename(g_logFilename, true);
}

void kslog_setLogToStdout(bool enabled)
{
g_logToStdout = enabled;
}

// ===========================================================================
#pragma mark - C -
Expand Down
8 changes: 8 additions & 0 deletions Source/KSCrash/Recording/Tools/KSLogger.h
Expand Up @@ -260,6 +260,14 @@ bool kslog_setLogFilename(const char* filename, bool overwrite);
/** Clear the log file. */
bool kslog_clearLogFile();

/** Set whether or not to log to stdout.
*
* @param enabled Whether or not stdout logging should be enabled.
*
* By default stdout logging is enabled.
*/
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);
Expand Down

0 comments on commit f2a7450

Please sign in to comment.