1
/*********************************************************
2
* Copyright (C) 2010 VMware, Inc. All rights reserved.
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.
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.
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.
17
*********************************************************/
22
* A very simplified version of a file logger that uses the standard output
23
* streams (stdout / stderr).
26
#include "vmtoolsInt.h"
30
static GStaticMutex gConsoleLock = G_STATIC_MUTEX_INIT;
31
static gint gRefCount = 0;
34
typedef struct StdLoggerData {
35
LogHandlerData handler;
43
******************************************************************************
44
* VMStdLoggerLog -- */ /**
46
* Logs a message to stdout or stderr depending on its severity.
48
* @param[in] domain Unused.
49
* @param[in] level Log level.
50
* @param[in] message Message to log.
51
* @param[in] data Logger data.
52
* @param[in] errfn Unused.
56
******************************************************************************
60
VMStdLoggerLog(const gchar *domain,
66
FILE *dest = (level < G_LOG_LEVEL_MESSAGE) ? stderr : stdout;
69
StdLoggerData *sdata = (StdLoggerData *) data;
71
if (!sdata->attached) {
72
g_static_mutex_lock(&gConsoleLock);
73
if (gRefCount != 0 || VMTools_AttachConsole()) {
75
sdata->attached = TRUE;
77
g_static_mutex_unlock(&gConsoleLock);
80
if (!sdata->attached) {
91
*******************************************************************************
92
* VMStdLoggerDestroy -- */ /**
94
* Cleans up the internal state of the logger.
96
* @param[in] data Logger data.
98
*******************************************************************************
102
VMStdLoggerDestroy(LogHandlerData *data)
105
StdLoggerData *sdata = (StdLoggerData *) data;
106
g_static_mutex_lock(&gConsoleLock);
107
if (sdata->attached && --gRefCount == 0) {
110
g_static_mutex_unlock(&gConsoleLock);
117
******************************************************************************
118
* VMStdLoggerConfig -- */ /**
120
* Configures a new std logger.
122
* @param[in] defaultDomain Unused.
123
* @param[in] domain Name of log domain.
124
* @param[in] name Name of log handler.
125
* @param[in] cfg Configuration data.
127
* @return The std logger data.
129
******************************************************************************
133
VMStdLoggerConfig(const gchar *defaultDomain,
138
StdLoggerData *data = g_new0(StdLoggerData, 1);
139
data->handler.logfn = VMStdLoggerLog;
140
data->handler.convertToLocal = TRUE;
141
data->handler.timestamp = TRUE;
142
data->handler.shared = FALSE;
143
data->handler.copyfn = NULL;
144
data->handler.dtor = VMStdLoggerDestroy;
145
return &data->handler;