From 8e3274f153f89ecf1611fa8468d2d7dbe596a23f Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Tue, 24 Mar 2009 11:51:12 +0100 Subject: added strlcpy and strlcat (safe BSD C functions) --- src/log/log.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'src/log') diff --git a/src/log/log.c b/src/log/log.c index 28218d2..edbc00d 100644 --- a/src/log/log.c +++ b/src/log/log.c @@ -706,12 +706,17 @@ void wolf_log_reopenlogtoeventlog( void ) { void wolf_log( wolf_log_level_t level, int category_id, int message_id, const char *format, ... ) { va_list ap; - char s[1024]; va_start( ap, format ); - vsnprintf( s, 1024, format, ap ); + wolf_log_ap( level, category_id, message_id, format, ap ); va_end( ap ); - +} + +void wolf_log_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap ) { + char s[1024]; + + vsnprintf( s, 1024, format, ap ); + if( level <= log_stderr_level ) fprintf( stderr, "%s: %s\n",wolf_log_level_to_str( level ), s ); @@ -746,13 +751,10 @@ void wolf_log( wolf_log_level_t level, int category_id, int message_id, const ch /* parse C-snprintf-like format string, map the placeholders * there to normal strings, then add them to a string array */ - va_list ap; int nof_placeholders = compute_nof_placeholders( format ); LPSTR *msg_arr = (LPSTR *)malloc( sizeof( LPCTSTR ) * nof_placeholders ); - va_start( ap, format ); map_placeholders_to_strings( msg_arr, format, ap ); - va_end( ap ); if( !ReportEvent( event_source, /* event source handle */ @@ -776,3 +778,42 @@ void wolf_log( wolf_log_level_t level, int category_id, int message_id, const ch } #endif /* defined HAVE_EVENTLOG */ } + +#if defined _WIN32 + +void wolf_log_win32( wolf_log_level_t level, int category_id, int message_id, const char *format, ... ) { + va_list ap; + + va_start( ap, format ); + wolf_log_win32_ap( level, category_id, message_id, format, ap ); + va_end( ap ); +} + +void wolf_log_win32_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap ) { + DWORD last_error = GetLastError( ); + LPVOID buf; + DWORD buf_size; + DWORD res; + + res = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, /* message is from system */ + last_error, /* there is a message with that id */ + 0, /* default language preference */ + (LPTSTR)&buf, /* buffer allocated internally with LocalAlloc */ + 0, /* minimum allocation size */ + NULL ); /* no arguments */ + + if( res == 0 ) { + /* FormatMessage failed, so log the error just with the + * GetLastError() number + */ + char new_format[1024]; + + + wolf_log_ap( level, category_id, message_id, new_format, ap ); + + } +} +#endif /* defined _WIN32 */ -- cgit v1.2.3-54-g00ecf