ReactOS  0.4.14-dev-358-gbef841c
log.c File Reference
#include "myservice.h"
Include dependency graph for log.c:

Go to the source code of this file.

Functions

static VOID LogToEventLog (LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
 
static BOOL OpenLogFile ()
 
static BOOL LogToFile (LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
 
VOID LogEvent (LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
 
VOID InitLogging ()
 

Variables

static LPTSTR lpEventSource = _T("Skeleton service")
 
static LPTSTR lpLogFileName = _T("C:\\skel_service.log")
 
static HANDLE hLogFile
 

Function Documentation

◆ InitLogging()

VOID InitLogging ( )

Definition at line 167 of file log.c.

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 CloseHandle
Definition: compat.h:406
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define DeleteFile
Definition: winbase.h:3588
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
static BOOL OpenLogFile()
Definition: log.c:43
smooth NULL
Definition: ftsmooth.c:416
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define _T(x)
Definition: vfdio.h:22
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
static LPTSTR lpLogFileName
Definition: log.c:13
static HANDLE hLogFile
Definition: log.c:14

◆ LogEvent()

VOID LogEvent ( LPCTSTR  lpMsg,
DWORD  errNum,
DWORD  exitCode,
UINT  flags 
)

Definition at line 152 of file log.c.

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 }
static VOID LogToEventLog(LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:18
static BOOL LogToFile(LPCTSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:59
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
#define LOG_ERROR
Definition: tcpsvcs.h:16
GLbitfield flags
Definition: glext.h:7161

◆ LogToEventLog()

static VOID LogToEventLog ( LPCTSTR  lpMsg,
DWORD  errNum,
DWORD  exitCode,
UINT  flags 
)
static

Definition at line 18 of file log.c.

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 }
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2630
smooth NULL
Definition: ftsmooth.c:416
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define EVENTLOG_SUCCESS
Definition: winnt_old.h:2629
GLbitfield flags
Definition: glext.h:7161
BOOL WINAPI CloseEventLog(IN HANDLE hEventLog)
Definition: eventlog.c:427
#define ReportEvent
Definition: winbase.h:3723
#define RegisterEventSource
Definition: winbase.h:3718
static LPTSTR lpEventSource
Definition: log.c:12

Referenced by InitLogging(), LogEvent(), and LogToFile().

◆ LogToFile()

static BOOL LogToFile ( LPCTSTR  lpMsg,
DWORD  errNum,
DWORD  exitCode,
UINT  flags 
)
static

Definition at line 59 of file log.c.

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 }
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 CloseHandle
Definition: compat.h:406
#define LANG_NEUTRAL
Definition: nls.h:22
#define SUBLANG_DEFAULT
Definition: nls.h:168
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#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 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 _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
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
#define FormatMessage
Definition: winbase.h:3619
#define _sntprintf
Definition: xmlstorage.h:201
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MAKELANGID(p, s)
Definition: nls.h:15
static HANDLE hLogFile
Definition: log.c:14
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by LogEvent().

◆ OpenLogFile()

static BOOL OpenLogFile ( )
static

Definition at line 43 of file log.c.

44 {
47  0,
48  NULL,
51  NULL);
53  return FALSE;
54 
55  return TRUE;
56 }
#define TRUE
Definition: types.h:120
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_ALWAYS
Definition: disk.h:70
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
static LPTSTR lpLogFileName
Definition: log.c:13
static HANDLE hLogFile
Definition: log.c:14

Referenced by InitLogging(), and LogToFile().

Variable Documentation

◆ hLogFile

HANDLE hLogFile
static

Definition at line 14 of file log.c.

Referenced by InitLogging(), LogToFile(), and OpenLogFile().

◆ lpEventSource

LPTSTR lpEventSource = _T("Skeleton service")
static

Definition at line 12 of file log.c.

Referenced by LogToEventLog().

◆ lpLogFileName

LPTSTR lpLogFileName = _T("C:\\skel_service.log")
static

Definition at line 13 of file log.c.

Referenced by InitLogging(), and OpenLogFile().