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
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
/* Copyright (c) 2008-2017 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de> */
/* Copyright (c) 2014-2022 Ulrich Sibiller <uli42@gmx.de> */
/* Copyright (c) 2014-2019 Mihai Moldovan <ionic@ionic.de> */
/* Copyright (c) 2011-2022 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
/* */
/* NXCOMP, NX protocol compression and NX extensions to this software */
/* are copyright of the aforementioned persons and companies. */
/* */
/* Redistribution and use of the present software is allowed according */
/* to terms specified in the file LICENSE.nxcomp which comes in the */
/* source distribution. */
/* */
/* All rights reserved. */
/* */
/* NOTE: This software has received contributions from various other */
/* contributors, only the core maintainers and supporters are listed as */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well. */
/* */
/**************************************************************************/
#ifndef NXLog_H
#define NXLog_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/time.h>
#include <map>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <stack>
/** Log severity level */
enum NXLogLevel
{
NXFATAL,
NXERROR,
NXWARNING,
NXINFO,
NXDEBUG,
NXLOG_LEVEL_COUNT
};
/**
* Log timestamp class
*
* Stores the timestamp, file, function, line number and log level.
* Acts as a manipulator on the NXLog class, telling it a new log
* severity level. For instance:
*
* nx_log << NXLogStamp(...,NXINFO)
*
* Tells nx_log that now NXINFO type messages are being logged. This
* will be applied until a new NXLogStamp with a different level
* is sent to the NXLog.
*/
class NXLogStamp
{
private:
std::string file_;
std::string function_;
size_t line_;
NXLogLevel level_;
struct timeval timestamp_;
public:
/** File where the event occurred */
std::string file() const
{
return file_;
}
/** Function where the event occurred */
std::string function() const
{
return function_;
}
/** Line where the event occurred */
size_t line() const
{
return line_;
}
/** Severity level of the event */
NXLogLevel level() const
{
return level_;
}
/** Time of the event */
struct timeval timestamp() const
{
return timestamp_;
}
NXLogStamp(NXLogLevel _level, const char *_file = "", const char *_function = "", size_t _line = 0) : file_(_file), function_(_function), line_(_line), level_(_level)
{
gettimeofday(×tamp_, NULL);
}
};
/**
* Log class
*
* Logs events to a stream, filters by file/level
*/
class NXLog
{
#ifdef INTERNAL_LOGGING_TEST
protected:
#endif
NXLogLevel level_;
std::ostream *stream_;
std::map< std::string, NXLogLevel > per_file_levels_;
bool synchronized_;
size_t thread_buffer_size_;
pthread_mutex_t output_lock_;
pthread_key_t tls_key_;
bool log_level_;
bool log_time_;
bool log_unix_time_;
bool log_location_;
bool log_thread_id_;
typedef struct per_thread_data_s
{
NXLogLevel current_level;
std::string* current_file;
std::string* thread_name;
std::stack<std::stringstream*> buffer;
NXLog* log_obj;
} per_thread_data;
static void free_thread_data(void* arg)
{
per_thread_data *pdt = (per_thread_data*)arg;
if ( !pdt )
return;
if ( pdt->log_obj ) {
// Ensure the buffer is flushed before thread deletion
pdt->log_obj->flush(pdt);
}
delete pdt->current_file;
delete pdt->thread_name;
while (!pdt->buffer.empty()) {
/*
* get the stringstream object created in new_stack_entry()
* from the stack and delete it after pop()
*/
std::stringstream* tmp = pdt->buffer.top();
(void) pdt->buffer.pop ();
delete tmp;
}
delete pdt;
}
per_thread_data* get_data_int() const
{
per_thread_data *ret = NULL;
if ( (ret = (per_thread_data*)pthread_getspecific(tls_key_)) == NULL )
{
ret = new per_thread_data;
ret->current_level = NXDEBUG;
ret->current_file = new std::string();
ret->thread_name = new std::string();
ret->log_obj = const_cast<NXLog*>(this);
pthread_setspecific(tls_key_, ret);
}
return ret;
}
per_thread_data* get_data()
{
return get_data_int();
}
const per_thread_data* get_data() const
{
return get_data_int();
}
/** Convert NXLogStamp to string according to the current configuration */
std::string stamp_to_string(const NXLogStamp& stamp) const;
void new_stack_entry()
{
per_thread_data *pdt = get_data();
pdt->buffer.push(new std::stringstream());
}
/**
* Internal flush function
*
* When a thread is being terminated and free_thread_data gets called,
* the TLS key gets set to NULL before the call to free_thread_data,
* and the destructor function gets the old value.
*
* This means that get_data() stops working correctly, and we need
* to be able to pass the old pointer.
*/
virtual /* Note: this function needs to be virtual for the logging test application. Don't remove. */
void flush(per_thread_data *pdt)
{
/*
* Block all signals until we are dong printing data.
* Ensures that a signal handler won't interrupt us
* and overwrite the buffer data mid-print, leading
* to confusing output.
*/
sigset_t orig_signal_mask,
tmp_signal_mask;
sigemptyset(&orig_signal_mask);
/* Set up new mask to block all signals. */
sigfillset(&tmp_signal_mask);
/* Block all signals. */
pthread_sigmask(SIG_BLOCK, &tmp_signal_mask, &orig_signal_mask);
if (!pdt->buffer.empty ()) {
/*
* get the stringstream object created in new_stack_entry()
* from the stack and delete it after pop()
*/
std::stringstream *tmp = pdt->buffer.top();
const std::string str = tmp->str();
if (!str.empty())
{
pthread_mutex_lock(&output_lock_);
(*stream()) << str;
pthread_mutex_unlock(&output_lock_);
}
/* Remove from stack. */
pdt->buffer.pop();
/* free memory */
delete tmp;
}
/* Restore old signal mask. */
pthread_sigmask(SIG_SETMASK, &orig_signal_mask, NULL);
}
public:
NXLog() : level_(NXWARNING), stream_(&std::cerr), synchronized_(true), thread_buffer_size_(1024),
log_level_(false), log_time_(false), log_unix_time_(false), log_location_(false), log_thread_id_(false)
{
if ( pthread_key_create(&tls_key_, free_thread_data) != 0 )
{
std::cerr << "pthread_key_create failed" << std::endl;
abort();
}
}
~NXLog()
{
per_thread_data *pdt = get_data();
// Flush any remaining output and delete TLS data
free_thread_data(pdt);
pthread_key_delete(tls_key_);
if ((stream_) && (stream_ != &std::cerr)) {
delete stream_;
}
}
/** Minimum severity level to output */
NXLogLevel level() const
{
return level_;
}
void level(NXLogLevel _level)
{
level_ = _level;
}
/** Current severity level */
NXLogLevel current_level() const
{
return get_data()->current_level;
}
void current_level(NXLogLevel _level)
{
get_data()->current_level = _level;
}
/** Source file from which messages are currently originating */
std::string current_file() const
{
return *get_data()->current_file;
}
void current_file(std::string val)
{
*get_data()->current_file = val;
}
std::ostream* stream() const
{
return stream_;
}
void stream(std::ostream *_stream)
{
flush();
stream_ = _stream;
}
bool synchronized() const {
return synchronized_;
}
void synchronized(bool val) {
synchronized_ = val;
}
bool log_level() const
{
return log_level_;
}
void log_level(bool val)
{
log_level_ = val;
}
bool log_time() const
{
return log_time_;
}
void log_time(bool val)
{
log_time_ = val;
}
bool log_unix_time() const
{
return log_unix_time_;
}
void log_unix_time(bool val)
{
log_unix_time_ = val;
}
bool log_location() const
{
return log_location_;
}
void log_location(bool val)
{
log_location_ = val;
}
bool log_thread_id() const
{
return log_thread_id_;
}
void log_thread_id(bool val)
{
log_thread_id_ = val;
}
void flush()
{
per_thread_data *pdt = get_data();
flush(pdt);
}
std::string thread_name() const
{
return *get_data()->thread_name;
}
void thread_name(std::string str)
{
*get_data()->thread_name = str;
}
void thread_name(const char *str)
{
*get_data()->thread_name = str;
}
/**
* True if a message sent to the NXLog object will be sent to the output
*
* This considers two things:
*
* If there's a per-file log level, then it is used
* Otherwise the global log level is used.
*
* If the log level permits the current message to be output, then the
* return value is true.
*/
bool will_log() const;
bool has_buffer() const
{
return (!(get_data()->buffer.empty ()));
}
/**
* This catches std::flush
*/
NXLog& operator<<(std::ostream& (*F)(std::ostream&))
{
if ( will_log() )
{
if ( synchronized() )
{
/* Verbosely discard data if we don't have a buffer. */
if (!(has_buffer()))
{
std::cerr << "WARNING: no buffer available! "
<< "Internal state error!\n" << "Log hunk will be discarded!" << std::endl;
}
else
{
per_thread_data *pdt = get_data();
assert (!pdt->buffer.empty ());
(*pdt->buffer.top()) << F;
flush();
}
}
else
{
*(stream()) << F;
}
}
return *this;
}
template<typename T>
friend NXLog& operator<<(NXLog& out, const T& value);
friend NXLog& operator<< (NXLog& out, const NXLogStamp& value);
};
extern NXLog nx_log;
#define nxstamp(l) NXLogStamp(l, __FILE__, __func__, __LINE__)
#define nxstamp_append(l) NXLogStamp(l)
#define nxdbg nx_log << nxstamp(NXDEBUG)
#define nxinfo nx_log << nxstamp(NXINFO)
#define nxwarn nx_log << nxstamp(NXWARNING)
#define nxerr nx_log << nxstamp(NXERROR)
#define nxfatal nx_log << nxstamp(NXFATAL)
/* Append data to already existing (i.e., same-level) line. */
#define nxdbg_append nx_log << nxstamp_append(NXDEBUG)
#define nxinfo_append nx_log << nxstamp_append(NXINFO)
#define nxwarn_append nx_log << nxstamp_append(NXWARNING)
#define nxerr_append nx_log << nxstamp_append(NXERROR)
#define nxfatal_append nx_log << nxstamp_append(NXFATAL)
NXLog& operator<< (NXLog& out, const NXLogStamp& value);
template <typename T>
bool has_newline(T &value)
{
return false;
}
template <char*>
static bool has_newline(char *value)
{
if (value)
{
return strstr(value, "\n") != NULL;
}
else
{
return false;
}
}
template <char>
static bool has_newline(char value)
{
return value == '\n';
}
template <std::string&>
static bool has_newline(std::string &value)
{
return value.find_first_of("\n") != std::string::npos;
}
static size_t ss_length(std::stringstream *ss)
{
size_t pos = ss->tellg();
size_t ret = 0;
ss->seekg(0, std::ios::end);
ret = ss->tellg();
ss->seekg(pos, std::ios::beg);
return ret;
}
template <typename T>
NXLog& operator<<(NXLog& out, const T& value)
{
if ( out.will_log() )
{
if ( out.synchronized() )
{
/* Verbosely discard data if we don't have a buffer. */
if (!(out.has_buffer()))
{
std::cerr << "WARNING: no buffer available! "
<< "Internal state error!\n" << "Log hunk will be discarded!" << std::endl;
}
else
{
// In synchronized mode, we buffer data until a newline, std::flush, or the buffer
// gets full. Then we dump the whole thing at once to the output stream, synchronizing
// with a mutex.
NXLog::per_thread_data *pdt = out.get_data();
assert (!pdt->buffer.empty ());
(*pdt->buffer.top()) << value;
if ( ss_length(pdt->buffer.top()) >= out.thread_buffer_size_ || has_newline(value) )
out.flush();
}
}
else
{
// In async mode we just dump data on the output stream as-is.
// Multithreaded code will have ugly output.
*(out.stream()) << value;
}
}
return out;
}
#endif
|