VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/logging.cpp@ 86117

Last change on this file since 86117 was 84501, checked in by vboxsync, 5 years ago

bugref:9637. Splitting logging logging related functions from main.cpp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: logging.cpp 84501 2020-05-25 13:36:55Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions - X11 Client.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#include <stdlib.h>
20#include <iprt/buildconfig.h>
21#include <iprt/file.h>
22#include <iprt/process.h>
23#include <iprt/stream.h>
24#include <iprt/system.h>
25#include <VBox/VBoxGuestLib.h>
26#include <package-generated.h>
27#include "VBoxClient.h"
28
29/** Logging parameters. */
30/** @todo Make this configurable later. */
31static PRTLOGGER g_pLoggerRelease = NULL;
32static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
33static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
34static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
35
36extern unsigned g_cRespawn;
37extern unsigned g_cVerbosity;
38
39/**
40 * Notifies the desktop environment with a message.
41 *
42 * @param pszMessage Message to notify desktop environment with.
43 */
44int vbclLogNotify(const char *pszMessage)
45{
46 AssertPtrReturn(pszMessage, VERR_INVALID_POINTER);
47
48 int rc = VINF_SUCCESS;
49
50 if (g_cRespawn == 0)
51 {
52 char *pszCommand = RTStrAPrintf2("notify-send \"VBoxClient: %s\"", pszMessage);
53 if (pszCommand)
54 {
55 int status = system(pszCommand);
56
57 RTStrFree(pszCommand);
58
59 if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
60 {
61 pszCommand = RTStrAPrintf2("xmessage -buttons OK:0 -center \"VBoxClient: %s\"",
62 pszMessage);
63 if (pszCommand)
64 {
65 status = system(pszCommand);
66 if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
67 {
68 RTPrintf("VBoxClient: %s", pszMessage);
69 }
70
71 RTStrFree(pszCommand);
72 }
73 else
74 rc = VERR_NO_MEMORY;
75 }
76 }
77 else
78 rc = VERR_NO_MEMORY;
79 }
80
81 return rc;
82}
83
84/**
85 * Logs a fatal error, notifies the desktop environment via a message and
86 * exits the application immediately.
87 *
88 * @param pszFormat Format string to log.
89 * @param ... Variable arguments for format string. Optional.
90 */
91void VBClLogFatalError(const char *pszFormat, ...)
92{
93 va_list args;
94 va_start(args, pszFormat);
95 char *psz = NULL;
96 RTStrAPrintfV(&psz, pszFormat, args);
97 va_end(args);
98
99 AssertPtr(psz);
100 LogFlowFunc(("%s", psz));
101 LogRel(("%s", psz));
102
103 vbclLogNotify(psz);
104
105 RTStrFree(psz);
106}
107
108/**
109 * Logs an error message to the (release) logging instance.
110 *
111 * @param pszFormat Format string to log.
112 */
113void VBClLogError(const char *pszFormat, ...)
114{
115 va_list args;
116 va_start(args, pszFormat);
117 char *psz = NULL;
118 RTStrAPrintfV(&psz, pszFormat, args);
119 va_end(args);
120
121 AssertPtr(psz);
122 LogFlowFunc(("%s", psz));
123 LogRel(("%s", psz));
124
125 RTStrFree(psz);
126}
127
128/**
129 * Logs an info message to the (release) logging instance.
130 *
131 * @param pszFormat Format string to log.
132 */
133void VBClLogInfo(const char *pszFormat, ...)
134{
135 va_list args;
136 va_start(args, pszFormat);
137 char *psz = NULL;
138 RTStrAPrintfV(&psz, pszFormat, args);
139 va_end(args);
140
141 AssertPtr(psz);
142 LogFlowFunc(("%s", psz));
143 LogRel(("%s", psz));
144
145 RTStrFree(psz);
146}
147
148/**
149 * @callback_method_impl{FNRTLOGPHASE, Release logger callback}
150 */
151static DECLCALLBACK(void) vbClLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
152{
153 /* Some introductory information. */
154 static RTTIMESPEC s_TimeSpec;
155 char szTmp[256];
156 if (enmPhase == RTLOGPHASE_BEGIN)
157 RTTimeNow(&s_TimeSpec);
158 RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
159
160 switch (enmPhase)
161 {
162 case RTLOGPHASE_BEGIN:
163 {
164 pfnLog(pLoggerRelease,
165 "VBoxClient %s r%s (verbosity: %u) %s (%s %s) release log\n"
166 "Log opened %s\n",
167 RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity, VBOX_BUILD_TARGET,
168 __DATE__, __TIME__, szTmp);
169
170 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
171 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
172 pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
173 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
174 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
175 pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
176 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
177 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
178 pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
179 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
180 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
181 pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
182
183 /* the package type is interesting for Linux distributions */
184 char szExecName[RTPATH_MAX];
185 char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
186 pfnLog(pLoggerRelease,
187 "Executable: %s\n"
188 "Process ID: %u\n"
189 "Package type: %s"
190#ifdef VBOX_OSE
191 " (OSE)"
192#endif
193 "\n",
194 pszExecName ? pszExecName : "unknown",
195 RTProcSelf(),
196 VBOX_PACKAGE_STRING);
197 break;
198 }
199
200 case RTLOGPHASE_PREROTATE:
201 pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
202 break;
203
204 case RTLOGPHASE_POSTROTATE:
205 pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
206 break;
207
208 case RTLOGPHASE_END:
209 pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
210 break;
211
212 default:
213 /* nothing */
214 break;
215 }
216}
217
218/**
219 * Creates the default release logger outputting to the specified file.
220 *
221 * Pass NULL to disabled logging.
222 *
223 * @return IPRT status code.
224 * @param pszLogFile Filename for log output. NULL disables custom handling.
225 */
226int VBClLogCreate(const char *pszLogFile)
227{
228 if (!pszLogFile)
229 return VINF_SUCCESS;
230
231 /* Create release logger (stdout + file). */
232 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
233 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME;
234#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
235 fFlags |= RTLOGFLAGS_USECRLF;
236#endif
237 int rc = RTLogCreateEx(&g_pLoggerRelease, fFlags, "all",
238#ifdef DEBUG
239 "VBOXCLIENT_LOG",
240#else
241 "VBOXCLIENT_RELEASE_LOG",
242#endif
243 RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX /*cMaxEntriesPerGroup*/,
244 RTLOGDEST_STDOUT | RTLOGDEST_USER,
245 vbClLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
246 NULL /*pErrInfo*/, "%s", pszLogFile ? pszLogFile : "");
247 if (RT_SUCCESS(rc))
248 {
249 /* register this logger as the release logger */
250 RTLogRelSetDefaultInstance(g_pLoggerRelease);
251
252 /* Explicitly flush the log in case of VBOXSERVICE_RELEASE_LOG=buffered. */
253 RTLogFlush(g_pLoggerRelease);
254 }
255
256 return rc;
257}
258
259/**
260 * Destroys the currently active logging instance.
261 */
262void VBClLogDestroy(void)
263{
264 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
265}
266
267void foo()
268{
269}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette