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