1 /*
2  * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
3  * Copyright (C) 2017 Belledonne Communications SARL
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 /**
21  * \file logging.h
22  * \brief Logging API.
23  *
24 **/
25 
26 #ifndef ORTP_LOGGING_H
27 #define ORTP_LOGGING_H
28 
29 #include <ortp/port.h>
30 
31 #ifndef ORTP_LOG_DOMAIN
32 #define ORTP_LOG_DOMAIN NULL
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40 typedef enum {
41 	ORTP_DEBUG=1,
42 	ORTP_TRACE=1<<1,
43 	ORTP_MESSAGE=1<<2,
44 	ORTP_WARNING=1<<3,
45 	ORTP_ERROR=1<<4,
46 	ORTP_FATAL=1<<5,
47 	ORTP_LOGLEV_END=1<<6
48 } OrtpLogLevel;
49 
50 
51 typedef void (*OrtpLogFunc)(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args);
52 
53 ORTP_PUBLIC void ortp_set_log_file(FILE *file);
54 ORTP_PUBLIC void ortp_set_log_handler(OrtpLogFunc func);
55 ORTP_PUBLIC OrtpLogFunc ortp_get_log_handler(void);
56 
57 ORTP_PUBLIC void ortp_logv_out(const char *domain, OrtpLogLevel level, const char *fmt, va_list args);
58 
59 #define ortp_log_level_enabled(domain, level)	(ortp_get_log_level_mask(domain) & (level))
60 
61 ORTP_PUBLIC void ortp_logv(const char *domain, OrtpLogLevel level, const char *fmt, va_list args);
62 
63 /**
64  * Flushes the log output queue.
65  * WARNING: Must be called from the thread that has been defined with ortp_set_log_thread_id().
66  */
67 ORTP_PUBLIC void ortp_logv_flush(void);
68 
69 /**
70  * Activate all log level greater or equal than specified level argument.
71 **/
72 ORTP_PUBLIC void ortp_set_log_level(const char *domain, OrtpLogLevel level);
73 
74 ORTP_PUBLIC void ortp_set_log_level_mask(const char *domain, int levelmask);
75 ORTP_PUBLIC unsigned int ortp_get_log_level_mask(const char *domain);
76 
77 /**
78  * Tell oRTP the id of the thread used to output the logs.
79  * This is meant to output all the logs from the same thread to prevent deadlock problems at the application level.
80  * @param[in] thread_id The id of the thread that will output the logs (can be obtained using ortp_thread_self()).
81  */
82 ORTP_PUBLIC void ortp_set_log_thread_id(unsigned long thread_id);
83 
84 #ifdef __GNUC__
85 #define CHECK_FORMAT_ARGS(m,n) __attribute__((format(printf,m,n)))
86 #else
87 #define CHECK_FORMAT_ARGS(m,n)
88 #endif
89 #ifdef __clang__
90 /*in case of compile with -g static inline can produce this type of warning*/
91 #pragma GCC diagnostic ignored "-Wunused-function"
92 #endif
93 #ifdef ORTP_DEBUG_MODE
ortp_debug(const char * fmt,...)94 static ORTP_INLINE void CHECK_FORMAT_ARGS(1,2) ortp_debug(const char *fmt,...)
95 {
96   va_list args;
97   va_start (args, fmt);
98   ortp_logv(ORTP_LOG_DOMAIN, ORTP_DEBUG, fmt, args);
99   va_end (args);
100 }
101 #else
102 
103 #define ortp_debug(...)
104 
105 #endif
106 
107 #ifdef ORTP_NOMESSAGE_MODE
108 
109 #define ortp_log(...)
110 #define ortp_message(...)
111 #define ortp_warning(...)
112 
113 #else
114 
ortp_log(OrtpLogLevel lev,const char * fmt,...)115 static ORTP_INLINE void CHECK_FORMAT_ARGS(2,3) ortp_log(OrtpLogLevel lev, const char *fmt,...) {
116 	va_list args;
117 	va_start (args, fmt);
118 	ortp_logv(ORTP_LOG_DOMAIN, lev, fmt, args);
119 	va_end (args);
120 }
121 
ortp_message(const char * fmt,...)122 static ORTP_INLINE void CHECK_FORMAT_ARGS(1,2) ortp_message(const char *fmt,...)
123 {
124 	va_list args;
125 	va_start (args, fmt);
126 	ortp_logv(ORTP_LOG_DOMAIN, ORTP_MESSAGE, fmt, args);
127 	va_end (args);
128 }
129 
ortp_warning(const char * fmt,...)130 static ORTP_INLINE void CHECK_FORMAT_ARGS(1,2) ortp_warning(const char *fmt,...)
131 {
132 	va_list args;
133 	va_start (args, fmt);
134 	ortp_logv(ORTP_LOG_DOMAIN, ORTP_WARNING, fmt, args);
135 	va_end (args);
136 }
137 
138 #endif
139 
ortp_error(const char * fmt,...)140 static ORTP_INLINE void CHECK_FORMAT_ARGS(1,2) ortp_error(const char *fmt,...)
141 {
142 	va_list args;
143 	va_start (args, fmt);
144 	ortp_logv(ORTP_LOG_DOMAIN, ORTP_ERROR, fmt, args);
145 	va_end (args);
146 }
147 
ortp_fatal(const char * fmt,...)148 static ORTP_INLINE void CHECK_FORMAT_ARGS(1,2) ortp_fatal(const char *fmt,...)
149 {
150 	va_list args;
151 	va_start (args, fmt);
152 	ortp_logv(ORTP_LOG_DOMAIN, ORTP_FATAL, fmt, args);
153 	va_end (args);
154 }
155 
156 
157 #ifdef __QNX__
158 void ortp_qnx_log_handler(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args);
159 #endif
160 
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif
167