source: trunk/src/cppbase/bs_logger.cpp@ 279

Last change on this file since 279 was 254, checked in by umoeller, 22 years ago

Minor fixes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1
2/*
3 *@@sourcefile bs_logger.cpp:
4 * this implements the various logger classes.
5 *
6 * See BSLoggerRoot and subclasses for details.
7 *
8 * The base logger class was renamed from INILogger and moved
9 * to this separate file (99-11-01) [umoeller].
10 * More logger classes were added with V0.9.9 (2001-03-30) [umoeller].
11 *
12 *@@header "cppbase\bs_logger.h"
13 *@@added V0.9.0 (99-11-01) [umoeller]
14 */
15
16/*
17 * This file Copyright (C) 1999-2002 Ulrich M”ller.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, in version 2 as it comes in the COPYING
21 * file of this distribution.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 */
27
28#define OS2EMX_PLAIN_CHAR
29 // this is needed for "os2emx.h"; if this is defined,
30 // emx will define PSZ as _signed_ char, otherwise
31 // as unsigned char
32
33#define INCL_DOSSEMAPHORES
34#define INCL_WINSHELLDATA
35#include <os2.h>
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <stdarg.h>
41#include <io.h>
42#include <fcntl.h>
43#include <sys\stat.h>
44
45#include "setup.h"
46#include "bldlevel.h"
47
48// include's from helpers
49#include "helpers\prfh.h"
50#include "helpers\xstring.h"
51
52// base includes
53#include "cppbase\bs_base.h"
54#include "cppbase\bs_list.h"
55#include "cppbase\bs_string.h"
56#include "cppbase\bs_errors.h"
57
58#include "cppbase\bs_logger.h"
59#include "cppbase\bs_config.h"
60
61#pragma hdrstop
62
63/* ******************************************************************
64 *
65 * BSLoggerRoot implementation
66 *
67 ********************************************************************/
68
69/* ******************************************************************
70 *
71 * BSMemLoggerBase implementation
72 *
73 ********************************************************************/
74
75/*
76 *@@ BSMemLoggerBase:
77 * default constructor for creating an empty log string.
78 *
79 * See BSMemLoggerBase::Append for example usage.
80 *
81 * BSMemLoggerBase::Store and BSMemLoggerBase::Load can be used for
82 * storing a logger into or retrieving one from an INI file.
83 */
84
85BSMemLoggerBase::BSMemLoggerBase()
86{
87 _pabLogString = 0;
88 _cbLogString = 0;
89}
90
91/*
92 *@@ ~BSMemLoggerBase:
93 * destructor
94 */
95
96BSMemLoggerBase::~BSMemLoggerBase()
97{
98 if (_pabLogString)
99 free(_pabLogString);
100}
101
102/*
103 *@@ BSMemLoggerBase:
104 * copy constructor to avoid a flat copy of the heap data.
105 *
106 *@@added V0.9.20 (2002-07-22) [umoeller]
107 */
108
109BSMemLoggerBase::BSMemLoggerBase(const BSMemLoggerBase &l)
110{
111 if (_cbLogString = l._cbLogString)
112 {
113 _pabLogString = (PSZ)malloc(_cbLogString);
114 memcpy(_pabLogString, l._pabLogString, _cbLogString);
115 }
116 else
117 _pabLogString = 0;
118}
119
120/*
121 *@@ operator=:
122 * assignment operator to avoid a flat copy of the heap data.
123 *
124 *@@added V0.9.20 (2002-07-22) [umoeller]
125 */
126
127BSMemLoggerBase& BSMemLoggerBase::operator=(const BSMemLoggerBase &l)
128{
129 if (_cbLogString = l._cbLogString)
130 {
131 _pabLogString = (PSZ)malloc(_cbLogString);
132 memcpy(_pabLogString, l._pabLogString, _cbLogString);
133 }
134 else
135 _pabLogString = 0;
136
137 return *this;
138}
139
140/*
141 *@@ StoreData:
142 * implementation for the pure virtual method
143 * in BSLoggerRoot to store a chunk of data.
144 *
145 * The implementation in BSMemLoggerBase stores
146 * this in the logger's memory block.
147 *
148 *@@added V0.9.9 (2001-03-30) [umoeller]
149 */
150
151void BSMemLoggerBase::StoreData(const char *pabData, unsigned long cbData)
152{
153 if (pabData)
154 {
155 if (_pabLogString == NULL)
156 {
157 // first run:
158 _pabLogString = (char*)malloc(cbData);
159 memcpy(_pabLogString, pabData, cbData);
160 _cbLogString = cbData;
161 }
162 else
163 {
164 // subsequent runs: append after the existing data
165 // V0.9.9 (2001-03-30) [umoeller]: now using realloc
166
167 _pabLogString = (char*)realloc(_pabLogString, _cbLogString + cbData);
168 // copy new attribs behind existing data
169 memcpy(_pabLogString + _cbLogString,
170 pabData,
171 cbData);
172 _cbLogString += cbData;
173
174 /* char* pszTemp = (char*)malloc(_cbLogString + cbData);
175 // copy old buffer
176 memcpy(pszTemp, _pabLogString, _cbLogString);
177 // copy new attribs behind last null byte
178 memcpy(pszTemp + _cbLogString,
179 pszData,
180 cbData);
181 // set new buffer
182 free(_pabLogString);
183 _pabLogString = pszTemp;
184 _cbLogString += cbData; */
185 }
186 }
187}
188
189/*
190 *@@ Append:
191 * this appends binary data to the log string.
192 * This can be any data, but you should be able
193 * to decode it again so if each data is variable
194 * in length, you should add some markers.
195 *
196 * This makes a copy of the binary data and appends
197 * it to the logger, if data already exists.
198 *
199 * Example 1: you could add plain text strings.
200 * Don't forget to store the null terminator then too.
201 *
202 + const char *pcsz = "Hello";
203 + Logger.Append(pcsz, strlen(pcsz) + 1);
204 *
205 * Example 2: you could add binary data, with
206 * each item having a predefined length.
207 *
208 + char *pabData = (char*)malloc(10);
209 + memset(pabData, 0, 10);
210 + Logger.Append(pabData, 10);
211 + free(pabData);
212 *
213 *@@changed V0.9.1 (2000-01-05) [umoeller]: fixed memory allocation problems
214 *@@changed V0.9.9 (2001-03-30) [umoeller]: now using realloc
215 */
216
217void BSMemLoggerBase::Append(const char *pabData, // in: data block to append (must be terminated with null)
218 unsigned long cbData) // in: sizeof(*pszData), including the null byte
219{
220 StoreData(pabData, cbData);
221}
222
223/*
224 *@@ Append:
225 * overloaded Append, which takes a BSUString as input.
226 * this takes a BSString as input. This is faster than
227 * the const char* method because BSString maintains the
228 * length of the string automatically.
229 *
230 * BSMemLoggerBase now only accepts ustrings, no longer
231 * codepage strings V0.9.18 (2002-03-08) [umoeller].
232 *
233 * Example:
234 + BSUString str("Hello");
235 + Logger.Append(str);
236 * is equal to
237 + Logger.Append("Hello", strlen("Hello") + 1);
238 *
239 *@@added V0.9.9 (2001-02-28) [umoeller]
240 *@@changed V0.9.12 (2001-05-22) [umoeller]: fixed missing null terminator (CONFIG.SYS garbage in database)
241 *@@changed V0.9.18 (2002-03-08) [umoeller]: now using ustrings
242 */
243
244void BSMemLoggerBase::Append(const ustring &ustr)
245{
246 StoreData(ustr.GetBuffer(),
247 ustr.size() + 1); // null terminator was missing,
248 // fixed V0.9.12 (2001-05-22) [umoeller]
249}
250
251/*
252 *@@ Store:
253 * this stores the whole BSMemLoggerBase in the given profile key.
254 * Returns TRUE if successfully written.
255 *
256 * Note: if the logger is currently empty, this will delete
257 * the given INI key.
258 */
259
260BOOL BSMemLoggerBase::Store(HINI hini, // in: INI handle
261 const char *pszApp, // in: INI application
262 const char *pszKey) // in: INI key
263 const
264{
265 return (PrfWriteProfileData(hini, pszApp, pszKey, _pabLogString, _cbLogString));
266}
267
268/*
269 *@@ Load:
270 * reverse to BSMemLoggerBase::Store, this loads the data from INI.
271 * This will overwrite the current contents of the logger.
272 *
273 * Returns TRUE if data was found.
274 */
275
276BOOL BSMemLoggerBase::Load(HINI hini, // in: INI handle
277 const char *pszApp, // in: INI application
278 const char *pszKey) // in: INI key
279{
280 if (_pabLogString)
281 free(_pabLogString);
282 _pabLogString = prfhQueryProfileData(hini,
283 (PSZ)pszApp,
284 (PSZ)pszKey,
285 &_cbLogString);
286 return (_pabLogString != 0);
287}
288
289/* ******************************************************************
290 *
291 * BSFileLogger implementation
292 *
293 ********************************************************************/
294
295BSMutex G_mtxFileLoggers; // V0.9.20 (2002-07-06) [umoeller]
296
297#if 0
298
299HMTX G_hmtxFileLoggers = NULLHANDLE;
300
301/*
302 *@@ LockFileLoggers:
303 *
304 *@@added V0.9.12 (2001-05-31) [umoeller]
305 */
306
307BOOL LockFileLoggers(VOID)
308{
309 if (!G_hmtxFileLoggers)
310 return (!DosCreateMutexSem(NULL,
311 &G_hmtxFileLoggers,
312 0,
313 TRUE));
314
315 return (!DosRequestMutexSem(G_hmtxFileLoggers, SEM_INDEFINITE_WAIT));
316}
317
318/*
319 *@@ UnlockFileLoggers:
320 *
321 *@@added V0.9.12 (2001-05-31) [umoeller]
322 */
323
324VOID UnlockFileLoggers(VOID)
325{
326 DosReleaseMutexSem(G_hmtxFileLoggers);
327}
328
329#endif
330
331/*
332 *@@ BSFileLogger:
333 * constructor to open a file logger with the
334 * specified file name.
335 *
336 *@@added V0.9.9 (2001-03-30) [umoeller]
337 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
338 *@@changed V0.9.19 (2002-07-01) [umoeller]: added bldlevel to log
339 */
340
341BSFileLogger::BSFileLogger(ULONG ulDummy,
342 const char *pcszFilename)
343{
344 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
345
346 _Pmpf(("BSFileLogger: constructor"));
347
348 _File = NULL;
349 _strFileName = pcszFilename;
350
351 _indent = 0;
352
353 if (pcszFilename)
354 {
355 _File = fopen(pcszFilename,
356 "a");
357 if (!_File)
358 {
359 CHAR szError[400];
360 sprintf(szError,
361 "Cannot open log file \"%s\" for writing.",
362 pcszFilename);
363 throw BSLoggerExcpt(szError);
364 }
365 }
366 else
367 throw BSLoggerExcpt("pcszFilename is NULL.");
368
369 DATETIME dt;
370 DosGetDateTime(&dt);
371
372 fprintf(_File,
373 "\n\nWarpIN " BLDLEVEL_VERSION " install log opened\n"
374 "Date: %04d-%02d-%02d, Time: %02d:%02d:%02d\n"
375 "--------------------------------\n",
376 dt.year, dt.month, dt.day,
377 dt.hours, dt.minutes, dt.seconds);
378}
379
380/*
381 *@@ ~BSFileLogger:
382 * destructor. Closes the log file.
383 *
384 *@@added V0.9.9 (2001-03-30) [umoeller]
385 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
386 */
387
388BSFileLogger::~BSFileLogger()
389{
390 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
391
392 _Pmpf(("BSFileLogger: destructor"));
393
394 if (_File)
395 {
396 fprintf(_File, "\nInstall log closed.\n");
397 fclose(_File);
398 _File = NULL;
399 }
400}
401
402/*
403 *@@ IncIndent:
404 * modifies indentation for the file output
405 * (spaces between date/time and the actual
406 * logger output).
407 *
408 * A positive "i" increases the spacing, a
409 * negative decreases it.
410 *
411 *@@added V0.9.9 (2001-03-30) [umoeller]
412 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
413 */
414
415void BSFileLogger::IncIndent(int i)
416{
417 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
418
419 _indent += i;
420
421 if (i < 0)
422 i = 0;
423}
424
425/*
426 *@@ Write:
427 *
428 * NOTE: This does not append \n for each string.
429 *
430 *@@added V0.9.9 (2001-03-30) [umoeller]
431 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
432 *@@changed V0.9.14 (2001-07-26) [umoeller]: added hundredths to log lines
433 *@@changed V0.9.18 (2002-03-08) [umoeller]: renamed from Append(); now adding \n after each line
434 */
435
436void BSFileLogger::WriteV(const char *pcszFormat, // in: format string
437 va_list arg_ptr)
438{
439 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
440
441 _Pmpf(("BSFileLogger::Append: got \"%s\"", pcszFormat));
442
443 DATETIME dt;
444 DosGetDateTime(&dt);
445
446 fprintf(_File,
447 "%02d:%02d:%02d.%02d ",
448 dt.hours, dt.minutes, dt.seconds, dt.hundredths);
449
450 if (_indent)
451 {
452 for (int i = 0;
453 i < _indent;
454 i++)
455 fprintf(_File, " ");
456 }
457
458 int iWritten = vfprintf(_File, pcszFormat, arg_ptr);
459 fprintf(_File, "\n");
460}
461
462/*
463 *@@ Append:
464 *
465 *@@added V0.9.18 (2002-03-08) [umoeller]
466 */
467
468void BSFileLogger::Write(const char *pcszFormat, // in: format string
469 ...) // in: variable arguments
470{
471 va_list arg_ptr;
472 va_start(arg_ptr, pcszFormat);
473 WriteV(pcszFormat, arg_ptr);
474 va_end(arg_ptr);
475}
476
Note: See TracBrowser for help on using the repository browser.