ReactOS  0.4.14-dev-358-gbef841c
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 
12 static LPTSTR lpEventSource = _T("Skeleton service");
13 static LPTSTR lpLogFileName = _T("C:\\skel_service.log");
15 
16 // needs work
17 static 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 
42 static BOOL
44 {
47  0,
48  NULL,
51  NULL);
53  return FALSE;
54 
55  return TRUE;
56 }
57 
58 static 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),
129  &bytesWritten,
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 
151 VOID
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
168 {
169  WCHAR wcBom = 0xFEFF;
170 
172 
173 #ifdef _UNICODE
174  if (OpenLogFile())
175  {
177 
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,
189  }
190 
192  }
193 #endif
194 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static VOID LogToEventLog(LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:18
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define CloseHandle
Definition: compat.h:406
BOOL InitLogging()
Definition: log.c:210
#define LANG_NEUTRAL
Definition: nls.h:22
#define SUBLANG_DEFAULT
Definition: nls.h:168
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static BOOL LogToFile(LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:59
#define DeleteFile
Definition: winbase.h:3588
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
CHAR * LPTSTR
Definition: xmlstorage.h:192
static BOOL OpenLogFile()
Definition: log.c:43
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2630
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
char TCHAR
Definition: xmlstorage.h:189
#define EVENTLOG_SUCCESS
Definition: winnt_old.h:2629
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define FILE_END
Definition: winbase.h:114
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define OPEN_ALWAYS
Definition: disk.h:70
GLbitfield flags
Definition: glext.h:7161
#define FormatMessage
Definition: winbase.h:3619
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
BOOL WINAPI CloseEventLog(IN HANDLE hEventLog)
Definition: eventlog.c:427
#define _sntprintf
Definition: xmlstorage.h:201
#define ReportEvent
Definition: winbase.h:3723
#define RegisterEventSource
Definition: winbase.h:3718
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
unsigned int UINT
Definition: ndis.h:50
#define MAKELANGID(p, s)
Definition: nls.h:15
static LPTSTR lpLogFileName
Definition: log.c:13
static HANDLE hLogFile
Definition: log.c:14
#define HeapFree(x, y, z)
Definition: compat.h:402
static LPTSTR lpEventSource
Definition: log.c:12