summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent_win32/log.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zabbix_agent_win32/log.cpp')
-rw-r--r--src/zabbix_agent_win32/log.cpp145
1 files changed, 115 insertions, 30 deletions
diff --git a/src/zabbix_agent_win32/log.cpp b/src/zabbix_agent_win32/log.cpp
index cba743d6..8609befd 100644
--- a/src/zabbix_agent_win32/log.cpp
+++ b/src/zabbix_agent_win32/log.cpp
@@ -39,23 +39,30 @@ static HANDLE mutexLogAccess;
void InitLog(void)
{
- char tbuf[32],buffer[256];
- struct tm *loc;
- time_t t;
- DWORD size;
+ if (optUseEventLog)
+ {
+ hLog=RegisterEventSource(NULL,ZABBIX_EVENT_SOURCE);
+ }
+ else
+ {
+ char tbuf[32],buffer[256];
+ struct tm *loc;
+ time_t t;
+ DWORD size;
- hLog=CreateFile(logFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,NULL);
- if (hLog==INVALID_HANDLE_VALUE)
- return;
- SetFilePointer(hLog,0,NULL,FILE_END);
- t=time(NULL);
- loc=localtime(&t);
- strftime(tbuf,32,"%d-%b-%Y %H:%M:%S",loc);
- sprintf(buffer,"**************************************************************\r\n[%s] Log file opened\r\n",tbuf);
- WriteFile(hLog,buffer,strlen(buffer),&size,NULL);
-
- mutexLogAccess=CreateMutex(NULL,FALSE,NULL);
+ hLog=CreateFile(logFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL,NULL);
+ if (hLog==INVALID_HANDLE_VALUE)
+ return;
+ SetFilePointer(hLog,0,NULL,FILE_END);
+ t=time(NULL);
+ loc=localtime(&t);
+ strftime(tbuf,32,"%d-%b-%Y %H:%M:%S",loc);
+ sprintf(buffer,"**************************************************************\r\n[%s] Log file opened\r\n",tbuf);
+ WriteFile(hLog,buffer,strlen(buffer),&size,NULL);
+
+ mutexLogAccess=CreateMutex(NULL,FALSE,NULL);
+ }
}
@@ -65,21 +72,27 @@ void InitLog(void)
void CloseLog(void)
{
- if (hLog!=INVALID_HANDLE_VALUE)
- CloseHandle(hLog);
- if (mutexLogAccess!=INVALID_HANDLE_VALUE)
- CloseHandle(mutexLogAccess);
+ if (optUseEventLog)
+ {
+ DeregisterEventSource(hLog);
+ }
+ else
+ {
+ if (hLog!=INVALID_HANDLE_VALUE)
+ CloseHandle(hLog);
+ if (mutexLogAccess!=INVALID_HANDLE_VALUE)
+ CloseHandle(mutexLogAccess);
+ }
}
//
-// Write log
+// Write record to log file
//
-void WriteLog(char *format,...)
+static void WriteLogToFile(char *message)
{
- va_list args;
- char *prefix,buffer[4096];
+ char *prefix,buffer[64];
DWORD size;
time_t t;
struct tm *loc;
@@ -102,13 +115,85 @@ void WriteLog(char *format,...)
printf("%s",prefix);
}
- va_start(args,format);
- vsprintf(buffer,format,args);
- va_end(args);
-
- WriteFile(hLog,buffer,strlen(buffer),&size,NULL);
+ WriteFile(hLog,message,strlen(message),&size,NULL);
if (optStandalone)
- printf("%s",buffer);
+ printf("%s",message);
ReleaseMutex(mutexLogAccess);
}
+
+
+//
+// Write log record
+// Parameters:
+// msg - Message ID
+// wType - Message type (see ReportEvent() for details)
+// format - Parameter format string, each parameter represented by one character.
+// The following format characters can be used:
+// s - String
+// d - Decimal integer
+// x - Hex integer
+//
+
+void WriteLog(DWORD msg,WORD wType,char *format...)
+{
+ va_list args;
+ char *strings[16];
+ int numStrings=0;
+
+ memset(strings,0,sizeof(char *)*16);
+
+ if (format!=NULL)
+ {
+ va_start(args,format);
+
+ for(;(format[numStrings]!=0)&&(numStrings<16);numStrings++)
+ {
+ switch(format[numStrings])
+ {
+ case 's':
+ strings[numStrings]=strdup(va_arg(args,char *));
+ break;
+ case 'd':
+ strings[numStrings]=(char *)malloc(16);
+ sprintf(strings[numStrings],"%d",va_arg(args,LONG));
+ break;
+ case 'x':
+ strings[numStrings]=(char *)malloc(16);
+ sprintf(strings[numStrings],"0x%08X",va_arg(args,DWORD));
+ break;
+ default:
+ strings[numStrings]=(char *)malloc(32);
+ sprintf(strings[numStrings],"BAD FORMAT (0x%08X)",va_arg(args,DWORD));
+ break;
+ }
+ }
+ va_end(args);
+ }
+
+ if (optUseEventLog)
+ {
+ ReportEvent(hLog,wType,0,msg,NULL,numStrings,0,(const char **)strings,NULL);
+ }
+ else
+ {
+ LPVOID lpMsgBuf;
+
+ if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
+ NULL,msg,0,(LPTSTR)&lpMsgBuf,0,strings)>0)
+ {
+ WriteLogToFile((char *)lpMsgBuf);
+ LocalFree(lpMsgBuf);
+ }
+ else
+ {
+ char message[64];
+
+ sprintf(message,"MSG 0x%08X - Unable to find message text\r\n",msg);
+ WriteLogToFile(message);
+ }
+ }
+
+ while(--numStrings>=0)
+ free(strings[numStrings]);
+}