5a941d03865ccc99c52d038faea3a366155d46f8
[reactos.git] / reactos / dll / win32 / syssetup / logfile.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: Log file functions
23 * FILE: lib/syssetup/logfile.c
24 * PROGRAMER: Eric Kohl
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "precomp.h"
30
31 /* GLOBALS ******************************************************************/
32
33 HANDLE hLogFile = NULL;
34
35 /* FUNCTIONS ****************************************************************/
36
37 BOOL WINAPI
38 InitializeSetupActionLog (BOOL bDeleteOldLogFile)
39 {
40 WCHAR szFileName[MAX_PATH];
41
42 GetWindowsDirectoryW(szFileName, MAX_PATH);
43
44 if (szFileName[wcslen(szFileName)] != L'\\')
45 {
46 wcsncat(szFileName,
47 L"\\",
48 MAX_PATH);
49 }
50 wcsncat(szFileName,
51 L"setuplog.txt",
52 MAX_PATH);
53
54 if (bDeleteOldLogFile)
55 {
56 SetFileAttributesW(szFileName, FILE_ATTRIBUTE_NORMAL);
57 DeleteFileW(szFileName);
58 }
59
60 hLogFile = CreateFileW(szFileName,
61 GENERIC_READ | GENERIC_WRITE,
62 FILE_SHARE_READ | FILE_SHARE_WRITE,
63 NULL,
64 OPEN_ALWAYS,
65 FILE_ATTRIBUTE_NORMAL,
66 NULL);
67 if (hLogFile == INVALID_HANDLE_VALUE)
68 {
69 hLogFile = NULL;
70 return FALSE;
71 }
72
73 return TRUE;
74 }
75
76
77 VOID WINAPI
78 TerminateSetupActionLog(VOID)
79 {
80 if (hLogFile != NULL)
81 {
82 CloseHandle (hLogFile);
83 hLogFile = NULL;
84 }
85 }
86
87
88 BOOL WINAPI
89 SYSSETUP_LogItem(IN const LPSTR lpFileName,
90 IN DWORD dwLineNumber,
91 IN DWORD dwSeverity,
92 IN LPWSTR lpMessageText)
93 {
94 LPCSTR lpSeverityString;
95 LPSTR lpMessageString;
96 DWORD dwMessageLength;
97 DWORD dwMessageSize;
98 DWORD dwWritten;
99 CHAR Buffer[6];
100 CHAR TimeBuffer[30];
101 SYSTEMTIME stTime;
102
103 /* Get the severity code string */
104 switch (dwSeverity)
105 {
106 case SYSSETUP_SEVERITY_INFORMATION:
107 lpSeverityString = "Information : ";
108 break;
109
110 case SYSSETUP_SEVERITY_WARNING:
111 lpSeverityString = "Warning : ";
112 break;
113
114 case SYSSETUP_SEVERITY_ERROR:
115 lpSeverityString = "Error : ";
116 break;
117
118 case SYSSETUP_SEVERITY_FATAL_ERROR:
119 lpSeverityString = "Fatal error : ";
120 break;
121
122 default:
123 lpSeverityString = "Unknown : ";
124 break;
125 }
126
127 /* Get length of the converted ansi string */
128 dwMessageLength = wcslen(lpMessageText) * sizeof(WCHAR);
129 RtlUnicodeToMultiByteSize(&dwMessageSize,
130 lpMessageText,
131 dwMessageLength);
132
133 /* Allocate message string buffer */
134 lpMessageString = (LPSTR) HeapAlloc(GetProcessHeap(),
135 HEAP_ZERO_MEMORY,
136 dwMessageSize);
137 if (!lpMessageString)
138 return FALSE;
139
140 /* Convert unicode to ansi */
141 RtlUnicodeToMultiByteN(lpMessageString,
142 dwMessageSize,
143 NULL,
144 lpMessageText,
145 dwMessageLength);
146
147 /* Set file pointer to the end of the file */
148 SetFilePointer(hLogFile,
149 0,
150 NULL,
151 FILE_END);
152
153 /* Write Time/Date */
154 GetLocalTime(&stTime);
155
156 snprintf(TimeBuffer, sizeof(TimeBuffer),
157 "%02d/%02d/%02d %02d:%02d:%02d.%03d",
158 stTime.wMonth,
159 stTime.wDay,
160 stTime.wYear,
161 stTime.wHour,
162 stTime.wMinute,
163 stTime.wSecond,
164 stTime.wMilliseconds);
165
166 WriteFile(hLogFile,
167 TimeBuffer,
168 strlen(TimeBuffer),
169 &dwWritten,
170 NULL);
171
172 /* Write comma */
173 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
174
175 /* Write file name */
176 WriteFile(hLogFile,
177 lpFileName,
178 strlen(lpFileName),
179 &dwWritten,
180 NULL);
181
182 /* Write comma */
183 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
184
185 /* Write line number */
186 snprintf(Buffer, sizeof(Buffer), "%lu", dwLineNumber);
187 WriteFile(hLogFile,
188 Buffer,
189 strlen(Buffer),
190 &dwWritten,
191 NULL);
192
193 /* Write comma */
194 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
195
196 /* Write severity code */
197 WriteFile(hLogFile,
198 lpSeverityString,
199 strlen(lpSeverityString),
200 &dwWritten,
201 NULL);
202
203 /* Write message string */
204 WriteFile(hLogFile,
205 lpMessageString,
206 dwMessageSize,
207 &dwWritten,
208 NULL);
209
210 /* Write newline */
211 WriteFile(hLogFile, "\r\n", 2, &dwWritten, NULL);
212
213 HeapFree(GetProcessHeap(),
214 0,
215 lpMessageString);
216
217 return TRUE;
218 }
219
220 /* EOF */