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