1 /*
2  * PROJECT:     ReactOS services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:
5  * PURPOSE:     skeleton service
6  * COPYRIGHT:   Copyright 2008 Ged Murphy <gedmurphy@reactos.org>
7  *
8  */
9 
10 #include "myservice.h"
11 
12 static LPTSTR lpEventSource = _T("Skeleton service");
13 static LPTSTR lpLogFileName = _T("C:\\skel_service.log");
14 static HANDLE hLogFile;
15 
16 // needs work
17 static VOID
18 LogToEventLog(LPCTSTR lpMsg,
19               DWORD errNum,
20               DWORD exitCode,
21               UINT flags)
22 {
23     HANDLE hEventLog;
24 
25     hEventLog = RegisterEventSource(NULL, lpEventSource);
26     if (hEventLog)
27     {
28         ReportEvent(hEventLog,
29                     (flags & LOG_ERROR) ? EVENTLOG_ERROR_TYPE : EVENTLOG_SUCCESS,
30                     0,
31                     0,
32                     NULL,
33                     1,
34                     0,
35                     &lpMsg,
36                     NULL);
37 
38         CloseEventLog(hEventLog);
39     }
40 }
41 
42 static BOOL
43 OpenLogFile()
44 {
45     hLogFile = CreateFile(lpLogFileName,
46                           GENERIC_WRITE,
47                           0,
48                           NULL,
49                           OPEN_ALWAYS,
50                           FILE_ATTRIBUTE_NORMAL,
51                           NULL);
52     if (hLogFile  == INVALID_HANDLE_VALUE)
53         return FALSE;
54 
55     return TRUE;
56 }
57 
58 static BOOL
59 LogToFile(LPCTSTR lpMsg,
60           DWORD errNum,
61           DWORD exitCode,
62           UINT flags)
63 {
64     LPTSTR lpFullMsg = NULL;
65     DWORD msgLen;
66 
67     if (!OpenLogFile())
68         return FALSE;
69 
70     msgLen = _tcslen(lpMsg) + 1;
71 
72     if (flags & LOG_ERROR)
73     {
74         LPVOID lpSysMsg;
75         DWORD eMsgLen;
76 
77         eMsgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
78                                 NULL,
79                                 errNum,
80                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
81                                 (LPTSTR)&lpSysMsg,
82                                 0,
83                                 NULL);
84 
85         msgLen = msgLen + eMsgLen + 40;
86 
87         lpFullMsg = HeapAlloc(GetProcessHeap(),
88                               0,
89                               msgLen * sizeof(TCHAR));
90         if (lpFullMsg)
91         {
92             _sntprintf(lpFullMsg,
93                        msgLen,
94                        _T("%s : %s\tErrNum = %lu ExitCode = %lu\r\n"),
95                        lpMsg,
96                        lpSysMsg,
97                        errNum,
98                        exitCode);
99         }
100 
101         LocalFree(lpSysMsg);
102 
103     }
104     else
105     {
106         msgLen += 2;
107 
108         lpFullMsg = HeapAlloc(GetProcessHeap(),
109                               0,
110                               msgLen * sizeof(TCHAR));
111         if (lpFullMsg)
112         {
113             _sntprintf(lpFullMsg,
114                        msgLen,
115                       _T("%s\r\n"),
116                       lpMsg);
117         }
118     }
119 
120     if (lpFullMsg)
121     {
122         DWORD bytesWritten;
123 
124         SetFilePointer(hLogFile, 0, NULL, FILE_END);
125 
126         WriteFile(hLogFile,
127                   lpFullMsg,
128                   _tcslen(lpFullMsg) * sizeof(TCHAR),
129                   &bytesWritten,
130                   NULL);
131         if (bytesWritten == 0)
132         {
133             LogToEventLog(_T("Failed to write to log file"),
134                           GetLastError(),
135                           0,
136                           LOG_EVENTLOG | LOG_ERROR);
137         }
138 
139         HeapFree(GetProcessHeap(),
140                  0,
141                  lpFullMsg);
142     }
143 
144     CloseHandle(hLogFile);
145 
146     if (exitCode > 0)
147         ExitProcess(exitCode);
148 }
149 
150 
151 VOID
152 LogEvent(LPCTSTR lpMsg,
153          DWORD errNum,
154          DWORD exitCode,
155          UINT flags)
156 {
157 #ifdef DEBUG
158     if (flags & LOG_FILE || flags & LOG_ERROR)
159         LogToFile(lpMsg, errNum, exitCode, flags);
160 #endif
161     if (flags & LOG_EVENTLOG)
162         LogToEventLog(lpMsg, errNum, exitCode, flags);
163 }
164 
165 
166 VOID
167 InitLogging()
168 {
169     WCHAR wcBom = 0xFEFF;
170 
171     DeleteFile(lpLogFileName);
172 
173 #ifdef _UNICODE
174     if (OpenLogFile())
175     {
176         DWORD bytesWritten;
177 
178         WriteFile(hLogFile,
179                   &wcBom,
180                   sizeof(WCHAR),
181                   &bytesWritten,
182                   NULL);
183         if (bytesWritten == 0)
184         {
185             LogToEventLog(_T("Failed to write to log file"),
186                           GetLastError(),
187                           0,
188                           LOG_EVENTLOG | LOG_ERROR);
189         }
190 
191         CloseHandle(hLogFile);
192     }
193 #endif
194 }
195