Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 * ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
166 *
167 * This will print the message to the console if the verbose level is set to a level >= 3
168 *
169 * Note the absence of a comma after the VERBOSE_PREFIX_3. This is important.
170 * VERBOSE_PREFIX_1 through VERBOSE_PREFIX_10 are defined.
171 *
172 * \version 11 added level parameter
173 */
174void __attribute__((format(printf, 5, 6))) __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
175
176/*!
177 * \brief Send a verbose message (based on verbose level) with deliberately specified callid
178 *
179 * \details just like __ast_verbose, only __ast_verbose_callid allows you to specify which callid is being used
180 * for the log without needing to bind it to a thread. NULL is a valid argument for this function and will
181 * allow you to specify that a log will never display a call id even when there is a call id bound to the
182 * thread.
183 */
184void __attribute__((format(printf, 6, 7))) __ast_verbose_callid(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, ...);
185
186#define ast_verbose(...) __ast_verbose(_A_, -1, __VA_ARGS__)
187#define ast_verbose_callid(callid, ...) __ast_verbose_callid(_A_, -1, callid, __VA_ARGS__)
188
189void __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);
190
191void __attribute__((format(printf, 2, 3))) ast_child_verbose(int level, const char *fmt, ...);
192
193/*
194 * These gymnastics are due to platforms which designate char as unsigned by
195 * default. Level is the negative character -- offset by 1, because \0 is
196 * the string terminator.
197 */
198#define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
199#define VERBOSE_HASMAGIC(x) (*(signed char *) (x) < 0)
200
201void ast_console_puts(const char *string);
202
203/*!
204 * \brief log the string to the console, and all attached console clients
205 *
206 * \param string The message to write to the console
207 * \param level The log level of the message
208 *
209 * \version 1.6.1 added level parameter
210 */
211void ast_console_puts_mutable(const char *string, int level);
212
213/*!
214 * \brief log the string to the console, and all attached console clients
215 * \since 14.0.0
216 *
217 * \param message The message to write to the console
218 * \param sublevel If the log level supports it, the sub-level of the message
219 * \param level The log level of the message
220 */
221void ast_console_puts_mutable_full(const char *message, int level, int sublevel);
222
223void ast_console_toggle_mute(int fd, int silent);
224
225/*!
226 * \brief enables or disables logging of a specified level to the console
227 * fd specifies the index of the console receiving the level change
228 * level specifies the index of the logging level being toggled
229 * state indicates whether logging will be on or off (0 for off, 1 for on)
230 */
231void ast_console_toggle_loglevel(int fd, int level, int state);
232
233/* Note: The AST_LOG_* macros below are the same as
234 * the LOG_* macros and are intended to eventually replace
235 * the LOG_* macros to avoid name collisions with the syslog(3)
236 * log levels. However, please do NOT remove
237 * the LOG_* macros from the source since these may be still
238 * needed for third-party modules
239 */
240
241#ifdef LOG_DEBUG
242#undef LOG_DEBUG
243#endif
244#define __LOG_DEBUG 0
245#define LOG_DEBUG __LOG_DEBUG, _A_
246
247#ifdef AST_LOG_DEBUG
248#undef AST_LOG_DEBUG
249#endif
250#define AST_LOG_DEBUG __LOG_DEBUG, _A_
251
252#ifdef LOG_TRACE
253#undef LOG_TRACE
254#endif
255#define __LOG_TRACE 1
256#define LOG_TRACE __LOG_TRACE, _A_
257
258#ifdef AST_LOG_TRACE
259#undef AST_LOG_TRACE
260#endif
261#define AST_LOG_TRACE __LOG_TRACE, _A_
262
263#ifdef LOG_NOTICE
264#undef LOG_NOTICE
265#endif
266#define __LOG_NOTICE 2
267#define LOG_NOTICE __LOG_NOTICE, _A_
268
269#ifdef AST_LOG_NOTICE
270#undef AST_LOG_NOTICE
271#endif
272#define AST_LOG_NOTICE __LOG_NOTICE, _A_
273
274#ifdef LOG_WARNING
275#undef LOG_WARNING
276#endif
277#define __LOG_WARNING 3
278#define LOG_WARNING __LOG_WARNING, _A_
279
280#ifdef AST_LOG_WARNING
281#undef AST_LOG_WARNING
282#endif
283#define AST_LOG_WARNING __LOG_WARNING, _A_
284
285#ifdef LOG_ERROR
286#undef LOG_ERROR
287#endif
288#define __LOG_ERROR 4
289#define LOG_ERROR __LOG_ERROR, _A_
290
291#ifdef AST_LOG_ERROR
292#undef AST_LOG_ERROR
293#endif
294#define AST_LOG_ERROR __LOG_ERROR, _A_
295
296#ifdef LOG_VERBOSE
297#undef LOG_VERBOSE
298#endif
299#define __LOG_VERBOSE 5
300#define LOG_VERBOSE __LOG_VERBOSE, _A_
301
302#ifdef AST_LOG_VERBOSE
303#undef AST_LOG_VERBOSE
304#endif
305#define AST_LOG_VERBOSE __LOG_VERBOSE, _A_
306
307#ifdef LOG_DTMF
308#undef LOG_DTMF
309#endif
310#define __LOG_DTMF 6
311#define LOG_DTMF __LOG_DTMF, _A_
312
313#ifdef AST_LOG_DTMF
314#undef AST_LOG_DTMF
315#endif
316#define AST_LOG_DTMF __LOG_DTMF, _A_
317
318#define NUMLOGLEVELS 32
319
320/*!
321 * \brief Get the debug level for a module
322 * \param module the name of module
323 * \return the debug level
324 */
325unsigned int ast_debug_get_by_module(const char *module);
326
327/*!
328 * \brief Register a new logger level
329 * \param name The name of the level to be registered
330 * \retval -1 if an error occurs
331 * \retval non-zero level to be used with ast_log for sending messages to this level
332 * \since 1.8
333 */
334int ast_logger_register_level(const char *name);
335
336/*!
337 * \brief Retrieve dynamic logging level id
338 * \param name The name of the level
339 * \return The unique integer id for the given level
340 * \retval -1 if level name not found
341 */
342int ast_logger_get_dynamic_level(const char *name);
343
344/*!
345 * \brief Unregister a previously registered logger level
346 * \param name The name of the level to be unregistered
347 * \since 1.8
348 */
349void ast_logger_unregister_level(const char *name);
350
351/*!
352 * \brief Get the logger configured date format
353 *
354 * \return The date format string
355 *
356 * \since 13.0.0
357 */
358const char *ast_logger_get_dateformat(void);
359
360/*!
361 * \brief factory function to create a new uniquely identifying callid.
362 *
363 * \return The call id
364 */
366
367/*!
368 * \brief extracts the callerid from the thread
369 *
370 * \retval Non-zero Call id related to the thread
371 * \retval 0 if no call_id is present in the thread
372 */
374
375/*!
376 * \brief Sets what is stored in the thread storage to the given
377 * callid if it does not match what is already there.
378 *
379 * \retval 0 - success
380 * \retval non-zero - failure
381 */
383
384/*!
385 * \brief Adds a known callid to thread storage of the calling thread
386 *
387 * \retval 0 - success
388 * \retval non-zero - failure
389 */
391
392/*!
393 * \brief Removes callid from thread storage of the calling thread
394 *
395 * \retval 0 - success
396 * \retval non-zero - failure
397 */
399
400/*!
401 * \brief Checks thread storage for a callid and stores a reference if it exists.
402 * If not, then a new one will be created, bound to the thread, and a reference
403 * to it will be stored.
404 *
405 * \param callid pointer to store the callid
406 * \retval 0 - callid was found
407 * \retval 1 - callid was created
408 * \retval -1 - the function failed somehow (presumably memory problems)
409 */
411
412/*!
413 * \brief Use in conjunction with ast_callid_threadstorage_auto. Cleans up the
414 * references and if the callid was created by threadstorage_auto, unbinds
415 * the callid from the threadstorage
416 * \param callid The callid set by ast_callid_threadstorage_auto
417 * \param callid_created The integer returned through ast_callid_threadstorage_auto
418 */
419void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created);
420
421/*!
422 * \brief copy a string representation of the callid into a target string
423 *
424 * \param buffer destination of callid string (should be able to store 13 characters or more)
425 * \param buffer_size maximum writable length of the string (Less than 13 will result in truncation)
426 * \param callid Callid for which string is being requested
427 */
428void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid);
429
430/*!
431 * \brief Send a log message to a dynamically registered log level
432 * \param level The log level to send the message to
433 *
434 * Like ast_log, the log message may include printf-style formats, and
435 * the data for these must be provided as additional parameters after
436 * the log message.
437 *
438 * \since 1.8
439 */
440
441#define ast_log_dynamic_level(level, ...) ast_log(level, _A_, __VA_ARGS__)
442
443#define DEBUG_ATLEAST(level) \
444 (option_debug >= (level) \
445 || (ast_opt_dbg_module \
446 && ((int)ast_debug_get_by_module(AST_MODULE) >= (level) \
447 || (int)ast_debug_get_by_module(__FILE__) >= (level))))
448
449/*!
450 * \brief Log a DEBUG message
451 * \param level The minimum value of option_debug for this message
452 * to get logged
453 */
454#define ast_debug(level, ...) \
455 do { \
456 if (DEBUG_ATLEAST(level)) { \
457 ast_log(AST_LOG_DEBUG, __VA_ARGS__); \
458 } \
459 } while (0)
460
461extern int ast_verb_sys_level;
462
463#define VERBOSITY_ATLEAST(level) ((level) <= ast_verb_sys_level)
464
465#define ast_verb(level, ...) \
466 do { \
467 if (VERBOSITY_ATLEAST(level) ) { \
468 __ast_verbose(_A_, level, __VA_ARGS__); \
469 } \
470 } while (0)
471
472#define ast_verb_callid(level, callid, ...) \
473 do { \
474 if (VERBOSITY_ATLEAST(level) ) { \
475 __ast_verbose_callid(_A_, level, callid, __VA_ARGS__); \
476 } \
477 } while (0)
478
479/*!
480 * \brief Re-evaluate the system max verbosity level (ast_verb_sys_level).
481 */
482void ast_verb_update(void);
483
484/*!
485 * \brief Register this thread's console verbosity level pointer.
486 *
487 * \param level Where the verbose level value is.
488 */
489void ast_verb_console_register(int *level);
490
491/*!
492 * \brief Unregister this thread's console verbosity level.
493 */
495
496/*!
497 * \brief Get this thread's console verbosity level.
498 *
499 * \return verbosity level of the console.
500 */
501int ast_verb_console_get(void);
502
503/*!
504 * \brief Set this thread's console verbosity level.
505 *
506 * \param verb_level New level to set.
507 */
508void ast_verb_console_set(int verb_level);
509
510/*!
511 * \brief Test if logger is initialized
512 *
513 * \retval true if the logger is initialized
514 */
516
517/*!
518 * \brief Set the maximum number of messages allowed in the processing queue
519 *
520 * \param queue_limit
521 */
522void ast_logger_set_queue_limit(int queue_limit);
523
524/*!
525 * \brief Get the maximum number of messages allowed in the processing queue
526 *
527 * \return Queue limit
528 */
530
531
532/*! \defgroup Scope_Trace Scope Trace
533 * @{
534\page basic Basic Usage
535
536The Scope Trace facility allows you to instrument code and output scope entry
537and exit messages with associated data.
538\par
539To start using it:
540 - You must have used --enable-dev-mode.
541 - In logger.conf, set a logger channel to output the "trace" level.
542 - Instrument your code as specified below.
543 - Use the cli or cli.conf to enable tracing:
544\verbatim CLI> core set trace <trace_level> [ module ] \endverbatim
545\par
546Its simplest usage requires only 1 macro call that...
547 - Registers a destructor for a special variable that gets called when the
548 variable goes out of scope. Uses the same principle as RAII_VAR.
549 The destructor prints the name of the function with an "exiting" indicator
550 along with an optional message.
551 - Prints the name of the function with an "entering" indicator along with
552 an optional message.
553
554Simple Example:
555The following code...
556\code
557static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv,
558 struct ast_sip_session *session, const pjmedia_sdp_session *offer)
559{
560 SCOPE_TRACE(1, "%s\n", ast_sip_session_get_name(session));
561 ...
562}
563\endcode
564would produce...
565\verbatim
566[2020-05-17 15:16:51 -0600] TRACE[953402] : --> res_pjsip_session.c:4283 create_local_sdp PJSIP/1173-00000001
567[2020-05-17 15:16:51 -0600] TRACE[953402] : <-- res_pjsip_session.c:4283 create_local_sdp PJSIP/1173-00000001
568\endverbatim
569
570There is one odd bit. There's no way to capture the line number of there the scope exited
571so it's always going to be the line where SCOPE_TRACE is located.
572\par
573Similar to RAII_VAR, any block scope can be traced including "if", "for", "while", etc.
574\note "case" statements don't create a scope block by themselves but you can create
575a block for it, or use the generic trace functions mentioned below.
576
577\par Scope Output and Level:
578Rather than sending trace messages to the debug facility, a new facility "trace" has been
579added to logger. A corresponding CLI command "core set trace", and a corresponding "trace"
580parameter in asterisk.conf were added. This allows a separate log channel to be created
581just for storing trace messages. The levels are the same as those for debug and verbose.
582
583\par Scope Indenting:
584Each time SCOPE_TRACE or SCOPE_TRACE is called, a thread-local indent value is
585incremented on scope enter, and decremented on scope exit. This allows output
586like the following (timestamp omitted for brevity):
587\verbatim
588TRACE[953402] : --> res_pjsip_session.c:3940 session_inv_on_tsx_state_changed PJSIP/1173-00000001 TSX State: Proceeding Inv State: CALLING
589TRACE[953402] : --> res_pjsip_session.c:3680 handle_incoming PJSIP/1173-00000001
590TRACE[953402] : --> res_pjsip_session.c:3661 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
591TRACE[953402] : --> res_pjsip_session.c:3669 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Supplement: chan_pjsip
592TRACE[953402] : --> chan_pjsip.c:3265 chan_pjsip_incoming_response_after_media PJSIP/1173-00000001 Method: INVITE Status: 100 After Media
593TRACE[953402] : --> chan_pjsip.c:3194 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
594TRACE[953402] : chan_pjsip.c:3245 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Ignored
595TRACE[953402] : <-- chan_pjsip.c:3194 chan_pjsip_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
596TRACE[953402] : <-- chan_pjsip.c:3265 chan_pjsip_incoming_response_after_media PJSIP/1173-00000001 Method: INVITE Status: 100 After Media
597TRACE[953402] : <-- res_pjsip_session.c:3669 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100 Supplement: chan_pjsip
598TRACE[953402] : <-- res_pjsip_session.c:3661 handle_incoming_response PJSIP/1173-00000001 Method: INVITE Status: 100
599TRACE[953402] : <-- res_pjsip_session.c:3680 handle_incoming PJSIP/1173-00000001
600TRACE[953402] : <-- res_pjsip_session.c:3940 session_inv_on_tsx_state_changed PJSIP/1173-00000001 TSX State: Proceeding Inv State: CALLING
601\endverbatim
602\note The trace level indicates which messages to print and has no effect on indent.
603
604\par Generic Trace Messages:
605Sometimes you may just want to print a message to the trace log with the appropriate indent
606such as when executing a "case" clause in a "switch" statement. For example, the deepest
607message in the sample output above (chan_pjsip.c:3245) is just a single message instead of
608an entry/exit message. To do so, you can use the ast_trace macros...
609\code
610 ast_trace(1, "%s Method: %.*s Status: %d Ignored\n", ast_sip_session_get_name(session),
611 (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
612\endcode
613
614\note Final note: The trace facility, like debug, is only available when AST_DEVMODE is defined.
615
616*/
617
618/*!
619\page TRACE_PREFIX TRACE_PREFIX
620The default prefix to each log and trace line is
621<tt>"filename:line function"</tt> which is defined in the
622macro \c _A_ at the top of this file:
623\code
624#define _A_ __FILE__, __LINE__, __FUNCTION__
625\endcode
626They become 3 arguments to the __ast_trace function
627and most of the ast_log* functions. For scope tracing,
628that may be unnecessary clutter in the trace output so
629you can now customise that with the \c _TRACE_PREFIX_
630macro. Like \c _A_, it MUST resolve to 3 arguments:
631\verbatim
632const char *, int, const char *
633\endverbatim
634so the minimum would be:
635\code
636#define _TRACE_PREFIX_ "",0,""
637\endcode
638Normally you should define \c _TRACE_PREFIX_ in your source
639file before including logger.h.
640\code
641#define _TRACE_PREFIX_ "", __LINE__, ""
642#include "asterisk/logger.h"
643\endcode
644You can also define it later in your source file
645but because logger.h sets it to a default value, you'll
646have to undefine it first, then define it your your liking.
647If you want to go back to the default, you'll have to
648undefine it again, then define it to \c _TRACE_PREFIX_DEFAULT_.
649\code
650#undef _TRACE_PREFIX_
651#define _TRACE_PREFIX_ "", __LINE__, ""
652<code>
653#undef _TRACE_PREFIX_
654#define _TRACE_PREFIX_ _TRACE_PREFIX_DEFAULT_
655\endcode
656
657\note Macros have a compilation unit scope so
658defining \c _TRACE_PREFIX_ in one source file does NOT
659make it apply to any others. So if you define it
660in source file A, then call a function in source
661file B, the trace output from B will display based
662on how \c _TRACE_PREFIX_ is defined in B, not A.
663 */
664
665#define _TRACE_PREFIX_DEFAULT_ _A_
666#ifndef _TRACE_PREFIX_
667#define _TRACE_PREFIX_ _TRACE_PREFIX_DEFAULT_
668#endif
669
670/*!
671 * \brief Get the trace level for a module
672 * \param module the name of module
673 * \return the trace level
674 */
675unsigned int ast_trace_get_by_module(const char *module);
676
677/*!
678 * \brief load logger.conf configuration for console socket connections
679 */
681
682#define TRACE_ATLEAST(level) \
683 (option_trace >= (level) \
684 || (ast_opt_trace_module \
685 && ((int)ast_trace_get_by_module(AST_MODULE) >= (level) \
686 || (int)ast_trace_get_by_module(__FILE__) >= (level))))
687
688/*!
689 * \brief Controls if and when indenting is applied.
690 */
692 /*! Use the existing indent level */
694 /*! Increment the indent before printing the message */
696 /*! Increment the indent after printing the message */
698 /*! Decrement the indent before printing the message */
700 /*! Decrement the indent after printing the message */
702 /*! Set the indent to the one provided */
704 /*! Don't use or alter the level */
706};
707
708#ifdef AST_DEVMODE
709
710void __attribute__((format (printf, 6, 7))) __ast_trace(const char *file, int line, const char *func,
711 enum ast_trace_indent_type indent_type, unsigned long indent, const char* format, ...);
712
713/*!
714 * \brief Print a trace message
715 *
716 * \param level The trace level
717 * \param indent_type One of the \ref ast_trace_indent_type values
718 * \param ... A printf style format string, optionally with arguments
719 *
720 */
721#define ast_trace_raw(level, indent_type, ...) \
722 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__); \
723 if (TRACE_ATLEAST(level < 0 ? __scope_level : level)) { \
724 __ast_trace(_TRACE_PREFIX_, indent_type, 0, " " __VA_ARGS__); \
725 }
726
727/*!
728 * \brief Print a basic trace message
729 *
730 * \param level The trace level
731 * \param ... A printf style format string, optionally with arguments
732 *
733 * This will print the file, line and function at the current indent level
734 */
735#define ast_trace(level, ...) \
736 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__); \
737 if (TRACE_ATLEAST(level < 0 ? __scope_level : level)) { \
738 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_SAME, 0, " " __VA_ARGS__); \
739 }
740
741/*!
742 * \brief Get the current indent level
743 *
744 * \return The current indent level
745 */
746unsigned long _ast_trace_get_indent(void);
747#define ast_trace_get_indent() _ast_trace_get_indent()
748
749/*!
750 * \brief Set the current indent level
751 *
752 * \param indent The new indent level
753 */
754void _ast_trace_set_indent(unsigned long indent);
755#define ast_trace_set_indent(indent) _ast_trace_set_indent(indent)
756
757/*!
758 * \brief Increment the indent level
759 *
760 * \return The new indent level
761 */
762unsigned long _ast_trace_inc_indent(void);
763#define ast_trace_inc_indent() _ast_trace_inc_indent()
764
765/*!
766 * \brief Decrement the indent level
767 *
768 * \return The new indent level
769 */
770unsigned long _ast_trace_dec_indent(void);
771#define ast_trace_dec_indent() _ast_trace_dec_indent()
772
773/*!
774 * \brief Print a trace message with details when a scope is entered or existed.
775 *
776 * \param level The trace level
777 * \param ... A printf style format string, optionally with arguments
778 *
779 * This will print the file, line and function plus details at the current indent level.
780 * \note Like RAII_VAR, this macro must be called before any code in the scope.
781 *
782 * \note The variables used to detect scope change will look like
783 * __scopevar1234__EXIT and __scopevar1234__ENTER.
784 * The ENTER variable and function are needed to prevent mixed code and declaration issues.
785 * If we simple called __ast_trace, then this macro would need to be the last line
786 * of scope variable declaration. The following would fail.
787 *
788 * SCOPE_TRACE(1, "Help!\n");
789 * int i;
790 */
791#define SCOPE_TRACE(level, ...) \
792 const char *__trace_funcname = __PRETTY_FUNCTION__; \
793 auto void __scopevar ## __LINE__ ## __EXIT(void * v); \
794 void __scopevar ## __LINE__ ## __EXIT(void * v __attribute__((unused))) { \
795 if (TRACE_ATLEAST(level)) { \
796 __ast_trace(__FILE__, __LINE__, __trace_funcname, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
797 } \
798 } \
799 void *__scopevar ## __LINE__ ## __TRACER __attribute__((cleanup(__scopevar ## __LINE__ ## __EXIT))) = (void *) __PRETTY_FUNCTION__ ; \
800 auto int __scopevar ## __LINE__ ## __ENTER(void); \
801 int __scopevar ## __LINE__ ## __ENTER(void) { \
802 if (TRACE_ATLEAST(level)) { \
803 __ast_trace(__FILE__, __LINE__, __trace_funcname, AST_TRACE_INDENT_INC_AFTER, 0, " " __VA_ARGS__); \
804 } \
805 return 0; \
806 } \
807 int __scopevar ## __LINE__ ## __RETURN __attribute__((unused)) = __scopevar ## __LINE__ ## __ENTER()
808
809/*!
810 * \brief Non RAII_VAR Scope Trace macros
811 * The advantage of these macros is that the EXITs will have the actual
812 * line number where the scope exited. Much less code is required as well.
813 */
814
815/*!
816 * \brief Scope Enter
817 *
818 * \param level The trace level
819 * \param ... A printf style format string, optionally with arguments
820 */
821#define SCOPE_ENTER(level, ...) \
822 int __scope_level = level; \
823 int __scope_task = 0; \
824 ast_debug(__scope_level, " " __VA_ARGS__); \
825 if (TRACE_ATLEAST(level)) { \
826 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_INC_AFTER, 0, " " __VA_ARGS__); \
827 } \
828
829#define SCOPE_ENTER_TASK(level, indent, ...) \
830 int __scope_level = level; \
831 int __scope_task = 1; \
832 ast_debug(__scope_level, " " __VA_ARGS__); \
833 if (TRACE_ATLEAST(level)) { \
834 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_PROVIDED, indent, " " __VA_ARGS__); \
835 } \
836
837/*!
838 * \def SCOPE_CALL
839 * \brief Wrap a function call in "--> Calling:" and "<-- Return from:" trace messages
840 *
841 * \param level The trace level (usually -1 to use the same level as the calling function)
842 * \param __funcname The name of the function to call (return value, if any, is ignored)
843 * \param ... Arguments to pass to the function
844 *
845 * Simple Example:
846 * The following code called from inside the app_voicemail.c leave_voicemail function...
847\code
848 SCOPE_CALL(-1, store_file, dir, vmu->mailbox, vmu->context, msgnum);
849\endcode
850 * would produce...
851\verbatim
8521 app_voicemail_odbc.c:7161 leave_voicemail: --> Calling store_file
8531 --> app_voicemail_odbc.c:4483 store_file: dir: /var/spool/asterisk/voicemail/default/1179/INBOX user: 1179 context: default msgnum: 8
8542 app_voicemail_odbc.c:4502 store_file: Formats: wav49|gsm|wav Using format: 'WAV'
8552 app_voicemail_odbc.c:4510 store_file: Base path: '/var/spool/asterisk/voicemail/default/1179/INBOX/msg0008'
8562 app_voicemail_odbc.c:4513 store_file: Basename: 'msg0008'
8571 <-- app_voicemail_odbc.c:4597 store_file: Success
8581 app_voicemail_odbc.c:7161 leave_voicemail: <-- Return from store_file
859\endverbatim
860 */
861#define SCOPE_CALL(level, __funcname, ...) \
862({ \
863 ast_trace(level, "--> Calling %s\n", #__funcname); \
864 __funcname(__VA_ARGS__); \
865 ast_trace(level, "<-- Return from %s\n", #__funcname); \
866})
867
868/*!
869 * \def SCOPE_CALL_WITH_RESULT
870 * \brief Wrap a function call returning a value in "--> Calling:" and "<-- Return from:" trece messages
871 *
872 * \param level The trace level (usually -1 to use the same level as the calling function)
873 * \param __type The return type of the function
874 * \param __funcname The name of the function to call
875 * \param ... Arguments to pass to the function
876 *
877 * Simple Example:
878 * The following code called from inside the app_voicemail.c leave_voicemail function...
879\code
880 int res = 0;
881 res = SCOPE_CALL_WITH_RESULT(-1, int, store_file, dir, vmu->mailbox, vmu->context, msgnum);
882\endcode
883 * would produce...
884\verbatim
8851 app_voicemail_odbc.c:7161 leave_voicemail: --> Calling store_file
8861 --> app_voicemail_odbc.c:4483 store_file: dir: /var/spool/asterisk/voicemail/default/1179/INBOX user: 1179 context: default msgnum: 8
8872 app_voicemail_odbc.c:4502 store_file: Formats: wav49|gsm|wav Using format: 'WAV'
8882 app_voicemail_odbc.c:4510 store_file: Base path: '/var/spool/asterisk/voicemail/default/1179/INBOX/msg0008'
8892 app_voicemail_odbc.c:4513 store_file: Basename: 'msg0008'
8901 <-- app_voicemail_odbc.c:4597 store_file: Success
8911 app_voicemail_odbc.c:7161 leave_voicemail: <-- Return from store_file
892\endverbatim
893 * ...and the return value of the store_file function would be assigned to the variable res.
894 */
895#define SCOPE_CALL_WITH_RESULT(level, __type, __funcname, ...) \
896({ \
897 __type __var; \
898 ast_trace(level, "--> Calling %s\n", #__funcname); \
899 __var = __funcname(__VA_ARGS__); \
900 ast_trace(level, "<-- Return from %s\n", #__funcname); \
901 __var; \
902})
903
904#define SCOPE_CALL_WITH_INT_RESULT(level, __funcname, ...) \
905 SCOPE_CALL_WITH_RESULT(level, int, __funcname, __VA_ARGS__)
906
907/*!
908 * \brief Scope Exit
909 *
910 * \param ... A printf style format string, optionally with arguments
911 *
912 * \details
913 * This macro can be used at the exit points of a statement block since it just prints the message.
914 */
915#define SCOPE_EXIT(...) \
916 ast_debug(__scope_level, " " __VA_ARGS__); \
917 if (TRACE_ATLEAST(__scope_level)) { \
918 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
919 if (__scope_task) { \
920 _ast_trace_set_indent(0); \
921 } \
922 } \
923
924/*!
925 * \brief Scope Exit with expression
926 *
927 * \param __expr An expression to execute after printing the message
928 * \param ... A printf style format string, optionally with arguments
929 *
930 * \details
931 * Handy for getting out of or continuing loops.
932 *
933 * \code
934 * while(something) {
935 * SCOPE_ENTER(2, "In a while\n");
936 * if (something) {
937 * SCOPE_EXIT_EXPR(break, "Somethiung broke me\n");
938 * } else {
939 * SCOPE_EXIT_EXPR(continue, "Somethiung continued me\n");
940 * }
941 * }
942 * \endcode
943 */
944#define SCOPE_EXIT_EXPR(__expr, ...) \
945 ast_debug(__scope_level, " " __VA_ARGS__); \
946 if (TRACE_ATLEAST(__scope_level)) { \
947 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
948 if (__scope_task) { \
949 _ast_trace_set_indent(0); \
950 } \
951 } \
952 __expr
953
954/*!
955 * \brief Scope Exit with return
956 *
957 * \param ... A printf style format string, optionally with arguments
958 *
959 * \details
960 * This macro can be used at the exit points of a function when no value
961 * needs to be returned.
962 */
963#define SCOPE_EXIT_RTN(...) \
964 ast_debug(__scope_level, " " __VA_ARGS__); \
965 if (TRACE_ATLEAST(__scope_level)) { \
966 __ast_trace(_TRACE_PREFIX_, AST_TRACE_INDENT_DEC_BEFORE, 0, " " __VA_ARGS__); \
967 if (__scope_task) { \
968 _ast_trace_set_indent(0); \
969 } \
970 } \
971 return
972
973/*!
974 * \brief Scope Exit with return value
975 *
976 * \param __return_value The return value
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 a value
981 * needs to be returned.
982 */
983#define SCOPE_EXIT_RTN_VALUE(__return_value, ...) \
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(__return_value)
992
993#else /* AST_DEVMODE */
994#define ast_trace_raw(level, indent_type, ...) \
995 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__)
996
997#define ast_trace(level, ...) \
998 ast_debug(level < 0 ? __scope_level : level, " " __VA_ARGS__)
999
1000#define ast_trace_get_indent() (0)
1001#define ast_trace_set_indent(indent)
1002#define ast_trace_inc_indent()
1003#define ast_trace_dec_indent()
1004#define SCOPE_TRACE(__level, ...)
1005
1006#define SCOPE_ENTER(level, ...) \
1007 int __scope_level = level; \
1008 ast_debug(level, " " __VA_ARGS__)
1009
1010#define SCOPE_ENTER_TASK(level, indent, ...) \
1011 int __scope_level = level; \
1012 ast_debug(level, " " __VA_ARGS__)
1013
1014#define SCOPE_CALL(level, __funcname, ...) \
1015 __funcname(__VA_ARGS__)
1016
1017#define SCOPE_CALL_WITH_RESULT(level, __var, __funcname, ...) \
1018 __funcname(__VA_ARGS__)
1019
1020#define SCOPE_CALL_WITH_INT_RESULT(level, __funcname, ...) \
1021 __funcname(__VA_ARGS__)
1022
1023#define SCOPE_EXIT(...) \
1024 ast_debug(__scope_level, " " __VA_ARGS__)
1025
1026#define SCOPE_EXIT_EXPR(__expr, ...) \
1027 ast_debug(__scope_level, " " __VA_ARGS__); \
1028 __expr
1029
1030#define SCOPE_EXIT_RTN(...) \
1031 ast_debug(__scope_level, " " __VA_ARGS__); \
1032 return
1033
1034#define SCOPE_EXIT_RTN_VALUE(__return_value, ...) \
1035 ast_debug(__scope_level, " " __VA_ARGS__); \
1036 return __return_value
1037
1038#endif /* AST_DEVMODE */
1039
1040/*!
1041 * The following macros will print log messages before running
1042 * the associated SCOPE_ macro.
1043 */
1044
1045#define SCOPE_EXIT_LOG(__log_level, ...) \
1046({ \
1047 ast_log(__log_level, " " __VA_ARGS__); \
1048 SCOPE_EXIT(" " __VA_ARGS__); \
1049})
1050
1051#define SCOPE_EXIT_LOG_RTN(__log_level, ...) \
1052({ \
1053 ast_log(__log_level, " " __VA_ARGS__); \
1054 SCOPE_EXIT_RTN(" " __VA_ARGS__); \
1055})
1056
1057#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level, ...) \
1058({ \
1059 ast_log(__log_level, " " __VA_ARGS__); \
1060 SCOPE_EXIT_RTN_VALUE(__value, " " __VA_ARGS__); \
1061})
1062
1063#define SCOPE_EXIT_LOG_EXPR(__expr, __log_level, ...) \
1064({ \
1065 ast_log(__log_level, " " __VA_ARGS__); \
1066 SCOPE_EXIT_EXPR(__expr, " " __VA_ARGS__); \
1067})
1068
1069#define ast_trace_log(__level, __log_level, ...) \
1070({ \
1071 ast_log(__log_level, " " __VA_ARGS__); \
1072 ast_trace(__level < 0 ? __scope_level : __level, " " __VA_ARGS__); \
1073})
1074
1075
1076#if defined(__cplusplus) || defined(c_plusplus)
1077}
1078#endif
1079
1080/*!
1081 * @}
1082 */
1083
1084#endif /* _ASTERISK_LOGGER_H */
jack_status_t status
Definition: app_jack.c:149
static const char type[]
Definition: chan_ooh323.c:109
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:710
unsigned int ast_trace_get_by_module(const char *module)
Get the trace level for a module.
Definition: main/cli.c:153
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:64
int ast_logger_get_dynamic_level(const char *name)
Retrieve dynamic logging level id.
Definition: logger.c:2855
int ast_logger_get_queue_limit(void)
Get the maximum number of messages allowed in the processing queue.
Definition: logger.c:2912
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:2520
int ast_logger_rotate_channel(const char *log_channel)
Rotate the specified log channel.
Definition: logger.c:1317
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:2263
int ast_logger_rotate(void)
Reload logger while rotating log files.
Definition: logger.c:1312
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:2353
void ast_verb_console_unregister(void)
Unregister this thread's console verbosity level.
Definition: logger.c:2637
int ast_logger_create_channel(const char *log_channel, const char *components)
Create a log channel.
Definition: logger.c:1506
void ast_verb_console_register(int *level)
Register this thread's console verbosity level pointer.
Definition: logger.c:2621
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:2282
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:2450
int ast_logger_remove_channel(const char *log_channel)
Delete the specified log channel.
Definition: logger.c:1572
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:1394
void ast_console_toggle_loglevel(int fd, int level, int state)
enables or disables logging of a specified level to the console fd specifies the index of the console...
Definition: asterisk.c:1258
int ast_is_logger_initialized(void)
Test if logger is initialized.
Definition: logger.c:2150
void ast_console_toggle_mute(int fd, int silent)
mute or unmute a console from logging
Definition: asterisk.c:1281
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2273
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2295
void ast_console_puts_mutable(const char *string, int level)
log the string to the console, and all attached console clients
Definition: asterisk.c:1323
ast_callid ast_create_callid(void)
factory function to create a new uniquely identifying callid.
Definition: logger.c:2268
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:1363
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:2537
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:2525
void ast_log_ap(int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
Definition: logger.c:2437
int ast_callid_threadassoc_remove(void)
Removes callid from thread storage of the calling thread.
Definition: logger.c:2314
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt,...)
Definition: logger.c:962
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:2331
@ 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:2826
void ast_logger_set_queue_limit(int queue_limit)
Set the maximum number of messages allowed in the processing queue.
Definition: logger.c:2907
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition: logger.c:2559
void ast_child_verbose(int level, const char *fmt,...)
Definition: logger.c:918
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: ael_main.c:130
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:2476
int ast_verb_console_get(void)
Get this thread's console verbosity level.
Definition: logger.c:2648
const char * ast_logger_get_dateformat(void)
Get the logger configured date format.
Definition: logger.c:2902
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2884
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:1330
unsigned int ast_debug_get_by_module(const char *module)
Get the debug level for a module.
Definition: main/cli.c:136
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition: logger.c:2485
void ast_verb_console_set(int verb_level)
Set this thread's console verbosity level.
Definition: logger.c:2666
Options provided by main asterisk program.
Definition: astman.c:222