Sync with trunk head (part 1 or 2)
[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 #define WIN32_NO_STATUS
29 #include <windows.h>
30 #define NTOS_MODE_USER
31 #include <ndk/ntndk.h>
32
33 #include <syssetup/syssetup.h>
34
35 /* GLOBALS ******************************************************************/
36
37 HANDLE hLogFile = NULL;
38
39
40 /* FUNCTIONS ****************************************************************/
41
42 BOOL WINAPI
43 InitializeSetupActionLog (BOOL bDeleteOldLogFile)
44 {
45 WCHAR szFileName[MAX_PATH];
46
47 GetWindowsDirectoryW(szFileName, MAX_PATH);
48
49 if (szFileName[wcslen(szFileName)] != L'\\')
50 {
51 wcsncat(szFileName,
52 L"\\",
53 MAX_PATH);
54 }
55 wcsncat(szFileName,
56 L"setuplog.txt",
57 MAX_PATH);
58
59 if (bDeleteOldLogFile)
60 {
61 SetFileAttributesW(szFileName, FILE_ATTRIBUTE_NORMAL);
62 DeleteFileW(szFileName);
63 }
64
65 hLogFile = CreateFileW(szFileName,
66 GENERIC_READ | GENERIC_WRITE,
67 FILE_SHARE_READ | FILE_SHARE_WRITE,
68 NULL,
69 OPEN_ALWAYS,
70 FILE_ATTRIBUTE_NORMAL,
71 NULL);
72 if (hLogFile == INVALID_HANDLE_VALUE)
73 {
74 hLogFile = NULL;
75 return FALSE;
76 }
77
78 return TRUE;
79 }
80
81
82 VOID WINAPI
83 TerminateSetupActionLog(VOID)
84 {
85 if (hLogFile != NULL)
86 {
87 CloseHandle (hLogFile);
88 hLogFile = NULL;
89 }
90 }
91
92
93 BOOL WINAPI
94 SYSSETUP_LogItem(IN const LPSTR lpFileName,
95 IN DWORD dwLineNumber,
96 IN DWORD dwSeverity,
97 IN LPWSTR lpMessageText)
98 {
99 const LPCSTR lpNewLine = "\r\n";
100 LPCSTR lpSeverityString;
101 LPSTR lpMessageString;
102 DWORD dwMessageLength;
103 DWORD dwMessageSize;
104 DWORD dwWritten;
105 CHAR Buffer[6];
106
107 /* Get the severity code string */
108 switch (dwSeverity)
109 {
110 case SYSSETUP_SEVERITY_INFORMATION:
111 lpSeverityString = "Information : ";
112 break;
113
114 case SYSSETUP_SEVERITY_WARNING:
115 lpSeverityString = "Warning : ";
116 break;
117
118 case SYSSETUP_SEVERITY_ERROR:
119 lpSeverityString = "Error : ";
120 break;
121
122 case SYSSETUP_SEVERITY_FATAL_ERROR:
123 lpSeverityString = "Fatal error : ";
124 break;
125
126 default:
127 lpSeverityString = "Unknown : ";
128 break;
129 }
130
131 /* Get length of the converted ansi string */
132 dwMessageLength = wcslen(lpMessageText) * sizeof(WCHAR);
133 RtlUnicodeToMultiByteSize(&dwMessageSize,
134 lpMessageText,
135 dwMessageLength);
136
137 /* Allocate message string buffer */
138 lpMessageString = (LPSTR) HeapAlloc(GetProcessHeap(),
139 HEAP_ZERO_MEMORY,
140 dwMessageSize);
141 if (!lpMessageString)
142 return FALSE;
143
144 /* Convert unicode to ansi */
145 RtlUnicodeToMultiByteN(lpMessageString,
146 dwMessageSize,
147 NULL,
148 lpMessageText,
149 dwMessageLength);
150
151 /* Set file pointer to the end of the file */
152 SetFilePointer(hLogFile,
153 0,
154 NULL,
155 FILE_END);
156
157 /* Write file name */
158 WriteFile(hLogFile,
159 lpFileName,
160 strlen(lpFileName),
161 &dwWritten,
162 NULL);
163
164 /* Write comma */
165 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
166
167 /* Write line number */
168 snprintf(Buffer, sizeof(Buffer), "%lu", dwLineNumber);
169 WriteFile(hLogFile,
170 Buffer,
171 strlen(Buffer),
172 &dwWritten,
173 NULL);
174
175 /* Write comma */
176 WriteFile(hLogFile, ",", 1, &dwWritten, NULL);
177
178 /* Write severity code */
179 WriteFile(hLogFile,
180 lpSeverityString,
181 strlen(lpSeverityString),
182 &dwWritten,
183 NULL);
184
185 /* Write message string */
186 WriteFile(hLogFile,
187 lpMessageString,
188 dwMessageSize,
189 &dwWritten,
190 NULL);
191
192 /* Write newline */
193 WriteFile(hLogFile,
194 lpNewLine,
195 sizeof(lpNewLine),
196 &dwWritten,
197 NULL);
198
199 HeapFree(GetProcessHeap(),
200 0,
201 lpMessageString);
202
203 return TRUE;
204 }
205
206 /* EOF */