ReactOS 0.4.15-dev-8390-g075894b
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),
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 HANDLE hLogFile
Definition: log.c:16
static BOOL OpenLogFile()
Definition: log.c:48
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
static LPTSTR lpLogFileName
Definition: log.c:13
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define _T(x)
Definition: vfdio.h:22
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define DeleteFile
Definition: winbase.h:3764
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ 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 LogToFile(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:67
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}
static LPWSTR lpEventSource
Definition: log.c:14
BOOL WINAPI CloseEventLog(IN HANDLE hEventLog)
Definition: eventlog.c:427
#define ReportEvent
Definition: winbase.h:3899
#define RegisterEventSource
Definition: winbase.h:3894
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2834
#define EVENTLOG_SUCCESS
Definition: winnt_old.h:2833

◆ 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),
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}
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define SetFilePointer
Definition: compat.h:743
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define LANG_NEUTRAL
Definition: nls.h:22
#define MAKELANGID(p, s)
Definition: nls.h:15
#define SUBLANG_DEFAULT
Definition: nls.h:168
#define FormatMessage
Definition: winbase.h:3795
#define FILE_END
Definition: winbase.h:114
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:423
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:419
char TCHAR
Definition: xmlstorage.h:189
#define _sntprintf
Definition: xmlstorage.h:201
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198

◆ 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:731
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define OPEN_ALWAYS
Definition: disk.h:70
#define GENERIC_WRITE
Definition: nt_native.h:90
#define CreateFile
Definition: winbase.h:3749

Variable Documentation

◆ hLogFile

HANDLE hLogFile
static

Definition at line 14 of file log.c.

◆ lpEventSource

LPTSTR lpEventSource = _T("Skeleton service")
static

Definition at line 12 of file log.c.

◆ lpLogFileName

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

Definition at line 13 of file log.c.

Referenced by InitLogging(), and OpenLogFile().