1 /* $Id$
2  *
3  * Lasso - A free implementation of the Liberty Alliance specifications.
4  *
5  * Copyright (C) 2004-2007 Entr'ouvert
6  * http://lasso.entrouvert.org
7  *
8  * Authors: See AUTHORS file in top-level directory.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef __LASSO_LOGGING_H__
25 #define __LASSO_LOGGING_H__ 1
26 
27 #include <glib.h>
28 #include "errors.h"
29 
30 #ifndef lasso_log
31 LASSO_EXPORT void lasso_log(GLogLevelFlags level, const char *filename,
32 		int line, const char *function, const char *format, ...);
33 #endif
34 
35 int lasso_log_error_code(GLogLevelFlags level, int error, ...);
36 
37 #ifndef __FUNCTION__
38 #  define __FUNCTION__  ""
39 #endif
40 
41 #if defined(__GNUC__)
42 #  define message(level, format, args...) \
43 	lasso_log(level, __FILE__, __LINE__, __FUNCTION__, format, ##args)
44 #elif defined(HAVE_VARIADIC_MACROS)
45 #  define message(level, ...) \
46 	lasso_log(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
47 #else
message(GLogLevelFlags level,const char * format,...)48 static inline void message(GLogLevelFlags level, const char *format, ...)
49 {
50 	va_list ap;
51 	char s[1024];
52 	va_start(ap, format);
53 	g_vsnprintf(s, 1024, format, ap);
54 	va_end(ap);
55 	lasso_log(level, __FILE__, __LINE__, __FUNCTION__, s);
56 }
57 #endif
58 
59 
60 /* debug logging */
61 #if defined(LASSO_DEBUG)
62 #if defined(__GNUC__)
63 #define debug(format, args...) \
64 	message(G_LOG_LEVEL_DEBUG, format, ##args)
65 #elif defined(HAVE_VARIADIC_MACROS)
66 #define debug(...)     message(G_LOG_LEVEL_DEBUG, __VA_ARGS__)
67 #else
debug(const char * format,...)68 	static inline void debug(const char *format, ...)
69 	{
70 		va_list ap;
71 		char s[1024];
72 		va_start(ap, format);
73 		g_vsnprintf(s, 1024, format, ap);
74 		va_end(ap);
75 		message(G_LOG_LEVEL_DEBUG, "%s", s);
76 	}
77 #endif
78 #else
79 #if defined(__GNUC__)
80 #  define debug(format, args...) ;
81 #elif defined(HAVE_VARIADIC_MACROS)
82 #  define debug(...) ;
83 #else
debug(const char * format,...)84 	static inline void debug(const char *format, ...)
85 	{
86 		va_list ap;
87 		va_start(ap, format);
88 		va_end(ap);
89 	}
90 #endif
91 #endif
92 
93 #if defined(__GNUC__)
94 #  define warning(format, args...) \
95 	message(G_LOG_LEVEL_WARNING, format, ##args)
96 #elif defined(HAVE_VARIADIC_MACROS)
97 #  define warning(...)     message(G_LOG_LEVEL_WARNING, __VA_ARGS__)
98 #else
warning(const char * format,...)99 static inline void warning(const char *format, ...)
100 {
101 	va_list ap;
102 	char s[1024];
103 	va_start(ap, format);
104 	g_vsnprintf(s, 1024, format, ap);
105 	va_end(ap);
106 	message(G_LOG_LEVEL_WARNING, "%s", s);
107 }
108 #endif
109 
110 #if defined(__GNUC__)
111 #  define critical(format, args...) \
112 	message(G_LOG_LEVEL_CRITICAL, format, ##args)
113 #elif defined(HAVE_VARIADIC_MACROS)
114 #  define critical(...)     message(G_LOG_LEVEL_CRITICAL, __VA_ARGS__)
115 #else
critical(const char * format,...)116 static inline void critical(const char *format, ...)
117 {
118 	va_list ap;
119 	char s[1024];
120 	va_start(ap, format);
121 	g_vsnprintf(s, 1024, format, ap);
122 	va_end(ap);
123 	message(G_LOG_LEVEL_CRITICAL, "%s", s);
124 }
125 #endif
126 
127 #if defined(__GNUC__)
128 #  define error(format, args...) \
129 	message(G_LOG_LEVEL_ERROR, format, ##args)
130 #elif defined(HAVE_VARIADIC_MACROS)
131 #  define error(...)     message(G_LOG_LEVEL_ERROR, __VA_ARGS__)
132 #else
error(const char * format,...)133 static inline void error(const char *format, ...)
134 {
135 	va_list ap;
136 	char s[1024];
137 	va_start(ap, format);
138 	g_vsnprintf(s, 1024, format, ap);
139 	va_end(ap);
140 	message(G_LOG_LEVEL_ERROR, "%s", s);
141 }
142 #endif
143 
144 #define critical_error(rc) (critical("%s", lasso_strerror(rc)), rc)
145 
146 #endif /* __LASSO_LOGGING_H_ */
147