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