Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ntp / libntp / log.c
1 /* Microsoft Developer Support Copyright (c) 1993 Microsoft Corporation. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <windows.h>
7
8 #include "messages.h"
9 #include "log.h"
10
11 #define PERR(bSuccess, api) {if(!(bSuccess)) printf("%s: Error %d from %s \
12     on line %d\n", __FILE__, GetLastError(), api, __LINE__);}
13
14
15 /*********************************************************************
16 * FUNCTION: addSourceToRegistry(void)                                *
17 *                                                                    *
18 * PURPOSE: Add a source name key, message DLL name value, and        *
19 *          message type supported value to the registry              *
20 *                                                                    *
21 * INPUT: source name, path of message DLL                            *
22 *                                                                    *
23 * RETURNS: none                                                      *
24 *********************************************************************/
25
26 void addSourceToRegistry(LPSTR pszAppname, LPSTR pszMsgDLL)
27 {
28   HKEY hk;                      /* registry key handle */
29   DWORD dwData;
30   BOOL bSuccess;
31
32   /* When an application uses the RegisterEventSource or OpenEventLog
33      function to get a handle of an event log, the event loggging service
34      searches for the specified source name in the registry. You can add a
35      new source name to the registry by opening a new registry subkey
36      under the Application key and adding registry values to the new
37      subkey. */
38
39   /* Create a new key for our application */
40   bSuccess = RegCreateKey(HKEY_LOCAL_MACHINE,
41       "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\NTP", &hk);
42   PERR(bSuccess == ERROR_SUCCESS, "RegCreateKey");
43
44   /* Add the Event-ID message-file name to the subkey. */
45   bSuccess = RegSetValueEx(hk,  /* subkey handle         */
46       "EventMessageFile",       /* value name            */
47       0,                        /* must be zero          */
48       REG_EXPAND_SZ,            /* value type            */
49       (LPBYTE) pszMsgDLL,       /* address of value data */
50       strlen(pszMsgDLL) + 1);   /* length of value data  */
51   PERR(bSuccess == ERROR_SUCCESS, "RegSetValueEx");
52
53   /* Set the supported types flags and addit to the subkey. */
54   dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
55       EVENTLOG_INFORMATION_TYPE;
56   bSuccess = RegSetValueEx(hk,  /* subkey handle                */
57       "TypesSupported",         /* value name                   */
58       0,                        /* must be zero                 */
59       REG_DWORD,                /* value type                   */
60       (LPBYTE) &dwData,         /* address of value data        */
61       sizeof(DWORD));           /* length of value data         */
62   PERR(bSuccess == ERROR_SUCCESS, "RegSetValueEx");
63   RegCloseKey(hk);
64   return;
65 }
66
67 /*********************************************************************
68 * FUNCTION: reportAnEvent(DWORD dwIdEvent, WORD cStrings,            *
69 *                         LPTSTR *ppszStrings);                      *
70 *                                                                    *
71 * PURPOSE: add the event to the event log                            *
72 *                                                                    *
73 * INPUT: the event ID to report in the log, the number of insert     *
74 *        strings, and an array of null-terminated insert strings     *
75 *                                                                    *
76 * RETURNS: none                                                      *
77 *********************************************************************/
78
79 void reportAnIEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
80 {
81   HANDLE hAppLog;
82   BOOL bSuccess;
83
84   /* Get a handle to the Application event log */
85   hAppLog = RegisterEventSource(NULL,   /* use local machine      */
86       "NTP");                   /* source name                 */
87   PERR(hAppLog, "RegisterEventSource");
88
89   /* Now report the event, which will add this event to the event log */
90   bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
91       EVENTLOG_INFORMATION_TYPE,      /* event type                  */
92       0,                        /* category zero               */
93       dwIdEvent,                /* event ID                    */
94       NULL,                     /* no user SID                 */
95       cStrings,                 /* number of substitution strings     */
96       0,                        /* no binary data              */
97       pszStrings,               /* string array                */
98       NULL);                    /* address of data             */
99   PERR(bSuccess, "ReportEvent");
100   DeregisterEventSource(hAppLog);
101   return;
102 }
103
104 void reportAnWEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
105 {
106   HANDLE hAppLog;
107   BOOL bSuccess;
108
109   /* Get a handle to the Application event log */
110   hAppLog = RegisterEventSource(NULL,   /* use local machine      */
111       "NTP");                   /* source name                 */
112   PERR(hAppLog, "RegisterEventSource");
113
114   /* Now report the event, which will add this event to the event log */
115   bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
116       EVENTLOG_WARNING_TYPE,      /* event type                  */
117       0,                        /* category zero               */
118       dwIdEvent,                /* event ID                    */
119       NULL,                     /* no user SID                 */
120       cStrings,                 /* number of substitution strings     */
121       0,                        /* no binary data              */
122       pszStrings,               /* string array                */
123       NULL);                    /* address of data             */
124   PERR(bSuccess, "ReportEvent");
125   DeregisterEventSource(hAppLog);
126   return;
127 }
128
129 void reportAnEEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
130 {
131   HANDLE hAppLog;
132   BOOL bSuccess;
133
134   /* Get a handle to the Application event log */
135   hAppLog = RegisterEventSource(NULL,   /* use local machine      */
136       "NTP");                   /* source name                 */
137   PERR(hAppLog, "RegisterEventSource");
138
139   /* Now report the event, which will add this event to the event log */
140   bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
141       EVENTLOG_ERROR_TYPE,      /* event type                  */
142       0,                        /* category zero               */
143       dwIdEvent,                /* event ID                    */
144       NULL,                     /* no user SID                 */
145       cStrings,                 /* number of substitution strings     */
146       0,                        /* no binary data              */
147       pszStrings,               /* string array                */
148       NULL);                    /* address of data             */
149   PERR(bSuccess, "ReportEvent");
150   DeregisterEventSource(hAppLog);
151   return;
152 }