open-vm-tools 11.3.5
log.h
Go to the documentation of this file.
1/*********************************************************
2 * Copyright (C) 2011-2021 VMware, Inc. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation version 2.1 and no later version.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11 * License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 *********************************************************/
18
19#ifndef _VMTOOLS_LOG_H_
20#define _VMTOOLS_LOG_H_
21
136#if !defined(G_LOG_DOMAIN)
137# error "G_LOG_DOMAIN must be defined."
138#endif
139
140#include <glib.h>
142
143#if defined(__GNUC__)
144# define FUNC __func__
145#else
146# define FUNC __FUNCTION__
147#endif
148
149/*
150 *******************************************************************************
151 * g_info -- */
161#if !defined(g_info)
162# define g_info(fmt, ...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, fmt, ## __VA_ARGS__)
163#endif
164
166#ifdef VMX86_DEBUG
167#define VMTOOLS_LOGGING_LEVEL_DEFAULT "info"
168#else
169#define VMTOOLS_LOGGING_LEVEL_DEFAULT "message"
170#endif
171
172
173/*
174 * As of version 2.46, glib thinks the Windows compiler where
175 * _MSC_VER >= 1400 can handle G_HAVE_ISO_VARARGS. This makes our
176 * magic macros wrapping their macros fail in the simplest case,
177 * where only the fmt arg is present (eg vm_debug("test").
178 * vm_debug("test %d", 123) works fine.
179 *
180 * Work around this by making g_debug() et all be inline functions,
181 * which is how it works if G_HAVE_ISO_VARARGS isn't set.
182 *
183 * Though experimentation we found that this also works:
184 *
185 * #define LOGHELPER(...) ,##_VA_ARGS__
186 * #define LOG(fmt, ...) g_debug_macro(__FUNCTION__, ": " fmt, LOGHELPER(__VA_ARGS__))
187 *
188 * but since its disgusting and even more magical, the inline variant was chosen
189 * instead.
190 */
191
192#if defined(_WIN32) && GLIB_CHECK_VERSION(2, 46, 0)
193static inline void
194g_critical_inline(const gchar *fmt,
195 ...)
196{
197 va_list args;
198 va_start(args, fmt);
199 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, fmt, args);
200 va_end(args);
201}
202
203/*
204 *******************************************************************************
205 * vm_{critical,debug,error,info,message,warning} -- */
216#define vm_critical(fmt, ...) g_critical_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
217
218static inline void
219g_debug_inline(const gchar *fmt,
220 ...)
221{
222 va_list args;
223 va_start(args, fmt);
224 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, fmt, args);
225 va_end(args);
226}
227
229#define vm_debug(fmt, ...) g_debug_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
230
231static inline void
232g_error_inline(const gchar *fmt,
233 ...)
234{
235 va_list args;
236 va_start(args, fmt);
237 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, fmt, args);
238 va_end(args);
239}
240
242#define vm_error(fmt, ...) g_error_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
243
244
245static inline void
246g_info_inline(const gchar *fmt,
247 ...)
248{
249 va_list args;
250 va_start(args, fmt);
251 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, fmt, args);
252 va_end(args);
253}
254
256#define vm_info(fmt, ...) g_info_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
257
258static inline void
259g_message_inline(const gchar *fmt,
260 ...)
261{
262 va_list args;
263 va_start(args, fmt);
264 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, args);
265 va_end(args);
266}
267
269#define vm_message(fmt, ...) g_message_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
270
271static inline void
272g_warning_inline(const gchar *fmt,
273 ...)
274{
275 va_list args;
276 va_start(args, fmt);
277 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, fmt, args);
278 va_end(args);
279}
280
282#define vm_warning(fmt, ...) g_warning_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
283
284#else // ! (windows & glib >= 2.46)
285
286/*
287 *******************************************************************************
288 * vm_{critical,debug,error,info,message,warning} -- */
299#define vm_critical(fmt, ...) g_critical("%s: " fmt, FUNC, ## __VA_ARGS__)
300
302#define vm_debug(fmt, ...) g_debug("%s: " fmt, FUNC, ## __VA_ARGS__)
303
305#define vm_error(fmt, ...) g_error("%s: " fmt, FUNC, ## __VA_ARGS__)
306
308#define vm_info(fmt, ...) g_info("%s: " fmt, FUNC, ## __VA_ARGS__)
309
311#define vm_message(fmt, ...) g_message("%s: " fmt, FUNC, ## __VA_ARGS__)
312
314#define vm_warning(fmt, ...) g_warning("%s: " fmt, FUNC, ## __VA_ARGS__)
315#endif // ! (windows & glib >= 2.46)
316
317/* Checks if a string is null before it is passed in logging function */
318#define VM_SAFE_STR(string) (string != NULL ? string : "(NULL)")
319
320G_BEGIN_DECLS
321
322void
323VMTools_ConfigLogToStdio(const gchar *domain);
324
325void
326VMTools_ConfigLogging(const gchar *defaultDomain,
327 GKeyFile *cfg,
328 gboolean force,
329 gboolean reset);
330
331void
332VMTools_UseVmxGuestLog(const gchar *appName);
333
334void
335VMTools_SetupVmxGuestLog(gboolean refreshRpcChannel, GKeyFile *cfg,
336 const gchar *level);
337
338void
340
341typedef enum {
342 TO_HOST,
343 IN_GUEST
344} LogWhere;
345
346void
347VMTools_Log(LogWhere where,
348 GLogLevelFlags level,
349 const gchar *domain,
350 const gchar *fmt,
351 ...);
352
353void
354VMTools_VmxLog(RpcChannel *chan,
355 const gchar *fmt,
356 ...);
357
358G_END_DECLS
359
360#define host_warning(fmt, ...) \
361 VMTools_Log(TO_HOST, G_LOG_LEVEL_WARNING, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
362
363#define guest_warning(fmt, ...) \
364 VMTools_Log(IN_GUEST, G_LOG_LEVEL_WARNING, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
365
366#define host_message(fmt, ...) \
367 VMTools_Log(TO_HOST, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
368
369#define guest_message(fmt, ...) \
370 VMTools_Log(IN_GUEST, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
371
372#define host_info(fmt, ...) \
373 VMTools_Log(TO_HOST, G_LOG_LEVEL_INFO, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
374
375#define guest_info(fmt, ...) \
376 VMTools_Log(IN_GUEST, G_LOG_LEVEL_INFO, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
377
378#define host_debug(fmt, ...) \
379 VMTools_Log(TO_HOST, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
380
381#define guest_debug(fmt, ...) \
382 VMTools_Log(IN_GUEST, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
383
386#endif /* _VMTOOLS_LOG_H_ */
void VMTools_UseVmxGuestLog(const gchar *appName)
Definition: vmtoolsLog.c:2471
G_BEGIN_DECLS void VMTools_ConfigLogToStdio(const gchar *domain)
Definition: vmtoolsLog.c:1360
void VMTools_Log(LogWhere where, GLogLevelFlags level, const gchar *domain, const gchar *fmt,...)
Definition: vmtoolsLog.c:2707
void VMTools_TeardownVmxGuestLog(void)
Definition: vmtoolsLog.c:2593
void VMTools_ConfigLogging(const gchar *defaultDomain, GKeyFile *cfg, gboolean force, gboolean reset)
Definition: vmtoolsLog.c:1624
void VMTools_SetupVmxGuestLog(gboolean refreshRpcChannel, GKeyFile *cfg, const gchar *level)
Definition: vmtoolsLog.c:2530