forked from dronekit/dronekit-la
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathla-log.cpp
96 lines (82 loc) · 1.86 KB
/
la-log.cpp
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
#include "la-log.h"
LALog lalog;
#include <stdlib.h> // for abort();
// globa functions for convenience
void la_log_syslog_open()
{
lalog.syslog_open();
}
void la_log_unsuppress()
{
lalog.unsupress();
}
void la_log(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
lalog.log_ap(priority, format, ap);
va_end(ap);
}
#ifdef _WIN32
void LALog::syslog_open()
{
::fprintf(stderr, "syslog not available");
abort();
}
#else
void LALog::syslog_open()
{
openlog("dl", LOG_NDELAY, LOG_LOCAL1);
use_syslog = true;
}
#endif
void LALog::log(const int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_ap(priority, format, ap);
va_end(ap);
}
bool LALog::should_suppress()
{
return _message_count_this_time_period >= _max_messages_per_time_period;
}
void LALog::unsupress()
{
_suppressing = false;
_message_count_this_time_period = 0;
_time_period_start = time(NULL);
if (_suppressed_message_count) {
// hmmm. recursion. hmmm.
log(LOG_ERR, "%d messages supressed", _suppressed_message_count);
_message_count_this_time_period--; // *cough* let one
// *message through
// *apart from us....
_suppressed_message_count = 0;
}
}
void LALog::log_ap(int priority, const char *format, va_list ap)
{
if (time(NULL) - _time_period_start > _time_period) {
unsupress();
}
if (should_suppress()) {
_suppressing = true;
}
if (suppressing()) {
_suppressed_message_count++;
return;
}
_message_count_this_time_period++;
if (use_syslog) {
#ifdef _WIN32
vfprintf(stderr, format, ap);
fprintf(stderr, "%d %s", priority, "\n");
#else
vsyslog(priority, format, ap);
#endif
} else {
vfprintf(stderr, format, ap);
fprintf(stderr, "%s", "\n");
}
}