Asterisk - The Open Source Telephony Project GIT-master-70eff7f
Loading...
Searching...
No Matches
include/asterisk/logger.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 *
22 * \brief Support for logging to various files, console and syslog
23 * Configuration in file logger.conf
24 */
25
26#ifndef _ASTERISK_LOGGER_H
27#define _ASTERISK_LOGGER_H
28
29#include "asterisk/options.h" /* need option_debug */
30
31#if defined(__cplusplus) || defined(c_plusplus)
32extern "C" {
33#endif
34
35#define EVENTLOG "event_log"
36#define QUEUELOG "queue_log"
37
38#define DEBUG_M(a) { \
39 a; \
40}
41
42#define _A_ __FILE__, __LINE__, __FUNCTION__
43
44#define VERBOSE_PREFIX_1 " "
45#define VERBOSE_PREFIX_2 " == "
46#define VERBOSE_PREFIX_3 " -- "
47#define VERBOSE_PREFIX_4 " > "
48#define VERBOSE_PREFIX_5 " > "
49#define VERBOSE_PREFIX_6 " > "
50#define VERBOSE_PREFIX_7 " > "
51#define VERBOSE_PREFIX_8 " > "
52#define VERBOSE_PREFIX_9 " > "
53#define VERBOSE_PREFIX_10 " > "
54
55#define AST_CALLID_BUFFER_LENGTH 13
56
58 AST_LOGGER_SUCCESS = 0, /*!< Log channel was created or deleted successfully */
59 AST_LOGGER_FAILURE = 1, /*!< Log channel already exists for create or doesn't exist for deletion of log channel */
60 AST_LOGGER_DECLINE = -1, /*!< Log channel request was not accepted */
61 AST_LOGGER_ALLOC_ERROR = -2 /*!< filename allocation error */
62};
63
64/*! \brief Used for sending a log message
65 This is the standard logger function. Probably the only way you will invoke it would be something like this:
66 ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain. We should get some more. Will %d be enough?\n", "flux capacitor", 10);
67 where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending
68 on which log you wish to output to. These are implemented as macros, that
69 will provide the function with the needed arguments.
70
71 \param level Type of log event
72 \param file Will be provided by the AST_LOG_* macro
73 \param line Will be provided by the AST_LOG_* macro
74 \param function Will be provided by the AST_LOG_* macro
75 \param fmt This is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)
76 */
77
78void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
79 __attribute__((format(printf, 5, 6)));
80
81void ast_log_ap(int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
82 __attribute__((format(printf, 5, 0)));
83
84/*!
85 * \brief Used for sending a log message with protection against recursion.
86 *
87 * \note This function should be used by all error messages that might be directly
88 * or indirectly caused by logging.
89 *
90 * \see ast_log for documentation on the parameters.
91 */
92void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt, ...)
93 __attribute__((format(printf, 5, 6)));
94
95/* XXX needs documentation */
96typedef unsigned int ast_callid;
97
98/*! \brief Used for sending a log message with a known call_id
99 This is a modified logger function which is functionally identical to the above logger function,
100 it just include a call_id argument as well. If NULL is specified here, no attempt will be made to
101 join the log message with a call_id.
102
103 \param level Type of log event
104 \param file Will be provided by the AST_LOG_* macro
105 \param line Will be provided by the AST_LOG_* macro
106 \param function Will be provided by the AST_LOG_* macro
107 \param callid This is the ast_callid that is associated with the log message. May be NULL.
108 \param fmt This is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)
109*/
110void ast_log_callid(int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt, ...)
111 __attribute__((format(printf, 6, 7)));
112
113/*!
114 * \brief Retrieve the existing log channels
115 * \param logentry A callback to an updater function
116 * \param data Data passed into the callback for manipulation
117 *
118 * For each of the logging channels, logentry will be executed with the
119 * channel file name, log type, status of the log, and configuration levels.
120 *
121 * \retval 0 on success
122 * \retval 1 on failure
123 * \retval -2 on allocation error
124 */
125int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
126 const char *status, const char *configuration, void *data), void *data);
127
128/*!
129 * \brief Create a log channel
130 *
131 * \param log_channel Log channel to create
132 * \param components Logging config levels to add to the log channel
133 */
134int ast_logger_create_channel(const char *log_channel, const char *components);
135
136/*!
137 * \brief Delete the specified log channel
138 *
139 * \param log_channel The log channel to delete
140 */
141int ast_logger_remove_channel(const char *log_channel);
142
143/*!
144 * \brief Log a backtrace of the current thread's execution stack to the Asterisk log
145 */
146void ast_log_backtrace(void);
147
148/*! \brief Reload logger while rotating log files */
149int ast_logger_rotate(void);
150
151/*!
152 * \brief Rotate the specified log channel.
153 *
154 * \param log_channel The log channel to rotate
155 */
156int ast_logger_rotate_channel(const char *log_channel);
157
158void __attribute__((format(printf, 5, 6))) ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...);
159
160/*!
161 * \brief Send a verbose message (based on verbose level)
162 *
163 * \details This works like ast_log, but prints verbose messages to the console depending on verbosity level set.
164 *
165 * \code
166 * ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
167 * \endcode
168 *
169 * This will print the message to the console if the verbose level is set to a level >= 3
170 *
171 * Note the absence of a comma after the VERBOSE_PREFIX_3. This is important.
172 * VERBOSE_PREFIX_1 through VERBOSE_PREFIX_10 are defined.
173 *
174 * \version 11 added level parameter
175 */
176void __attribute__((format(printf, 5, 6))) __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
177
178/*!
179 * \brief Send a verbose message (based on verbose level) with deliberately specified callid
180 *
181 * \details just like __ast_verbose, only __ast_verbose_callid allows you to specify which callid is being used
182 * for the log without needing to bind it to a thread. NULL is a valid argument for this function and will
183 * allow you to specify that a log will never display a call id even when there is a call id bound to the
184 * thread.
185 */
186void __attribute__((format(printf, 6, 7))) __ast_verbose_callid(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, ...);
187
188#define ast_verbose(...) __ast_verbose(_A_, -1, __VA_ARGS__)
189#define ast_verbose_callid(callid, ...) __ast_verbose_callid(_A_, -1, callid, __VA_ARGS__)
190
191void __attribute__((format(printf, 6, 0))) __ast_verbose_ap(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap);
192
193void __attribute__((format(printf, 2, 3))) ast_child_verbose(int level, const char *fmt, ...);
194
195/*
196 * These gymnastics are due to platforms which designate char as unsigned by
197 * default. Level is the negative character -- offset by 1, because \0 is
198 * the string terminator.
199 */
200#define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
201#define VERBOSE_HASMAGIC(x) (*(signed char *) (x) < 0)
202
203void ast_console_puts(const char *string);
204
205/*!
206 * \brief log the string to the console, and all attached console clients
207 *
208 * \param string The message to write to the console
209 * \param level The log level of the message
210 *
211 * \version 1.6.1 added level parameter
212 */
213void ast_console_puts_mutable(const char *string, int level);
214
215/*!
216 * \brief log the string to the console, and all attached console clients
217 * \since 14.0.0
218 *
219 * \param message The message to write to the console
220 * \param sublevel If the log level supports it, the sub-level of the message
221 * \param level The log level of the message
222 */
223void ast_console_puts_mutable_full(const char *message, int level, int sublevel);
224
225void ast_console_toggle_mute(int fd, int silent);
226
227/*!
228 * \brief enables or disables logging of a specified level to the console
229 * \param fd specifies the index of the console receiving the level change
230 * \param level specifies the index of the logging level being toggled
231 * \param state indicates whether logging will be on or off (0 for off, 1 for on)
232 */
233void ast_console_toggle_loglevel(int fd, int level, int state);
234
235/* Note: The AST_LOG_* macros below are the same as
236 * the LOG_* macros and are intended to eventually replace
237 * the LOG_* macros to avoid name collisions with the syslog(3)
238 * log levels. However, please do NOT remove
239 * the LOG_* macros from the source since these may be still
240 * needed for third-party modules
241 */
242
243#ifdef LOG_DEBUG
244#undef LOG_DEBUG
245#endif
246#define __LOG_DEBUG 0
247#define LOG_DEBUG __LOG_DEBUG, _A_
248
249#ifdef AST_LOG_DEBUG
250#undef AST_LOG_DEBUG
251#endif
252#define AST_LOG_DEBUG __LOG_DEBUG, _A_
253
254#ifdef LOG_TRACE
255#undef LOG_TRACE
256#endif
257#define __LOG_TRACE 1
258#define LOG_TRACE __LOG_TRACE, _A_
259
260#ifdef AST_LOG_TRACE
261#undef AST_LOG_TRACE
262#endif
263#define AST_LOG_TRACE __LOG_TRACE, _A_
264
265#ifdef LOG_NOTICE
266#undef LOG_NOTICE
267#endif
268#define __LOG_NOTICE 2
269#define LOG_NOTICE __LOG_NOTICE, _A_
270
271#ifdef AST_LOG_NOTICE
272#undef AST_LOG_NOTICE
273#endif
274#define AST_LOG_NOTICE __LOG_NOTICE, _A_
275
276#ifdef LOG_WARNING
277#undef LOG_WARNING
278#endif
279#define __LOG_WARNING 3
280#define LOG_WARNING __LOG_WARNING, _A_
281
282#ifdef AST_LOG_WARNING
283#undef AST_LOG_WARNING
284#endif
285#define AST_LOG_WARNING __LOG_WARNING, _A_
286
287#ifdef LOG_ERROR
288#undef LOG_ERROR
289#endif
290#define __LOG_ERROR 4
291#define LOG_ERROR __LOG_ERROR, _A_
292
293#ifdef AST_LOG_ERROR
294#undef AST_LOG_ERROR
295#endif
296#define AST_LOG_ERROR __LOG_ERROR, _A_
297
298#ifdef LOG_VERBOSE
299#undef LOG_VERBOSE
300#endif
301#define __LOG_VERBOSE 5
302#define LOG_VERBOSE __LOG_VERBOSE, _A_
303
304#ifdef AST_LOG_VERBOSE
305#undef AST_LOG_VERBOSE
306#endif
307#define AST_LOG_VERBOSE __LOG_VERBOSE, _A_
308
309#ifdef LOG_DTMF
310#undef LOG_DTMF
311#endif
312#define __LOG_DTMF 6
313#define LOG_DTMF __LOG_DTMF, _A_
314
315#ifdef AST_LOG_DTMF
316#undef AST_LOG_DTMF
317#endif
318#define AST_LOG_DTMF __LOG_DTMF, _A_
319
320#define NUMLOGLEVELS 32
321
322/*!
323 * \brief Get the debug level for a module
324 * \param module the name of module
325 * \return the debug level
326 */
327unsigned int ast_debug_get_by_module(const char *module);
328
329/*!
330 * \brief Register a new logger level
331 * \param name The name of the level to be registered
332 * \retval -1 if an error occurs
333 * \retval non-zero level to be used with ast_log for sending messages to this level
334 * \since 1.8
335 */
336int ast_logger_register_level(const char *name);
337
338/*!
339 * \brief Retrieve dynamic logging level id
340 * \param name The name of the level
341 * \return The unique integer id for the given level
342 * \retval -1 if level name not found
343 */
344int ast_logger_get_dynamic_level(const char *name);
345
346/*!
347 * \brief Unregister a previously registered logger level
348 * \param name The name of the level to be unregistered
349 * \since 1.8
350 */
351void ast_logger_unregister_level(const char *name);
352
353/*!
354 * \brief Get the logger configured date format
355 *
356 * \return The date format string
357 *
358 * \since 13.0.0
359 */
360const char *ast_logger_get_dateformat(void);
361
362/*!
363 * \brief factory function to create a new uniquely identifying callid.
364 *
365 * \return The call id
366 */
368
369/*!
370 * \brief extracts the callid from the thread
371 *
372 * \retval Non-zero Call id related to the thread
373 * \retval 0 if no call_id is present in the thread
374 */
376
377/*!
378 * \brief Sets what is stored in the thread storage to the given
379 * callid if it does not match what is already there.
380 *
381 * \retval 0 - success
382 * \retval non-zero - failure
383 */
385
386/*!
387 * \brief Adds a known callid to thread storage of the calling thread
388 *
389 * \retval 0 - success
390 * \retval non-zero - failure
391 */
393
394/*!
395 * \brief Removes callid from thread storage of the calling thread
396 *
397 * \retval 0 - success
398 * \retval non-zero - failure
399 */
401
402/*!
403 * \brief Checks thread storage for a callid and stores a reference if it exists.
404 * If not, then a new one will be created, bound to the thread, and a reference
405 * to it will be stored.
406 *
407 * \param callid pointer to store the callid
408 * \retval 0 - callid was found
409 * \retval 1 - callid was created
410 * \retval -1 - the function failed somehow (presumably memory problems)
411 */
413
414/*!
415 * \brief Use in conjunction with ast_callid_threadstorage_auto. Cleans up the
416 * references and if the callid was created by threadstorage_auto, unbinds
417 * the callid from the threadstorage
418 * \param callid The callid set by ast_callid_threadstorage_auto
419 * \param callid_created The integer returned through ast_callid_threadstorage_auto
420 */
421void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created);
422
423/*!
424 * \brief copy a string representation of the callid into a target string
425 *
426 * \param buffer destination of callid string (should be able to store 13 characters or more)
427 * \param buffer_size maximum writable length of the string (Less than 13 will result in truncation)
428 * \param callid Callid for which string is being requested
429 */
430void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid);
431
432/*!
433 * \brief Send a log message to a dynamically registered log level
434 * \param level The log level to send the message to
435 *
436 * Like ast_log, the log message may include printf-style formats, and
437 * the data for these must be provided as additional parameters after
438 * the log message.
439 *
440 * \since 1.8
441 */
442
443#define ast_log_dynamic_level(level, ...) ast_log(level, _A_, __VA_ARGS__)
444
445#define DEBUG_ATLEAST(level) \
446 (option_debug >= (level) \
447 || (ast_opt_dbg_module \
448 && ((int)ast_debug_get_by_module(AST_MODULE) >= (level) \
449 || (int)ast_debug_get_by_module(__FILE__) >= (level))))
450
451/*!
452 * \brief Log a DEBUG message
453 * \param level The minimum value of option_debug for this message
454 * to get logged
455 */
456#define ast_debug(level, ...) \
457 do { \
458 if (DEBUG_ATLEAST(level)) { \
459 ast_log(AST_LOG_DEBUG, __VA_ARGS__); \
460 } \
461 } while (0)
462
463extern int ast_verb_sys_level;
464
465#define VERBOSITY_ATLEAST(level) ((level) <= ast_verb_sys_level)
466
467#define ast_verb(level, ...) \
468 do { \
469 if (VERBOSITY_ATLEAST(level) ) { \
470 __ast_verbose(_A_, level, __VA_ARGS__); \
471 } \
472 } while (0)
473
474#define ast_verb_callid(level, callid, ...) \
475 do { \
476 if (VERBOSITY_ATLEAST(level) ) { \
477 __ast_verbose_callid(_A_, level, callid, __VA_ARGS__); \
478 } \
479 } while (0)
480
481/*!
482 * \brief Re-evaluate the system max verbosity level (ast_verb_sys_level).
483 */
484void ast_verb_update(void);
485
486/*!
487 * \brief Register this thread's console verbosity level pointer.
488 *
489 * \param level Where the verbose level value is.
490 */
491void ast_verb_console_register(int *level);
492
493/*!
494 * \brief Unregister this thread's console verbosity level.
495 */
497
498/*!
499 * \brief Get this thread's console verbosity level.
500 *
501 * \return verbosity level of the console.
502 */
503int ast_verb_console_get(void);
504
505/*!
506 * \brief Set this thread's console verbosity level.
507 *
508 * \param verb_level New level to set.
509 */
510void ast_verb_console_set(int verb_level);
511
512/*!
513 * \brief Test if logger is initialized
514 *
515 * \retval true if the logger is initialized
516 */
518
519/*!
520 * \brief Set the maximum number of messages allowed in the processing queue
521 *
522 * \param queue_limit
523 */
524void ast_logger_set_queue_limit(int queue_limit);
525
526/*!
527 * \brief Get the maximum number of messages allowed in the processing queue
528 *
529 * \return Queue limit
530 */
532
533/*!
534 * \brief Set the threshold for the maximum number of WARNING/ERROR messages allowed in the
535 * processing queue that can exceed the logger queue's threshold. This acts as a buffer for
536 * WARNING/ERROR messages so that we can continue to queue them while discarding other message
537 * types. This threshold sits on top of the logger queue's message threshold, so the actual
538 * queue size will be the logger queue threshold PLUS this threshold.
539 *
540 * \param limit
541 */
543
544/*!
545 * \brief Get the over threshold maximum number of messages allowed in the processing queue
546 *
547 * \return Over threshold queue limit
548 */
550
551
552/*! \defgroup Scope_Trace Scope Trace
553 * @{
554\page basic Basic Usage
555
556The Scope Trace facility allows you to instrument code and output scope entry
557and exit messages with associated data.
558\par
559To start using it:
560 - You must have used --enable-dev-mode.
561 - In logger.conf, set a logger channel to output the "trace" level.
562 - Instrument your code as specified below.
563 - Use the cli or cli.conf to enable tracing:
564\verbatim CLI> core set trace <trace_level> [ module ] \endverbatim
565\par
566Its simplest usage requires only 1 macro call that...
567 - Registers a destructor for a special variable that gets called when the
568 variable goes out of scope. Uses the same principle as RAII_VAR.
569 The destructor prints the name of the function with an "exiting" indicator
570 along with an optional message.
571 - Prints the name of the function with an "entering" indicator along with
572 an optional message.
573
574Simple Example:
575The following code...
576\code
577static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv,
578 struct ast_sip_session *session, const pjmedia_sdp_session *offer)
579{
580 SCOPE_TRACE(1, "%s\n", ast_sip_session_get_name(session));
581 ...
582}
583\endcode
584would produce...
585\verbatim
586[2020-05-17 15:16:51 -0600] TRACE[953402] : --> res_pjsip_session.c:4283 create_local_sdp PJSIP/1173-00000001
587[2020-05-17 15:16:51 -0600] TRACE[953402] : <-- res_pjsip_session.c:4283 create_local_sdp PJSIP/1173-00000001
588\endverbatim
589
590There is one odd bit. There's no way to capture the line number of there the scope exited
591so it's always going to be the line where SCOPE_TRACE is located.
592\par
593Similar to RAII_VAR, any block scope can be traced including "if", "for", "while", etc.
594\note "case" statements don't create a scope block by themselves but you can create
595a block for it, or use the generic trace functions mentioned below.
596
597\par Scope Output and Level:
598Rather than sending trace messages to the debug facility, a new facility "trace" has been
599added to logger. A corresponding CLI command "core set trace", and a corresponding "trace"
600parameter in asterisk.conf were added. This allows a separate log channel to be created
601just for storing trace messages. The levels are the same as those for debug and verbose.
602
603\par Scope Indenting:
604Each time SCOPE_TRACE or SCOPE_TRACE is called, a thread-local indent value is
605incremented on scope enter, and decremented on scope exit. This allows output
606like the following (timestamp omitted for brevity):
607\verbatim
608TRACE[953402] : --> res_pjsip_session.c:3940 session_inv_on_tsx_state_changed PJSIP/1173-00000001 TSX State: Proceeding Inv State: CALLING
609TRACE[953402] : --> res_pjsip_session.c:3680 handle_incoming PJSIP/1173-00000001
610TRACE[953402] : --> res_pjsip_session.c:3661 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
611TRACE[953402] : --> res_pjsip_session.c:3669 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Supplement: chan_pjsip
612TRACE[953402] : --> chan_pjsip.c:3265 chan_pjsip_incoming_response_after_media PJSIP/1173-00000001 Method: INVITE Status: 100 After Media
613TRACE[953402] : --> chan_pjsip.c:3194 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
614TRACE[953402] : chan_pjsip.c:3245 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Ignored
615TRACE[953402] : <-- chan_pjsip.c:3194 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
616TRACE[953402] : <-- chan_pjsip.c:3265 chan_pjsip_incoming_response_after_media PJSIP/1173-00000001 Method: INVITE Status: 100 After Media
617TRACE[953402] : <-- res_pjsip_session.c:3669 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Supplement: chan_pjsip
618TRACE[953402] : <-- res_pjsip_session.c:3661 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
619TRACE[953402] : <-- res_pjsip_session.c:3680 handle_incoming PJSIP/1173-00000001
620TRACE[953402] : <-- res_pjsip_session.c:3940 session_inv_on_tsx_state_changed PJSIP/1173-00000001 TSX State: Proceeding Inv State: CALLING
621\endverbatim
622\note The trace level indicates which messages to print and has no effect on indent.
623
624\par Generic Trace Messages:
625Sometimes you may just want to print a message to the trace log with the appropriate indent
626such as when executing a "case" clause in a "switch" statement. For example, the deepest
627message in the sample output above (chan_pjsip.c:3245) is just a single message instead of
628an entry/exit message. To do so, you can use the ast_trace macros...
629\code
630 ast_trace(1, "%s Method: %.*s Status: %d Ignored\n", ast_sip_session_get_name(session),
631 (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
632\endcode
633
634\note Final note: The trace facility, like debug, is only available when AST_DEVMODE is defined.
635
636*/
637
638/*!
639\page TRACE_PREFIX TRACE_PREFIX
640The default prefix to each log and trace line is
641<tt>"filename:line function"</tt> which is defined in the
642macro \c _A_ at the top of this file:
643\code
644#define _A_ __FILE__, __LINE__, __FUNCTION__
645\endcode
646They become 3 arguments to the __ast_trace function
647and most of the ast_log* functions. For scope tracing,
648that may be unnecessary clutter in the trace output so
649you can now customise that with the \c _TRACE_PREFIX_
650macro. Like \c _A_, it MUST resolve to 3 arguments:
651\verbatim
652const char *, int, const char *
653\endverbatim
654so the minimum would be:
655\code
656#define _TRACE_PREFIX_ "",0,""
657\endcode
658Normally you should define \c _TRACE_PREFIX_ in your source
659file before including logger.h.
660\code
661#define _TRACE_PREFIX_ "", __LINE__, ""
662#include "asterisk/logger.h"
663\endcode
664You can also define it later in your source file
665but because logger.h sets it to a default value, you'll
666have to undefine it first, then define it your your liking.
667If you want to go back to the default, you'll have to
668undefine it again, then define it to \c _TRACE_PREFIX_DEFAULT_.
669\code
670#undef _TRACE_PREFIX_
671#define _TRACE_PREFIX_ "", __LINE__, ""
672<code>
673#undef _TRACE_PREFIX_
674#define _TRACE_PREFIX_ _TRACE_PREFIX_DEFAULT_
675\endcode
676
677\note Macros have a compilation unit scope so
678defining \c _TRACE_PREFIX_ in one source file does NOT
679make it apply to any others. So if you define it
680in source file A, then call a function in source
681file B, the trace output from B will display based
682on how \c _TRACE_PREFIX_ is defined in B, not A.
683 */
684
685#define _TRACE_PREFIX_DEFAULT_ _A_
686#ifndef _TRACE_PREFIX_
687#define _TRACE_PREFIX_ _TRACE_PREFIX_DEFAULT_
688#endif
689
690/*!
691 * \brief Get the trace level for a module
692 * \param module the name of module
693 * \return the trace level
694 */
695unsigned int ast_trace_get_by_module(const char *module);
696
697/*!
698 * \brief load logger.conf configuration for console socket connections
699 */
701
702#define TRACE_ATLEAST(level) \
703 (option_trace >= (level) \
704 || (ast_opt_trace_module \
705 && ((int)ast_trace_get_by_module(AST_MODULE) >= (level) \
706 || (int)ast_trace_get_by_module(__FILE__) >= (level))))
707
708/*!
709 * \brief Controls if and when indenting is applied.
710 */
712 /*! Use the existing indent level */
714 /*! Increment the indent before printing the message */
716 /*! Increment the indent after printing the message */
718 /*! Decrement the indent before printing the message */
720 /*! Decrement the indent after printing the message */
722 /*! Set the indent to the one provided */
724 /*! Don't use or alter the level */
726};
727
728#ifdef AST_DEVMODE
729
730void __attribute__((format (printf, 6, 7))) __ast_trace(const char *file, int line, const char *func,
731 enum ast_trace_indent_type indent_type, unsigned long indent, const char* format, ...);
732
733/*!
734 * \brief Print a trace message
735 *
736 * \param level The trace level
737 * \param indent_type One of the \ref ast_trace_indent_type values
738 * \param ... A printf style format string, optionally with arguments
739 *
740 */
741#define ast_trace_raw(level, indent_type, ...) \
742 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__); \
743 if (TRACE_ATLEAST(level < 0 ? __scope_level : level)) { \
744 __ast_trace(_TRACE_PREFIX_, indent_type, 0, " " __VA_ARGS__); \
745 }
746
747/*!
748 * \brief Print a basic trace message
749 *
750 * \param level The trace level
751 * \param ... A printf style format string, optionally with arguments
752 *
753 * This will print the file, line and function at the current indent level
754 */
755#define ast_trace(level, ...) \
756 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__); \
757 if (TRACE_ATLEAST(level < 0 ? __scope_level : level)) { \
758 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_SAME, 0, " " __VA_ARGS__); \
759 }
760
761/*!
762 * \brief Get the current indent level
763 *
764 * \return The current indent level
765 */
766unsigned long _ast_trace_get_indent(void);
767#define ast_trace_get_indent() _ast_trace_get_indent()
768
769/*!
770 * \brief Set the current indent level
771 *
772 * \param indent The new indent level
773 */
774void _ast_trace_set_indent(unsigned long indent);
775#define ast_trace_set_indent(indent) _ast_trace_set_indent(indent)
776
777/*!
778 * \brief Increment the indent level
779 *
780 * \return The new indent level
781 */
782unsigned long _ast_trace_inc_indent(void);
783#define ast_trace_inc_indent() _ast_trace_inc_indent()
784
785/*!
786 * \brief Decrement the indent level
787 *
788 * \return The new indent level
789 */
790unsigned long _ast_trace_dec_indent(void);
791#define ast_trace_dec_indent() _ast_trace_dec_indent()
792
793/*!
794 * \brief Print a trace message with details when a scope is entered or existed.
795 *
796 * \param level The trace level
797 * \param ... A printf style format string, optionally with arguments
798 *
799 * This will print the file, line and function plus details at the current indent level.
800 * \note Like RAII_VAR, this macro must be called before any code in the scope.
801 *
802 * \note The variables used to detect scope change will look like
803 * __scopevar1234__EXIT and __scopevar1234__ENTER.
804 * The ENTER variable and function are needed to prevent mixed code and declaration issues.
805 * If we simple called __ast_trace, then this macro would need to be the last line
806 * of scope variable declaration. The following would fail.
807 *
808 * SCOPE_TRACE(1, "Help!\n");
809 * int i;
810 */
811#define SCOPE_TRACE(level, ...) \
812 const char *__trace_funcname = __PRETTY_FUNCTION__; \
813 auto void __scopevar ## __LINE__ ## __EXIT(void * v); \
814 void __scopevar ## __LINE__ ## __EXIT(void * v __attribute__((unused))) { \
815 if (TRACE_ATLEAST(level)) { \
816 __ast_trace(__FILE__, __LINE__, __trace_funcname, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
817 } \
818 } \
819 void *__scopevar ## __LINE__ ## __TRACER __attribute__((cleanup(__scopevar ## __LINE__ ## __EXIT))) = (void *) __PRETTY_FUNCTION__ ; \
820 auto int __scopevar ## __LINE__ ## __ENTER(void); \
821 int __scopevar ## __LINE__ ## __ENTER(void) { \
822 if (TRACE_ATLEAST(level)) { \
823 __ast_trace(__FILE__, __LINE__, __trace_funcname, AST_TRACE_INDENT_INC_AFTER, 0, " " __VA_ARGS__); \
824 } \
825 return 0; \
826 } \
827 int __scopevar ## __LINE__ ## __RETURN __attribute__((unused)) = __scopevar ## __LINE__ ## __ENTER()
828
829/*!
830 * \brief Non RAII_VAR Scope Trace macros
831 * The advantage of these macros is that the EXITs will have the actual
832 * line number where the scope exited. Much less code is required as well.
833 */
834
835/*!
836 * \brief Scope Enter
837 *
838 * \param level The trace level
839 * \param ... A printf style format string, optionally with arguments
840 */
841#define SCOPE_ENTER(level, ...) \
842 int __scope_level = level; \
843 int __scope_task = 0; \
844 ast_debug(__scope_level, " " __VA_ARGS__); \
845 if (TRACE_ATLEAST(level)) { \
846 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_INC_AFTER, 0, " " __VA_ARGS__); \
847 } \
848
849#define SCOPE_ENTER_TASK(level, indent, ...) \
850 int __scope_level = level; \
851 int __scope_task = 1; \
852 ast_debug(__scope_level, " " __VA_ARGS__); \
853 if (TRACE_ATLEAST(level)) { \
854 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_PROVIDED, indent, " " __VA_ARGS__); \
855 } \
856
857/*!
858 * \def SCOPE_CALL
859 * \brief Wrap a function call in "--> Calling:" and "<-- Return from:" trace messages
860 *
861 * \param level The trace level (usually -1 to use the same level as the calling function)
862 * \param __funcname The name of the function to call (return value, if any, is ignored)
863 * \param ... Arguments to pass to the function
864 *
865 * Simple Example:
866 * The following code called from inside the app_voicemail.c leave_voicemail function...
867\code
868 SCOPE_CALL(-1, store_file, dir, vmu->mailbox, vmu->context, msgnum);
869\endcode
870 * would produce...
871\verbatim
8721 app_voicemail_odbc.c:7161 leave_voicemail: --> Calling store_file
8731 --> app_voicemail_odbc.c:4483 store_file: dir: /var/spool/asterisk/voicemail/default/1179/INBOX user: 1179 context: default msgnum: 8
8742 app_voicemail_odbc.c:4502 store_file: Formats: wav49|gsm|wav Using format: 'WAV'
8752 app_voicemail_odbc.c:4510 store_file: Base path: '/var/spool/asterisk/voicemail/default/1179/INBOX/msg0008'
8762 app_voicemail_odbc.c:4513 store_file: Basename: 'msg0008'
8771 <-- app_voicemail_odbc.c:4597 store_file: Success
8781 app_voicemail_odbc.c:7161 leave_voicemail: <-- Return from store_file
879\endverbatim
880 */
881#define SCOPE_CALL(level, __funcname, ...) \
882({ \
883 ast_trace(level, "--> Calling %s\n", #__funcname); \
884 __funcname(__VA_ARGS__); \
885 ast_trace(level, "<-- Return from %s\n", #__funcname); \
886})
887
888/*!
889 * \def SCOPE_CALL_WITH_RESULT
890 * \brief Wrap a function call returning a value in "--> Calling:" and "<-- Return from:" trece messages
891 *
892 * \param level The trace level (usually -1 to use the same level as the calling function)
893 * \param __type The return type of the function
894 * \param __funcname The name of the function to call
895 * \param ... Arguments to pass to the function
896 *
897 * Simple Example:
898 * The following code called from inside the app_voicemail.c leave_voicemail function...
899\code
900 int res = 0;
901 res = SCOPE_CALL_WITH_RESULT(-1, int, store_file, dir, vmu->mailbox, vmu->context, msgnum);
902\endcode
903 * would produce...
904\verbatim
9051 app_voicemail_odbc.c:7161 leave_voicemail: --> Calling store_file
9061 --> app_voicemail_odbc.c:4483 store_file: dir: /var/spool/asterisk/voicemail/default/1179/INBOX user: 1179 context: default msgnum: 8
9072 app_voicemail_odbc.c:4502 store_file: Formats: wav49|gsm|wav Using format: 'WAV'
9082 app_voicemail_odbc.c:4510 store_file: Base path: '/var/spool/asterisk/voicemail/default/1179/INBOX/msg0008'
9092 app_voicemail_odbc.c:4513 store_file: Basename: 'msg0008'
9101 <-- app_voicemail_odbc.c:4597 store_file: Success
9111 app_voicemail_odbc.c:7161 leave_voicemail: <-- Return from store_file
912\endverbatim
913 * ...and the return value of the store_file function would be assigned to the variable res.
914 */
915#define SCOPE_CALL_WITH_RESULT(level, __type, __funcname, ...) \
916({ \
917 __type __var; \
918 ast_trace(level, "--> Calling %s\n", #__funcname); \
919 __var = __funcname(__VA_ARGS__); \
920 ast_trace(level, "<-- Return from %s\n", #__funcname); \
921 __var; \
922})
923
924#define SCOPE_CALL_WITH_INT_RESULT(level, __funcname, ...) \
925 SCOPE_CALL_WITH_RESULT(level, int, __funcname, __VA_ARGS__)
926
927/*!
928 * \brief Scope Exit
929 *
930 * \param ... A printf style format string, optionally with arguments
931 *
932 * \details
933 * This macro can be used at the exit points of a statement block since it just prints the message.
934 */
935#define SCOPE_EXIT(...) \
936 ast_debug(__scope_level, " " __VA_ARGS__); \
937 if (TRACE_ATLEAST(__scope_level)) { \
938 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
939 if (__scope_task) { \
940 _ast_trace_set_indent(0); \
941 } \
942 } \
943
944/*!
945 * \brief Scope Exit with expression
946 *
947 * \param __expr An expression to execute after printing the message
948 * \param ... A printf style format string, optionally with arguments
949 *
950 * \details
951 * Handy for getting out of or continuing loops.
952 *
953 * \code
954 * while(something) {
955 * SCOPE_ENTER(2, "In a while\n");
956 * if (something) {
957 * SCOPE_EXIT_EXPR(break, "Somethiung broke me\n");
958 * } else {
959 * SCOPE_EXIT_EXPR(continue, "Somethiung continued me\n");
960 * }
961 * }
962 * \endcode
963 */
964#define SCOPE_EXIT_EXPR(__expr, ...) \
965 ast_debug(__scope_level, " " __VA_ARGS__); \
966 if (TRACE_ATLEAST(__scope_level)) { \
967 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
968 if (__scope_task) { \
969 _ast_trace_set_indent(0); \
970 } \
971 } \
972 __expr
973
974/*!
975 * \brief Scope Exit with return
976 *
977 * \param ... A printf style format string, optionally with arguments
978 *
979 * \details
980 * This macro can be used at the exit points of a function when no value
981 * needs to be returned.
982 */
983#define SCOPE_EXIT_RTN(...) \
984 ast_debug(__scope_level, " " __VA_ARGS__); \
985 if (TRACE_ATLEAST(__scope_level)) { \
986 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
987 if (__scope_task) { \
988 _ast_trace_set_indent(0); \
989 } \
990 } \
991 return
992
993/*!
994 * \brief Scope Exit with return value
995 *
996 * \param __return_value The return value
997 * \param ... A printf style format string, optionally with arguments
998 *
999 * \details
1000 * This macro can be used at the exit points of a function when a value
1001 * needs to be returned.
1002 */
1003#define SCOPE_EXIT_RTN_VALUE(__return_value, ...) \
1004 ast_debug(__scope_level, " " __VA_ARGS__); \
1005 if (TRACE_ATLEAST(__scope_level)) { \
1006 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
1007 if (__scope_task) { \
1008 _ast_trace_set_indent(0); \
1009 } \
1010 } \
1011 return(__return_value)
1012
1013#else /* AST_DEVMODE */
1014#define ast_trace_raw(level, indent_type, ...) \
1015 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__)
1016
1017#define ast_trace(level, ...) \
1018 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__)
1019
1020#define ast_trace_get_indent() (0)
1021#define ast_trace_set_indent(indent)
1022#define ast_trace_inc_indent()
1023#define ast_trace_dec_indent()
1024#define SCOPE_TRACE(__level, ...)
1025
1026#define SCOPE_ENTER(level, ...) \
1027 int __scope_level = level; \
1028 ast_debug(level, " " __VA_ARGS__)
1029
1030#define SCOPE_ENTER_TASK(level, indent, ...) \
1031 int __scope_level = level; \
1032 ast_debug(level, " " __VA_ARGS__)
1033
1034#define SCOPE_CALL(level, __funcname, ...) \
1035 __funcname(__VA_ARGS__)
1036
1037#define SCOPE_CALL_WITH_RESULT(level, __var, __funcname, ...) \
1038 __funcname(__VA_ARGS__)
1039
1040#define SCOPE_CALL_WITH_INT_RESULT(level, __funcname, ...) \
1041 __funcname(__VA_ARGS__)
1042
1043#define SCOPE_EXIT(...) \
1044 ast_debug(__scope_level, " " __VA_ARGS__)
1045
1046#define SCOPE_EXIT_EXPR(__expr, ...) \
1047 ast_debug(__scope_level, " " __VA_ARGS__); \
1048 __expr
1049
1050#define SCOPE_EXIT_RTN(...) \
1051 ast_debug(__scope_level, " " __VA_ARGS__); \
1052 return
1053
1054#define SCOPE_EXIT_RTN_VALUE(__return_value, ...) \
1055 ast_debug(__scope_level, " " __VA_ARGS__); \
1056 return __return_value
1057
1058#endif /* AST_DEVMODE */
1059
1060/*!
1061 * The following macros will print log messages before running
1062 * the associated SCOPE_ macro.
1063 */
1064
1065#define SCOPE_EXIT_LOG(__log_level, ...) \
1066({ \
1067 ast_log(__log_level, " " __VA_ARGS__); \
1068 SCOPE_EXIT(" " __VA_ARGS__); \
1069})
1070
1071#define SCOPE_EXIT_LOG_RTN(__log_level, ...) \
1072({ \
1073 ast_log(__log_level, " " __VA_ARGS__); \
1074 SCOPE_EXIT_RTN(" " __VA_ARGS__); \
1075})
1076
1077#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level, ...) \
1078({ \
1079 ast_log(__log_level, " " __VA_ARGS__); \
1080 SCOPE_EXIT_RTN_VALUE(__value, " " __VA_ARGS__); \
1081})
1082
1083#define SCOPE_EXIT_LOG_EXPR(__expr, __log_level, ...) \
1084({ \
1085 ast_log(__log_level, " " __VA_ARGS__); \
1086 SCOPE_EXIT_EXPR(__expr, " " __VA_ARGS__); \
1087})
1088
1089#define ast_trace_log(__level, __log_level, ...) \
1090({ \
1091 ast_log(__log_level, " " __VA_ARGS__); \
1092 ast_trace(__level < 0 ? __scope_level : __level, " " __VA_ARGS__); \
1093})
1094
1095
1096#if defined(__cplusplus) || defined(c_plusplus)
1097}
1098#endif
1099
1100/*!
1101 * @}
1102 */
1103
1104#endif /* _ASTERISK_LOGGER_H */
jack_status_t status
Definition app_jack.c:149
#define ast_log
Definition astobj2.c:42
static const char type[]
static const char name[]
Definition format_mp3.c:68
void ast_init_logger_for_socket_console(void)
load logger.conf configuration for console socket connections
Definition logger.c:726
unsigned int ast_trace_get_by_module(const char *module)
Get the trace level for a module.
Definition main/cli.c:156
ast_trace_indent_type
Controls if and when indenting is applied.
@ AST_TRACE_INDENT_INC_BEFORE
@ AST_TRACE_INDENT_SAME
@ AST_TRACE_INDENT_PROVIDED
@ AST_TRACE_INDENT_NONE
@ AST_TRACE_INDENT_INC_AFTER
@ AST_TRACE_INDENT_DEC_BEFORE
@ AST_TRACE_INDENT_DEC_AFTER
int ast_verb_sys_level
Definition options.c:65
int ast_logger_get_dynamic_level(const char *name)
Retrieve dynamic logging level id.
Definition logger.c:2947
int ast_logger_get_queue_limit(void)
Get the maximum number of messages allowed in the processing queue.
Definition logger.c:3004
void __ast_verbose_ap(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap)
Definition logger.c:2612
int ast_logger_rotate_channel(const char *log_channel)
Rotate the specified log channel.
Definition logger.c:1328
void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid)
copy a string representation of the callid into a target string
Definition logger.c:2277
int ast_logger_rotate(void)
Reload logger while rotating log files.
Definition logger.c:1323
void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created)
Use in conjunction with ast_callid_threadstorage_auto. Cleans up the references and if the callid was...
Definition logger.c:2367
void ast_verb_console_unregister(void)
Unregister this thread's console verbosity level.
Definition logger.c:2729
int ast_logger_create_channel(const char *log_channel, const char *components)
Create a log channel.
Definition logger.c:1517
void ast_verb_console_register(int *level)
Register this thread's console verbosity level pointer.
Definition logger.c:2713
int ast_callid_threadassoc_change(ast_callid callid)
Sets what is stored in the thread storage to the given callid if it does not match what is already th...
Definition logger.c:2296
void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message with protection against recursion.
Definition logger.c:2542
int ast_logger_remove_channel(const char *log_channel)
Delete the specified log channel.
Definition logger.c:1583
int ast_logger_get_channels(int(*logentry)(const char *channel, const char *type, const char *status, const char *configuration, void *data), void *data)
Retrieve the existing log channels.
Definition logger.c:1405
void ast_console_toggle_loglevel(int fd, int level, int state)
enables or disables logging of a specified level to the console
Definition asterisk.c:1264
int ast_is_logger_initialized(void)
Test if logger is initialized.
Definition logger.c:2164
void ast_console_toggle_mute(int fd, int silent)
mute or unmute a console from logging
Definition asterisk.c:1287
ast_callid ast_read_threadstorage_callid(void)
extracts the callid from the thread
Definition logger.c:2287
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition logger.c:2309
void ast_console_puts_mutable(const char *string, int level)
log the string to the console, and all attached console clients
Definition asterisk.c:1329
ast_callid ast_create_callid(void)
factory function to create a new uniquely identifying callid.
Definition logger.c:2282
unsigned int ast_callid
void ast_console_puts(const char *string)
write the string to the root console, and all attached network console clients
Definition asterisk.c:1369
void __ast_verbose_callid(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt,...)
Send a verbose message (based on verbose level) with deliberately specified callid.
Definition logger.c:2629
void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt,...)
Send a verbose message (based on verbose level)
Definition logger.c:2617
void ast_log_ap(int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
Definition logger.c:2529
int ast_callid_threadassoc_remove(void)
Removes callid from thread storage of the calling thread.
Definition logger.c:2328
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt,...)
Definition logger.c:973
int ast_callid_threadstorage_auto(ast_callid *callid)
Checks thread storage for a callid and stores a reference if it exists. If not, then a new one will b...
Definition logger.c:2345
@ AST_LOGGER_DECLINE
@ AST_LOGGER_FAILURE
@ AST_LOGGER_SUCCESS
@ AST_LOGGER_ALLOC_ERROR
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition logger.c:2918
void ast_logger_set_queue_limit(int queue_limit)
Set the maximum number of messages allowed in the processing queue.
Definition logger.c:2999
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition logger.c:2651
int ast_logger_get_over_threshold_queue_limit(void)
Get the over threshold maximum number of messages allowed in the processing queue.
Definition logger.c:3014
void ast_child_verbose(int level, const char *fmt,...)
Definition logger.c:929
void ast_log_callid(int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt,...)
Used for sending a log message with a known call_id This is a modified logger function which is funct...
Definition logger.c:2568
void ast_logger_set_over_threshold_queue_limit(int limit)
Set the threshold for the maximum number of WARNING/ERROR messages allowed in the processing queue th...
Definition logger.c:3009
int ast_verb_console_get(void)
Get this thread's console verbosity level.
Definition logger.c:2740
const char * ast_logger_get_dateformat(void)
Get the logger configured date format.
Definition logger.c:2994
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition logger.c:2976
void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
log the string to the console, and all attached console clients
Definition asterisk.c:1336
unsigned int ast_debug_get_by_module(const char *module)
Get the debug level for a module.
Definition main/cli.c:139
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition logger.c:2577
void ast_verb_console_set(int verb_level)
Set this thread's console verbosity level.
Definition logger.c:2758
Options provided by main asterisk program.