Skip to content

Commit

Permalink
Added a custom log callback facility to capture log output
Browse files Browse the repository at this point in the history
  • Loading branch information
trcwm committed Oct 4, 2017
1 parent ee67dd8 commit ff53270
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
4 changes: 4 additions & 0 deletions common/libmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ DLLPUBLIC CapResult Cap_getAutoProperty(CapContext ctx, CapStream stream, CapPro
return CAPRESULT_ERR;
}

DLLPUBLIC void Cap_installCustomLogFunction(CapCustomLogFunc logFunc)
{
installCustomLogFunction(logFunc);
}

DLLPUBLIC const char* Cap_getLibraryVersion()
{
Expand Down
46 changes: 39 additions & 7 deletions common/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@
#include <stdarg.h>
#include "logging.h"

/* In their infinite "wisdom" Microsoft have declared snprintf deprecated
and we must therefore resort to a macro to fix something that shouldn't
be a problem */
#ifdef _MSC_VER
#define snprintf _snprintf
#endif

static uint32_t gs_logLevel = LOG_NOTICE;
static customLogFunc gs_logFunc = NULL;

void installCustomLogFunction(customLogFunc logfunc)
{
gs_logFunc = logfunc;
}

void LOG(uint32_t logLevel, const char *format, ...)
{
Expand All @@ -37,32 +50,51 @@ void LOG(uint32_t logLevel, const char *format, ...)
return;
}

char logbuffer[1024];
char *ptr = logbuffer;

switch(logLevel)
{
case LOG_CRIT:
fprintf(stderr,"[CRIT] ");
snprintf(logbuffer,1024,"[CRIT] ");
ptr += 7;
break;
case LOG_ERR:
fprintf(stderr,"[ERR ] ");
snprintf(logbuffer,1024,"[ERR ] ");
ptr += 7;
break;
case LOG_INFO:
fprintf(stderr,"[INFO] ");
snprintf(logbuffer,1024,"[INFO] ");
ptr += 7;
break;
case LOG_DEBUG:
fprintf(stderr,"[DBG ] ");
snprintf(logbuffer,1024,"[DBG ] ");
ptr += 7;
break;
case LOG_VERBOSE:
fprintf(stderr,"[VERB] ");
snprintf(logbuffer,1024,"[VERB] ");
ptr += 7;
break;
default:
break;
}

va_list args;

va_start(args, format);
vfprintf(stderr, format, args);
vsnprintf(ptr, 1024-7, format, args);
va_end(args);

if (gs_logFunc != nullptr)
{
// custom log functions to no include the
// prefix.
gs_logFunc(logLevel, ptr);
}
else
{
fprintf(stderr, logbuffer);
}
}

void setLogLevel(uint32_t logLevel)
Expand Down
15 changes: 15 additions & 0 deletions common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,22 @@
#define LOG_DEBUG 7
#define LOG_VERBOSE 8

/** Log information or an error. The format is the same as printf */
void LOG(uint32_t logLevel, const char *format, ...);

/** Set the log leveel */
void setLogLevel(uint32_t logLevel);

/** Get the log level */
uint32_t getLogLevel();

/** define a custom logging function callback */
typedef void (*customLogFunc)(uint32_t logLevel, const char *logString);

/** Install a custom callback function for the log.
Note: your custom callback function might not be called
from the same thead, depending on the platform!
*/
void installCustomLogFunction(customLogFunc logfunc);

#endif
12 changes: 12 additions & 0 deletions include/openpnp-capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ DLLPUBLIC CapResult Cap_getAutoProperty(CapContext ctx, CapStream stream, CapPro
*/
DLLPUBLIC void Cap_setLogLevel(uint32_t level);


typedef void (*CapCustomLogFunc)(uint32_t level, const char *string);

/** install a custom callback for a logging function.
the callback function must have the following
structure:
void func(uint32_t level, const char *string);
*/
DLLPUBLIC void Cap_installCustomLogFunction(CapCustomLogFunc logFunc);

/** Return the version of the library as a string.
In addition to a version number, this should
contain information on the platform,
Expand Down
7 changes: 7 additions & 0 deletions win/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include "openpnp-capture.h"
#include "../common/context.h"

void myCustomLogFunction(uint32_t level, const char *string)
{
printf("== %s", string);
}

std::string FourCCToString(uint32_t fourcc)
{
std::string v;
Expand Down Expand Up @@ -144,6 +149,8 @@ int main(int argc, char*argv[])
uint32_t deviceFormatID = 0;
uint32_t deviceID = 0;

Cap_installCustomLogFunction(myCustomLogFunction);

printf("==============================\n");
printf(" OpenPNP Capture Test Program\n");
printf(" %s\n", Cap_getLibraryVersion());
Expand Down

0 comments on commit ff53270

Please sign in to comment.