ReactOS 0.4.16-dev-306-g647d351
log.c
Go to the documentation of this file.
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
12static LPTSTR lpEventSource = _T("Skeleton service");
13static LPTSTR lpLogFileName = _T("C:\\skel_service.log");
15
16// needs work
17static VOID
19 DWORD errNum,
20 DWORD exitCode,
21 UINT flags)
22{
23 HANDLE hEventLog;
24
26 if (hEventLog)
27 {
28 ReportEvent(hEventLog,
30 0,
31 0,
32 NULL,
33 1,
34 0,
35 &lpMsg,
36 NULL);
37
38 CloseEventLog(hEventLog);
39 }
40}
41
42static BOOL
44{
47 0,
48 NULL,
51 NULL);
53 return FALSE;
54
55 return TRUE;
56}
57
58static BOOL
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
78 NULL,
79 errNum,
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 {
123
125
127 lpFullMsg,
128 _tcslen(lpFullMsg) * sizeof(TCHAR),
130 NULL);
131 if (bytesWritten == 0)
132 {
133 LogToEventLog(_T("Failed to write to log file"),
134 GetLastError(),
135 0,
137 }
138
140 0,
141 lpFullMsg);
142 }
143
145
146 if (exitCode > 0)
147 ExitProcess(exitCode);
148}
149
150
151VOID
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
166VOID
168{
169 WCHAR wcBom = 0xFEFF;
170
172
173#ifdef _UNICODE
174 if (OpenLogFile())
175 {
177
179 &wcBom,
180 sizeof(WCHAR),
182 NULL);
183 if (bytesWritten == 0)
184 {
185 LogToEventLog(_T("Failed to write to log file"),
186 GetLastError(),
187 0,
189 }
190
192 }
193#endif
194}
static VOID LogToFile(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:67
static HANDLE hLogFile
Definition: log.c:16
BOOL InitLogging()
Definition: log.c:210
static LPWSTR lpEventSource
Definition: log.c:14
static BOOL OpenLogFile()
Definition: log.c:48
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL WINAPI CloseEventLog(IN HANDLE hEventLog)
Definition: eventlog.c:427
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define SetFilePointer
Definition: compat.h:743
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
GLbitfield flags
Definition: glext.h:7161
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define OPEN_ALWAYS
Definition: disk.h:70
static LPTSTR lpLogFileName
Definition: log.c:13
unsigned int UINT
Definition: ndis.h:50
#define GENERIC_WRITE
Definition: nt_native.h:90
#define LANG_NEUTRAL
Definition: nls.h:22
#define MAKELANGID(p, s)
Definition: nls.h:15
#define SUBLANG_DEFAULT
Definition: nls.h:168
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define _T(x)
Definition: vfdio.h:22
#define FormatMessage
Definition: winbase.h:3820
#define FILE_END
Definition: winbase.h:115
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ReportEvent
Definition: winbase.h:3924
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:449
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:445
#define DeleteFile
Definition: winbase.h:3789
#define RegisterEventSource
Definition: winbase.h:3919
#define CreateFile
Definition: winbase.h:3774
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2863
#define EVENTLOG_SUCCESS
Definition: winnt_old.h:2862
char TCHAR
Definition: xmlstorage.h:189
__wchar_t WCHAR
Definition: xmlstorage.h:180
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define _sntprintf
Definition: xmlstorage.h:201
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198