source: trunk/src/cppbase/bs_logger.cpp

Last change on this file was 371, checked in by pr, 17 years ago

Add missing xprf() functions. Fix broken ones also.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 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-2008 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\xprf.h"
51#include "helpers\xstring.h"
52
53// base includes
54#include "cppbase\bs_base.h"
55#include "cppbase\bs_list.h"
56#include "cppbase\bs_string.h"
57#include "cppbase\bs_errors.h"
58
59#include "cppbase\bs_logger.h"
60#include "cppbase\bs_config.h"
61
62#pragma hdrstop
63
64/* ******************************************************************
65 *
66 * BSLoggerRoot implementation
67 *
68 ********************************************************************/
69
70/* ******************************************************************
71 *
72 * BSMemLoggerBase implementation
73 *
74 ********************************************************************/
75
76/*
77 *@@ BSMemLoggerBase:
78 * default constructor for creating an empty log string.
79 *
80 * See BSMemLoggerBase::Append for example usage.
81 *
82 * BSMemLoggerBase::Store and BSMemLoggerBase::Load can be used for
83 * storing a logger into or retrieving one from an INI file.
84 */
85
86BSMemLoggerBase::BSMemLoggerBase()
87{
88 _pabLogString = 0;
89 _cbLogString = 0;
90}
91
92/*
93 *@@ ~BSMemLoggerBase:
94 * destructor
95 */
96
97BSMemLoggerBase::~BSMemLoggerBase()
98{
99 if (_pabLogString)
100 free(_pabLogString);
101}
102
103/*
104 *@@ BSMemLoggerBase:
105 * copy constructor to avoid a flat copy of the heap data.
106 *
107 *@@added V0.9.20 (2002-07-22) [umoeller]
108 */
109
110BSMemLoggerBase::BSMemLoggerBase(const BSMemLoggerBase &l)
111{
112 if (_cbLogString = l._cbLogString)
113 {
114 _pabLogString = (PSZ)malloc(_cbLogString);
115 memcpy(_pabLogString, l._pabLogString, _cbLogString);
116 }
117 else
118 _pabLogString = 0;
119}
120
121/*
122 *@@ operator=:
123 * assignment operator to avoid a flat copy of the heap data.
124 *
125 *@@added V0.9.20 (2002-07-22) [umoeller]
126 */
127
128BSMemLoggerBase& BSMemLoggerBase::operator=(const BSMemLoggerBase &l)
129{
130 if (_cbLogString = l._cbLogString)
131 {
132 _pabLogString = (PSZ)malloc(_cbLogString);
133 memcpy(_pabLogString, l._pabLogString, _cbLogString);
134 }
135 else
136 _pabLogString = 0;
137
138 return *this;
139}
140
141/*
142 *@@ StoreData:
143 * implementation for the pure virtual method
144 * in BSLoggerRoot to store a chunk of data.
145 *
146 * The implementation in BSMemLoggerBase stores
147 * this in the logger's memory block.
148 *
149 *@@added V0.9.9 (2001-03-30) [umoeller]
150 */
151
152void BSMemLoggerBase::StoreData(const char *pabData, unsigned long cbData)
153{
154 if (pabData)
155 {
156 if (_pabLogString == NULL)
157 {
158 // first run:
159 _pabLogString = (char*)malloc(cbData);
160 memcpy(_pabLogString, pabData, cbData);
161 _cbLogString = cbData;
162 }
163 else
164 {
165 // subsequent runs: append after the existing data
166 // V0.9.9 (2001-03-30) [umoeller]: now using realloc
167
168 _pabLogString = (char*)realloc(_pabLogString, _cbLogString + cbData);
169 // copy new attribs behind existing data
170 memcpy(_pabLogString + _cbLogString,
171 pabData,
172 cbData);
173 _cbLogString += cbData;
174
175 /* char* pszTemp = (char*)malloc(_cbLogString + cbData);
176 // copy old buffer
177 memcpy(pszTemp, _pabLogString, _cbLogString);
178 // copy new attribs behind last null byte
179 memcpy(pszTemp + _cbLogString,
180 pszData,
181 cbData);
182 // set new buffer
183 free(_pabLogString);
184 _pabLogString = pszTemp;
185 _cbLogString += cbData; */
186 }
187 }
188}
189
190/*
191 *@@ Append:
192 * this appends binary data to the log string.
193 * This can be any data, but you should be able
194 * to decode it again so if each data is variable
195 * in length, you should add some markers.
196 *
197 * This makes a copy of the binary data and appends
198 * it to the logger, if data already exists.
199 *
200 * Example 1: you could add plain text strings.
201 * Don't forget to store the null terminator then too.
202 *
203 + const char *pcsz = "Hello";
204 + Logger.Append(pcsz, strlen(pcsz) + 1);
205 *
206 * Example 2: you could add binary data, with
207 * each item having a predefined length.
208 *
209 + char *pabData = (char*)malloc(10);
210 + memset(pabData, 0, 10);
211 + Logger.Append(pabData, 10);
212 + free(pabData);
213 *
214 *@@changed V0.9.1 (2000-01-05) [umoeller]: fixed memory allocation problems
215 *@@changed V0.9.9 (2001-03-30) [umoeller]: now using realloc
216 */
217
218void BSMemLoggerBase::Append(const char *pabData, // in: data block to append (must be terminated with null)
219 unsigned long cbData) // in: sizeof(*pszData), including the null byte
220{
221 StoreData(pabData, cbData);
222}
223
224/*
225 *@@ Append:
226 * overloaded Append, which takes a BSUString as input.
227 * this takes a BSString as input. This is faster than
228 * the const char* method because BSString maintains the
229 * length of the string automatically.
230 *
231 * BSMemLoggerBase now only accepts ustrings, no longer
232 * codepage strings V0.9.18 (2002-03-08) [umoeller].
233 *
234 * Example:
235 + BSUString str("Hello");
236 + Logger.Append(str);
237 * is equal to
238 + Logger.Append("Hello", strlen("Hello") + 1);
239 *
240 *@@added V0.9.9 (2001-02-28) [umoeller]
241 *@@changed V0.9.12 (2001-05-22) [umoeller]: fixed missing null terminator (CONFIG.SYS garbage in database)
242 *@@changed V0.9.18 (2002-03-08) [umoeller]: now using ustrings
243 */
244
245void BSMemLoggerBase::Append(const ustring &ustr)
246{
247 StoreData(ustr.GetBuffer(),
248 ustr.size() + 1); // null terminator was missing,
249 // fixed V0.9.12 (2001-05-22) [umoeller]
250}
251
252/*
253 *@@ Clear:
254 * this clears the whole Logger string.
255 *
256 *@@added WarpIN V1.0.10 (2006-04-05) [pr]
257 */
258
259void BSMemLoggerBase::Clear(void)
260{
261 if (_pabLogString)
262 {
263 free(_pabLogString);
264 _pabLogString = 0;
265 _cbLogString = 0;
266 }
267}
268
269/*
270 *@@ Store:
271 * this stores the whole BSMemLoggerBase in the given profile key.
272 * Returns TRUE if successfully written.
273 *
274 * Note: if the logger is currently empty, this will delete
275 * the given INI key.
276 *
277 *@@changed WarpIN V1.0.18 (2008-10-06) [pr]: added Store(PXINI...)
278 */
279
280BOOL BSMemLoggerBase::Store(HINI hini, // in: INI handle
281 const char *pszApp, // in: INI application
282 const char *pszKey) // in: INI key
283 const
284{
285 return (PrfWriteProfileData(hini, pszApp, pszKey, _pabLogString, _cbLogString));
286}
287
288BOOL BSMemLoggerBase::Store(PXINI pXIni, // in: INI handle
289 const char *pszApp, // in: INI application
290 const char *pszKey) // in: INI key
291 const
292{
293 return (xprfWriteProfileData(pXIni, pszApp, pszKey, _pabLogString, _cbLogString));
294}
295
296/*
297 *@@ Load:
298 * reverse to BSMemLoggerBase::Store, this loads the data from INI.
299 * This will overwrite the current contents of the logger.
300 *
301 * Returns TRUE if data was found.
302 *
303 *@@changed WarpIN V1.0.18 (2008-10-06) [pr]: added Load(PXINI...)
304 */
305
306BOOL BSMemLoggerBase::Load(HINI hini, // in: INI handle
307 const char *pszApp, // in: INI application
308 const char *pszKey) // in: INI key
309{
310 if (_pabLogString)
311 free(_pabLogString);
312 _pabLogString = prfhQueryProfileData(hini,
313 (PSZ)pszApp,
314 (PSZ)pszKey,
315 &_cbLogString);
316 return (_pabLogString != 0);
317}
318
319BOOL BSMemLoggerBase::Load(PXINI pXIni, // in: INI handle
320 const char *pszApp, // in: INI application
321 const char *pszKey) // in: INI key
322{
323 if (_pabLogString)
324 free(_pabLogString);
325 _pabLogString = xprfhQueryProfileData(pXIni,
326 (PSZ)pszApp,
327 (PSZ)pszKey,
328 &_cbLogString);
329 return (_pabLogString != 0);
330}
331
332/* ******************************************************************
333 *
334 * BSFileLogger implementation
335 *
336 ********************************************************************/
337
338BSMutex G_mtxFileLoggers; // V0.9.20 (2002-07-06) [umoeller]
339
340#if 0
341
342HMTX G_hmtxFileLoggers = NULLHANDLE;
343
344/*
345 *@@ LockFileLoggers:
346 *
347 *@@added V0.9.12 (2001-05-31) [umoeller]
348 */
349
350BOOL LockFileLoggers(VOID)
351{
352 if (!G_hmtxFileLoggers)
353 return (!DosCreateMutexSem(NULL,
354 &G_hmtxFileLoggers,
355 0,
356 TRUE));
357
358 return (!DosRequestMutexSem(G_hmtxFileLoggers, SEM_INDEFINITE_WAIT));
359}
360
361/*
362 *@@ UnlockFileLoggers:
363 *
364 *@@added V0.9.12 (2001-05-31) [umoeller]
365 */
366
367VOID UnlockFileLoggers(VOID)
368{
369 DosReleaseMutexSem(G_hmtxFileLoggers);
370}
371
372#endif
373
374/*
375 *@@ BSFileLogger:
376 * constructor to open a file logger with the
377 * specified file name.
378 *
379 *@@added V0.9.9 (2001-03-30) [umoeller]
380 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
381 *@@changed V0.9.19 (2002-07-01) [umoeller]: added bldlevel to log
382 *@@changed WarpIN V1.0.14 (2006-11-30) [pr]: removed WarpIN specific header message
383 */
384
385BSFileLogger::BSFileLogger(ULONG ulDummy,
386 const char *pcszFilename)
387{
388 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
389
390 _Pmpf(("BSFileLogger: constructor"));
391
392 _File = NULL;
393 _strFileName = pcszFilename;
394
395 _indent = 0;
396
397 if (pcszFilename)
398 {
399 _File = fopen(pcszFilename,
400 "a");
401 if (!_File)
402 {
403 CHAR szError[400];
404 sprintf(szError,
405 "Cannot open log file \"%s\" for writing.",
406 pcszFilename);
407 throw BSLoggerExcpt(szError);
408 }
409 }
410 else
411 throw BSLoggerExcpt("pcszFilename is NULL.");
412}
413
414/*
415 *@@ ~BSFileLogger:
416 * destructor. Closes the log file.
417 *
418 *@@added V0.9.9 (2001-03-30) [umoeller]
419 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
420 *@@changed WarpIN V1.0.14 (2006-11-30) [pr]: removed "install log closed" message
421 */
422
423BSFileLogger::~BSFileLogger()
424{
425 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
426
427 _Pmpf(("BSFileLogger: destructor"));
428
429 if (_File)
430 {
431 fclose(_File);
432 _File = NULL;
433 }
434}
435
436/*
437 *@@ IncIndent:
438 * modifies indentation for the file output
439 * (spaces between date/time and the actual
440 * logger output).
441 *
442 * A positive "i" increases the spacing, a
443 * negative decreases it.
444 *
445 *@@added V0.9.9 (2001-03-30) [umoeller]
446 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
447 */
448
449void BSFileLogger::IncIndent(int i)
450{
451 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
452
453 _indent += i;
454
455 if (i < 0)
456 i = 0;
457}
458
459/*
460 *@@ WriteV:
461 *
462 * NOTE: This appends \n for each string.
463 *
464 *@@added V0.9.9 (2001-03-30) [umoeller]
465 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
466 *@@changed V0.9.14 (2001-07-26) [umoeller]: added hundredths to log lines
467 *@@changed V0.9.18 (2002-03-08) [umoeller]: renamed from Append(); now adding \n after each line
468 */
469
470void BSFileLogger::WriteV(const char *pcszFormat, // in: format string
471 va_list arg_ptr)
472{
473 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
474
475 DATETIME dt;
476 DosGetDateTime(&dt);
477
478 fprintf(_File,
479 "%02d:%02d:%02d.%02d ",
480 dt.hours, dt.minutes, dt.seconds, dt.hundredths);
481
482 if (_indent)
483 {
484 for (int i = 0;
485 i < _indent;
486 i++)
487 fprintf(_File, " ");
488 }
489
490 vfprintf(_File, pcszFormat, arg_ptr);
491 fprintf(_File, "\n");
492}
493
494/*
495 *@@ Write:
496 *
497 *@@added V0.9.18 (2002-03-08) [umoeller]
498 */
499
500void BSFileLogger::Write(const char *pcszFormat, // in: format string
501 ...) // in: variable arguments
502{
503 va_list arg_ptr;
504 va_start(arg_ptr, pcszFormat);
505 WriteV(pcszFormat, arg_ptr);
506 va_end(arg_ptr);
507}
508
509/*
510 *@@ WriteRawV:
511 *
512 *@@added WarpIN V1.0.14 (2006-11-30) [pr]
513 */
514
515void BSFileLogger::WriteRawV(const char *pcszFormat, // in: format string
516 va_list arg_ptr)
517{
518 BSLock lock(G_mtxFileLoggers);
519
520 vfprintf(_File, pcszFormat, arg_ptr);
521}
522
523/*
524 *@@ WriteRaw:
525 *
526 *@@added WarpIN V1.0.14 (2006-11-30) [pr]
527 */
528
529void BSFileLogger::WriteRaw(const char *pcszFormat, // in: format string
530 ...) // in: variable arguments
531{
532 va_list arg_ptr;
533 va_start(arg_ptr, pcszFormat);
534 WriteRawV(pcszFormat, arg_ptr);
535 va_end(arg_ptr);
536}
537
Note: See TracBrowser for help on using the repository browser.