From: Kern Sibbald Date: Sat, 24 Oct 2009 09:26:31 +0000 (+0200) Subject: Implement syslog on Windows X-Git-Tag: Release-5.0.0~281^2~54 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=45f38fdbf5ae24a3c0c4981e9824a2514b0fbb3c;p=bacula%2Fbacula Implement syslog on Windows --- diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index 8caa8c2954..ae217cbe4e 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -1100,12 +1100,30 @@ gettimeofday(struct timeval *tv, struct timezone *) } -/* For apcupsd this is in src/lib/wincompat.c */ +/* + * Write in Windows System log + */ extern "C" void syslog(int type, const char *fmt, ...) { -/*#ifndef HAVE_CONSOLE - MessageBox(NULL, msg, "Bacula", MB_OK); -#endif*/ + va_list arg_ptr; + int len, maxlen; + POOLMEM *msg; + + msg = get_pool_memory(PM_EMSG); + + for (;;) { + maxlen = sizeof_pool_memory(msg) - 1; + va_start(arg_ptr, fmt); + len = bvsnprintf(msg, maxlen, fmt, arg_ptr); + va_end(arg_ptr); + if (len < 0 || len >= (maxlen-5)) { + msg = realloc_pool_memory(msg, maxlen + maxlen/2); + continue; + } + break; + } + LogErrorMsg((const char *)msg); + free_memory(msg); } void @@ -2580,3 +2598,28 @@ int munmap(void *start, size_t length) /* syslog function, added by Nicolas Boichat */ void openlog(const char *ident, int option, int facility) {} #endif //HAVE_MINGW + +/* Log an error message */ +void LogErrorMsg(const char *message) +{ + HANDLE eventHandler; + const char *strings[2]; + + /* Use the OS event logging to log the error */ + eventHandler = RegisterEventSource(NULL, "Bacula"); + + strings[0] = _("\n\nBacula ERROR: "); + strings[1] = message; + + if (eventHandler) { + ReportEvent(eventHandler, EVENTLOG_ERROR_TYPE, + 0, /* category */ + 0, /* ID */ + NULL, /* SID */ + 2, /* Number of strings */ + 0, /* raw data size */ + (const char **)strings, /* error strings */ + NULL); /* raw data */ + DeregisterEventSource(eventHandler); + } +} diff --git a/bacula/src/win32/compat/compat.h b/bacula/src/win32/compat/compat.h index 4df0e732b8..0cd5efbdb1 100644 --- a/bacula/src/win32/compat/compat.h +++ b/bacula/src/win32/compat/compat.h @@ -381,6 +381,8 @@ void closelog(); void openlog(const char *ident, int option, int facility); #endif //HAVE_MINGW +extern void LogErrorMsg(const char *message); + #if !defined(INVALID_FILE_ATTRIBUTES) #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) #endif diff --git a/bacula/src/win32/libwin32/protos.h b/bacula/src/win32/libwin32/protos.h index ad1b9d567b..b9bc514f6f 100644 --- a/bacula/src/win32/libwin32/protos.h +++ b/bacula/src/win32/libwin32/protos.h @@ -31,10 +31,10 @@ * Version $Id$ */ -#define log_error_message(msg) LogErrorMsg((msg), __FILE__, __LINE__) +#define log_error_message(msg) LogLastErrorMsg((msg), __FILE__, __LINE__) extern int BaculaAppMain(); -extern void LogErrorMsg(const char *msg, const char *fname, int lineno); +extern void LogLastErrorMsg(const char *msg, const char *fname, int lineno); extern int BaculaMain(int argc, char *argv[]); extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint); diff --git a/bacula/src/win32/libwin32/service.cpp b/bacula/src/win32/libwin32/service.cpp index cc29860023..c77f8e286b 100644 --- a/bacula/src/win32/libwin32/service.cpp +++ b/bacula/src/win32/libwin32/service.cpp @@ -470,8 +470,8 @@ BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint) return result; } -/* Log an error message */ -void LogErrorMsg(const char *message, const char *fname, int lineno) +/* Log an error message for the last Windows error */ +void LogLastErrorMsg(const char *message, const char *fname, int lineno) { char msgbuf[500]; HANDLE eventHandler;