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