migrate substitution keywords to SVN
[reactos.git] / reactos / lib / 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * PURPOSE: Log file functions
24 * FILE: lib/syssetup/logfile.c
25 * PROGRAMER: Eric Kohl
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <windows.h>
31 #include <ntos.h>
32 #include <string.h>
33 #include <tchar.h>
34
35 #include <syssetup.h>
36
37
38 /* GLOBALS ******************************************************************/
39
40 HANDLE hLogFile = NULL;
41
42
43 /* FUNCTIONS ****************************************************************/
44
45 BOOL STDCALL
46 InitializeSetupActionLog (BOOL bDeleteOldLogFile)
47 {
48 WCHAR szFileName[MAX_PATH];
49
50 GetWindowsDirectoryW (szFileName,
51 MAX_PATH);
52
53 if (szFileName[wcslen (szFileName)] != L'\\')
54 {
55 wcsncat (szFileName,
56 L"\\",
57 MAX_PATH);
58 }
59 wcsncat (szFileName,
60 L"setuplog.txt",
61 MAX_PATH);
62
63 if (bDeleteOldLogFile != FALSE)
64 {
65 SetFileAttributesW (szFileName,
66 FILE_ATTRIBUTE_NORMAL);
67 DeleteFileW (szFileName);
68 }
69
70 hLogFile = CreateFileW (szFileName,
71 GENERIC_READ | GENERIC_WRITE,
72 FILE_SHARE_READ | FILE_SHARE_WRITE,
73 NULL,
74 OPEN_ALWAYS,
75 FILE_ATTRIBUTE_NORMAL,
76 NULL);
77 if (hLogFile == INVALID_HANDLE_VALUE)
78 {
79 hLogFile = NULL;
80 return FALSE;
81 }
82
83 return TRUE;
84 }
85
86
87 VOID STDCALL
88 TerminateSetupActionLog (VOID)
89 {
90 if (hLogFile != NULL)
91 {
92 CloseHandle (hLogFile);
93 hLogFile = NULL;
94 }
95 }
96
97
98 BOOL STDCALL
99 LogItem (DWORD dwSeverity,
100 LPWSTR lpMessageText)
101 {
102 LPSTR lpNewLine = "\r\n";
103 LPSTR lpSeverityString;
104 LPSTR lpMessageString;
105 DWORD dwMessageLength;
106 DWORD dwMessageSize;
107 DWORD dwWritten;
108
109 /* Get the severity code string */
110 switch (dwSeverity)
111 {
112 case SYSSETUP_SEVERITY_INFORMATION:
113 lpSeverityString = "Information : ";
114 break;
115
116 case SYSSETUP_SEVERITY_WARNING:
117 lpSeverityString = "Warning : ";
118 break;
119
120 case SYSSETUP_SEVERITY_ERROR:
121 lpSeverityString = "Error : ";
122 break;
123
124 case SYSSETUP_SEVERITY_FATAL_ERROR:
125 lpSeverityString = "Fatal error : ";
126 break;
127
128 default:
129 lpSeverityString = "Unknown : ";
130 break;
131 }
132
133 /* Get length of the converted ansi string */
134 dwMessageLength = wcslen(lpMessageText) * sizeof(WCHAR);
135 RtlUnicodeToMultiByteSize (&dwMessageSize,
136 lpMessageText,
137 dwMessageLength);
138
139 /* Allocate message string buffer */
140 lpMessageString = (LPSTR) HeapAlloc (GetProcessHeap (),
141 HEAP_ZERO_MEMORY,
142 dwMessageSize);
143 if (lpMessageString == NULL)
144 {
145 return FALSE;
146 }
147
148 /* Convert unicode to ansi */
149 RtlUnicodeToMultiByteN (lpMessageString,
150 dwMessageSize,
151 NULL,
152 lpMessageText,
153 dwMessageLength);
154
155 /* Set file pointer to the end of the file */
156 SetFilePointer (hLogFile,
157 0,
158 NULL,
159 FILE_END);
160
161 /* Write severity code */
162 WriteFile (hLogFile,
163 lpSeverityString,
164 strlen (lpSeverityString),
165 &dwWritten,
166 NULL);
167
168 /* Write message string */
169 WriteFile (hLogFile,
170 lpMessageString,
171 dwMessageSize,
172 &dwWritten,
173 NULL);
174
175 /* Write newline */
176 WriteFile (hLogFile,
177 lpNewLine,
178 2,
179 &dwWritten,
180 NULL);
181
182 HeapFree (GetProcessHeap (),
183 0,
184 lpMessageString);
185
186 return TRUE;
187 }
188
189 /* EOF */