- Update to r53061
[reactos.git] / 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 #include "precomp.h"
29
30 /* GLOBALS ******************************************************************/
31
32 HANDLE hLogFile = NULL;
33
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 const LPCSTR lpNewLine = "\r\n";
95 LPCSTR lpSeverityString;
96 LPSTR lpMessageString;
97 DWORD dwMessageLength;
98 DWORD dwMessageSize;
99 DWORD dwWritten;
100 CHAR Buffer[6];
101
102 /* Get the severity code string */
103 switch (dwSeverity)
104 {
105 case SYSSETUP_SEVERITY_INFORMATION:
106 lpSeverityString = "Information : ";
107 break;
108
109 case SYSSETUP_SEVERITY_WARNING:
110 lpSeverityString = "Warning : ";
111 break;
112
113 case SYSSETUP_SEVERITY_ERROR:
114 lpSeverityString = "Error : ";
115 break;
116
117 case SYSSETUP_SEVERITY_FATAL_ERROR:
118 lpSeverityString = "Fatal error : ";
119 break;
120
121 default:
122 lpSeverityString = "Unknown : ";
123 break;
124 }
125
126 /* Get length of the converted ansi string */
127 dwMessageLength = wcslen(lpMessageText) * sizeof(WCHAR);
128 RtlUnicodeToMultiByteSize(&dwMessageSize,
129 lpMessageText,
130 dwMessageLength);
131
132 /* Allocate message string buffer */
133 lpMessageString = (LPSTR) HeapAlloc(GetProcessHeap(),
134 HEAP_ZERO_MEMORY,
135 dwMessageSize);
136 if (!lpMessageString)
137 return FALSE;
138
139 /* Convert unicode to ansi */
140 RtlUnicodeToMultiByteN(lpMessageString,
141 dwMessageSize,
142 NULL,
143 lpMessageText,
144 dwMessageLength);
145
146 /* Set file pointer to the end of the file */
147 SetFilePointer(hLogFile,
148 0,
149 NULL,
150 FILE_END);
151
152 /* Write file name */
153 WriteFile(hLogFile,
154 lpFileName,
155 strlen(lpFileName),
156 &dwWritten,
157 NULL);
158
159 /* Write comma */
160 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
161
162 /* Write line number */
163 snprintf(Buffer, sizeof(Buffer), "%lu", dwLineNumber);
164 WriteFile(hLogFile,
165 Buffer,
166 strlen(Buffer),
167 &dwWritten,
168 NULL);
169
170 /* Write comma */
171 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
172
173 /* Write severity code */
174 WriteFile(hLogFile,
175 lpSeverityString,
176 strlen(lpSeverityString),
177 &dwWritten,
178 NULL);
179
180 /* Write message string */
181 WriteFile(hLogFile,
182 lpMessageString,
183 dwMessageSize,
184 &dwWritten,
185 NULL);
186
187 /* Write newline */
188 WriteFile(hLogFile,
189 lpNewLine,
190 sizeof(lpNewLine),
191 &dwWritten,
192 NULL);
193
194 HeapFree(GetProcessHeap(),
195 0,
196 lpMessageString);
197
198 return TRUE;
199 }
200
201 /* EOF */