1 /*
2    Copyright (C) 2012 Red Hat, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef H_SPICE_COMMON_LOG
19 #define H_SPICE_COMMON_LOG
20 
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <glib.h>
24 #include <spice/macros.h>
25 
26 #include "macros.h"
27 
28 SPICE_BEGIN_DECLS
29 
30 #ifdef SPICE_LOG_DOMAIN
31 #error Do not use obsolete SPICE_LOG_DOMAIN macro, is currently unused
32 #endif
33 
34 #define SPICE_STRLOC  __FILE__ ":" G_STRINGIFY (__LINE__)
35 
36 void spice_log(GLogLevelFlags log_level,
37                const char *strloc,
38                const char *function,
39                const char *format,
40                ...) G_GNUC_PRINTF(4, 5);
41 
42 /* FIXME: name is misleading, this aborts.. */
43 #define spice_return_if_fail(x) G_STMT_START {                          \
44     if G_LIKELY(x) { } else {                                           \
45         spice_critical("condition `%s' failed", #x);                    \
46         return;                                                         \
47     }                                                                   \
48 } G_STMT_END
49 
50 /* FIXME: name is misleading, this aborts.. */
51 #define spice_return_val_if_fail(x, val) G_STMT_START {                 \
52     if G_LIKELY(x) { } else {                                           \
53         spice_critical("condition `%s' failed", #x);                    \
54         return (val);                                                   \
55     }                                                                   \
56 } G_STMT_END
57 
58 #define spice_warn_if_reached() G_STMT_START {                          \
59     spice_log(G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \
60 } G_STMT_END
61 
62 #define spice_info(...) G_STMT_START {                                  \
63     spice_log(G_LOG_LEVEL_INFO, SPICE_STRLOC, __FUNCTION__, "" __VA_ARGS__); \
64 } G_STMT_END
65 
66 #define spice_debug(...) G_STMT_START {                                 \
67     spice_log(G_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, "" __VA_ARGS__); \
68 } G_STMT_END
69 
70 #define spice_warning(...) G_STMT_START {                               \
71     spice_log(G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "" __VA_ARGS__); \
72 } G_STMT_END
73 
74 #define spice_critical(...) G_STMT_START {                              \
75     spice_log(G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "" __VA_ARGS__); \
76     SPICE_UNREACHABLE;                                                  \
77 } G_STMT_END
78 
79 #define spice_error(...) G_STMT_START {                                 \
80     spice_log(G_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, "" __VA_ARGS__); \
81     SPICE_UNREACHABLE;                                                  \
82 } G_STMT_END
83 
84 #define spice_warn_if_fail(x) G_STMT_START {            \
85     if G_LIKELY(x) { } else {                           \
86         spice_warning("condition `%s' failed", #x);     \
87     }                                                   \
88 } G_STMT_END
89 
90 #define spice_assert(x) G_STMT_START {                  \
91     if G_LIKELY(x) { } else {                           \
92         spice_error("assertion `%s' failed", #x);       \
93     }                                                   \
94 } G_STMT_END
95 
96 #if ENABLE_EXTRA_CHECKS
97 enum { spice_extra_checks = 1 };
98 #else
99 enum { spice_extra_checks = 0 };
100 #endif
101 
102 #define spice_extra_assert(x) G_STMT_START {            \
103     if (!spice_extra_checks || G_LIKELY(x)) { } else {  \
104         spice_error("assertion `%s' failed", #x);       \
105     }                                                   \
106 } G_STMT_END
107 
108 SPICE_END_DECLS
109 
110 #endif // H_SPICE_COMMON_LOG
111