File: vmtoolsLog.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (829 lines) | stat: -rw-r--r-- 22,971 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*********************************************************
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @file vmtoolsLog.c
 *
 *    Defines a logging infrastructure for the vmtools library based
 *    on glib's logging facilities. Wrap the commonly used logging functions
 *    (Log/Warning/Debug), and provides configurability for where logs should
 *    go to.
 *
 *    To choose the logging domain for your source file, define G_LOG_DOMAIN
 *    before including glib.h.
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib/gstdio.h>
#if defined(G_PLATFORM_WIN32)
#  include <process.h>
#else
#  include <unistd.h>
#endif

#include "vmtools.h"
#include "codeset.h"
#if defined(G_PLATFORM_WIN32)
#  include "coreDump.h"
#endif
#include "file.h"
#include "hostinfo.h"
#include "str.h"
#include "system.h"

#if defined(G_PLATFORM_WIN32)
#  define  DEFAULT_HANDLER    VMToolsLogOutputDebugString
#else
#  define  DEFAULT_HANDLER    VMToolsLogFile
#endif

#define LOGGING_GROUP         "logging"
#define MAX_DOMAIN_LEN        64

/*
 * glib will send log messages to the default handler if there's no
 * handler specified for a given log level. We want to avoid that,
 * and force configuration at each different log domain, so we always
 * register the handlers for all log levels, and filter at the handler
 * function.
 */
#define ALL_LOG_LEVELS        G_LOG_LEVEL_ERROR    |  \
                              G_LOG_LEVEL_CRITICAL |  \
                              G_LOG_LEVEL_WARNING  |  \
                              G_LOG_LEVEL_MESSAGE  |  \
                              G_LOG_LEVEL_INFO     |  \
                              G_LOG_LEVEL_DEBUG

/** Tells whether the given log level is a fatal error. */
#define IS_FATAL(level) ((level) & G_LOG_FLAG_FATAL)

/**
 * Tells whether a message should be logged. All fatal messages are logged,
 * regardless of what the configuration says. Otherwise, the log domain's
 * configuration is respected.
 */
#define SHOULD_LOG(level, data) (IS_FATAL(level) || \
                                 (gLogEnabled && ((data)->mask & (level))))


static void
VMToolsLogFile(const gchar *domain,
               GLogLevelFlags level,
               const gchar *message,
               gpointer _data);

#if defined(G_PLATFORM_WIN32)
static void
VMToolsLogOutputDebugString(const gchar *domain,
                            GLogLevelFlags level,
                            const gchar *message,
                            gpointer _data);
#endif

void VMTools_ResetLogging(gboolean cleanDefault);

typedef struct LogHandlerData {
   gchar            *domain;
   GLogLevelFlags    mask;
   FILE             *file;
   gchar            *path;
   gboolean          append;
   guint             handlerId;
   gboolean          inherited;
   gboolean          error;
} LogHandlerData;

static gchar *gLogDomain = NULL;
static gboolean gEnableCoreDump = TRUE;
static gboolean gLogEnabled = FALSE;
static guint gPanicCount = 0;
static LogHandlerData *gDefaultData = NULL;
static GLogFunc gDefaultLogFunc = DEFAULT_HANDLER;
static GPtrArray *gDomains = NULL;

/* Internal functions. */

/**
 * Opens a log file for writing, backing up the existing log file if one is
 * present. Only one old log file is preserved.
 *
 * @param[in] path   Path to log file.
 * @param[in] append Whether to open the log for appending (if TRUE, a backup
 *                   file is not generated).
 *
 * @return File pointer for writing to the file (NULL on error).
 */

static FILE *
VMToolsLogOpenFile(const gchar *path,
                   gboolean append)
{
   FILE *logfile = NULL;
   gchar *pathLocal;

   ASSERT(path != NULL);
   pathLocal = VMTOOLS_GET_FILENAME_LOCAL(path, NULL);

   if (!append && g_file_test(path, G_FILE_TEST_EXISTS)) {
      /* Back up existing log file. */
      gchar *bakFile = g_strdup_printf("%s.old", pathLocal);
      if (!g_file_test(bakFile, G_FILE_TEST_IS_DIR) &&
          (!g_file_test(bakFile, G_FILE_TEST_EXISTS) ||
           g_unlink(bakFile) == 0)) {
         g_rename(pathLocal, bakFile);
      }
      g_free(bakFile);
   }

   logfile = g_fopen(pathLocal, append ? "a" : "w");
   VMTOOLS_RELEASE_FILENAME_LOCAL(pathLocal);
   return logfile;
}


/**
 * Creates a formatted message to be logged. The format of the message will be:
 *
 *    [timestamp] [domain] [level] Log message
 *
 * @param[in] message      User log message.
 * @param[in] domain       Log domain.
 * @param[in] level        Log level.
 * @param[in] timestamp    Whether to print the timestamp.
 *
 * @return Formatted log message in the current encoding. Should be free()'d.
 */

static char *
VMToolsLogFormat(const gchar *message,
                 const gchar *domain,
                 GLogLevelFlags level,
                 gboolean timestamp)
{
   char *msg = NULL;
   char *msgCurr = NULL;
   char *slevel;

   if (domain == NULL) {
      domain = gLogDomain;
   }

   switch (level & G_LOG_LEVEL_MASK) {
   case G_LOG_LEVEL_ERROR:
      slevel = "error";
      break;

   case G_LOG_LEVEL_CRITICAL:
      slevel = "critical";
      break;

   case G_LOG_LEVEL_WARNING:
      slevel = "warning";
      break;

   case G_LOG_LEVEL_MESSAGE:
      slevel = "message";
      break;

   case G_LOG_LEVEL_INFO:
      slevel = "info";
      break;

   case G_LOG_LEVEL_DEBUG:
      slevel = "debug";
      break;

   default:
      slevel = "unknown";
   }

   if (timestamp) {
      char *tstamp;

      tstamp = System_GetTimeAsString();
      msg = Str_Asprintf(NULL, "[%s] [%8s] [%s] %s\n",
                         (tstamp != NULL) ? tstamp : "no time",
                         slevel, domain, message);
      free(tstamp);
   } else {
      msg = Str_Asprintf(NULL, "[%8s] [%s] %s", slevel, domain, message);
   }

   if (msg != NULL) {
      size_t len;
      CodeSet_Utf8ToCurrent(msg, strlen(msg), &msgCurr, &len);

      /*
       * The log messages from glib itself (and probably other libraries based
       * on glib) do not include a trailing new line. Most of our code does. So
       * we detect whether the original message already had a new line, and
       * remove it, to avoid having two newlines when printing our log messages.
       */
      if (msgCurr != NULL && msgCurr[len - 2] == '\n') {
         msgCurr[len - 1] = '\0';
      }
   }

   if (msgCurr != NULL) {
      free(msg);
      return msgCurr;
   }
   return msg;
}


/**
 * Aborts the program, optionally creating a core dump.
 */

static INLINE NORETURN void
VMToolsLogPanic(void)
{
   if (gEnableCoreDump) {
#if defined(G_PLATFORM_WIN32)
      CoreDump_CoreDump();
#else
      abort();
#endif
   }
   /* Same behavior as Panic_Panic(). */
   exit(-1);
}


#if defined(G_PLATFORM_WIN32)
/**
 * Logs a message to OutputDebugString.
 *
 * @param[in] domain    Log domain.
 * @param[in] level     Log level.
 * @param[in] message   Message to log.
 * @param[in] _data     LogHandlerData pointer.
 */

static void
VMToolsLogOutputDebugString(const gchar *domain,
                            GLogLevelFlags level,
                            const gchar *message,
                            gpointer _data)
{
   LogHandlerData *data = _data;
   if (SHOULD_LOG(level, data)) {
      char *msg = VMToolsLogFormat(message, domain, level, FALSE);
      if (msg != NULL) {
         OutputDebugStringA(msg);
         free(msg);
      }
   }
   if (IS_FATAL(level)) {
      VMToolsLogPanic();
   }
}
#endif


/**
 * Logs a message to a file streams. When writing to the standard streams,
 * any level >= MESSAGE will cause the message to go to stdout; otherwise,
 * it will go to stderr.
 *
 * @param[in] domain    Log domain.
 * @param[in] level     Log level.
 * @param[in] message   Message to log.
 * @param[in] _data     LogHandlerData pointer.
 */

static void
VMToolsLogFile(const gchar *domain,
               GLogLevelFlags level,
               const gchar *message,
               gpointer _data)
{
   LogHandlerData *data = _data;
   if (SHOULD_LOG(level, data)) {
      FILE *dest;
      data = data->inherited ? gDefaultData : data;
      if (!data->error && data->file == NULL && data->path != NULL) {
         data->file = VMToolsLogOpenFile(data->path, data->append);
         if (data->file == NULL) {
            /*
             * glib's documentation says that we can set up log handlers that
             * handle G_LOG_FLAG_RECURSION, but looking at the source code of
             * the g_logv() function that's not really true (at least up to
             * current top of tree - glib 2.20?). So we have to avoid recursion
             * here and bypass the log system.
             */
            gchar warn[1024];
            g_snprintf(warn, sizeof warn,
                       "Unable to open log file %s for domain %s.\n",
                       data->path, data->domain);

            data->error = TRUE;
            DEFAULT_HANDLER(domain, G_LOG_LEVEL_WARNING | G_LOG_FLAG_RECURSION,
                            warn, gDefaultData);
         }
      }
      if (!(level & G_LOG_FLAG_RECURSION) && data->error) {
         DEFAULT_HANDLER(domain, level | G_LOG_FLAG_RECURSION, message, gDefaultData);
      } else {
         char *msg = VMToolsLogFormat(message, domain, level, TRUE);
         if (msg != NULL) {
            dest = (data->file != NULL) ? data->file
                     : ((level < G_LOG_LEVEL_MESSAGE) ? stderr : stdout);
            fputs(msg, dest);
            fflush(dest);
            vm_free(msg);
         }
      }
   }
   if (IS_FATAL(level)) {
      VMToolsLogPanic();
   }
}


/**
 * Configures the given log domain based on the data provided in the given
 * dictionary. If the log domain being configured doesn't match the default
 * (@see VMTools_GetDefaultLogDomain()), and no specific handler is defined
 * for the domain, the handler is inherited from the default domain, instead
 * of using the default handler. This allows reusing the same log file, for
 * example, while maintaining the ability to enable different log levels
 * for different domains.
 *
 * For the above to properly work, the default log domain has to be configured
 * before any other domains.
 *
 * @param[in]  domain      Name of domain being configured.
 * @param[in]  cfg         Dictionary with config data.
 */

static void
VMToolsConfigLogDomain(const gchar *domain,
                       GKeyFile *cfg)
{
   gchar *handler = NULL;
   gchar *level = NULL;
   gchar *logpath = NULL;
   gchar key[128];

   GLogFunc handlerFn = NULL;
   GLogLevelFlags levelsMask;
   LogHandlerData *data;

   /* Arbitrary limit. */
   if (strlen(domain) > MAX_DOMAIN_LEN) {
      g_warning("Domain name too long: %s\n", domain);
      goto exit;
   } else if (strlen(domain) == 0) {
      g_warning("Invalid domain declaration, missing name.\n");
      goto exit;
   }

   Str_Sprintf(key, sizeof key, "%s.level", domain);
   level = g_key_file_get_string(cfg, LOGGING_GROUP, key, NULL);
   if (level == NULL) {
#ifdef VMX86_DEBUG
      level = g_strdup("message");
#else
      level = g_strdup("warning");
#endif
   }

   /* Parse the handler information. */
   Str_Sprintf(key, sizeof key, "%s.handler", domain);
   handler = g_key_file_get_string(cfg, LOGGING_GROUP, key, NULL);

   if (handler == NULL) {
      if (strcmp(domain, VMTools_GetDefaultLogDomain()) == 0) {
         handlerFn = DEFAULT_HANDLER;
      } else {
         handlerFn = gDefaultLogFunc;
      }
   } else if (strcmp(handler, "std") == 0) {
      handlerFn = VMToolsLogFile;
   } else if (strcmp(handler, "file") == 0 ||
              strcmp(handler, "file+") == 0) {
      /* Don't set up the file sink if logging is disabled. */
      if (strcmp(level, "none") != 0) {
         handlerFn = VMToolsLogFile;
         Str_Sprintf(key, sizeof key, "%s.data", domain);
         logpath = g_key_file_get_string(cfg, LOGGING_GROUP, key, NULL);
         if (logpath == NULL) {
            g_warning("Missing log path for file handler (%s).\n", domain);
            goto exit;
         } else {
            /*
             * Do some variable expansion in the input string. Currently only
             * ${USER} and ${PID} are expanded.
             */
            gchar *vars[] = {
               "${USER}",  NULL,
               "${PID}",   NULL
            };
            size_t i;

            vars[1] = Hostinfo_GetUser();
            vars[3] = g_strdup_printf("%"FMTPID, getpid());

            for (i = 0; i < ARRAYSIZE(vars); i += 2) {
               char *last = logpath;
               char *start;
               while ((start = strstr(last, vars[i])) != NULL) {
                  gchar *tmp;
                  char *end = start + strlen(vars[i]);
                  size_t offset = (start - last) + strlen(vars[i+1]);

                  *start = '\0';
                  tmp = g_strdup_printf("%s%s%s", logpath, vars[i+1], end);
                  g_free(logpath);
                  logpath = tmp;
                  last = logpath + offset;
               }
            }

            free(vars[1]);
            g_free(vars[3]);
         }
      }
#if defined(G_PLATFORM_WIN32)
   } else if (strcmp(handler, "outputdebugstring") == 0) {
      handlerFn = VMToolsLogOutputDebugString;
#endif
   } else {
      g_warning("Unknown log handler: %s\n", handler);
      goto exit;
   }

   /* Parse the log level configuration, and build the mask. */
   if (strcmp(level, "error") == 0) {
      levelsMask = G_LOG_LEVEL_ERROR;
   } else if (strcmp(level, "critical") == 0) {
      levelsMask = G_LOG_LEVEL_ERROR |
                   G_LOG_LEVEL_CRITICAL;
   } else if (strcmp(level, "warning") == 0) {
      levelsMask = G_LOG_LEVEL_ERROR |
                   G_LOG_LEVEL_CRITICAL |
                   G_LOG_LEVEL_WARNING;
   } else if (strcmp(level, "message") == 0) {
      levelsMask = G_LOG_LEVEL_ERROR |
                   G_LOG_LEVEL_CRITICAL |
                   G_LOG_LEVEL_WARNING |
                   G_LOG_LEVEL_MESSAGE;
   } else if (strcmp(level, "info") == 0) {
      levelsMask = G_LOG_LEVEL_ERROR |
                   G_LOG_LEVEL_CRITICAL |
                   G_LOG_LEVEL_WARNING |
                   G_LOG_LEVEL_MESSAGE |
                   G_LOG_LEVEL_INFO;
   } else if (strcmp(level, "debug") == 0) {
      levelsMask = ALL_LOG_LEVELS;
   } else if (strcmp(level, "none") == 0) {
      levelsMask = 0;
   } else {
      g_warning("Unknown log level (%s): %s\n", domain, level);
      goto exit;
   }

   data = g_malloc0(sizeof *data);
   data->domain = g_strdup(domain);
   data->mask = levelsMask;
   data->path = logpath;
   data->append = (handler != NULL && strcmp(handler, "file+") == 0);
   logpath = NULL;

   if (strcmp(domain, VMTools_GetDefaultLogDomain()) == 0) {
      /*
       * Replace the global log configuration. If the default log domain was
       * logging to a file and the file path hasn't changed, then keep the old
       * file handle open, instead of rotating the log.
       */
      LogHandlerData *old = gDefaultData;

      if (old->file != NULL) {
         ASSERT(old->path);
         if (data->path != NULL && strcmp(data->path, old->path) == 0) {
            g_free(data->path);
            data->file = old->file;
            data->path = old->path;
            old->path = NULL;
         } else {
            fclose(old->file);
            g_free(old->path);
         }
      }

      g_log_set_default_handler(handlerFn, data);
      gDefaultData = data;
      data = NULL;
      gDefaultLogFunc = handlerFn;
      g_free(old);
   } else if (handler == NULL) {
      ASSERT(data->file == NULL);
      data->inherited = TRUE;
   }

   if (data != NULL) {
      if (gDomains == NULL) {
         gDomains = g_ptr_array_new();
      }
      g_ptr_array_add(gDomains, data);
      data->handlerId = g_log_set_handler(domain, ALL_LOG_LEVELS | G_LOG_FATAL_MASK,
                                          handlerFn, data);
   }

exit:
   g_free(handler);
   g_free(logpath);
   g_free(level);
}


/* Public API. */

/**
 * Returns the default log domain for the application.
 *
 * @return  A string with the name of the log domain.
 */

const char *
VMTools_GetDefaultLogDomain(void)
{
   return gLogDomain;
}


/**
 * Sets the default log domain. This only changes the output of the default
 * log handler.
 *
 * @param[in]  domain   The log domain.
 */

void
VMTools_SetDefaultLogDomain(const gchar *domain)
{
   g_assert(domain != NULL);
   if (gLogDomain != NULL) {
      g_free(gLogDomain);
   }
   gLogDomain = g_strdup(domain);
}


/**
 * Configures the logging system according to the configuration provided from
 * the given dictionary.
 *
 * @param[in] cfg    The configuration data.
 */

void
VMTools_ConfigLogging(GKeyFile *cfg)
{
   gchar **list;
   gchar **curr;

   VMTools_ResetLogging(FALSE);

   if (!g_key_file_has_group(cfg, LOGGING_GROUP)) {
      return;
   }

   /*
    * Configure the default domain first. See function documentation for
    * VMToolsConfigLogDomain() for the reason.
    */
   VMToolsConfigLogDomain(VMTools_GetDefaultLogDomain(), cfg);

   list = g_key_file_get_keys(cfg, LOGGING_GROUP, NULL, NULL);

   for (curr = list; curr != NULL && *curr != NULL; curr++) {
      gchar *domain = *curr;

      /* Check whether it's a domain "declaration". */
      if (!g_str_has_suffix(domain, ".level")) {
         continue;
      }

      /* Trims ".level" from the key to get the domain name. */
      domain[strlen(domain) - 6] = '\0';

      /* Skip the default domain. */
      if (strcmp(domain, VMTools_GetDefaultLogDomain()) == 0) {
         continue;
      }

      VMToolsConfigLogDomain(domain, cfg);
   }

   g_strfreev(list);

   gLogEnabled = g_key_file_get_boolean(cfg, LOGGING_GROUP, "log", NULL);
   if (g_key_file_has_key(cfg, LOGGING_GROUP, "enableCoreDump", NULL)) {
      gEnableCoreDump = g_key_file_get_boolean(cfg, LOGGING_GROUP,
                                               "enableCoreDump", NULL);
   }
}


/**
 * Enables of disables all the log domains configured by the vmtools library.
 * This doesn't affect other log domains that may have configured by other
 * code.
 *
 * @param[in] enable    Whether logging should be enabled.
 */

void
VMTools_EnableLogging(gboolean enable)
{
   gLogEnabled = enable;
}


/**
 * Resets the vmtools logging subsystem, freeing up data and optionally
 * restoring the original glib configuration.
 *
 * @param[in]  cleanDefault   Whether to clean up the default handler and
 *                            restore the original glib handler.
 */

void
VMTools_ResetLogging(gboolean cleanDefault)
{
   gboolean oldLogEnabled = gLogEnabled;
   gchar *currentPath = NULL;
   FILE *currentFile = NULL;

   /* Disable logging while we're playing with the configuration. */
   gLogEnabled = FALSE;

   if (cleanDefault) {
      g_log_set_default_handler(g_log_default_handler, NULL);
   }

   if (gDomains != NULL) {
      guint i;
      for (i = 0; i < gDomains->len; i++) {
         LogHandlerData *data = g_ptr_array_index(gDomains, i);
         g_log_remove_handler(data->domain, data->handlerId);
         if (data->file != NULL) {
            fclose(data->file);
         }
         g_free(data->path);
         g_free(data->domain);
         g_free(data);
      }
      g_ptr_array_free(gDomains, TRUE);
      gDomains = NULL;
   }

   if (gDefaultData != NULL) {
      currentFile = gDefaultData->file;
      currentPath = gDefaultData->path;
      g_free(gDefaultData);
      gDefaultData = NULL;
   }

   if (cleanDefault && gLogDomain != NULL) {
      g_free(gLogDomain);
      gLogDomain = NULL;
   }

   gDefaultLogFunc = DEFAULT_HANDLER;

   if (!cleanDefault) {
      if (gLogDomain == NULL) {
         gLogDomain = g_strdup("vmtools");
      }
      gDefaultData = g_malloc0(sizeof *gDefaultData);
      gDefaultData->mask = G_LOG_LEVEL_ERROR |
                           G_LOG_LEVEL_CRITICAL |
                           G_LOG_LEVEL_WARNING;
#if defined(VMX86_DEBUG)
      gDefaultData->mask |= G_LOG_LEVEL_MESSAGE;
#endif
      gDefaultData->file = currentFile;
      gDefaultData->path = currentPath;
      gLogEnabled = oldLogEnabled;
      g_log_set_default_handler(gDefaultLogFunc, gDefaultData);
   } else {
      if (currentFile != NULL) {
         fclose(currentFile);
      }
      g_free(currentPath);
   }
}


/* Wrappers for VMware's logging functions. */

/**
 * Logs a message using the G_LOG_LEVEL_DEBUG level.
 *
 * @param[in] fmt Log message format.
 */

void
Debug(const char *fmt, ...)
{
   va_list args;
   va_start(args, fmt);
   g_logv(gLogDomain, G_LOG_LEVEL_DEBUG, fmt, args);
   va_end(args);
}


/**
 * Logs a message using the G_LOG_LEVEL_MESSAGE level.
 *
 * @param[in] fmt Log message format.
 */

void
Log(const char *fmt, ...)
{
   /* CoreDump_CoreDump() calls Log(), avoid that message. */
   if (gPanicCount == 0) {
      va_list args;
      va_start(args, fmt);
      g_logv(gLogDomain, G_LOG_LEVEL_MESSAGE, fmt, args);
      va_end(args);
   }
}


/**
 * Logs a message using the G_LOG_LEVEL_ERROR level. In the default
 * configuration, this will cause the application to terminate and,
 * if enabled, to dump core.
 *
 * @param[in] fmt Log message format.
 */

void
Panic(const char *fmt, ...)
{
   va_list args;

   gPanicCount++;
   va_start(args, fmt);
   if (gPanicCount == 1) {
      g_logv(gLogDomain, G_LOG_LEVEL_ERROR, fmt, args);
   } else {
      char *msg;
      g_vasprintf(&msg, fmt, args);
      if (gPanicCount == 2) {
         fprintf(stderr, "Recursive panic: %s\n", msg);
      } else {
         fprintf(stderr, "Recursive panic, giving up: %s\n", msg);
         exit(-1);
      }
      g_free(msg);
   }
   va_end(args);
   /*
    * In case an user-installed custom handler doesn't panic on error, force a
    * core dump. Also force a dump in the recursive case.
    */
   VMToolsLogPanic();
}


/**
 * Logs a message using the G_LOG_LEVEL_WARNING level.
 *
 * @param[in] fmt Log message format.
 */

void
Warning(const char *fmt, ...)
{
   /* CoreDump_CoreDump() may call Warning(), avoid that message. */
   if (gPanicCount == 0) {
      va_list args;
      va_start(args, fmt);
      g_logv(gLogDomain, G_LOG_LEVEL_WARNING, fmt, args);
      va_end(args);
   }
}