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

Last change on this file since 341 was 341, checked in by pr, 19 years ago

Remove WarpIN specific messages. Add WriteRaw().

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.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-2006 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 *@@ Clear:
253 * this clears the whole Logger string.
254 *
255 *@@added WarpIN V1.0.10 (2006-04-05) [pr]
256 */
257
258void BSMemLoggerBase::Clear(void)
259{
260 if (_pabLogString)
261 {
262 free(_pabLogString);
263 _pabLogString = 0;
264 _cbLogString = 0;
265 }
266}
267
268/*
269 *@@ Store:
270 * this stores the whole BSMemLoggerBase in the given profile key.
271 * Returns TRUE if successfully written.
272 *
273 * Note: if the logger is currently empty, this will delete
274 * the given INI key.
275 */
276
277BOOL BSMemLoggerBase::Store(HINI hini, // in: INI handle
278 const char *pszApp, // in: INI application
279 const char *pszKey) // in: INI key
280 const
281{
282 return (PrfWriteProfileData(hini, pszApp, pszKey, _pabLogString, _cbLogString));
283}
284
285/*
286 *@@ Load:
287 * reverse to BSMemLoggerBase::Store, this loads the data from INI.
288 * This will overwrite the current contents of the logger.
289 *
290 * Returns TRUE if data was found.
291 */
292
293BOOL BSMemLoggerBase::Load(HINI hini, // in: INI handle
294 const char *pszApp, // in: INI application
295 const char *pszKey) // in: INI key
296{
297 if (_pabLogString)
298 free(_pabLogString);
299 _pabLogString = prfhQueryProfileData(hini,
300 (PSZ)pszApp,
301 (PSZ)pszKey,
302 &_cbLogString);
303 return (_pabLogString != 0);
304}
305
306/* ******************************************************************
307 *
308 * BSFileLogger implementation
309 *
310 ********************************************************************/
311
312BSMutex G_mtxFileLoggers; // V0.9.20 (2002-07-06) [umoeller]
313
314#if 0
315
316HMTX G_hmtxFileLoggers = NULLHANDLE;
317
318/*
319 *@@ LockFileLoggers:
320 *
321 *@@added V0.9.12 (2001-05-31) [umoeller]
322 */
323
324BOOL LockFileLoggers(VOID)
325{
326 if (!G_hmtxFileLoggers)
327 return (!DosCreateMutexSem(NULL,
328 &G_hmtxFileLoggers,
329 0,
330 TRUE));
331
332 return (!DosRequestMutexSem(G_hmtxFileLoggers, SEM_INDEFINITE_WAIT));
333}
334
335/*
336 *@@ UnlockFileLoggers:
337 *
338 *@@added V0.9.12 (2001-05-31) [umoeller]
339 */
340
341VOID UnlockFileLoggers(VOID)
342{
343 DosReleaseMutexSem(G_hmtxFileLoggers);
344}
345
346#endif
347
348/*
349 *@@ BSFileLogger:
350 * constructor to open a file logger with the
351 * specified file name.
352 *
353 *@@added V0.9.9 (2001-03-30) [umoeller]
354 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
355 *@@changed V0.9.19 (2002-07-01) [umoeller]: added bldlevel to log
356 *@@changed WarpIN V1.0.14 (2006-11-30) [pr]: removed WarpIN specific header message
357 */
358
359BSFileLogger::BSFileLogger(ULONG ulDummy,
360 const char *pcszFilename)
361{
362 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
363
364 _Pmpf(("BSFileLogger: constructor"));
365
366 _File = NULL;
367 _strFileName = pcszFilename;
368
369 _indent = 0;
370
371 if (pcszFilename)
372 {
373 _File = fopen(pcszFilename,
374 "a");
375 if (!_File)
376 {
377 CHAR szError[400];
378 sprintf(szError,
379 "Cannot open log file \"%s\" for writing.",
380 pcszFilename);
381 throw BSLoggerExcpt(szError);
382 }
383 }
384 else
385 throw BSLoggerExcpt("pcszFilename is NULL.");
386}
387
388/*
389 *@@ ~BSFileLogger:
390 * destructor. Closes the log file.
391 *
392 *@@added V0.9.9 (2001-03-30) [umoeller]
393 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
394 *@@changed WarpIN V1.0.14 (2006-11-30) [pr]: removed "install log closed" message
395 */
396
397BSFileLogger::~BSFileLogger()
398{
399 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
400
401 _Pmpf(("BSFileLogger: destructor"));
402
403 if (_File)
404 {
405 fclose(_File);
406 _File = NULL;
407 }
408}
409
410/*
411 *@@ IncIndent:
412 * modifies indentation for the file output
413 * (spaces between date/time and the actual
414 * logger output).
415 *
416 * A positive "i" increases the spacing, a
417 * negative decreases it.
418 *
419 *@@added V0.9.9 (2001-03-30) [umoeller]
420 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
421 */
422
423void BSFileLogger::IncIndent(int i)
424{
425 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
426
427 _indent += i;
428
429 if (i < 0)
430 i = 0;
431}
432
433/*
434 *@@ WriteV:
435 *
436 * NOTE: This appends \n for each string.
437 *
438 *@@added V0.9.9 (2001-03-30) [umoeller]
439 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
440 *@@changed V0.9.14 (2001-07-26) [umoeller]: added hundredths to log lines
441 *@@changed V0.9.18 (2002-03-08) [umoeller]: renamed from Append(); now adding \n after each line
442 */
443
444void BSFileLogger::WriteV(const char *pcszFormat, // in: format string
445 va_list arg_ptr)
446{
447 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
448
449 DATETIME dt;
450 DosGetDateTime(&dt);
451
452 fprintf(_File,
453 "%02d:%02d:%02d.%02d ",
454 dt.hours, dt.minutes, dt.seconds, dt.hundredths);
455
456 if (_indent)
457 {
458 for (int i = 0;
459 i < _indent;
460 i++)
461 fprintf(_File, " ");
462 }
463
464 vfprintf(_File, pcszFormat, arg_ptr);
465 fprintf(_File, "\n");
466}
467
468/*
469 *@@ Write:
470 *
471 *@@added V0.9.18 (2002-03-08) [umoeller]
472 */
473
474void BSFileLogger::Write(const char *pcszFormat, // in: format string
475 ...) // in: variable arguments
476{
477 va_list arg_ptr;
478 va_start(arg_ptr, pcszFormat);
479 WriteV(pcszFormat, arg_ptr);
480 va_end(arg_ptr);
481}
482
483/*
484 *@@ WriteRawV:
485 *
486 *@@added WarpIN V1.0.14 (2006-11-30) [pr]
487 */
488
489void BSFileLogger::WriteRawV(const char *pcszFormat, // in: format string
490 va_list arg_ptr)
491{
492 BSLock lock(G_mtxFileLoggers);
493
494 vfprintf(_File, pcszFormat, arg_ptr);
495}
496
497/*
498 *@@ WriteRaw:
499 *
500 *@@added WarpIN V1.0.14 (2006-11-30) [pr]
501 */
502
503void BSFileLogger::WriteRaw(const char *pcszFormat, // in: format string
504 ...) // in: variable arguments
505{
506 va_list arg_ptr;
507 va_start(arg_ptr, pcszFormat);
508 WriteRawV(pcszFormat, arg_ptr);
509 va_end(arg_ptr);
510}
511
Note: See TracBrowser for help on using the repository browser.