aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils/logger.h
blob: 52c21e6ef73e216a955dd28496d4cc0082cd643d (plain)
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
/**
 * @file logger.h
 *
 * @brief Interface of logger_t.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#ifndef LOGGER_H_
#define LOGGER_H_

#include <stdio.h>

#include <types.h>


typedef enum logger_level_t logger_level_t;

/**
 * @brief Log Levels supported by the logger object.
 * 
 * Logleves are devided in two different kinds:
 * - levels to specify the type of the log
 * - levels to specify the detail-level of the log
 * 
 * Use combinations of these to build detailed loglevels, such
 * as CONTROL|MORE fore a detailed cotrol level, or
 * use RAW to see all raw data dumps (except private).
 * 
 * @ingroup utils
 */
enum logger_level_t {
	/**
	 * Control flow.
	 */
	CONTROL = 1,
	/**
	 * Error reporting.
	 */
	ERROR = 2,
	/**
	 * Logs important for the sysadmin.
	 */
	AUDIT = 4,
	/**
	 * Raw data dumps.
	 */
	RAW = 8,
	/**
	 * Private data dumps.
	 */
	PRIVATE = 16,
	
	/**
	 * Log most important output, can be omitted.
	 */
	LEVEL0 = 0,
	/**
	 * Log more detailed output.
	 */
	LEVEL1 = 32, 
	/**
	 * Log even more detailed output.
	 */
	LEVEL2 = LEVEL1 + 64,
	/**
	 * Use maximum detailed output.
	 */
	LEVEL3 = LEVEL2 + 128,
	
	/**
	 * Summary for all types with all detail-levels.
	 */
	FULL = LEVEL3 + CONTROL + ERROR + RAW + PRIVATE + AUDIT
};

typedef struct logger_t logger_t;

/**
 * @brief Class to simplify logging.
 * 
 * @b Constructors:
 *  - logger_create()
 * 
 * @ingroup utils
 */
struct logger_t {

	/**
	 * @brief Log an entry, using printf()-like params.
	 *
	 * All specified loglevels must be activated that
	 * the log is done.
	 *
	 * @param this 		logger_t object
	 * @param loglevel 	or'ed set of logger_level_t's
	 * @param format 	printf like format string
	 * @param ... 		printf like parameters
	 */
	void (*log) (logger_t *this, logger_level_t log_level, char *format, ...);

	/**
	 * @brief Log some bytes, useful for debugging.
	 *
	 * All specified loglevels must be activated that
	 * the log is done.
	 *
	 * @param this 		logger_t object
	 * @param loglevel 	or'ed set of logger_level_t's
	 * @param label 	a labeling name, logged with the bytes
	 * @param bytes 	pointer to the bytes to dump
	 * @param len	 	number of bytes to dump
	 */
	void (*log_bytes) (logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len);

	/**
	 * @brief Log a chunk, useful for debugging.
	 *
	 * All specified loglevels must be activated that
	 * the log is done.
	 *
	 * @param this 		logger_t object
	 * @param loglevel 	or'ed set of logger_level_t's
	 * @param label 	a labeling name, logged with the bytes
	 * @param chunk		chunk to log
	 */
	void (*log_chunk) (logger_t *this, logger_level_t loglevel, char *label, chunk_t chunk);

	/**
	 * @brief Enables a loglevel for the current logger_t object.
	 *
	 * @param 			this logger_t object
	 * @param 			log_level loglevel to enable
	 */
	void (*enable_level) (logger_t *this, logger_level_t log_level);

	/**
	 * @brief Disables a loglevel for the current logger_t object.
	 *
	 * @param 			this logger_t object
	 * @param 			log_level loglevel to enable
	 */
	void (*disable_level) (logger_t *this, logger_level_t log_level);

	/**
	 * @brief Destroys a logger_t object.
	 *
	 * @param this		logger_t object
	 */
	void (*destroy) (logger_t *this);
};

/**
 * @brief Constructor to create a logger_t object.
 *
 * @param logger_name 	name for the logger_t object
 * @param log_level		or'ed set of log_levels to assign to the new logger_t object
 * @param log_thread_id	TRUE if thread id should also be logged
 * @param output		FILE * if log has to go on a file output, NULL for syslog
 * @return 				logger_t object 
 * 
 * @ingroup utils
 */
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_thread_id, FILE * output);


#endif /*LOGGER_H_*/