win95-initial-port-20010430
[openafs.git] / src / WINNT / eventlog / logevent.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <stddef.h>
15 #include <stdarg.h>
16 #include <WINNT/afsreg.h>
17
18 #include "logevent.h"
19
20 /* AFS NT alternative event logging functions.
21  *
22  * All event logging functions take a variable number of arguments,
23  * with the trailing (variable) arguments being a set of message text
24  * insertion strings terminated by a 0 (NULL) argument.
25  * Status values are logged as raw data if the status is not zero.
26  *
27  * All functions return 0 if message is logged successfully and -1 otherwise.
28  *
29  * Example: ReportErrorEventAlt(eventId, status, filenameString, 0);
30  */
31
32 static int
33 ReportEventAlt(WORD eventType,
34                DWORD eventId,
35                WORD insertStrCount,
36                char **insertStr,
37                int status)
38 {
39     HANDLE eventLog = NULL;
40     BOOL result;
41
42     /* open write handle to the event log */
43     eventLog = RegisterEventSource(NULL /* local machine */,
44                                    AFSREG_SVR_APPLOG_SUBKEY);
45     if (eventLog == NULL) {
46         return -1;
47     }
48
49     /* log the event */
50     result = ReportEvent(eventLog, eventType, 0 /* category */, eventId,
51                          NULL /* SID */,
52                          insertStrCount, (status ? sizeof(status) : 0),
53                          insertStr, &status);
54
55     (void) DeregisterEventSource(eventLog);
56
57     return (result ? 0 : -1);
58 }
59
60
61 int
62 ReportErrorEventAlt(unsigned int eventId, int status, char *str, ...)
63 {
64     va_list strArgs;
65     char *iStrings[AFSEVT_MAXARGS];
66     int i;
67
68     /* construct iStrings (insertion strings) array */
69     va_start(strArgs, str);
70     for (i = 0; str && (i < AFSEVT_MAXARGS); i++) {
71         iStrings[i] = str;
72         str = va_arg(strArgs, char *);
73     }
74     va_end(strArgs);
75
76     if (str) {
77         /* too many args */
78         return -1;
79     }
80
81     /* report error event */
82     return ReportEventAlt(EVENTLOG_ERROR_TYPE,
83                           (DWORD) eventId, (WORD) i, iStrings, status);
84 }
85
86
87 int
88 ReportWarningEventAlt(unsigned int eventId, int status, char *str, ...)
89 {
90     va_list strArgs;
91     char *iStrings[AFSEVT_MAXARGS];
92     int i;
93
94     /* construct iStrings (insertion strings) array */
95     va_start(strArgs, str);
96     for(i = 0; str && (i < AFSEVT_MAXARGS); i++) {
97         iStrings[i] = str;
98         str = va_arg(strArgs, char *);
99     }
100     va_end(strArgs);
101
102     if (str) {
103         /* too many args */
104         return -1;
105     }
106
107     /* report warning event */
108     return ReportEventAlt(EVENTLOG_WARNING_TYPE,
109                           (DWORD) eventId, (WORD) i, iStrings, status);
110 }
111
112
113 int
114 ReportInformationEventAlt(unsigned int eventId, char *str, ...)
115 {
116     va_list strArgs;
117     char *iStrings[AFSEVT_MAXARGS];
118     int i;
119
120     /* construct iStrings (insertion strings) array */
121     va_start(strArgs, str);
122     for(i = 0; str && (i < AFSEVT_MAXARGS); i++) {
123         iStrings[i] = str;
124         str = va_arg(strArgs, char *);
125     }
126     va_end(strArgs);
127
128     if (str) {
129         /* too many args */
130         return -1;
131     }
132
133     /* report information event */
134     return ReportEventAlt(EVENTLOG_INFORMATION_TYPE,
135                           (DWORD) eventId, (WORD) i, iStrings, 0);
136 }