-
Notifications
You must be signed in to change notification settings - Fork 80
/
log_output_delegate.c
134 lines (112 loc) · 3.71 KB
/
log_output_delegate.c
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
/*
Serval DNA logging output to a delegate
Copyright 2017 Flinders University
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "log.h"
#include "log_output.h"
#include "log_output_delegate.h"
#include "strbuf.h"
/* An implementation of the Serval logging API that constructs log messages in
* a buffer then passes the buffer to a delegate.
*/
/* Private state for delegate log output.
*/
struct log_output_delegate_state {
bool_t opened;
// Buffer to hold log messages before the log file is open and ready for writing:
struct strbuf strbuf;
char buf[8192];
};
static struct log_output_delegate_state static_state = {
.opened = 0,
.strbuf = STRUCT_STRBUF_EMPTY,
.buf = ""
};
/* Functions for querying configuration.
*/
static int log_delegate_minimum_level(const struct log_output *UNUSED(out))
{
return serval_log_delegate.minimum_level;
}
static bool_t log_delegate_show_pid(const struct log_output *UNUSED(out))
{
return serval_log_delegate.show_pid;
}
static bool_t log_delegate_show_time(const struct log_output *UNUSED(out))
{
return serval_log_delegate.show_time;
}
/* Log output operations.
*/
static inline struct log_output_delegate_state *_state(struct log_output *out)
{
assert(out->state);
return (struct log_output_delegate_state *)(out->state);
}
static bool_t is_log_delegate_available(const struct log_output_iterator *UNUSED(it))
{
return serval_log_delegate.print != NULL;
}
static void log_delegate_open(struct log_output_iterator *it)
{
struct log_output_delegate_state *state = _state(*it->output);
if (serval_log_delegate.print && !state->opened) {
state->opened = 1;
if (serval_log_delegate.show_prolog)
serval_log_print_prolog(it);
}
}
static void log_delegate_start_line(struct log_output_iterator *it, int UNUSED(level))
{
struct log_output_delegate_state *state = _state(*it->output);
strbuf sb = &state->strbuf;
assert(strbuf_len(sb) == 0);
strbuf_init(sb, state->buf, sizeof state->buf);
it->xpf = XPRINTF_STRBUF(sb);
}
static void log_delegate_end_line(struct log_output_iterator *it, int level)
{
struct log_output_delegate_state *state = _state(*it->output);
strbuf sb = &state->strbuf;
serval_log_delegate.print(level, strbuf_str(sb), strbuf_overrun(sb));
strbuf_reset(sb);
}
static void flush_log_delegate(struct log_output_iterator *UNUSED(it))
{
if (serval_log_delegate.flush)
serval_log_delegate.flush();
}
static struct log_output static_log_output = {
.minimum_level = log_delegate_minimum_level,
.show_pid = log_delegate_show_pid,
.show_time = log_delegate_show_time,
.state = &static_state,
.open = log_delegate_open,
.capture_fd = NULL,
.is_available = is_log_delegate_available,
.start_line = log_delegate_start_line,
.end_line = log_delegate_end_line,
.flush = flush_log_delegate,
.close = NULL
};
DEFINE_LOG_OUTPUT(&static_log_output);
// These are defaults only; the delegate fills this in as it wishes.
struct log_delegate serval_log_delegate = {
.minimum_level = LOG_LEVEL_INFO,
.show_prolog = 1,
.show_pid = 0,
.show_time = 1,
.print = NULL,
.flush = NULL
};