GLog.cpp
22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/***************************************************************************
* GLog.cpp - Information logger *
* ----------------------------------------------------------------------- *
* copyright (C) 2010 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/**
* @file GLog.cpp
* @brief Information logger class implementation
* @author Jurgen Knodlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h> // fopen, fgets, fclose, etc...
#include <string.h> // memcpy
#include <stdarg.h> // for "va_list" type
#include "GLog.hpp"
#include "GTools.hpp"
/* __ Method name definitions ____________________________________________ */
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
***************************************************************************/
GLog::GLog(void)
{
// Initialise private members for clean destruction
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Log file constructor
*
* @param[in] filename Name of log file.
* @param[in] clobber If true overwrite any existing file, otherwise append.
*
* Construct a logger with an open log file.
***************************************************************************/
GLog::GLog(const std::string& filename, bool clobber)
{
// Initialise private members for clean destruction
init_members();
// Open file
open(filename, clobber);
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] log Object from which the instance should be built.
***************************************************************************/
GLog::GLog(const GLog& log)
{
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(log);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GLog::~GLog(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] log Object which should be assigned.
***************************************************************************/
GLog& GLog::operator= (const GLog& log)
{
// Execute only if object is not identical
if (this != &log) {
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(log);
} // endif: object was not identical
// Return
return *this;
}
/**************************************************************************//**
* @brief Message logging operator
*
* @param[in] msgFormat Pointer to message format.
* @param[in] ... Message to put into log.
*
* This operator provides message logging capabilities in C style. It expects
* a C-style format string followed by an arbitrary number of arguments that
* is used to fill the format.
*
* @todo A static buffer of length 8192 is allocated. Although this is likely
* sufficient for any normal application there is nothing that prevents in
* principle the overflow of the buffer. A more sophisticated scheme should be
* implemented.
******************************************************************************/
void GLog::operator()(const char *msgFormat, ...)
{
// Allocate message buffer
char buffer[8192];
// Put message into buffer
va_list vl;
va_start(vl, msgFormat);
vsprintf(buffer, msgFormat, vl);
va_end(vl);
// Append message to string buffer
m_buffer.append(buffer);
m_buffer.append("\n");
// Flush buffer
flush(true);
// Return
return;
}
/***********************************************************************//**
* @brief Insert logger into logger
*
* @param[in] log Logger to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const GLog& log)
{
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert C++ string into logger
*
* @param[in] str C++ string to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const std::string& str)
{
// Add string to buffer
m_buffer.append(str);
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert C string into logger
*
* @param[in] str C string to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const char* str)
{
// Add string to buffer
m_buffer.append(str);
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert character value into logger
*
* @param[in] value Character value to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const char& value)
{
// Allocate output stream
std::ostringstream oss;
// Put value into stream
oss << value;
// Append stream to buffer
m_buffer.append(oss.str());
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert unsigned character value into logger
*
* @param[in] value Unsigned character value to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const unsigned char& value)
{
// Allocate output stream
std::ostringstream oss;
// Put value into stream
oss << value;
// Append stream to buffer
m_buffer.append(oss.str());
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert bool into logger
*
* @param[in] value Boolean to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const bool& value)
{
// Add boolean to buffer
if (value)
m_buffer.append("true");
else
m_buffer.append("false");
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert integer into logger
*
* @param[in] value Integer to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const int& value)
{
// Allocate output stream
std::ostringstream oss;
// Put value into stream
oss << value;
// Append stream to buffer
m_buffer.append(oss.str());
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert unsigned integer into logger
*
* @param[in] value Unsigned integer to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const unsigned int& value)
{
// Allocate output stream
std::ostringstream oss;
// Put value into stream
oss << value;
// Append stream to buffer
m_buffer.append(oss.str());
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Insert double precision value into logger
*
* @param[in] value Double precision value to be inserted.
***************************************************************************/
GLog& GLog::operator<<(const double& value)
{
// Allocate output stream
std::ostringstream oss;
// Put value into stream
oss << value;
// Append stream to buffer
m_buffer.append(oss.str());
// Flush buffer
flush();
// Return logger
return *this;
}
/***********************************************************************//**
* @brief Ostream function output operator
*
* @param[in] fn Ostream function.
*
* The function output operator is interpreted as a line feed operator.
*
* @todo This is a quick an dirty implementation of the std::endl operator.
* A clean GLog::endl operator should be implemented instead, yet this needs
* some deeper understanding of our the
***************************************************************************/
GLog& GLog::operator<<(std::ostream& (*fn)(std::ostream&))
{
// Append CR to buffer
m_buffer.append("\n");
// Flush buffer
flush(true);
// Return logger
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Clear object
***************************************************************************/
void GLog::clear(void)
{
// Free members
free_members();
// Init members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Return the number of characters that are actually in the buffer
***************************************************************************/
int GLog::size(void) const
{
// Return
return m_buffer.size();
}
/***********************************************************************//**
* @brief Open log file
*
* @param[in] filename Name of log file.
* @param[in] clobber If true overwrite any existing file, otherwise append.
*
* Opens a file for logging. If a log file was already open it is closed
* before opening a new file.
***************************************************************************/
void GLog::open(const std::string& filename, bool clobber)
{
// Close any existing file
close();
// Open file
if (clobber)
m_file = fopen(filename.c_str(), "w");
else
m_file = fopen(filename.c_str(), "a");
// Return
return;
}
/***********************************************************************//**
* @brief Close log file
*
* Close log file. This method even works if no file has been opened. In any
* case flushing of the buffer is enforced.
***************************************************************************/
void GLog::close(void)
{
// Flush buffer
flush(true);
// Close any open file
if (m_file != NULL) {
fclose(m_file);
m_file = NULL;
}
// Return
return;
}
/***********************************************************************//**
* @brief Set data flag that controls date prefixing
*
* @param[in] flag Enable/disable date flag (true/false).
***************************************************************************/
void GLog::date(bool flag)
{
// Set flag
m_use_date = flag;
// Return
return;
}
/***********************************************************************//**
* @brief Enables/disables logging into standard output stream
*
* @param[in] flag Enable/disable logging (true/false).
*
* Enables or disables logging into the standard output stream.
***************************************************************************/
void GLog::cout(bool flag)
{
// Set flag
m_stdout = flag;
// Return
return;
}
/***********************************************************************//**
* @brief Enables/disables logging into standard error stream
*
* @param[in] flag Enable/disable logging (true/false).
*
* Enables or disables logging into the standard error stream.
***************************************************************************/
void GLog::cerr(bool flag)
{
// Set flag
m_stderr = flag;
// Return
return;
}
/***********************************************************************//**
* @brief Set name to put into prefix
*
* @param[in] name Name to put into prefix.
***************************************************************************/
void GLog::name(const std::string& name)
{
// Set name
m_name = name;
// Return
return;
}
/***********************************************************************//**
* @brief Set the maximum buffer size
*
* @param[in] size Maximum buffer size.
*
* Set the maximum number of characters allowed in the internal buffer before
* the buffer is flushed into the specified streams or file.
*
* @todo No maximum buffer size checking is performed. This could be
* implemented but is not really crucial since the physical buffer size can
* indeed be larger than this maximum size.
***************************************************************************/
void GLog::max_size(int size)
{
// Set size
m_max_length = size;
// Return
return;
}
/***********************************************************************//**
* @brief Set indentation
*
* @param[in] indent Indentation in number of characters.
*
* Set the indentation of the logger. This specifies the number of whitespace
* characters that is added between the prefix and the log message of each
* line. Indentation allows for easy formatting of text in the log file.
***************************************************************************/
void GLog::indent(int indent)
{
// Set size
m_indent = indent;
// Return
return;
}
/*==========================================================================
= =
= Private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GLog::init_members(void)
{
// Initialise members
m_max_length = 8192;
m_indent = 0;
m_stdout = false;
m_stderr = false;
m_use_date = false;
m_newline = true;
m_file = NULL;
m_name.clear();
m_buffer.clear();
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] log Object from which members which should be copied.
***************************************************************************/
void GLog::copy_members(const GLog& log)
{
// Copy attributes
m_max_length = log.m_max_length;
m_indent = log.m_indent;
m_stdout = log.m_stdout;
m_stderr = log.m_stderr;
m_use_date = log.m_use_date;
m_newline = log.m_newline;
m_name = log.m_name;
m_buffer = log.m_buffer;
m_file = log.m_file;
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
*
* Deletes class member after flushing the buffer and closing the log file.
***************************************************************************/
void GLog::free_members(void)
{
// Flush buffer
flush(true);
// Close log file
close();
// Return
return;
}
/***********************************************************************//**
* @brief Flush string buffer into log file
*
* @param[in] force If true, force flushing.
*
* Flush string buffer if it is full or if flushing is enforced. This method
* writes the output string in the relevant streams. It decomposes the
* buffer in lines that a separated by a '\n' character and preprends, if
* requested, the current date and name. The following streams are currently
* implemented (and will be filled in parallel): stdout, stderr, and an ASCII
* file.
***************************************************************************/
void GLog::flush(bool force)
{
// Check if buffer should be flushed
bool flush = (force || size() > m_max_length);
// Flush buffer
if (flush) {
// Flush buffer until it is empty
while (m_buffer.size() > 0) {
// Initialise newline for next line
bool newline = m_newline;
// Find next CR
std::string line;
size_t pos = m_buffer.find_first_of("\n", 0);
if (pos == std::string::npos) {
line = m_buffer;
newline = false;
m_buffer.clear();
}
else {
line = m_buffer.substr(0, pos) + "\n";
m_buffer = m_buffer.substr(pos+1, m_buffer.size()-pos);
newline = true;
}
// Prepend prefix if we are at the beginning of a new line
if (m_newline) {
std::string prefix = "";
if (m_use_date)
prefix.append(strdate());
if (m_name.length() > 0)
prefix.append(" "+m_name);
if (prefix.length() > 0)
prefix.append(": ");
line = prefix + fill(" ", m_indent) + line;
}
// Put line into requested streams
if (m_stdout)
std::cout << line;
if (m_stderr)
std::cerr << line;
if (m_file != NULL)
fprintf(m_file, line.c_str());
// Set newline flag for next line
m_newline = newline;
} // endwhile: flush until empty
} // endif: flush was required
// Return
return;
}
/***********************************************************************//**
* @brief Write string as header into logger
*
* @param[in] arg Header string.
* @param[in] level Header level (0,1,...).
*
* Writes a string as header into the logger. Various header levels exist:
* 0: 80 character wide centred header
* 1: Header framed by '=' characters
* 2: Header framed by '-' characters
***************************************************************************/
void GLog::header(const std::string& arg, int level)
{
// Declare frame and text strings
std::string frame;
std::string text;
// Create level dependent header strings
switch (level) {
case 0:
break;
case 1:
case 2:
text = "| " + arg + " |";
frame = (level == 1) ? "+" + fill("=", text.length()-2) + "+"
: "+" + fill("-", text.length()-2) + "+";
break;
default:
break;
}
// Write header into logger
if (text.length() > 0) {
m_buffer.append(frame);
m_buffer.append("\n");
m_buffer.append(text);
m_buffer.append("\n");
m_buffer.append(frame);
m_buffer.append("\n");
flush(true);
}
// Return
return;
}
/***********************************************************************//**
* @brief Return current date
*
* Returns the current date as string in the format yyyy-mm-ddThh:mm:ss.
***************************************************************************/
std::string GLog::strdate(void)
{
// Allocate variables
struct tm timeStruct;
time_t now;
char buffer[100];
// Get time
now = time(NULL);
#ifdef HAVE_GMTIME_R
gmtime_r(&now, &timeStruct);
#else
memcpy(&timeStruct, gmtime(&now), sizeof(struct tm));
#endif
// Write message type, time and task name to buffer
sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
timeStruct.tm_year + 1900,
timeStruct.tm_mon + 1,
timeStruct.tm_mday,
timeStruct.tm_hour,
timeStruct.tm_min,
timeStruct.tm_sec);
// Build string from buffer
std::string date = buffer;
// Return date
return date;
}
/*==========================================================================
= =
= Friends =
= =
==========================================================================*/