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

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

Added Clear method to BSMemLoggerBase

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 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 *@@ 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 */
357
358BSFileLogger::BSFileLogger(ULONG ulDummy,
359 const char *pcszFilename)
360{
361 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
362
363 _Pmpf(("BSFileLogger: constructor"));
364
365 _File = NULL;
366 _strFileName = pcszFilename;
367
368 _indent = 0;
369
370 if (pcszFilename)
371 {
372 _File = fopen(pcszFilename,
373 "a");
374 if (!_File)
375 {
376 CHAR szError[400];
377 sprintf(szError,
378 "Cannot open log file \"%s\" for writing.",
379 pcszFilename);
380 throw BSLoggerExcpt(szError);
381 }
382 }
383 else
384 throw BSLoggerExcpt("pcszFilename is NULL.");
385
386 DATETIME dt;
387 DosGetDateTime(&dt);
388
389 fprintf(_File,
390 "\n\nWarpIN " BLDLEVEL_VERSION " install log opened\n"
391 "Date: %04d-%02d-%02d, Time: %02d:%02d:%02d\n"
392 "--------------------------------\n",
393 dt.year, dt.month, dt.day,
394 dt.hours, dt.minutes, dt.seconds);
395}
396
397/*
398 *@@ ~BSFileLogger:
399 * destructor. Closes the log file.
400 *
401 *@@added V0.9.9 (2001-03-30) [umoeller]
402 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
403 */
404
405BSFileLogger::~BSFileLogger()
406{
407 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
408
409 _Pmpf(("BSFileLogger: destructor"));
410
411 if (_File)
412 {
413 fprintf(_File, "\nInstall log closed.\n");
414 fclose(_File);
415 _File = NULL;
416 }
417}
418
419/*
420 *@@ IncIndent:
421 * modifies indentation for the file output
422 * (spaces between date/time and the actual
423 * logger output).
424 *
425 * A positive "i" increases the spacing, a
426 * negative decreases it.
427 *
428 *@@added V0.9.9 (2001-03-30) [umoeller]
429 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
430 */
431
432void BSFileLogger::IncIndent(int i)
433{
434 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
435
436 _indent += i;
437
438 if (i < 0)
439 i = 0;
440}
441
442/*
443 *@@ Write:
444 *
445 * NOTE: This does not append \n for each string.
446 *
447 *@@added V0.9.9 (2001-03-30) [umoeller]
448 *@@changed V0.9.12 (2001-05-31) [umoeller]: added mutex
449 *@@changed V0.9.14 (2001-07-26) [umoeller]: added hundredths to log lines
450 *@@changed V0.9.18 (2002-03-08) [umoeller]: renamed from Append(); now adding \n after each line
451 */
452
453void BSFileLogger::WriteV(const char *pcszFormat, // in: format string
454 va_list arg_ptr)
455{
456 BSLock lock(G_mtxFileLoggers); // V0.9.20 (2002-07-06) [umoeller]
457
458 _Pmpf(("BSFileLogger::Append: got \"%s\"", pcszFormat));
459
460 DATETIME dt;
461 DosGetDateTime(&dt);
462
463 fprintf(_File,
464 "%02d:%02d:%02d.%02d ",
465 dt.hours, dt.minutes, dt.seconds, dt.hundredths);
466
467 if (_indent)
468 {
469 for (int i = 0;
470 i < _indent;
471 i++)
472 fprintf(_File, " ");
473 }
474
475 int iWritten = vfprintf(_File, pcszFormat, arg_ptr);
476 fprintf(_File, "\n");
477}
478
479/*
480 *@@ Append:
481 *
482 *@@added V0.9.18 (2002-03-08) [umoeller]
483 */
484
485void BSFileLogger::Write(const char *pcszFormat, // in: format string
486 ...) // in: variable arguments
487{
488 va_list arg_ptr;
489 va_start(arg_ptr, pcszFormat);
490 WriteV(pcszFormat, arg_ptr);
491 va_end(arg_ptr);
492}
493
Note: See TracBrowser for help on using the repository browser.