File: log.h

package info (click to toggle)
mrd6 0.9.5-release-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,308 kB
  • ctags: 3,956
  • sloc: cpp: 25,728; perl: 462; makefile: 281; ansic: 142; sh: 67
file content (244 lines) | stat: -rw-r--r-- 5,824 bytes parent folder | download
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
/*
 * Multicast Routing Daemon (MRD)
 *   log.h
 *
 * Copyright (C) 2004, 2005
 *   Universidade de Aveiro, Instituto Telecomunicacoes - Polo Aveiro
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:	Hugo Santos <hsantos@av.it.pt>
 */

#ifndef _mrd_log_h_
#define _mrd_log_h_

#include <string>
#include <mrd/node.h>

class stream_flusher {
public:
	virtual ~stream_flusher();

	virtual void flushed(const char *buffer, bool newline) = 0;
};

struct el_def {};
extern el_def endl;

/*!
 * base log stream
 */
class base_stream {
public:
	base_stream();
	base_stream(stream_flusher *);
	~base_stream();

	/*! appends a new string chunk to the stream buffer */
	void append_chunk(const char *);
	void append_chunk(const char *, int);
	/*! requests a buffer part of minimal size n for direct writing.
	 * if there isn't enough space, returns NULL */
	char *req_buffer(int n);
	/*! after directly accessing the buffer, must commit changes
	 * of n characters */
	void commit_change(int n);
	/*! clears the stream buffer */
	void clear();
	/*! returns a null-terminated string pointer to the stream buffer */
	const char *str() const;

	/*! provides printf-like semantics to base_stream */
	base_stream &printf(const char *, ...);

	/*! method (hopefully better performant) that provides a very
	 * simple printf-like semantics to base_stream */
	base_stream &xprintf(const char *, ...);

	/*! increase indenting level */
	void inc_level();
	/*! decrease indenting level */
	void dec_level();

	/*! produce n spaces of output */
	void spaces(int n);

	/*!
	 * controls whether a stream flush distributes the buffer
	 * for logging or not
	 */
	void set_decision(bool);

	/*!
	 * flushes the stream buffer. if decision=true, all log_nodes
	 * are notified and prompted to log
	 */
	void flush();

	friend base_stream &operator << (base_stream &, const el_def &);

protected:
	void ident_start();
	void append_chunk(const char *, int, bool);
	char *req_buffer(int, bool);

	stream_flusher *fl;
	int level;
	bool dec;

	char buffer[256];
	int ptr;
};

base_stream &operator << (base_stream &, bool);
base_stream &operator << (base_stream &, int);
base_stream &operator << (base_stream &, uint32_t);
base_stream &operator << (base_stream &, uint64_t);
base_stream &operator << (base_stream &, const char *);
base_stream &operator << (base_stream &, const std::string &);
base_stream &operator << (base_stream &, const void *);
base_stream &operator << (base_stream &, double);

class log_base;

/*!
 * log nodes are notified by log_base whenever there is info to be
 * logged. i.e. after a base_stream::flush
 */
class log_node : public node {
public:
	log_node(log_base *, const char *name, int infolevel);

	bool check_startup();

	void set_level(const char *);
	bool set_property(const char *, const char *);

	static bool parse_infolevel(const char *, int &);

	/*! method called by log_base with logging info */
	virtual void log(int, int, const char *, bool newline) = 0;

protected:
	bool will_log(int, int) const;

	property_def *infolevel;

	friend class log_base;
};

/*! syslog based log_node */
class syslog_log_node : public log_node {
public:
	syslog_log_node(log_base *, const char *, int);
	~syslog_log_node();

	bool check_startup();
	void log(int, int, const char *, bool newline);
};

class tb_log_node : public log_node {
public:
	tb_log_node(log_base *, const char *name, int infolevel);

	const char *now_s();

	char _fbuf[64];
	time_t _lnow;
};

/*! file based log_node (also supports stderr) */
class file_log_node : public tb_log_node {
public:
	file_log_node(log_base *, const char *, int, const char *, bool flush);
	file_log_node(log_base *, const char *, int, FILE *);
	~file_log_node();

	bool check_startup();
	void log(int, int, const char *, bool newline);

	void event(int, void *);

	FILE *_fp;
	std::string _base_filename;
	property_def *_flush, *_detailed;
};

enum {
	QUIET = 1,
	NORMAL = 5,
	VERBOSE = 10,
	DEBUG = 15,
	EXTRADEBUG = 20,
	MESSAGE_ERR = 25,
	MESSAGE_SIG = 40,
	MESSAGE_CONTENT = 60,
	INTERNAL_FLOW = 250,
	LOG_ALL = 1000000
};

/*!
 * provides the architecture's logging interface
 */
class log_base : public node, public stream_flusher {
public:
	log_base(node *);
	~log_base();

	const char *description() const;

	bool check_startup();

	/*! starts a info-level logging context with specified level */
	base_stream &info(int level);
	base_stream &warn();
	base_stream &fatal();

	/*! returns true if would log at the specified level */
	bool would_log(int level) const;

	/*! simulates POSIX's perror using the internal logging mechanism */
	void perror(const char *);

	/*! attaches a new logging node to the architecture */
	bool attach_node(log_node *);
	void dettach_node(log_node *);

	bool call_method(int, base_stream &,
			 const std::vector<std::string> &);
	bool negate_method(int, base_stream &,
			   const std::vector<std::string> &);

	enum {
		ReloadEvent = 'R',
	};

	void reload_logs();

protected:
	void flushed(const char *, bool);

	void remove_child_node(node *);

	bool attach_node(const std::vector<std::string> &);

	int _current, _level;

	base_stream _base;
};

#endif