Skip to content

Instantly share code, notes, and snippets.

@superwills
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superwills/457b07129cb4503a1fa0 to your computer and use it in GitHub Desktop.
Save superwills/457b07129cb4503a1fa0 to your computer and use it in GitHub Desktop.
Info,warning,error functions
#include <stdio.h>
#include <stdarg.h>
// bitfield because some error messages can
// be categorized as Info | Warning | Error
enum ErrorLevel{
Info = 1 << 0, Warning = 1 << 1, Error = 1 << 2
};
const char* ErrorLevelName[] = {
"None", //0
"Info", //1
"Warning", //2
"Info/Warning",//3
"Error", //4
"Info/Error","Warning/Error","Info/Warning/Error" //5,6,7
} ;
// decorates the log message with [appname][thread][error level][current time]: message
void logDecorate( int logLevel, const char *fmt, va_list args )
{
// to be threadsafe, removed static
char msgBuffer[ 4096 ] ; // oops. Had a 623 char error (from shader) and it err-d out.
vsprintf( msgBuffer, fmt, args ) ;
// write time into timeBuff. Should be about 8 chars hh:mm:ss
char timeBuf[ 32 ] ;
strftime( timeBuf, 255, "%X", getCurrentTime() ) ;
// Put it all together
char buf[ 4096 ] ;
sprintf( buf, "[ %s ][ %s ][ %s ]: %s", progname, ErrorLevelName[ logLevel ], timeBuf, msgBuffer ) ;
//printf( "%s\n", buf ) ; // don't want inserted.
puts( buf ) ;
}
void error( const char *fmt, ... )
{
va_list lp ;
va_start( lp, fmt ) ;
logDecorate( ErrorLevel::Error, fmt, lp ) ;
}
void warning( const char *fmt, ... )
{
va_list lp ;
va_start( lp, fmt ) ;
logDecorate( ErrorLevel::Warning, fmt, lp ) ;
}
void info( const char *fmt, ... )
{
va_list lp ;
va_start( lp, fmt ) ;
logDecorate( ErrorLevel::Info, fmt, lp ) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment