Merge pull request #41 from ejohnstown/logging

Logging Update
pull/42/head
JacobBarthelmeh 2017-09-29 15:37:16 -06:00 committed by GitHub
commit 4715a00d58
3 changed files with 61 additions and 64 deletions

View File

@ -1,4 +1,4 @@
/* log.c
/* log.c
*
* Copyright (C) 2014-2016 wolfSSL Inc.
*
@ -34,27 +34,33 @@
#include <wolfssh/log.h>
#include <wolfssh/error.h>
#ifdef DEBUG_WOLFSSH
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#ifndef NO_TIMESTAMP
#include <time.h>
#endif
static wolfSSH_LoggingCb logFunction = NULL;
static int loggingEnabled = 0;
static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT;
#endif /* DEBUG_WOLFSSH */
#ifndef WOLFSSH_DEFAULT_LOG_WIDTH
#define WOLFSSH_DEFAULT_LOG_WIDTH 120
#endif
static void DefaultLoggingCb(enum wolfSSH_LogLevel, const char *const);
static wolfSSH_LoggingCb logFunction = DefaultLoggingCb;
static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT;
#ifdef DEBUG_WOLFSSH
static int logEnable = 0;
#endif
/* turn debugging on if supported */
int wolfSSH_Debugging_ON(void)
void wolfSSH_Debugging_ON(void)
{
#ifdef DEBUG_WOLFSSH
loggingEnabled = 1;
return WS_SUCCESS;
#else
return WS_NOT_COMPILED;
logEnable = 1;
#endif
}
@ -63,32 +69,27 @@ int wolfSSH_Debugging_ON(void)
void wolfSSH_Debugging_OFF(void)
{
#ifdef DEBUG_WOLFSSH
loggingEnabled = 0;
logEnable = 0;
#endif
}
/* set logging callback function */
int wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF)
void wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF)
{
#ifdef DEBUG_WOLFSSH
int ret = 0;
if (logF)
logFunction = logF;
else
ret = WS_BAD_ARGUMENT;
return ret;
#else
(void)logF;
return WS_NOT_COMPILED;
#endif /* DEUBG_WOLFSSH */
}
INLINE int wolfSSH_LogEnabled(void)
{
#ifdef DEBUG_WOLFSSH
return logEnable;
#else
return 0;
#endif
}
/* log level string */
@ -116,23 +117,10 @@ static const char* GetLogStr(enum wolfSSH_LogLevel level)
}
/* our default logger */
void WLOG(enum wolfSSH_LogLevel level, const char *const fmt, ...)
void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr)
{
va_list vlist;
char timeStr[80];
char msgStr[80*2];
if (loggingEnabled == 0)
return; /* not on */
if (level < logLevel)
return; /* don't need to output */
/* prefix strings */
msgStr[0] = '\0';
timeStr[0] = '\0';
#ifndef NO_TIMESTAMP
{
time_t current;
@ -141,23 +129,28 @@ void WLOG(enum wolfSSH_LogLevel level, const char *const fmt, ...)
current = time(NULL);
if (WLOCALTIME(&current, &local)) {
/* make pretty */
strftime(timeStr, sizeof(timeStr), "%b %d %T %Y", &local);
}
timeStr[sizeof(timeStr)-1] = '\0';
strftime(timeStr, sizeof(timeStr), "%b %d %T %Y: ", &local);
}
}
#endif /* NO_TIMESTAMP */
fprintf(stdout, "%s[%s] %s\n", timeStr, GetLogStr(level), msgStr);
}
/* our default logger */
void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...)
{
va_list vlist;
char msgStr[WOLFSSH_DEFAULT_LOG_WIDTH];
if (level < logLevel)
return; /* don't need to output */
/* format msg */
va_start(vlist, fmt);
WVSNPRINTF(msgStr, sizeof(msgStr), fmt, vlist);
va_end(vlist);
msgStr[sizeof(msgStr)-1] = '\0';
if (logFunction)
logFunction(level, msgStr);
else
fprintf(stdout, "%s: [%s] %s\n", timeStr, GetLogStr(level), msgStr);
}
#endif /* DEBUG_WOLFSSH */

View File

@ -47,20 +47,24 @@ enum wolfSSH_LogLevel {
typedef void (*wolfSSH_LoggingCb)(enum wolfSSH_LogLevel,
const char *const logMsg);
WOLFSSH_API int wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF);
WOLFSSH_API void wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF);
WOLFSSH_API int wolfSSH_LogEnabled(void);
#ifdef DEBUG_WOLFSSH
WOLFSSH_API void WLOG(enum wolfSSH_LogLevel,const char *const logMsg, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)));
#else
; /* end decl */
#endif /* __GNUC__ */
#ifdef __GNUC__
#define FMTCHECK __attribute__((format(printf,2,3)))
#else
#define WLOG(a, b, ...)
#endif /* DEBUG_WOLFSSH */
#define FMTCHECK
#endif /* __GNUC__ */
WOLFSSH_API void wolfSSH_Log(enum wolfSSH_LogLevel,
const char *const, ...) FMTCHECK;
#define WLOG(...) do { \
if (wolfSSH_LogEnabled()) \
wolfSSH_Log(__VA_ARGS__); \
} while (0)
#ifdef __cplusplus

View File

@ -47,7 +47,7 @@ WOLFSSH_API int wolfSSH_Init(void);
WOLFSSH_API int wolfSSH_Cleanup(void);
/* debugging output functions */
WOLFSSH_API int wolfSSH_Debugging_ON(void);
WOLFSSH_API void wolfSSH_Debugging_ON(void);
WOLFSSH_API void wolfSSH_Debugging_OFF(void);
/* context functions */