Debian Codegraph @

lib/wp/log.h

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #ifndef __WIREPLUMBER_LOG_H__
10 #define __WIREPLUMBER_LOG_H__
11
12 #include <glib-object.h>
13 #include "defs.h"
14
15 G_BEGIN_DECLS
16
17 /*!
18 * \brief A custom GLib log level for trace messages (extension of GLogLevelFlags)
19 * \ingroup wplog
20 */
21 static const guint WP_LOG_LEVEL_TRACE = (1 << 8);
22
23 /*
24 The above WP_LOG_LEVEL_TRACE constant is intended to be defined as
25 (1 << G_LOG_LEVEL_USER_SHIFT), but due to a gobject-introspection bug
26 we define it with the value of G_LOG_LEVEL_USER_SHIFT, which is 8, so
27 that it ends up correctly in the bindings. To avoid value mismatches,
28 we statically verify here that G_LOG_LEVEL_USER_SHIFT is indeed 8.
29 See https://gitlab.freedesktop.org/pipewire/wireplumber/-/issues/540
30 */
31 G_STATIC_ASSERT (G_LOG_LEVEL_USER_SHIFT == 8);
32
33 #define WP_OBJECT_FORMAT "<%s:%p>"
34 #define WP_OBJECT_ARGS(object) \
35 (object ? G_OBJECT_TYPE_NAME(object) : "invalid"), object
36
37 WP_PRIVATE_API
38 void wp_log_init (gint flags);
39
40 WP_API
41 gboolean wp_log_set_level (const gchar *log_level);
42
43 /*!
44 * \brief WpLogTopic flags
45 * \ingroup wplog
46 */
47 typedef enum { /*< flags >*/
48 /*! the lower 16 bits of the flags are GLogLevelFlags */
49 WP_LOG_TOPIC_LEVEL_MASK = 0xFFFF,
50 /*! the log topic has infinite lifetime (lives on static storage) */
51 WP_LOG_TOPIC_FLAG_STATIC = 1u << 30,
52 /*! the log topic has been initialized */
53 WP_LOG_TOPIC_FLAG_INITIALIZED = 1u << 31,
54 } WpLogTopicFlags;
55
56 /*!
57 * \brief A structure representing a log topic
58 * \ingroup wplog
59 */
60 typedef struct {
61 const char *topic_name;
62 WpLogTopicFlags flags;
63
64 /*< private >*/
65 WP_PADDING(3)
66 } WpLogTopic;
67
68 #define WP_LOG_TOPIC_EXTERN(var) \
69 extern WpLogTopic * var;
70
71 #define WP_LOG_TOPIC(var, name) \
72 WpLogTopic var##_struct = { .topic_name = name, .flags = WP_LOG_TOPIC_FLAG_STATIC }; \
73 WpLogTopic * var = &(var##_struct);
74
75 #define WP_LOG_TOPIC_STATIC(var, name) \
76 static WpLogTopic var##_struct = { .topic_name = name, .flags = WP_LOG_TOPIC_FLAG_STATIC }; \
77 static G_GNUC_UNUSED WpLogTopic * var = &(var##_struct);
78
79 #define WP_DEFINE_LOCAL_LOG_TOPIC(name) \
80 WP_LOG_TOPIC_STATIC(WP_LOCAL_LOG_TOPIC, name)
81
82 /* make glib log functions also use the local log topic */
83 #ifdef WP_USE_LOCAL_LOG_TOPIC_IN_G_LOG
84 # ifdef G_LOG_DOMAIN
85 # undef G_LOG_DOMAIN
86 # endif
87 # define G_LOG_DOMAIN (WP_LOCAL_LOG_TOPIC->topic_name)
88 #endif
89
90 WP_API
91 void wp_log_topic_init (WpLogTopic *topic);
92
93 WP_API
94 void wp_log_topic_register (WpLogTopic *topic);
95
96 WP_API
97 void wp_log_topic_unregister (WpLogTopic *topic);
98
99 static inline gboolean
100 wp_log_topic_is_initialized (WpLogTopic *topic)
101 {
102 return (topic->flags & WP_LOG_TOPIC_FLAG_INITIALIZED) != 0;
103 }
104
105 static inline gboolean
106 wp_log_topic_is_enabled (WpLogTopic *topic, GLogLevelFlags log_level)
107 {
108 /* first time initialization */
109 if (G_UNLIKELY (!wp_log_topic_is_initialized (topic)))
110 wp_log_topic_init (topic);
111
112 return (topic->flags & log_level & WP_LOG_TOPIC_LEVEL_MASK) != 0;
113 }
114
115 #define wp_local_log_topic_is_enabled(log_level) \
116 (wp_log_topic_is_enabled (WP_LOCAL_LOG_TOPIC, log_level))
117
118 WP_API
119 GLogWriterOutput wp_log_writer_default (GLogLevelFlags log_level,
120 const GLogField *fields, gsize n_fields, gpointer user_data);
121
122 WP_API
123 void wp_log_checked (const gchar *log_topic, GLogLevelFlags log_level,
124 const gchar *file, const gchar *line, const gchar *func,
125 GType object_type, gconstpointer object,
126 const gchar *message_format, ...) G_GNUC_PRINTF (8, 9) G_GNUC_DEPRECATED_FOR (wp_logt_checked);
127
128 WP_API
129 void wp_logt_checked (const WpLogTopic *topic, GLogLevelFlags log_level,
130 const gchar *file, const gchar *line, const gchar *func,
131 GType object_type, gconstpointer object,
132 const gchar *message_format, ...) G_GNUC_PRINTF (8, 9);
133
134 #define wp_log(topic, level, type, object, ...) \
135 ({ \
136 if (G_UNLIKELY (wp_log_topic_is_enabled (topic, level))) \
137 wp_logt_checked (topic, level, __FILE__, G_STRINGIFY (__LINE__), \
138 G_STRFUNC, type, object, __VA_ARGS__); \
139 })
140
141 #define wp_critical(...) \
142 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_CRITICAL, 0, NULL, __VA_ARGS__)
143 #define wp_warning(...) \
144 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_WARNING, 0, NULL, __VA_ARGS__)
145 #define wp_notice(...) \
146 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_MESSAGE, 0, NULL, __VA_ARGS__)
147 #define wp_info(...) \
148 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_INFO, 0, NULL, __VA_ARGS__)
149 #define wp_debug(...) \
150 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_DEBUG, 0, NULL, __VA_ARGS__)
151 #define wp_trace(...) \
152 wp_log (WP_LOCAL_LOG_TOPIC, WP_LOG_LEVEL_TRACE, 0, NULL, __VA_ARGS__)
153
154 #define wp_critical_object(object, ...) \
155 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_CRITICAL, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
156 #define wp_warning_object(object, ...) \
157 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_WARNING, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
158 #define wp_notice_object(object, ...) \
159 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_MESSAGE, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
160 #define wp_info_object(object, ...) \
161 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_INFO, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
162 #define wp_debug_object(object, ...) \
163 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_DEBUG, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
164 #define wp_trace_object(object, ...) \
165 wp_log (WP_LOCAL_LOG_TOPIC, WP_LOG_LEVEL_TRACE, (object) ? G_TYPE_FROM_INSTANCE (object) : G_TYPE_NONE, object, __VA_ARGS__)
166
167 #define wp_critical_boxed(type, object, ...) \
168 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_CRITICAL, type, object, __VA_ARGS__)
169 #define wp_warning_boxed(type, object, ...) \
170 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_WARNING, type, object, __VA_ARGS__)
171 #define wp_notice_boxed(type, object, ...) \
172 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_MESSAGE, type, object, __VA_ARGS__)
173 #define wp_info_boxed(type, object, ...) \
174 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_INFO, type, object, __VA_ARGS__)
175 #define wp_debug_boxed(type, object, ...) \
176 wp_log (WP_LOCAL_LOG_TOPIC, G_LOG_LEVEL_DEBUG, type, object, __VA_ARGS__)
177 #define wp_trace_boxed(type, object, ...) \
178 wp_log (WP_LOCAL_LOG_TOPIC, WP_LOG_LEVEL_TRACE, type, object, __VA_ARGS__)
179
180 struct spa_log;
181
182 WP_API
183 struct spa_log * wp_spa_log_get_instance (void);
184
185 G_END_DECLS
186
187 #endif