Last active
August 29, 2015 14:26
-
-
Save superwills/457b07129cb4503a1fa0 to your computer and use it in GitHub Desktop.
Info,warning,error functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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