ReactOS  0.4.14-dev-599-g2d4d3f5
log.c File Reference
#include "tcpsvcs.h"
Include dependency graph for log.c:

Go to the source code of this file.

Macros

#define DEBUG
 

Functions

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

Variables

static LPWSTR lpEventSource = L"tcpsvcs"
 
static WCHAR szLogFileName [MAX_PATH]
 
static HANDLE hLogFile = NULL
 
static OVERLAPPED olWrite
 

Macro Definition Documentation

◆ DEBUG

#define DEBUG

Definition at line 12 of file log.c.

Function Documentation

◆ InitLogging()

BOOL InitLogging ( )

Definition at line 210 of file log.c.

211 {
212 #ifdef DEBUG
213  BOOL bRet = FALSE;
214 
216  {
218  }
219  StringCchCatW(szLogFileName, ARRAYSIZE(szLogFileName), L"\\tcpsvcs_log.log");
220 
221  ZeroMemory(&olWrite, sizeof(OVERLAPPED));
222  olWrite.Offset = 0xFFFFFFFF;
223  olWrite.OffsetHigh = 0xFFFFFFFF;
225  if (olWrite.hEvent)
226  {
228 
229  if (OpenLogFile())
230  {
231  WCHAR wcBom = 0xFEFF;
233 
234  bRet = WriteFile(hLogFile,
235  &wcBom,
236  sizeof(WCHAR),
237  &bytesWritten,
238  &olWrite);
239  if (!bRet)
240  {
242  {
243  LogToEventLog(L"Failed to write to log file",
244  GetLastError(),
245  0,
247  }
248  else
249  {
250  bRet = TRUE;
251  }
252  }
253  }
254  }
255 
256  return bRet;
257 #else
258  return TRUE;
259 #endif
260 }
#define CreateEvent
Definition: winbase.h:3582
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define TRUE
Definition: types.h:120
static HANDLE hLogFile
Definition: log.c:16
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1642
DWORD Offset
Definition: winbase.h:787
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
STRSAFEAPI StringCchCatW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:325
static OVERLAPPED olWrite
Definition: log.c:18
unsigned int BOOL
Definition: ntddk_ex.h:94
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
static BOOL OpenLogFile()
Definition: log.c:48
HANDLE hEvent
Definition: winbase.h:792
smooth NULL
Definition: ftsmooth.c:416
#define LOG_ERROR
Definition: tcpsvcs.h:16
__wchar_t WCHAR
Definition: xmlstorage.h:180
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
static WCHAR szLogFileName[MAX_PATH]
Definition: log.c:15
DWORD OffsetHigh
Definition: winbase.h:788
#define GetEnvironmentVariableW(x, y, z)
Definition: compat.h:420

Referenced by _tmain().

◆ LogEvent()

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

Definition at line 196 of file log.c.

200 {
201 #ifdef DEBUG
202  if (flags & LOG_FILE || flags & LOG_ERROR)
203  LogToFile(lpMsg, errNum, exitCode, flags);
204 #endif
205  if (flags & LOG_EVENTLOG)
206  LogToEventLog(lpMsg, errNum, exitCode, flags);
207 }
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
static VOID LogToFile(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:67
#define LOG_ERROR
Definition: tcpsvcs.h:16
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
GLbitfield flags
Definition: glext.h:7161

Referenced by _tmain(), AcceptConnections(), ChargenHandler(), CreateServers(), CreateServiceThread(), DaytimeHandler(), DiscardHandler(), EchoHandler(), EchoIncomingPackets(), QotdHandler(), ReceiveIncomingPackets(), RetrieveQuote(), SendLine(), SendTime(), ServerCtrlHandler(), ServiceMain(), SetUpListener(), ShutdownConnection(), StartServer(), and UpdateStatus().

◆ LogToEventLog()

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

Definition at line 23 of file log.c.

27 {
28  HANDLE hEventLog;
29 
31  if (hEventLog)
32  {
33  ReportEventW(hEventLog,
35  0,
36  0,
37  NULL,
38  1,
39  0,
40  &lpMsg,
41  NULL);
42 
43  CloseEventLog(hEventLog);
44  }
45 }
static LPWSTR lpEventSource
Definition: log.c:14
#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
BOOL WINAPI ReportEventW(IN HANDLE hEventLog, IN WORD wType, IN WORD wCategory, IN DWORD dwEventID, IN PSID lpUserSid, IN WORD wNumStrings, IN DWORD dwDataSize, IN LPCWSTR *lpStrings, IN LPVOID lpRawData)
Definition: eventlog.c:1516
HANDLE WINAPI RegisterEventSourceW(IN LPCWSTR lpUNCServerName, IN LPCWSTR lpSourceName)
Definition: eventlog.c:1295

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

◆ LogToFile()

static VOID LogToFile ( LPCWSTR  lpMsg,
DWORD  errNum,
DWORD  exitCode,
UINT  flags 
)
static

Definition at line 67 of file log.c.

71 {
72  LPWSTR lpFullMsg = NULL;
73  SIZE_T msgLen;
74 
75  msgLen = wcslen(lpMsg) + 1;
76 
77  if (flags & LOG_ERROR)
78  {
79  LPWSTR lpSysMsg;
80  DWORD eMsgLen;
81 
83  NULL,
84  errNum,
86  (LPWSTR)&lpSysMsg,
87  0,
88  NULL);
89 
90  msgLen = msgLen + eMsgLen + 40;
91 
92  lpFullMsg = HeapAlloc(GetProcessHeap(),
93  0,
94  msgLen * sizeof(TCHAR));
95  if (lpFullMsg)
96  {
97  _snwprintf(lpFullMsg,
98  msgLen,
99  L"%s : %s\tErrNum = %lu ExitCode = %lu\r\n",
100  lpMsg,
101  lpSysMsg,
102  errNum,
103  exitCode);
104  }
105 
106  LocalFree(lpSysMsg);
107 
108  }
109  else
110  {
111  msgLen += 2;
112 
113  lpFullMsg = HeapAlloc(GetProcessHeap(),
114  0,
115  msgLen * sizeof(TCHAR));
116  if (lpFullMsg)
117  {
118  _snwprintf(lpFullMsg,
119  msgLen,
120  L"%s\r\n",
121  lpMsg);
122  }
123  }
124 
125  /* Make sure the length in bytes doesn't overflow a DWORD */
126  msgLen = wcslen(lpFullMsg);
127  if (msgLen > (MAXDWORD / sizeof(WCHAR)))
128  {
130  }
131 
132  if (lpFullMsg)
133  {
135  DWORD dwRet;
136  BOOL bRet;
137 
138  bRet = WriteFile(hLogFile,
139  lpFullMsg,
140  (DWORD)msgLen * sizeof(WCHAR),
141  &bytesWritten,
142  &olWrite);
143  if (!bRet)
144  {
146  {
147  bRet = FALSE;
148  }
149  else
150  {
151  // Write is pending
153 
154  switch (dwRet)
155  {
156  // event has been signaled
157  case WAIT_OBJECT_0:
158  {
160  &olWrite,
161  &bytesWritten,
162  FALSE);
163  break;
164  }
165 
166  default:
167  // An error has occurred in WaitForSingleObject.
168  // This usually indicates a problem with the
169  // OVERLAPPED structure's event handle.
170  bRet = FALSE;
171  break;
172  }
173  }
174  }
175 
176  if (!bRet || bytesWritten == 0)
177  {
178  LogToEventLog(L"Failed to write to log file",
179  GetLastError(),
180  0,
182  }
183 
185  0,
186  lpFullMsg);
187  }
188 
189  if (exitCode > 0)
190  ExitProcess(exitCode);
191 }
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 HANDLE hLogFile
Definition: log.c:16
BOOL WINAPI GetOverlappedResult(IN HANDLE hFile, IN LPOVERLAPPED lpOverlapped, OUT LPDWORD lpNumberOfBytesTransferred, IN BOOL bWait)
Definition: iocompl.c:204
#define LANG_NEUTRAL
Definition: nls.h:22
#define SUBLANG_DEFAULT
Definition: nls.h:168
#define EXCEPTION_INT_OVERFLOW
Definition: winbase.h:321
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
static OVERLAPPED olWrite
Definition: log.c:18
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
HANDLE hEvent
Definition: winbase.h:792
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
smooth NULL
Definition: ftsmooth.c:416
#define MAXDWORD
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
char TCHAR
Definition: xmlstorage.h:189
#define WAIT_OBJECT_0
Definition: winbase.h:387
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
static const WCHAR L[]
Definition: oid.c:1250
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
ULONG_PTR SIZE_T
Definition: typedefs.h:78
VOID WINAPI RaiseException(IN DWORD dwExceptionCode, IN DWORD dwExceptionFlags, IN DWORD nNumberOfArguments, IN CONST ULONG_PTR *lpArguments OPTIONAL)
Definition: except.c:693
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MAKELANGID(p, s)
Definition: nls.h:15
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define INFINITE
Definition: serial.h:102
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by LogEvent().

◆ OpenLogFile()

static BOOL OpenLogFile ( )
static

Definition at line 48 of file log.c.

49 {
53  NULL,
56  NULL);
58  {
59  hLogFile = NULL;
60  return FALSE;
61  }
62 
63  return TRUE;
64 }
#define TRUE
Definition: types.h:120
static HANDLE hLogFile
Definition: log.c:16
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define FILE_SHARE_READ
Definition: compat.h:125
#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
static WCHAR szLogFileName[MAX_PATH]
Definition: log.c:15
#define CreateFileW
Definition: compat.h:408
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46

Referenced by InitLogging().

◆ UninitLogging()

VOID UninitLogging ( )

Definition at line 263 of file log.c.

264 {
265  if (hLogFile)
266  {
269  }
270 
271  if (olWrite.hEvent)
272  {
274  }
275 }
#define CloseHandle
Definition: compat.h:406
static HANDLE hLogFile
Definition: log.c:16
static OVERLAPPED olWrite
Definition: log.c:18
HANDLE hEvent
Definition: winbase.h:792
BOOL WINAPI FlushFileBuffers(IN HANDLE hFile)
Definition: fileinfo.c:175

Referenced by _tmain().

Variable Documentation

◆ hLogFile

HANDLE hLogFile = NULL
static

Definition at line 16 of file log.c.

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

◆ lpEventSource

LPWSTR lpEventSource = L"tcpsvcs"
static

Definition at line 14 of file log.c.

Referenced by AddNewEventSource(), and LogToEventLog().

◆ olWrite

OVERLAPPED olWrite
static

Definition at line 18 of file log.c.

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

◆ szLogFileName

WCHAR szLogFileName[MAX_PATH]
static

Definition at line 15 of file log.c.

Referenced by InitLogging(), and OpenLogFile().