open-vm-tools 12.4.0
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
135
136#if !defined(G_LOG_DOMAIN)
137# error "G_LOG_DOMAIN must be defined."
138#endif
139
140#include <glib.h>
142#include "vm_basic_types.h"
143
144#if defined(__GNUC__)
145# define FUNC __func__
146#else
147# define FUNC __FUNCTION__
148#endif
149
150/*
151 *******************************************************************************
152 * g_info -- */
162#if !defined(g_info)
163# define g_info(fmt, ...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, fmt, ## __VA_ARGS__)
164#endif
165
167#ifdef VMX86_DEBUG
168#define VMTOOLS_LOGGING_LEVEL_DEFAULT "info"
169#else
170#define VMTOOLS_LOGGING_LEVEL_DEFAULT "message"
171#endif
172
173
174/*
175 * As of version 2.46, glib thinks the Windows compiler where
176 * _MSC_VER >= 1400 can handle G_HAVE_ISO_VARARGS. This makes our
177 * magic macros wrapping their macros fail in the simplest case,
178 * where only the fmt arg is present (eg vm_debug("test").
179 * vm_debug("test %d", 123) works fine.
180 *
181 * Work around this by making g_debug() et all be inline functions,
182 * which is how it works if G_HAVE_ISO_VARARGS isn't set.
183 *
184 * Though experimentation we found that this also works:
185 *
186 * #define LOGHELPER(...) ,##_VA_ARGS__
187 * #define LOG(fmt, ...) g_debug_macro(__FUNCTION__, ": " fmt, LOGHELPER(__VA_ARGS__))
188 *
189 * but since its disgusting and even more magical, the inline variant was chosen
190 * instead.
191 */
192
193#if defined(_WIN32) && GLIB_CHECK_VERSION(2, 46, 0)
194static inline void
195g_critical_inline(const gchar *fmt,
196 ...)
197{
198 va_list args;
199 va_start(args, fmt);
200 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, fmt, args);
201 va_end(args);
202}
203
204/*
205 *******************************************************************************
206 * vm_{critical,debug,error,info,message,warning} -- */
217#define vm_critical(fmt, ...) g_critical_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
218
219static inline void
220g_debug_inline(const gchar *fmt,
221 ...)
222{
223 va_list args;
224 va_start(args, fmt);
225 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, fmt, args);
226 va_end(args);
227}
228
230#define vm_debug(fmt, ...) g_debug_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
231
232static inline void
233g_error_inline(const gchar *fmt,
234 ...)
235{
236 va_list args;
237 va_start(args, fmt);
238 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, fmt, args);
239 va_end(args);
240}
241
243#define vm_error(fmt, ...) g_error_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
244
245
246static inline void
247g_info_inline(const gchar *fmt,
248 ...)
249{
250 va_list args;
251 va_start(args, fmt);
252 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, fmt, args);
253 va_end(args);
254}
255
257#define vm_info(fmt, ...) g_info_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
258
259static inline void
260g_message_inline(const gchar *fmt,
261 ...)
262{
263 va_list args;
264 va_start(args, fmt);
265 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, args);
266 va_end(args);
267}
268
270#define vm_message(fmt, ...) g_message_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
271
272static inline void
273g_warning_inline(const gchar *fmt,
274 ...)
275{
276 va_list args;
277 va_start(args, fmt);
278 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, fmt, args);
279 va_end(args);
280}
281
283#define vm_warning(fmt, ...) g_warning_inline("%s: " fmt, FUNC, ## __VA_ARGS__)
284
285#else // ! (windows & glib >= 2.46)
286
287/*
288 *******************************************************************************
289 * vm_{critical,debug,error,info,message,warning} -- */
300#define vm_critical(fmt, ...) g_critical("%s: " fmt, FUNC, ## __VA_ARGS__)
301
303#define vm_debug(fmt, ...) g_debug("%s: " fmt, FUNC, ## __VA_ARGS__)
304
306#define vm_error(fmt, ...) g_error("%s: " fmt, FUNC, ## __VA_ARGS__)
307
309#define vm_info(fmt, ...) g_info("%s: " fmt, FUNC, ## __VA_ARGS__)
310
312#define vm_message(fmt, ...) g_message("%s: " fmt, FUNC, ## __VA_ARGS__)
313
315#define vm_warning(fmt, ...) g_warning("%s: " fmt, FUNC, ## __VA_ARGS__)
316#endif // ! (windows & glib >= 2.46)
317
318/* Checks if a string is null before it is passed in logging function */
319#define VM_SAFE_STR(string) (string != NULL ? string : "(NULL)")
320
321G_BEGIN_DECLS
322
323void
324VMTools_ConfigLogToStdio(const gchar *domain);
325
326void
327VMTools_ConfigLogging(const gchar *defaultDomain,
328 GKeyFile *cfg,
329 gboolean force,
330 gboolean reset);
331
332void
333VMTools_UseVmxGuestLog(const gchar *appName);
334
335void
336VMTools_SetupVmxGuestLog(gboolean refreshRpcChannel, GKeyFile *cfg,
337 const gchar *level);
338
339void
341
342typedef enum {
343 TO_HOST,
344 IN_GUEST
345} LogWhere;
346
347void
348VMTools_Log(LogWhere where,
349 GLogLevelFlags level,
350 const gchar *domain,
351 const gchar *fmt,
352 ...);
353
354void
355VMTools_VmxLog(RpcChannel *chan,
356 const gchar *fmt,
357 ...);
358
359void
360VMTools_VmxLogThrottled(uint32 *count,
361 RpcChannel *chan,
362 const gchar *fmt,
363 ...);
364
365G_END_DECLS
366
367#define host_warning(fmt, ...) \
368 VMTools_Log(TO_HOST, G_LOG_LEVEL_WARNING, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
369
370#define guest_warning(fmt, ...) \
371 VMTools_Log(IN_GUEST, G_LOG_LEVEL_WARNING, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
372
373#define host_message(fmt, ...) \
374 VMTools_Log(TO_HOST, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
375
376#define guest_message(fmt, ...) \
377 VMTools_Log(IN_GUEST, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
378
379#define host_info(fmt, ...) \
380 VMTools_Log(TO_HOST, G_LOG_LEVEL_INFO, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
381
382#define guest_info(fmt, ...) \
383 VMTools_Log(IN_GUEST, G_LOG_LEVEL_INFO, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
384
385#define host_debug(fmt, ...) \
386 VMTools_Log(TO_HOST, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
387
388#define guest_debug(fmt, ...) \
389 VMTools_Log(IN_GUEST, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, fmt, ## __VA_ARGS__)
390
392
393#endif /* _VMTOOLS_LOG_H_ */
void VMTools_UseVmxGuestLog(const gchar *appName)
Definition vmtoolsLog.c:2540
G_BEGIN_DECLS void VMTools_ConfigLogToStdio(const gchar *domain)
Definition vmtoolsLog.c:1400
void VMTools_Log(LogWhere where, GLogLevelFlags level, const gchar *domain, const gchar *fmt,...)
Definition vmtoolsLog.c:2776
void VMTools_TeardownVmxGuestLog(void)
Definition vmtoolsLog.c:2662
void VMTools_ConfigLogging(const gchar *defaultDomain, GKeyFile *cfg, gboolean force, gboolean reset)
Definition vmtoolsLog.c:1693
void VMTools_SetupVmxGuestLog(gboolean refreshRpcChannel, GKeyFile *cfg, const gchar *level)
Definition vmtoolsLog.c:2599