This repository has been archived on 2025-02-12. You can view files and clone it, but cannot push or open issues or pull requests.
CLF/win32/log.c
2004-10-05 04:49:26 +00:00

167 lines
4 KiB
C

/*
Copyright (c) 1998-2003, Purdue University
All rights reserved.
Redistribution and use in source and binary forms are permitted provided
that:
(1) source distributions retain this entire copyright notice and comment,
and
(2) distributions including binaries display the following acknowledgement:
"This product includes software developed by Purdue University."
in the documentation or other materials provided with the distribution
and in all advertising materials mentioning features or use of this
software.
The name of the University may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
This software was developed by:
Curtis Smith
Purdue University
Engineering Computer Network
465 Northwestern Avenue
West Lafayette, Indiana 47907-2035 U.S.A.
Send all comments, suggestions, or bug reports to:
software@ecn.purdue.edu
*/
/* Include files */
#include "main.h"
#include "log.h"
#include "syslog.h"
#include "wsock.h"
#include <io.h>
/* Indicate if interactive logging is available */
int LogInteractive = 0;
/* Indicate if eventlog is initialized */
static HANDLE LogSource = NULL;
/* Start using eventlog */
int LogStart()
{
/* Indicate if interactive logging is available */
LogInteractive = _isatty(_fileno(stdout));
/* Open connection to event logger */
LogSource = RegisterEventSource(NULL, "CLFAgent");
if (LogSource == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot register source for event logging");
return 1;
}
/* Success */
return 0;
}
/* Stop using eventlog */
void LogStop()
{
/* Check indicator */
if (LogSource != NULL) {
/* Deregister source */
DeregisterEventSource(LogSource);
/* Reset indicators */
LogSource = NULL;
}
}
/* Send a message to the eventlog */
static int LogSend(WORD level, char * message)
{
char * messages[1];
/* Check that the event log is open */
if (LogSource) {
/* Set up array */
messages[0] = message;
/* Process event */
if (ReportEvent(LogSource, level, 0, 1, NULL, COUNT_OF(messages), 0, messages, NULL) == FALSE)
return 1;
}
/* Success */
return 0;
}
/* Print out an error message */
void Log(int level, char * message, ...)
{
WORD eventlog_priority;
char windows_message[256];
char error_message[1024];
int syslog_level;
va_list args;
static BOOL logging = FALSE;
/* This prevents recursive errors */
if (logging)
return;
logging = TRUE;
/* Format and output system message */
if (level & LOG_SYS)
GetError(GetLastError(), windows_message, sizeof(windows_message));
/* Format and output message */
va_start(args, message);
_vsnprintf(error_message, sizeof(error_message), message, args);
va_end(args);
/* Append system error message */
if (level & LOG_SYS) {
/* Remove bit */
level &= ~LOG_SYS;
/* Add windows error message */
_snprintf(error_message, sizeof(error_message), "%s: %s", error_message, windows_message);
}
/* Convert local level to eventlog or syslog priority */
switch (level) {
case LOG_ERROR:
eventlog_priority = EVENTLOG_ERROR_TYPE;
syslog_level = SYSLOG_BUILD(SYSLOG_DAEMON, SYSLOG_ERR);
break;
case LOG_WARNING:
eventlog_priority = EVENTLOG_WARNING_TYPE;
syslog_level = SYSLOG_BUILD(SYSLOG_DAEMON, SYSLOG_WARNING);
break;
case LOG_INFO:
eventlog_priority = EVENTLOG_INFORMATION_TYPE;
syslog_level = SYSLOG_BUILD(SYSLOG_DAEMON, SYSLOG_NOTICE);
break;
}
/* Send to syslog if network running */
if (WSockSocket == INVALID_SOCKET || SyslogSend(error_message, syslog_level))
/* Otherwise, send to eventlog */
LogSend(eventlog_priority, error_message);
/* Output to console */
if (LogInteractive) {
fputs(error_message, stderr);
fputc('\n', stderr);
}
/* Okay to log again */
logging = FALSE;
}