Debian Codegraph @

c/include/proton/logger.h

raw
1 #ifndef LOGGER_H
2 #define LOGGER_H
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /**
23 * @file
24 * @copybrief logger
25 * @copydetails logger
26 *
27 * @defgroup logger Logger
28 * @ingroup core
29 */
30
31 #include <proton/import_export.h>
32 #include <proton/object.h>
33
34 #include <stdarg.h>
35 #include <stdint.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 * @addtogroup logger
43 * @{
44 *
45 * Facility for logging messages
46 *
47 * The proton library has internal logging which provides information about the
48 * functioning of the library. This Logger system (see @ref pn_logger_t) allows applications
49 * to customize how the logging is recorded and output.
50 *
51 * Each logged message has an attached level (see @ref pn_log_level_t) which
52 * indicates how important the message is; and also has an attached subsystem
53 * (see @ref pn_log_subsystem_t) which indicates which part of proton is producing#
54 * the log message. The levels go from "Critical" which indicates a condition the library cannot
55 * recover from.
56 *
57 * Applications receive log messages by registering a callback function (@ref pn_log_sink_t). This
58 * receives the logged message and other information and allows the application to consume the
59 * logged messages as it wants. Applications can filter the messages that they receive by setting
60 * bit masks in the logger. They can set the logger filtering both by subsystem and by level.
61 * Additionally, since the callback contains both the subsystem and level information for each
62 * logged message, applications can impose further, more complex, filters of their own.
63 *
64 * Each application will have a default logger which can be retrieved with @ref pn_default_logger.
65 * The default logger is used as the template for every other logger that is created. So an efficient
66 * way to configure logging is to configure the default logger at the very start of the application
67 * before any other loggers get created.
68 *
69 * Loggers are associated with different proton objects, primarily the transport object
70 * (@ref pn_transport_t) and each logger controls the logging for the associated object. This means
71 * that for example in order to control the logging for an AMQP connection you need to acquire the
72 * logger object from the transport object using @ref pn_transport_logger and to configure that.
73 *
74 * Initially the defaults are to log every subsystem but not any level (except 'Critical' which is
75 * always logged). This means in effect that it is only necessary to turn on the log levels that
76 * are interesting and all subsystems will log. Of course you can turn off subsystems that are not
77 * interesting or are too verbose.
78 *
79 * There is also a default log sink if the application does not register their own, but logging
80 * is turned on - this will output the log message to standard error.
81 */
82
83 /**
84 * The logger object allows library logging to be controlled
85 */
86 typedef struct pn_logger_t pn_logger_t;
87
88 /**
89 * Definitions for different subsystems that can log messages.
90 * Note that these are exclusive bits so that you can specify multiple
91 * subsystems to filter
92 */
93 typedef enum pn_log_subsystem_t {
94 PN_SUBSYSTEM_NONE = 0, /**< No subsystem */
95 PN_SUBSYSTEM_MEMORY = 1, /**< Memory usage */
96 PN_SUBSYSTEM_IO = 2, /**< Low level Input/Output */
97 PN_SUBSYSTEM_EVENT = 4, /**< Events */
98 PN_SUBSYSTEM_AMQP = 8, /**< AMQP protocol processing */
99 PN_SUBSYSTEM_SSL = 16, /**< TLS/SSL protocol processing */
100 PN_SUBSYSTEM_SASL = 32, /**< SASL protocol processing */
101 PN_SUBSYSTEM_BINDING = 64, /**< Language binding */
102 PN_SUBSYSTEM_ALL = 65535 /**< Every subsystem */
103 } pn_log_subsystem_t; /* We hint to the compiler it can use 16 bits for this value */
104
105 /**
106 * Definitions for different severities of log messages
107 * Note that these are exclusive bits so that you can specify multiple
108 * severities to filter
109 */
110 typedef enum pn_log_level_t {
111 PN_LEVEL_NONE = 0, /**< No level */
112 PN_LEVEL_CRITICAL = 1, /**< Something is wrong and can't be fixed - probably a library bug */
113 PN_LEVEL_ERROR = 2, /**< Something went wrong */
114 PN_LEVEL_WARNING = 4, /**< Something unusual happened but not necessarily an error */
115 PN_LEVEL_INFO = 8, /**< Something that might be interesting happened */
116 PN_LEVEL_DEBUG = 16, /**< Something you might want to know about happened */
117 PN_LEVEL_TRACE = 32, /**< Detail about something that happened */
118 PN_LEVEL_FRAME = 64, /**< Protocol frame traces */
119 PN_LEVEL_RAW = 128, /**< Raw protocol bytes */
120 PN_LEVEL_ALL = 65535 /**< Every possible level */
121 } pn_log_level_t; /* We hint to the compiler that it can use 16 bits for this value */
122
123 /**
124 * Callback for sinking logger messages.
125 */
126 typedef void (*pn_log_sink_t)(intptr_t sink_context, pn_log_subsystem_t subsystem, pn_log_level_t severity, const char *message);
127
128 /**
129 * Return the default library logger
130 *
131 * @return The global default logger
132 */
133 PN_EXTERN pn_logger_t *pn_default_logger(void);
134
135 /**
136 * Get a human readable name for a logger severity
137 *
138 * @param[in] level the logging level
139 * @return readable name for that level
140 */
141 PN_EXTERN const char *pn_logger_level_name(pn_log_level_t level);
142
143 /**
144 * Get a human readable name for a logger subsystem
145 *
146 * @param[in] subsystem
147 * @return readable name for that subsystem
148 */
149 PN_EXTERN const char *pn_logger_subsystem_name(pn_log_subsystem_t subsystem);
150
151 /**
152 * Set a logger's tracing flags.
153 *
154 * Set individual trace flags to control what a logger logs.
155 *
156 * The trace flags for a logger control what sort of information is
157 * logged. See @ref pn_log_level_t and @ref pn_log_subsystem_t for more details.
158 *
159 * Note that log messages with a level of @ref PN_LEVEL_CRITICAL will always be logged.
160 * Otherwise log message are only logged if the subsystem and level flags both match a
161 * flag in the masks held by the logger.
162 *
163 * If you don't want to affect the subsystem flags then you can set subsystem to
164 * PN_SUBSYSTEM_NONE. likewise level to PN_LEVEL_NONE if you don't want to
165 * affect the level flags.
166 *
167 * @param[in] logger the logger
168 * @param[in] subsystem bits representing subsystems to turn on trace for
169 * @param[in] level bits representing log levels to turn on trace for
170 */
171 PN_EXTERN void pn_logger_set_mask(pn_logger_t *logger, uint16_t subsystem, uint16_t level);
172
173 /**
174 * Clear a logger's tracing flags.
175 *
176 * Clear individual trace flags to control what a logger logs.
177 *
178 * The trace flags for a logger control what sort of information is
179 * logged. See @ref pn_log_level_t and @ref pn_log_subsystem_t for more details.
180 *
181 * Note that log messages with a level of @ref PN_LEVEL_CRITICAL will always be logged.
182 * Otherwise log message are only logged if the subsystem and level flags both match a
183 * flag in the masks held by the logger.
184 *
185 * If you don't want to affect the subsystem flags then you can set subsystem to
186 * PN_SUBSYSTEM_NONE. likewise level to PN_LEVEL_NONE if you don't want to
187 * affect the level flags.
188 *
189 * @param[in] logger the logger
190 * @param[in] subsystem bits representing subsystems to turn off trace for
191 * @param[in] level bits representing log levels to turn off trace for
192 */
193 PN_EXTERN void pn_logger_reset_mask(pn_logger_t *logger, uint16_t subsystem, uint16_t level);
194
195 /**
196 * Set the tracing function used by a logger.
197 *
198 * The tracing function is called to perform logging. Overriding this
199 * function allows embedding applications to divert the engine's
200 * logging to a place of their choice.
201 *
202 * @param[in] logger the logger
203 * @param[in] sink the tracing function
204 * @param[in] sink_context user specified context to pass into each call of the tracing callback
205 */
206 PN_EXTERN void pn_logger_set_log_sink(pn_logger_t *logger, pn_log_sink_t sink, intptr_t sink_context);
207
208 /**
209 * Get the tracing function used by a logger.
210 *
211 * @param[in] logger the logger
212 * @return the tracing sink function used by a logger
213 */
214 PN_EXTERN pn_log_sink_t pn_logger_get_log_sink(pn_logger_t *logger);
215
216 /**
217 * Get the sink context used by a logger.
218 *
219 * @param[in] logger the logger
220 * @return the sink context used by a logger
221 */
222 PN_EXTERN intptr_t pn_logger_get_log_sink_context(pn_logger_t *logger);
223
224 /**
225 * Log a printf formatted message using the logger
226 *
227 * This is mainly for use in proton internals , but will allow application log messages to
228 * be processed the same way.
229 *
230 * @param[in] logger the logger
231 * @param[in] subsystem the subsystem which is producing this log message
232 * @param[in] level the log level of the log message
233 * @param[in] fmt the printf formatted message to be logged
234 */
235 PN_EXTERN void pn_logger_logf(pn_logger_t *logger, pn_log_subsystem_t subsystem, pn_log_level_t level, const char *fmt, ...);
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241 /**
242 * @}
243 */
244
245 #endif