1 | /* $Id: log.cpp 65545 2017-01-31 15:27:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Runtime VBox - Logger.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/log.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #ifndef IN_RC
|
---|
35 | # include <iprt/alloc.h>
|
---|
36 | # include <iprt/process.h>
|
---|
37 | # include <iprt/semaphore.h>
|
---|
38 | # include <iprt/thread.h>
|
---|
39 | # include <iprt/mp.h>
|
---|
40 | #endif
|
---|
41 | #ifdef IN_RING3
|
---|
42 | # include <iprt/env.h>
|
---|
43 | # include <iprt/file.h>
|
---|
44 | # include <iprt/lockvalidator.h>
|
---|
45 | # include <iprt/path.h>
|
---|
46 | #endif
|
---|
47 | #include <iprt/time.h>
|
---|
48 | #include <iprt/asm.h>
|
---|
49 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
50 | # include <iprt/asm-amd64-x86.h>
|
---|
51 | #endif
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/err.h>
|
---|
54 | #include <iprt/param.h>
|
---|
55 |
|
---|
56 | #include <iprt/stdarg.h>
|
---|
57 | #include <iprt/string.h>
|
---|
58 | #include <iprt/ctype.h>
|
---|
59 | #ifdef IN_RING3
|
---|
60 | # include <iprt/alloca.h>
|
---|
61 | # include <stdio.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 |
|
---|
65 | /*********************************************************************************************************************************
|
---|
66 | * Defined Constants And Macros *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 | /** @def RTLOG_RINGBUF_DEFAULT_SIZE
|
---|
69 | * The default ring buffer size. */
|
---|
70 | /** @def RTLOG_RINGBUF_MAX_SIZE
|
---|
71 | * The max ring buffer size. */
|
---|
72 | /** @def RTLOG_RINGBUF_MIN_SIZE
|
---|
73 | * The min ring buffer size. */
|
---|
74 | #ifdef IN_RING0
|
---|
75 | # define RTLOG_RINGBUF_DEFAULT_SIZE _64K
|
---|
76 | # define RTLOG_RINGBUF_MAX_SIZE _4M
|
---|
77 | # define RTLOG_RINGBUF_MIN_SIZE _1K
|
---|
78 | #elif defined(IN_RING3) || defined(DOXYGEN_RUNNING)
|
---|
79 | # define RTLOG_RINGBUF_DEFAULT_SIZE _512K
|
---|
80 | # define RTLOG_RINGBUF_MAX_SIZE _1G
|
---|
81 | # define RTLOG_RINGBUF_MIN_SIZE _4K
|
---|
82 | #endif
|
---|
83 | /** The start of ring buffer eye catcher (16 bytes). */
|
---|
84 | #define RTLOG_RINGBUF_EYE_CATCHER "START RING BUF\0"
|
---|
85 | AssertCompile(sizeof(RTLOG_RINGBUF_EYE_CATCHER) == 16);
|
---|
86 | /** The end of ring buffer eye catcher (16 bytes). This also ensures that the ring buffer
|
---|
87 | * forms are properly terminated C string (leading zero chars). */
|
---|
88 | #define RTLOG_RINGBUF_EYE_CATCHER_END "\0\0\0END RING BUF"
|
---|
89 | AssertCompile(sizeof(RTLOG_RINGBUF_EYE_CATCHER_END) == 16);
|
---|
90 |
|
---|
91 |
|
---|
92 | /*********************************************************************************************************************************
|
---|
93 | * Structures and Typedefs *
|
---|
94 | *********************************************************************************************************************************/
|
---|
95 | /**
|
---|
96 | * Arguments passed to the output function.
|
---|
97 | */
|
---|
98 | typedef struct RTLOGOUTPUTPREFIXEDARGS
|
---|
99 | {
|
---|
100 | /** The logger instance. */
|
---|
101 | PRTLOGGER pLogger;
|
---|
102 | /** The flags. (used for prefixing.) */
|
---|
103 | unsigned fFlags;
|
---|
104 | /** The group. (used for prefixing.) */
|
---|
105 | unsigned iGroup;
|
---|
106 | } RTLOGOUTPUTPREFIXEDARGS, *PRTLOGOUTPUTPREFIXEDARGS;
|
---|
107 |
|
---|
108 | #ifndef IN_RC
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Internal logger data.
|
---|
112 | *
|
---|
113 | * @remarks Don't make casual changes to this structure.
|
---|
114 | */
|
---|
115 | typedef struct RTLOGGERINTERNAL
|
---|
116 | {
|
---|
117 | /** The structure revision (RTLOGGERINTERNAL_REV). */
|
---|
118 | uint32_t uRevision;
|
---|
119 | /** The size of the internal logger structure. */
|
---|
120 | uint32_t cbSelf;
|
---|
121 |
|
---|
122 | /** Spinning mutex semaphore. Can be NIL. */
|
---|
123 | RTSEMSPINMUTEX hSpinMtx;
|
---|
124 | /** Pointer to the flush function. */
|
---|
125 | PFNRTLOGFLUSH pfnFlush;
|
---|
126 |
|
---|
127 | /** Custom prefix callback. */
|
---|
128 | PFNRTLOGPREFIX pfnPrefix;
|
---|
129 | /** Prefix callback argument. */
|
---|
130 | void *pvPrefixUserArg;
|
---|
131 | /** This is set if a prefix is pending. */
|
---|
132 | bool fPendingPrefix;
|
---|
133 | /** Alignment padding. */
|
---|
134 | bool afPadding1[2];
|
---|
135 | /** Set if fully created. Used to avoid confusing in a few functions used to
|
---|
136 | * parse logger settings from environment variables. */
|
---|
137 | bool fCreated;
|
---|
138 |
|
---|
139 | /** The max number of groups that there is room for in afGroups and papszGroups.
|
---|
140 | * Used by RTLogCopyGroupAndFlags(). */
|
---|
141 | uint32_t cMaxGroups;
|
---|
142 | /** Pointer to the group name array.
|
---|
143 | * (The data is readonly and provided by the user.) */
|
---|
144 | const char * const *papszGroups;
|
---|
145 |
|
---|
146 | /** The number of log entries per group. NULL if
|
---|
147 | * RTLOGFLAGS_RESTRICT_GROUPS is not specified. */
|
---|
148 | uint32_t *pacEntriesPerGroup;
|
---|
149 | /** The max number of entries per group. */
|
---|
150 | uint32_t cMaxEntriesPerGroup;
|
---|
151 |
|
---|
152 | /** @name Ring buffer logging
|
---|
153 | * The ring buffer records the last cbRingBuf - 1 of log output. The
|
---|
154 | * other configured log destinations are not touched until someone calls
|
---|
155 | * RTLogFlush(), when the ring buffer content is written to them all.
|
---|
156 | *
|
---|
157 | * The aim here is a fast logging destination, that avoids wasting storage
|
---|
158 | * space saving disk space when dealing with huge log volumes where the
|
---|
159 | * interesting bits usually are found near the end of the log. This is
|
---|
160 | * typically the case for scenarios that crashes or hits assertions.
|
---|
161 | *
|
---|
162 | * RTLogFlush() is called implicitly when hitting an assertion. While on a
|
---|
163 | * crash the most debuggers are able to make calls these days, it's usually
|
---|
164 | * possible to view the ring buffer memory.
|
---|
165 | *
|
---|
166 | * @{ */
|
---|
167 | /** Ring buffer size (including both eye catchers). */
|
---|
168 | uint32_t cbRingBuf;
|
---|
169 | /** Number of bytes passing thru the ring buffer since last RTLogFlush call.
|
---|
170 | * (This is used to avoid writing out the same bytes twice.) */
|
---|
171 | uint64_t volatile cbRingBufUnflushed;
|
---|
172 | /** Ring buffer pointer (points at RTLOG_RINGBUF_EYE_CATCHER). */
|
---|
173 | char *pszRingBuf;
|
---|
174 | /** Current ring buffer position (where to write the next char). */
|
---|
175 | char * volatile pchRingBufCur;
|
---|
176 | /** @} */
|
---|
177 |
|
---|
178 | # ifdef IN_RING3 /* Note! Must be at the end! */
|
---|
179 | /** @name File logging bits for the logger.
|
---|
180 | * @{ */
|
---|
181 | /** Pointer to the function called when starting logging, and when
|
---|
182 | * ending or starting a new log file as part of history rotation.
|
---|
183 | * This can be NULL. */
|
---|
184 | PFNRTLOGPHASE pfnPhase;
|
---|
185 |
|
---|
186 | /** Handle to log file (if open). */
|
---|
187 | RTFILE hFile;
|
---|
188 | /** Log file history settings: maximum amount of data to put in a file. */
|
---|
189 | uint64_t cbHistoryFileMax;
|
---|
190 | /** Log file history settings: current amount of data in a file. */
|
---|
191 | uint64_t cbHistoryFileWritten;
|
---|
192 | /** Log file history settings: maximum time to use a file (in seconds). */
|
---|
193 | uint32_t cSecsHistoryTimeSlot;
|
---|
194 | /** Log file history settings: in what time slot was the file created. */
|
---|
195 | uint32_t uHistoryTimeSlotStart;
|
---|
196 | /** Log file history settings: number of older files to keep.
|
---|
197 | * 0 means no history. */
|
---|
198 | uint32_t cHistory;
|
---|
199 | /** Pointer to filename. */
|
---|
200 | char szFilename[RTPATH_MAX];
|
---|
201 | /** @} */
|
---|
202 | # endif /* IN_RING3 */
|
---|
203 | } RTLOGGERINTERNAL;
|
---|
204 |
|
---|
205 | /** The revision of the internal logger structure. */
|
---|
206 | # define RTLOGGERINTERNAL_REV UINT32_C(10)
|
---|
207 |
|
---|
208 | # ifdef IN_RING3
|
---|
209 | /** The size of the RTLOGGERINTERNAL structure in ring-0. */
|
---|
210 | # define RTLOGGERINTERNAL_R0_SIZE RT_OFFSETOF(RTLOGGERINTERNAL, pfnPhase)
|
---|
211 | AssertCompileMemberAlignment(RTLOGGERINTERNAL, hFile, sizeof(void *));
|
---|
212 | AssertCompileMemberAlignment(RTLOGGERINTERNAL, cbHistoryFileMax, sizeof(uint64_t));
|
---|
213 | # endif
|
---|
214 | AssertCompileMemberAlignment(RTLOGGERINTERNAL, cbRingBufUnflushed, sizeof(uint64_t));
|
---|
215 |
|
---|
216 | #endif /* !IN_RC */
|
---|
217 |
|
---|
218 |
|
---|
219 | /*********************************************************************************************************************************
|
---|
220 | * Internal Functions *
|
---|
221 | *********************************************************************************************************************************/
|
---|
222 | #ifndef IN_RC
|
---|
223 | static unsigned rtlogGroupFlags(const char *psz);
|
---|
224 | #endif
|
---|
225 | #ifdef IN_RING0
|
---|
226 | static void rtR0LogLoggerExFallback(uint32_t fDestFlags, uint32_t fFlags, PRTLOGGERINTERNAL pInt,
|
---|
227 | const char *pszFormat, va_list va);
|
---|
228 | #endif
|
---|
229 | #ifdef IN_RING3
|
---|
230 | static int rtlogFileOpen(PRTLOGGER pLogger, char *pszErrorMsg, size_t cchErrorMsg);
|
---|
231 | static void rtlogRotate(PRTLOGGER pLogger, uint32_t uTimeSlot, bool fFirst);
|
---|
232 | #endif
|
---|
233 | #ifndef IN_RC
|
---|
234 | static void rtLogRingBufFlush(PRTLOGGER pLogger);
|
---|
235 | #endif
|
---|
236 | static void rtlogFlush(PRTLOGGER pLogger);
|
---|
237 | static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars);
|
---|
238 | static DECLCALLBACK(size_t) rtLogOutputPrefixed(void *pv, const char *pachChars, size_t cbChars);
|
---|
239 | static void rtlogLoggerExVLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args);
|
---|
240 | #ifndef IN_RC
|
---|
241 | static void rtlogLoggerExFLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, ...);
|
---|
242 | #endif
|
---|
243 |
|
---|
244 |
|
---|
245 | /*********************************************************************************************************************************
|
---|
246 | * Global Variables *
|
---|
247 | *********************************************************************************************************************************/
|
---|
248 | #ifdef IN_RC
|
---|
249 | /** Default logger instance. Make it weak because our RC module loader does not
|
---|
250 | * necessarily resolve this symbol and the compiler _must_ check if this is
|
---|
251 | * the case or not. That doesn't work for Darwin (``incompatible feature used:
|
---|
252 | * .weak_reference (must specify "-dynamic" to be used'') */
|
---|
253 | # ifdef RT_OS_DARWIN
|
---|
254 | extern "C" DECLIMPORT(RTLOGGERRC) g_Logger;
|
---|
255 | # else
|
---|
256 | extern "C" DECLWEAK(DECLIMPORT(RTLOGGERRC)) g_Logger;
|
---|
257 | # endif
|
---|
258 | #else /* !IN_RC */
|
---|
259 | /** Default logger instance. */
|
---|
260 | static PRTLOGGER g_pLogger;
|
---|
261 | #endif /* !IN_RC */
|
---|
262 | #ifdef IN_RING3
|
---|
263 | /** The RTThreadGetWriteLockCount() change caused by the logger mutex semaphore. */
|
---|
264 | static uint32_t volatile g_cLoggerLockCount;
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | #ifdef IN_RING0
|
---|
268 | /** Number of per-thread loggers. */
|
---|
269 | static int32_t volatile g_cPerThreadLoggers;
|
---|
270 | /** Per-thread loggers.
|
---|
271 | * This is just a quick TLS hack suitable for debug logging only.
|
---|
272 | * If we run out of entries, just unload and reload the driver. */
|
---|
273 | static struct RTLOGGERPERTHREAD
|
---|
274 | {
|
---|
275 | /** The thread. */
|
---|
276 | RTNATIVETHREAD volatile NativeThread;
|
---|
277 | /** The (process / session) key. */
|
---|
278 | uintptr_t volatile uKey;
|
---|
279 | /** The logger instance.*/
|
---|
280 | PRTLOGGER volatile pLogger;
|
---|
281 | } g_aPerThreadLoggers[8] =
|
---|
282 | {
|
---|
283 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
284 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
285 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
286 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
287 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
288 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
289 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
290 | { NIL_RTNATIVETHREAD, 0, 0}
|
---|
291 | };
|
---|
292 | #endif /* IN_RING0 */
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Logger flags instructions.
|
---|
296 | */
|
---|
297 | static struct
|
---|
298 | {
|
---|
299 | const char *pszInstr; /**< The name */
|
---|
300 | size_t cchInstr; /**< The size of the name. */
|
---|
301 | uint32_t fFlag; /**< The flag value. */
|
---|
302 | bool fInverted; /**< Inverse meaning? */
|
---|
303 | } const g_aLogFlags[] =
|
---|
304 | {
|
---|
305 | { "disabled", sizeof("disabled" ) - 1, RTLOGFLAGS_DISABLED, false },
|
---|
306 | { "enabled", sizeof("enabled" ) - 1, RTLOGFLAGS_DISABLED, true },
|
---|
307 | { "buffered", sizeof("buffered" ) - 1, RTLOGFLAGS_BUFFERED, false },
|
---|
308 | { "unbuffered", sizeof("unbuffered" ) - 1, RTLOGFLAGS_BUFFERED, true },
|
---|
309 | { "usecrlf", sizeof("usecrlf" ) - 1, RTLOGFLAGS_USECRLF, false },
|
---|
310 | { "uself", sizeof("uself" ) - 1, RTLOGFLAGS_USECRLF, true },
|
---|
311 | { "append", sizeof("append" ) - 1, RTLOGFLAGS_APPEND, false },
|
---|
312 | { "overwrite", sizeof("overwrite" ) - 1, RTLOGFLAGS_APPEND, true },
|
---|
313 | { "rel", sizeof("rel" ) - 1, RTLOGFLAGS_REL_TS, false },
|
---|
314 | { "abs", sizeof("abs" ) - 1, RTLOGFLAGS_REL_TS, true },
|
---|
315 | { "dec", sizeof("dec" ) - 1, RTLOGFLAGS_DECIMAL_TS, false },
|
---|
316 | { "hex", sizeof("hex" ) - 1, RTLOGFLAGS_DECIMAL_TS, true },
|
---|
317 | { "writethru", sizeof("writethru" ) - 1, RTLOGFLAGS_WRITE_THROUGH, false },
|
---|
318 | { "writethrough", sizeof("writethrough") - 1, RTLOGFLAGS_WRITE_THROUGH, false },
|
---|
319 | { "flush", sizeof("flush" ) - 1, RTLOGFLAGS_FLUSH, false },
|
---|
320 | { "lockcnts", sizeof("lockcnts" ) - 1, RTLOGFLAGS_PREFIX_LOCK_COUNTS, false },
|
---|
321 | { "cpuid", sizeof("cpuid" ) - 1, RTLOGFLAGS_PREFIX_CPUID, false },
|
---|
322 | { "pid", sizeof("pid" ) - 1, RTLOGFLAGS_PREFIX_PID, false },
|
---|
323 | { "flagno", sizeof("flagno" ) - 1, RTLOGFLAGS_PREFIX_FLAG_NO, false },
|
---|
324 | { "flag", sizeof("flag" ) - 1, RTLOGFLAGS_PREFIX_FLAG, false },
|
---|
325 | { "groupno", sizeof("groupno" ) - 1, RTLOGFLAGS_PREFIX_GROUP_NO, false },
|
---|
326 | { "group", sizeof("group" ) - 1, RTLOGFLAGS_PREFIX_GROUP, false },
|
---|
327 | { "tid", sizeof("tid" ) - 1, RTLOGFLAGS_PREFIX_TID, false },
|
---|
328 | { "thread", sizeof("thread" ) - 1, RTLOGFLAGS_PREFIX_THREAD, false },
|
---|
329 | { "custom", sizeof("custom" ) - 1, RTLOGFLAGS_PREFIX_CUSTOM, false },
|
---|
330 | { "timeprog", sizeof("timeprog" ) - 1, RTLOGFLAGS_PREFIX_TIME_PROG, false },
|
---|
331 | { "time", sizeof("time" ) - 1, RTLOGFLAGS_PREFIX_TIME, false },
|
---|
332 | { "msprog", sizeof("msprog" ) - 1, RTLOGFLAGS_PREFIX_MS_PROG, false },
|
---|
333 | { "tsc", sizeof("tsc" ) - 1, RTLOGFLAGS_PREFIX_TSC, false }, /* before ts! */
|
---|
334 | { "ts", sizeof("ts" ) - 1, RTLOGFLAGS_PREFIX_TS, false },
|
---|
335 | /* We intentionally omit RTLOGFLAGS_RESTRICT_GROUPS. */
|
---|
336 | };
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Logger destination instructions.
|
---|
340 | */
|
---|
341 | static struct
|
---|
342 | {
|
---|
343 | const char *pszInstr; /**< The name. */
|
---|
344 | size_t cchInstr; /**< The size of the name. */
|
---|
345 | uint32_t fFlag; /**< The corresponding destination flag. */
|
---|
346 | } const g_aLogDst[] =
|
---|
347 | {
|
---|
348 | { RT_STR_TUPLE("file"), RTLOGDEST_FILE }, /* Must be 1st! */
|
---|
349 | { RT_STR_TUPLE("dir"), RTLOGDEST_FILE }, /* Must be 2nd! */
|
---|
350 | { RT_STR_TUPLE("history"), 0 }, /* Must be 3rd! */
|
---|
351 | { RT_STR_TUPLE("histsize"), 0 }, /* Must be 4th! */
|
---|
352 | { RT_STR_TUPLE("histtime"), 0 }, /* Must be 5th! */
|
---|
353 | { RT_STR_TUPLE("ringbuf"), RTLOGDEST_RINGBUF }, /* Must be 6th! */
|
---|
354 | { RT_STR_TUPLE("stdout"), RTLOGDEST_STDOUT },
|
---|
355 | { RT_STR_TUPLE("stderr"), RTLOGDEST_STDERR },
|
---|
356 | { RT_STR_TUPLE("debugger"), RTLOGDEST_DEBUGGER },
|
---|
357 | { RT_STR_TUPLE("com"), RTLOGDEST_COM },
|
---|
358 | { RT_STR_TUPLE("user"), RTLOGDEST_USER },
|
---|
359 | };
|
---|
360 |
|
---|
361 | #ifdef IN_RING3
|
---|
362 | /** Log rotation backoff table - millisecond sleep intervals.
|
---|
363 | * Important on Windows host, especially for VBoxSVC release logging. Only a
|
---|
364 | * medium term solution, until a proper fix for log file handling is available.
|
---|
365 | * 10 seconds total.
|
---|
366 | */
|
---|
367 | static const uint32_t g_acMsLogBackoff[] =
|
---|
368 | { 10, 10, 10, 20, 50, 100, 200, 200, 200, 200, 500, 500, 500, 500, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };
|
---|
369 | #endif
|
---|
370 |
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Locks the logger instance.
|
---|
374 | *
|
---|
375 | * @returns See RTSemSpinMutexRequest().
|
---|
376 | * @param pLogger The logger instance.
|
---|
377 | */
|
---|
378 | DECLINLINE(int) rtlogLock(PRTLOGGER pLogger)
|
---|
379 | {
|
---|
380 | #ifndef IN_RC
|
---|
381 | PRTLOGGERINTERNAL pInt = pLogger->pInt;
|
---|
382 | AssertMsgReturn(pInt->uRevision == RTLOGGERINTERNAL_REV, ("%#x != %#x\n", pInt->uRevision, RTLOGGERINTERNAL_REV),
|
---|
383 | VERR_LOG_REVISION_MISMATCH);
|
---|
384 | AssertMsgReturn(pInt->cbSelf == sizeof(*pInt), ("%#x != %#x\n", pInt->cbSelf, sizeof(*pInt)),
|
---|
385 | VERR_LOG_REVISION_MISMATCH);
|
---|
386 | if (pInt->hSpinMtx != NIL_RTSEMSPINMUTEX)
|
---|
387 | {
|
---|
388 | int rc = RTSemSpinMutexRequest(pInt->hSpinMtx);
|
---|
389 | if (RT_FAILURE(rc))
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 | #else
|
---|
393 | NOREF(pLogger);
|
---|
394 | #endif
|
---|
395 | return VINF_SUCCESS;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Unlocks the logger instance.
|
---|
401 | * @param pLogger The logger instance.
|
---|
402 | */
|
---|
403 | DECLINLINE(void) rtlogUnlock(PRTLOGGER pLogger)
|
---|
404 | {
|
---|
405 | #ifndef IN_RC
|
---|
406 | if (pLogger->pInt->hSpinMtx != NIL_RTSEMSPINMUTEX)
|
---|
407 | RTSemSpinMutexRelease(pLogger->pInt->hSpinMtx);
|
---|
408 | #else
|
---|
409 | NOREF(pLogger);
|
---|
410 | #endif
|
---|
411 | return;
|
---|
412 | }
|
---|
413 |
|
---|
414 | #ifndef IN_RC
|
---|
415 | # ifdef IN_RING3
|
---|
416 |
|
---|
417 | # ifdef SOME_UNUSED_FUNCTION
|
---|
418 | /**
|
---|
419 | * Logging to file, output callback.
|
---|
420 | *
|
---|
421 | * @param pvArg User argument.
|
---|
422 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
423 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
424 | */
|
---|
425 | static DECLCALLBACK(size_t) rtlogPhaseWrite(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
426 | {
|
---|
427 | PRTLOGGER pLogger = (PRTLOGGER)pvArg;
|
---|
428 | RTFileWrite(pLogger->pInt->hFile, pachChars, cbChars, NULL);
|
---|
429 | return cbChars;
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Callback to format VBox formatting extentions.
|
---|
435 | * See @ref pg_rt_str_format for a reference on the format types.
|
---|
436 | *
|
---|
437 | * @returns The number of bytes formatted.
|
---|
438 | * @param pvArg Formatter argument.
|
---|
439 | * @param pfnOutput Pointer to output function.
|
---|
440 | * @param pvArgOutput Argument for the output function.
|
---|
441 | * @param ppszFormat Pointer to the format string pointer. Advance this till the char
|
---|
442 | * after the format specifier.
|
---|
443 | * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
|
---|
444 | * @param cchWidth Format Width. -1 if not specified.
|
---|
445 | * @param cchPrecision Format Precision. -1 if not specified.
|
---|
446 | * @param fFlags Flags (RTSTR_NTFS_*).
|
---|
447 | * @param chArgSize The argument size specifier, 'l' or 'L'.
|
---|
448 | */
|
---|
449 | static DECLCALLBACK(size_t) rtlogPhaseFormatStr(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
450 | const char **ppszFormat, va_list *pArgs, int cchWidth,
|
---|
451 | int cchPrecision, unsigned fFlags, char chArgSize)
|
---|
452 | {
|
---|
453 | char ch = *(*ppszFormat)++;
|
---|
454 |
|
---|
455 | AssertMsgFailed(("Invalid logger phase format type '%%%c%.10s'!\n", ch, *ppszFormat)); NOREF(ch);
|
---|
456 |
|
---|
457 | return 0;
|
---|
458 | }
|
---|
459 |
|
---|
460 | # endif /* SOME_UNUSED_FUNCTION */
|
---|
461 |
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Log phase callback function, assumes the lock is already held
|
---|
465 | *
|
---|
466 | * @param pLogger The logger instance.
|
---|
467 | * @param pszFormat Format string.
|
---|
468 | * @param ... Optional arguments as specified in the format string.
|
---|
469 | */
|
---|
470 | static DECLCALLBACK(void) rtlogPhaseMsgLocked(PRTLOGGER pLogger, const char *pszFormat, ...)
|
---|
471 | {
|
---|
472 | va_list args;
|
---|
473 | AssertPtrReturnVoid(pLogger);
|
---|
474 | AssertPtrReturnVoid(pLogger->pInt);
|
---|
475 | Assert(pLogger->pInt->hSpinMtx != NIL_RTSEMSPINMUTEX);
|
---|
476 |
|
---|
477 | va_start(args, pszFormat);
|
---|
478 | rtlogLoggerExVLocked(pLogger, 0, ~0U, pszFormat, args);
|
---|
479 | va_end(args);
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Log phase callback function, assumes the lock is not held.
|
---|
485 | *
|
---|
486 | * @param pLogger The logger instance.
|
---|
487 | * @param pszFormat Format string.
|
---|
488 | * @param ... Optional arguments as specified in the format string.
|
---|
489 | */
|
---|
490 | static DECLCALLBACK(void) rtlogPhaseMsgNormal(PRTLOGGER pLogger, const char *pszFormat, ...)
|
---|
491 | {
|
---|
492 | va_list args;
|
---|
493 | AssertPtrReturnVoid(pLogger);
|
---|
494 | AssertPtrReturnVoid(pLogger->pInt);
|
---|
495 | Assert(pLogger->pInt->hSpinMtx != NIL_RTSEMSPINMUTEX);
|
---|
496 |
|
---|
497 | va_start(args, pszFormat);
|
---|
498 | RTLogLoggerExV(pLogger, 0, ~0U, pszFormat, args);
|
---|
499 | va_end(args);
|
---|
500 | }
|
---|
501 |
|
---|
502 | # endif /* IN_RING3 */
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * Adjusts the ring buffer.
|
---|
506 | *
|
---|
507 | * @returns IPRT status code.
|
---|
508 | * @param pLogger The logger instance.
|
---|
509 | * @param cbNewSize The new ring buffer size (0 == default).
|
---|
510 | * @param fForce Whether to do this even if the logger instance hasn't
|
---|
511 | * really been fully created yet (i.e. during RTLogCreate).
|
---|
512 | */
|
---|
513 | static int rtLogRingBufAdjust(PRTLOGGER pLogger, uint32_t cbNewSize, bool fForce)
|
---|
514 | {
|
---|
515 | /*
|
---|
516 | * If this is early logger init, don't do anything.
|
---|
517 | */
|
---|
518 | if (!pLogger->pInt->fCreated && !fForce)
|
---|
519 | return VINF_SUCCESS;
|
---|
520 |
|
---|
521 | /*
|
---|
522 | * Lock the logger and make the necessary changes.
|
---|
523 | */
|
---|
524 | int rc = rtlogLock(pLogger);
|
---|
525 | if (RT_SUCCESS(rc))
|
---|
526 | {
|
---|
527 | if (cbNewSize == 0)
|
---|
528 | cbNewSize = RTLOG_RINGBUF_DEFAULT_SIZE;
|
---|
529 | if ( pLogger->pInt->cbRingBuf != cbNewSize
|
---|
530 | || !pLogger->pInt->pchRingBufCur)
|
---|
531 | {
|
---|
532 | uintptr_t offOld = pLogger->pInt->pchRingBufCur - pLogger->pInt->pszRingBuf;
|
---|
533 | if (offOld < sizeof(RTLOG_RINGBUF_EYE_CATCHER))
|
---|
534 | offOld = sizeof(RTLOG_RINGBUF_EYE_CATCHER);
|
---|
535 | else if (offOld >= cbNewSize)
|
---|
536 | {
|
---|
537 | memmove(pLogger->pInt->pszRingBuf, &pLogger->pInt->pszRingBuf[offOld - cbNewSize], cbNewSize);
|
---|
538 | offOld = sizeof(RTLOG_RINGBUF_EYE_CATCHER);
|
---|
539 | }
|
---|
540 |
|
---|
541 | void *pvNew = RTMemRealloc(pLogger->pInt->pchRingBufCur, cbNewSize);
|
---|
542 | if (pvNew)
|
---|
543 | {
|
---|
544 | pLogger->pInt->pszRingBuf = (char *)pvNew;
|
---|
545 | pLogger->pInt->pchRingBufCur = (char *)pvNew + offOld;
|
---|
546 | pLogger->pInt->cbRingBuf = cbNewSize;
|
---|
547 | memcpy(pvNew, RTLOG_RINGBUF_EYE_CATCHER, sizeof(RTLOG_RINGBUF_EYE_CATCHER));
|
---|
548 | memcpy((char *)pvNew + cbNewSize - sizeof(RTLOG_RINGBUF_EYE_CATCHER_END),
|
---|
549 | RTLOG_RINGBUF_EYE_CATCHER_END, sizeof(RTLOG_RINGBUF_EYE_CATCHER_END));
|
---|
550 | rc = VINF_SUCCESS;
|
---|
551 | }
|
---|
552 | else
|
---|
553 | rc = VERR_NO_MEMORY;
|
---|
554 | }
|
---|
555 | rtlogUnlock(pLogger);
|
---|
556 | }
|
---|
557 |
|
---|
558 | return rc;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Writes text to the ring buffer.
|
---|
564 | *
|
---|
565 | * @param pInt The internal logger data structure.
|
---|
566 | * @param pachText The text to write.
|
---|
567 | * @param cchText The number of chars (bytes) to write.
|
---|
568 | */
|
---|
569 | static void rtLogRingBufWrite(PRTLOGGERINTERNAL pInt, const char *pachText, size_t cchText)
|
---|
570 | {
|
---|
571 | /*
|
---|
572 | * Get the ring buffer data, adjusting it to only describe the writable
|
---|
573 | * part of the buffer.
|
---|
574 | */
|
---|
575 | char * const pchStart = &pInt->pszRingBuf[sizeof(RTLOG_RINGBUF_EYE_CATCHER)];
|
---|
576 | size_t const cchBuf = pInt->cbRingBuf - sizeof(RTLOG_RINGBUF_EYE_CATCHER) - sizeof(RTLOG_RINGBUF_EYE_CATCHER_END);
|
---|
577 | char *pchCur = pInt->pchRingBufCur;
|
---|
578 | size_t cchLeft = pchCur - pchStart;
|
---|
579 | if (RT_LIKELY(cchLeft < cchBuf))
|
---|
580 | cchLeft = cchBuf - cchLeft;
|
---|
581 | else
|
---|
582 | {
|
---|
583 | /* May happen in ring-0 where a thread or two went ahead without getting the lock. */
|
---|
584 | pchCur = pchStart;
|
---|
585 | cchLeft = cchBuf;
|
---|
586 | }
|
---|
587 | Assert(cchBuf < pInt->cbRingBuf);
|
---|
588 |
|
---|
589 | if (cchText < cchLeft)
|
---|
590 | {
|
---|
591 | /*
|
---|
592 | * The text fits in the remaining space.
|
---|
593 | */
|
---|
594 | memcpy(pchCur, pachText, cchText);
|
---|
595 | pchCur[cchText] = '\0';
|
---|
596 | pInt->pchRingBufCur = &pchCur[cchText];
|
---|
597 | pInt->cbRingBufUnflushed += cchText;
|
---|
598 | }
|
---|
599 | else
|
---|
600 | {
|
---|
601 | /*
|
---|
602 | * The text wraps around. Taking the simple but inefficient approach
|
---|
603 | * to input texts that are longer than the ring buffer since that
|
---|
604 | * is unlikely to the be a frequent case.
|
---|
605 | */
|
---|
606 | /* Fill to the end of the buffer. */
|
---|
607 | memcpy(pchCur, pachText, cchLeft);
|
---|
608 | pachText += cchLeft;
|
---|
609 | cchText -= cchLeft;
|
---|
610 | pInt->cbRingBufUnflushed += cchLeft;
|
---|
611 | pInt->pchRingBufCur = pchStart;
|
---|
612 |
|
---|
613 | /* Ring buffer overflows (the plainly inefficient bit). */
|
---|
614 | while (cchText >= cchBuf)
|
---|
615 | {
|
---|
616 | memcpy(pchStart, pachText, cchBuf);
|
---|
617 | pachText += cchBuf;
|
---|
618 | cchText -= cchBuf;
|
---|
619 | pInt->cbRingBufUnflushed += cchBuf;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /* The final bit, if any. */
|
---|
623 | if (cchText > 0)
|
---|
624 | {
|
---|
625 | memcpy(pchStart, pachText, cchText);
|
---|
626 | pInt->cbRingBufUnflushed += cchText;
|
---|
627 | }
|
---|
628 | pchStart[cchText] = '\0';
|
---|
629 | pInt->pchRingBufCur = &pchStart[cchText];
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | /**
|
---|
635 | * Flushes the ring buffer to all the other log destinations.
|
---|
636 | *
|
---|
637 | * @param pLogger The logger instance which ring buffer should be flushed.
|
---|
638 | */
|
---|
639 | static void rtLogRingBufFlush(PRTLOGGER pLogger)
|
---|
640 | {
|
---|
641 | const char *pszPreamble;
|
---|
642 | size_t cchPreamble;
|
---|
643 | const char *pszFirst;
|
---|
644 | size_t cchFirst;
|
---|
645 | const char *pszSecond;
|
---|
646 | size_t cchSecond;
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Get the ring buffer data, adjusting it to only describe the writable
|
---|
650 | * part of the buffer.
|
---|
651 | */
|
---|
652 | uint64_t cchUnflushed = pLogger->pInt->cbRingBufUnflushed;
|
---|
653 | char * const pszBuf = &pLogger->pInt->pszRingBuf[sizeof(RTLOG_RINGBUF_EYE_CATCHER)];
|
---|
654 | size_t const cchBuf = pLogger->pInt->cbRingBuf - sizeof(RTLOG_RINGBUF_EYE_CATCHER) - sizeof(RTLOG_RINGBUF_EYE_CATCHER_END);
|
---|
655 | size_t offCur = pLogger->pInt->pchRingBufCur - pszBuf;
|
---|
656 | size_t cchAfter;
|
---|
657 | if (RT_LIKELY(offCur < cchBuf))
|
---|
658 | cchAfter = cchBuf - offCur;
|
---|
659 | else /* May happen in ring-0 where a thread or two went ahead without getting the lock. */
|
---|
660 | {
|
---|
661 | offCur = 0;
|
---|
662 | cchAfter = cchBuf;
|
---|
663 | }
|
---|
664 |
|
---|
665 | pLogger->pInt->cbRingBufUnflushed = 0;
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * Figure out whether there are one or two segments that needs writing,
|
---|
669 | * making the last segment is terminated. (The first is always
|
---|
670 | * terminated because of the eye-catcher at the end of the buffer.)
|
---|
671 | */
|
---|
672 | if (cchUnflushed == 0)
|
---|
673 | return;
|
---|
674 | pszBuf[offCur] = '\0';
|
---|
675 | if (cchUnflushed >= cchBuf)
|
---|
676 | {
|
---|
677 | pszFirst = &pszBuf[offCur + 1];
|
---|
678 | cchFirst = cchAfter ? cchAfter - 1 : 0;
|
---|
679 | pszSecond = pszBuf;
|
---|
680 | cchSecond = offCur;
|
---|
681 | pszPreamble = "\n*FLUSH RING BUF*\n";
|
---|
682 | cchPreamble = sizeof("\n*FLUSH RING BUF*\n") - 1;
|
---|
683 | }
|
---|
684 | else if ((size_t)cchUnflushed <= offCur)
|
---|
685 | {
|
---|
686 | cchFirst = (size_t)cchUnflushed;
|
---|
687 | pszFirst = &pszBuf[offCur - cchFirst];
|
---|
688 | pszSecond = "";
|
---|
689 | cchSecond = 0;
|
---|
690 | pszPreamble = "";
|
---|
691 | cchPreamble = 0;
|
---|
692 | }
|
---|
693 | else
|
---|
694 | {
|
---|
695 | cchFirst = (size_t)cchUnflushed - offCur;
|
---|
696 | pszFirst = &pszBuf[cchBuf - cchFirst];
|
---|
697 | pszSecond = pszBuf;
|
---|
698 | cchSecond = offCur;
|
---|
699 | pszPreamble = "";
|
---|
700 | cchPreamble = 0;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Write the ring buffer to all other destiations.
|
---|
705 | */
|
---|
706 | if (pLogger->fDestFlags & RTLOGDEST_USER)
|
---|
707 | {
|
---|
708 | if (cchPreamble)
|
---|
709 | RTLogWriteUser(pszPreamble, cchPreamble);
|
---|
710 | if (cchFirst)
|
---|
711 | RTLogWriteUser(pszFirst, cchFirst);
|
---|
712 | if (cchSecond)
|
---|
713 | RTLogWriteUser(pszSecond, cchSecond);
|
---|
714 | }
|
---|
715 |
|
---|
716 | if (pLogger->fDestFlags & RTLOGDEST_DEBUGGER)
|
---|
717 | {
|
---|
718 | if (cchPreamble)
|
---|
719 | RTLogWriteDebugger(pszPreamble, cchPreamble);
|
---|
720 | if (cchFirst)
|
---|
721 | RTLogWriteDebugger(pszFirst, cchFirst);
|
---|
722 | if (cchSecond)
|
---|
723 | RTLogWriteDebugger(pszSecond, cchSecond);
|
---|
724 | }
|
---|
725 |
|
---|
726 | # ifdef IN_RING3
|
---|
727 | if (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
728 | {
|
---|
729 | if (pLogger->pInt->hFile != NIL_RTFILE)
|
---|
730 | {
|
---|
731 | if (cchPreamble)
|
---|
732 | RTFileWrite(pLogger->pInt->hFile, pszPreamble, cchPreamble, NULL);
|
---|
733 | if (cchFirst)
|
---|
734 | RTFileWrite(pLogger->pInt->hFile, pszFirst, cchFirst, NULL);
|
---|
735 | if (cchSecond)
|
---|
736 | RTFileWrite(pLogger->pInt->hFile, pszSecond, cchSecond, NULL);
|
---|
737 | if (pLogger->fFlags & RTLOGFLAGS_FLUSH)
|
---|
738 | RTFileFlush(pLogger->pInt->hFile);
|
---|
739 | }
|
---|
740 | if (pLogger->pInt->cHistory)
|
---|
741 | pLogger->pInt->cbHistoryFileWritten += cchFirst + cchSecond;
|
---|
742 | }
|
---|
743 | # endif
|
---|
744 |
|
---|
745 | if (pLogger->fDestFlags & RTLOGDEST_STDOUT)
|
---|
746 | {
|
---|
747 | if (cchPreamble)
|
---|
748 | RTLogWriteStdOut(pszPreamble, cchPreamble);
|
---|
749 | if (cchFirst)
|
---|
750 | RTLogWriteStdOut(pszFirst, cchFirst);
|
---|
751 | if (cchSecond)
|
---|
752 | RTLogWriteStdOut(pszSecond, cchSecond);
|
---|
753 | }
|
---|
754 |
|
---|
755 | if (pLogger->fDestFlags & RTLOGDEST_STDERR)
|
---|
756 | {
|
---|
757 | if (cchPreamble)
|
---|
758 | RTLogWriteStdErr(pszPreamble, cchPreamble);
|
---|
759 | if (cchFirst)
|
---|
760 | RTLogWriteStdErr(pszFirst, cchFirst);
|
---|
761 | if (cchSecond)
|
---|
762 | RTLogWriteStdErr(pszSecond, cchSecond);
|
---|
763 | }
|
---|
764 |
|
---|
765 | # if defined(IN_RING0) && !defined(LOG_NO_COM)
|
---|
766 | if (pLogger->fDestFlags & RTLOGDEST_COM)
|
---|
767 | {
|
---|
768 | if (cchPreamble)
|
---|
769 | RTLogWriteCom(pszPreamble, cchPreamble);
|
---|
770 | if (cchFirst)
|
---|
771 | RTLogWriteCom(pszFirst, cchFirst);
|
---|
772 | if (cchSecond)
|
---|
773 | RTLogWriteCom(pszSecond, cchSecond);
|
---|
774 | }
|
---|
775 | # endif
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 |
|
---|
780 |
|
---|
781 | RTDECL(int) RTLogCreateExV(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
|
---|
782 | const char *pszEnvVarBase, unsigned cGroups, const char * const *papszGroups,
|
---|
783 | uint32_t fDestFlags, PFNRTLOGPHASE pfnPhase, uint32_t cHistory,
|
---|
784 | uint64_t cbHistoryFileMax, uint32_t cSecsHistoryTimeSlot,
|
---|
785 | char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, va_list args)
|
---|
786 | {
|
---|
787 | int rc;
|
---|
788 | size_t offInternal;
|
---|
789 | size_t cbLogger;
|
---|
790 | PRTLOGGER pLogger;
|
---|
791 |
|
---|
792 | /*
|
---|
793 | * Validate input.
|
---|
794 | */
|
---|
795 | if ( (cGroups && !papszGroups)
|
---|
796 | || !VALID_PTR(ppLogger) )
|
---|
797 | {
|
---|
798 | AssertMsgFailed(("Invalid parameters!\n"));
|
---|
799 | return VERR_INVALID_PARAMETER;
|
---|
800 | }
|
---|
801 | *ppLogger = NULL;
|
---|
802 |
|
---|
803 | if (pszErrorMsg)
|
---|
804 | RTStrPrintf(pszErrorMsg, cchErrorMsg, N_("unknown error"));
|
---|
805 |
|
---|
806 | AssertMsgReturn(cHistory < _1M, ("%#x", cHistory), VERR_OUT_OF_RANGE);
|
---|
807 |
|
---|
808 | /*
|
---|
809 | * Allocate a logger instance.
|
---|
810 | */
|
---|
811 | offInternal = RT_OFFSETOF(RTLOGGER, afGroups[cGroups]);
|
---|
812 | offInternal = RT_ALIGN_Z(offInternal, sizeof(uint64_t));
|
---|
813 | cbLogger = offInternal + sizeof(RTLOGGERINTERNAL);
|
---|
814 | if (fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
815 | cbLogger += cGroups * sizeof(uint32_t);
|
---|
816 | pLogger = (PRTLOGGER)RTMemAllocZVar(cbLogger);
|
---|
817 | if (pLogger)
|
---|
818 | {
|
---|
819 | # if defined(RT_ARCH_X86) && (!defined(LOG_USE_C99) || !defined(RT_WITHOUT_EXEC_ALLOC))
|
---|
820 | uint8_t *pu8Code;
|
---|
821 | # endif
|
---|
822 | pLogger->u32Magic = RTLOGGER_MAGIC;
|
---|
823 | pLogger->cGroups = cGroups;
|
---|
824 | pLogger->fFlags = fFlags;
|
---|
825 | pLogger->fDestFlags = fDestFlags;
|
---|
826 | pLogger->pInt = (PRTLOGGERINTERNAL)((uintptr_t)pLogger + offInternal);
|
---|
827 | pLogger->pInt->uRevision = RTLOGGERINTERNAL_REV;
|
---|
828 | pLogger->pInt->cbSelf = sizeof(RTLOGGERINTERNAL);
|
---|
829 | pLogger->pInt->hSpinMtx = NIL_RTSEMSPINMUTEX;
|
---|
830 | pLogger->pInt->pfnFlush = NULL;
|
---|
831 | pLogger->pInt->pfnPrefix = NULL;
|
---|
832 | pLogger->pInt->pvPrefixUserArg = NULL;
|
---|
833 | pLogger->pInt->afPadding1[0] = false;
|
---|
834 | pLogger->pInt->afPadding1[1] = false;
|
---|
835 | pLogger->pInt->fCreated = false;
|
---|
836 | pLogger->pInt->cMaxGroups = cGroups;
|
---|
837 | pLogger->pInt->papszGroups = papszGroups;
|
---|
838 | if (fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
839 | pLogger->pInt->pacEntriesPerGroup = (uint32_t *)(pLogger->pInt + 1);
|
---|
840 | else
|
---|
841 | pLogger->pInt->pacEntriesPerGroup = NULL;
|
---|
842 | pLogger->pInt->cMaxEntriesPerGroup = UINT32_MAX;
|
---|
843 | # ifdef IN_RING3
|
---|
844 | pLogger->pInt->pfnPhase = pfnPhase;
|
---|
845 | pLogger->pInt->hFile = NIL_RTFILE;
|
---|
846 | pLogger->pInt->cHistory = cHistory;
|
---|
847 | if (cbHistoryFileMax == 0)
|
---|
848 | pLogger->pInt->cbHistoryFileMax = UINT64_MAX;
|
---|
849 | else
|
---|
850 | pLogger->pInt->cbHistoryFileMax = cbHistoryFileMax;
|
---|
851 | if (cSecsHistoryTimeSlot == 0)
|
---|
852 | pLogger->pInt->cSecsHistoryTimeSlot = UINT32_MAX;
|
---|
853 | else
|
---|
854 | pLogger->pInt->cSecsHistoryTimeSlot = cSecsHistoryTimeSlot;
|
---|
855 | # else /* !IN_RING3 */
|
---|
856 | RT_NOREF_PV(pfnPhase); RT_NOREF_PV(cHistory); RT_NOREF_PV(cbHistoryFileMax); RT_NOREF_PV(cSecsHistoryTimeSlot);
|
---|
857 | # endif /* !IN_RING3 */
|
---|
858 | if (pszGroupSettings)
|
---|
859 | RTLogGroupSettings(pLogger, pszGroupSettings);
|
---|
860 |
|
---|
861 | # if defined(RT_ARCH_X86) && (!defined(LOG_USE_C99) || !defined(RT_WITHOUT_EXEC_ALLOC))
|
---|
862 | /*
|
---|
863 | * Emit wrapper code.
|
---|
864 | */
|
---|
865 | pu8Code = (uint8_t *)RTMemExecAlloc(64);
|
---|
866 | if (pu8Code)
|
---|
867 | {
|
---|
868 | pLogger->pfnLogger = *(PFNRTLOGGER*)&pu8Code;
|
---|
869 | *pu8Code++ = 0x68; /* push imm32 */
|
---|
870 | *(void **)pu8Code = pLogger;
|
---|
871 | pu8Code += sizeof(void *);
|
---|
872 | *pu8Code++ = 0xe8; /* call rel32 */
|
---|
873 | *(uint32_t *)pu8Code = (uintptr_t)RTLogLogger - ((uintptr_t)pu8Code + sizeof(uint32_t));
|
---|
874 | pu8Code += sizeof(uint32_t);
|
---|
875 | *pu8Code++ = 0x8d; /* lea esp, [esp + 4] */
|
---|
876 | *pu8Code++ = 0x64;
|
---|
877 | *pu8Code++ = 0x24;
|
---|
878 | *pu8Code++ = 0x04;
|
---|
879 | *pu8Code++ = 0xc3; /* ret near */
|
---|
880 | AssertMsg((uintptr_t)pu8Code - (uintptr_t)pLogger->pfnLogger <= 64,
|
---|
881 | ("Wrapper assembly is too big! %d bytes\n", (uintptr_t)pu8Code - (uintptr_t)pLogger->pfnLogger));
|
---|
882 | rc = VINF_SUCCESS;
|
---|
883 | }
|
---|
884 | else
|
---|
885 | {
|
---|
886 | # ifdef RT_OS_LINUX
|
---|
887 | if (pszErrorMsg) /* Most probably SELinux causing trouble since the larger RTMemAlloc succeeded. */
|
---|
888 | RTStrPrintf(pszErrorMsg, cchErrorMsg, N_("mmap(PROT_WRITE | PROT_EXEC) failed -- SELinux?"));
|
---|
889 | # endif
|
---|
890 | rc = VERR_NO_MEMORY;
|
---|
891 | }
|
---|
892 | if (RT_SUCCESS(rc))
|
---|
893 | # endif /* X86 wrapper code*/
|
---|
894 | {
|
---|
895 | # ifdef IN_RING3 /* files and env.vars. are only accessible when in R3 at the present time. */
|
---|
896 | /*
|
---|
897 | * Format the filename.
|
---|
898 | */
|
---|
899 | if (pszFilenameFmt)
|
---|
900 | {
|
---|
901 | /** @todo validate the length, fail on overflow. */
|
---|
902 | RTStrPrintfV(pLogger->pInt->szFilename, sizeof(pLogger->pInt->szFilename), pszFilenameFmt, args);
|
---|
903 | pLogger->fDestFlags |= RTLOGDEST_FILE;
|
---|
904 | }
|
---|
905 |
|
---|
906 | /*
|
---|
907 | * Parse the environment variables.
|
---|
908 | */
|
---|
909 | if (pszEnvVarBase)
|
---|
910 | {
|
---|
911 | /* make temp copy of environment variable base. */
|
---|
912 | size_t cchEnvVarBase = strlen(pszEnvVarBase);
|
---|
913 | char *pszEnvVar = (char *)alloca(cchEnvVarBase + 16);
|
---|
914 | memcpy(pszEnvVar, pszEnvVarBase, cchEnvVarBase);
|
---|
915 |
|
---|
916 | /*
|
---|
917 | * Destination.
|
---|
918 | */
|
---|
919 | strcpy(pszEnvVar + cchEnvVarBase, "_DEST");
|
---|
920 | const char *pszValue = RTEnvGet(pszEnvVar);
|
---|
921 | if (pszValue)
|
---|
922 | RTLogDestinations(pLogger, pszValue);
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * The flags.
|
---|
926 | */
|
---|
927 | strcpy(pszEnvVar + cchEnvVarBase, "_FLAGS");
|
---|
928 | pszValue = RTEnvGet(pszEnvVar);
|
---|
929 | if (pszValue)
|
---|
930 | RTLogFlags(pLogger, pszValue);
|
---|
931 |
|
---|
932 | /*
|
---|
933 | * The group settings.
|
---|
934 | */
|
---|
935 | pszEnvVar[cchEnvVarBase] = '\0';
|
---|
936 | pszValue = RTEnvGet(pszEnvVar);
|
---|
937 | if (pszValue)
|
---|
938 | RTLogGroupSettings(pLogger, pszValue);
|
---|
939 | }
|
---|
940 | # else /* !IN_RING3 */
|
---|
941 | RT_NOREF_PV(pszEnvVarBase); RT_NOREF_PV(pszFilenameFmt); RT_NOREF_PV(args);
|
---|
942 | # endif /* !IN_RING3 */
|
---|
943 |
|
---|
944 | /*
|
---|
945 | * Open the destination(s).
|
---|
946 | */
|
---|
947 | rc = VINF_SUCCESS;
|
---|
948 | # ifdef IN_RING3
|
---|
949 | if (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
950 | {
|
---|
951 | if (pLogger->fFlags & RTLOGFLAGS_APPEND)
|
---|
952 | {
|
---|
953 | rc = rtlogFileOpen(pLogger, pszErrorMsg, cchErrorMsg);
|
---|
954 |
|
---|
955 | /* Rotate in case of appending to a too big log file,
|
---|
956 | otherwise this simply doesn't do anything. */
|
---|
957 | rtlogRotate(pLogger, 0, true /* fFirst */);
|
---|
958 | }
|
---|
959 | else
|
---|
960 | {
|
---|
961 | /* Force rotation if it is configured. */
|
---|
962 | pLogger->pInt->cbHistoryFileWritten = UINT64_MAX;
|
---|
963 | rtlogRotate(pLogger, 0, true /* fFirst */);
|
---|
964 |
|
---|
965 | /* If the file is not open then rotation is not set up. */
|
---|
966 | if (pLogger->pInt->hFile == NIL_RTFILE)
|
---|
967 | {
|
---|
968 | pLogger->pInt->cbHistoryFileWritten = 0;
|
---|
969 | rc = rtlogFileOpen(pLogger, pszErrorMsg, cchErrorMsg);
|
---|
970 | }
|
---|
971 | }
|
---|
972 | }
|
---|
973 | # endif /* IN_RING3 */
|
---|
974 |
|
---|
975 | if ((pLogger->fDestFlags & RTLOGDEST_RINGBUF) && RT_SUCCESS(rc))
|
---|
976 | rc = rtLogRingBufAdjust(pLogger, pLogger->pInt->cbRingBuf, true /*fForce*/);
|
---|
977 |
|
---|
978 | /*
|
---|
979 | * Create mutex and check how much it counts when entering the lock
|
---|
980 | * so that we can report the values for RTLOGFLAGS_PREFIX_LOCK_COUNTS.
|
---|
981 | */
|
---|
982 | if (RT_SUCCESS(rc))
|
---|
983 | {
|
---|
984 | rc = RTSemSpinMutexCreate(&pLogger->pInt->hSpinMtx, RTSEMSPINMUTEX_FLAGS_IRQ_SAFE);
|
---|
985 | if (RT_SUCCESS(rc))
|
---|
986 | {
|
---|
987 | # ifdef IN_RING3 /** @todo do counters in ring-0 too? */
|
---|
988 | RTTHREAD Thread = RTThreadSelf();
|
---|
989 | if (Thread != NIL_RTTHREAD)
|
---|
990 | {
|
---|
991 | int32_t c = RTLockValidatorWriteLockGetCount(Thread);
|
---|
992 | RTSemSpinMutexRequest(pLogger->pInt->hSpinMtx);
|
---|
993 | c = RTLockValidatorWriteLockGetCount(Thread) - c;
|
---|
994 | RTSemSpinMutexRelease(pLogger->pInt->hSpinMtx);
|
---|
995 | ASMAtomicWriteU32(&g_cLoggerLockCount, c);
|
---|
996 | }
|
---|
997 |
|
---|
998 | /* Use the callback to generate some initial log contents. */
|
---|
999 | Assert(VALID_PTR(pLogger->pInt->pfnPhase) || pLogger->pInt->pfnPhase == NULL);
|
---|
1000 | if (pLogger->pInt->pfnPhase)
|
---|
1001 | pLogger->pInt->pfnPhase(pLogger, RTLOGPHASE_BEGIN, rtlogPhaseMsgNormal);
|
---|
1002 | # endif
|
---|
1003 | pLogger->pInt->fCreated = true;
|
---|
1004 | *ppLogger = pLogger;
|
---|
1005 | return VINF_SUCCESS;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | if (pszErrorMsg)
|
---|
1009 | RTStrPrintf(pszErrorMsg, cchErrorMsg, N_("failed to create semaphore"));
|
---|
1010 | }
|
---|
1011 | # ifdef IN_RING3
|
---|
1012 | RTFileClose(pLogger->pInt->hFile);
|
---|
1013 | # endif
|
---|
1014 | # if defined(LOG_USE_C99) && defined(RT_WITHOUT_EXEC_ALLOC)
|
---|
1015 | RTMemFree(*(void **)&pLogger->pfnLogger);
|
---|
1016 | # else
|
---|
1017 | RTMemExecFree(*(void **)&pLogger->pfnLogger, 64);
|
---|
1018 | # endif
|
---|
1019 | }
|
---|
1020 | RTMemFree(pLogger);
|
---|
1021 | }
|
---|
1022 | else
|
---|
1023 | rc = VERR_NO_MEMORY;
|
---|
1024 |
|
---|
1025 | return rc;
|
---|
1026 | }
|
---|
1027 | RT_EXPORT_SYMBOL(RTLogCreateExV);
|
---|
1028 |
|
---|
1029 |
|
---|
1030 | RTDECL(int) RTLogCreate(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
|
---|
1031 | const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
|
---|
1032 | uint32_t fDestFlags, const char *pszFilenameFmt, ...)
|
---|
1033 | {
|
---|
1034 | va_list args;
|
---|
1035 | int rc;
|
---|
1036 |
|
---|
1037 | va_start(args, pszFilenameFmt);
|
---|
1038 | rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups,
|
---|
1039 | fDestFlags, NULL /*pfnPhase*/, 0 /*cHistory*/, 0 /*cbHistoryFileMax*/, 0 /*cSecsHistoryTimeSlot*/,
|
---|
1040 | NULL /*pszErrorMsg*/, 0 /*cchErrorMsg*/, pszFilenameFmt, args);
|
---|
1041 | va_end(args);
|
---|
1042 | return rc;
|
---|
1043 | }
|
---|
1044 | RT_EXPORT_SYMBOL(RTLogCreate);
|
---|
1045 |
|
---|
1046 |
|
---|
1047 | RTDECL(int) RTLogCreateEx(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
|
---|
1048 | const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
|
---|
1049 | uint32_t fDestFlags, PFNRTLOGPHASE pfnPhase, uint32_t cHistory,
|
---|
1050 | uint64_t cbHistoryFileMax, uint32_t cSecsHistoryTimeSlot,
|
---|
1051 | char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, ...)
|
---|
1052 | {
|
---|
1053 | va_list args;
|
---|
1054 | int rc;
|
---|
1055 |
|
---|
1056 | va_start(args, pszFilenameFmt);
|
---|
1057 | rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups,
|
---|
1058 | fDestFlags, pfnPhase, cHistory, cbHistoryFileMax, cSecsHistoryTimeSlot,
|
---|
1059 | pszErrorMsg, cchErrorMsg, pszFilenameFmt, args);
|
---|
1060 | va_end(args);
|
---|
1061 | return rc;
|
---|
1062 | }
|
---|
1063 | RT_EXPORT_SYMBOL(RTLogCreateEx);
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * Destroys a logger instance.
|
---|
1068 | *
|
---|
1069 | * The instance is flushed and all output destinations closed (where applicable).
|
---|
1070 | *
|
---|
1071 | * @returns iprt status code.
|
---|
1072 | * @param pLogger The logger instance which close destroyed. NULL is fine.
|
---|
1073 | */
|
---|
1074 | RTDECL(int) RTLogDestroy(PRTLOGGER pLogger)
|
---|
1075 | {
|
---|
1076 | int rc;
|
---|
1077 | uint32_t iGroup;
|
---|
1078 | RTSEMSPINMUTEX hSpinMtx;
|
---|
1079 |
|
---|
1080 | /*
|
---|
1081 | * Validate input.
|
---|
1082 | */
|
---|
1083 | if (!pLogger)
|
---|
1084 | return VINF_SUCCESS;
|
---|
1085 | AssertPtrReturn(pLogger, VERR_INVALID_POINTER);
|
---|
1086 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
1087 | AssertPtrReturn(pLogger->pInt, VERR_INVALID_POINTER);
|
---|
1088 |
|
---|
1089 | /*
|
---|
1090 | * Acquire logger instance sem and disable all logging. (paranoia)
|
---|
1091 | */
|
---|
1092 | rc = rtlogLock(pLogger);
|
---|
1093 | AssertMsgRCReturn(rc, ("%Rrc\n", rc), rc);
|
---|
1094 |
|
---|
1095 | pLogger->fFlags |= RTLOGFLAGS_DISABLED;
|
---|
1096 | iGroup = pLogger->cGroups;
|
---|
1097 | while (iGroup-- > 0)
|
---|
1098 | pLogger->afGroups[iGroup] = 0;
|
---|
1099 |
|
---|
1100 | /*
|
---|
1101 | * Flush it.
|
---|
1102 | */
|
---|
1103 | rtlogFlush(pLogger);
|
---|
1104 |
|
---|
1105 | # ifdef IN_RING3
|
---|
1106 | /*
|
---|
1107 | * Add end of logging message.
|
---|
1108 | */
|
---|
1109 | if ( (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
1110 | && pLogger->pInt->hFile != NIL_RTFILE)
|
---|
1111 | pLogger->pInt->pfnPhase(pLogger, RTLOGPHASE_END, rtlogPhaseMsgLocked);
|
---|
1112 |
|
---|
1113 | /*
|
---|
1114 | * Close output stuffs.
|
---|
1115 | */
|
---|
1116 | if (pLogger->pInt->hFile != NIL_RTFILE)
|
---|
1117 | {
|
---|
1118 | int rc2 = RTFileClose(pLogger->pInt->hFile);
|
---|
1119 | AssertRC(rc2);
|
---|
1120 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
1121 | rc = rc2;
|
---|
1122 | pLogger->pInt->hFile = NIL_RTFILE;
|
---|
1123 | }
|
---|
1124 | # endif
|
---|
1125 |
|
---|
1126 | /*
|
---|
1127 | * Free the mutex, the wrapper and the instance memory.
|
---|
1128 | */
|
---|
1129 | hSpinMtx = pLogger->pInt->hSpinMtx;
|
---|
1130 | pLogger->pInt->hSpinMtx = NIL_RTSEMSPINMUTEX;
|
---|
1131 | if (hSpinMtx != NIL_RTSEMSPINMUTEX)
|
---|
1132 | {
|
---|
1133 | int rc2;
|
---|
1134 | RTSemSpinMutexRelease(hSpinMtx);
|
---|
1135 | rc2 = RTSemSpinMutexDestroy(hSpinMtx);
|
---|
1136 | AssertRC(rc2);
|
---|
1137 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
1138 | rc = rc2;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | if (pLogger->pfnLogger)
|
---|
1142 | {
|
---|
1143 | # if defined(LOG_USE_C99) && defined(RT_WITHOUT_EXEC_ALLOC)
|
---|
1144 | RTMemFree(*(void **)&pLogger->pfnLogger);
|
---|
1145 | # else
|
---|
1146 | RTMemExecFree(*(void **)&pLogger->pfnLogger, 64);
|
---|
1147 | # endif
|
---|
1148 | pLogger->pfnLogger = NULL;
|
---|
1149 | }
|
---|
1150 | RTMemFree(pLogger);
|
---|
1151 |
|
---|
1152 | return rc;
|
---|
1153 | }
|
---|
1154 | RT_EXPORT_SYMBOL(RTLogDestroy);
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | /**
|
---|
1158 | * Create a logger instance clone for RC usage.
|
---|
1159 | *
|
---|
1160 | * @returns iprt status code.
|
---|
1161 | *
|
---|
1162 | * @param pLogger The logger instance to be cloned.
|
---|
1163 | * @param pLoggerRC Where to create the RC logger instance.
|
---|
1164 | * @param cbLoggerRC Amount of memory allocated to for the RC logger
|
---|
1165 | * instance clone.
|
---|
1166 | * @param pfnLoggerRCPtr Pointer to logger wrapper function for this
|
---|
1167 | * instance (RC Ptr).
|
---|
1168 | * @param pfnFlushRCPtr Pointer to flush function (RC Ptr).
|
---|
1169 | * @param fFlags Logger instance flags, a combination of the RTLOGFLAGS_* values.
|
---|
1170 | */
|
---|
1171 | RTDECL(int) RTLogCloneRC(PRTLOGGER pLogger, PRTLOGGERRC pLoggerRC, size_t cbLoggerRC,
|
---|
1172 | RTRCPTR pfnLoggerRCPtr, RTRCPTR pfnFlushRCPtr, uint32_t fFlags)
|
---|
1173 | {
|
---|
1174 | /*
|
---|
1175 | * Validate input.
|
---|
1176 | */
|
---|
1177 | if ( !pLoggerRC
|
---|
1178 | || !pfnFlushRCPtr
|
---|
1179 | || !pfnLoggerRCPtr)
|
---|
1180 | {
|
---|
1181 | AssertMsgFailed(("Invalid parameters!\n"));
|
---|
1182 | return VERR_INVALID_PARAMETER;
|
---|
1183 | }
|
---|
1184 | if (cbLoggerRC < sizeof(*pLoggerRC))
|
---|
1185 | {
|
---|
1186 | AssertMsgFailed(("%d min=%d\n", cbLoggerRC, sizeof(*pLoggerRC)));
|
---|
1187 | return VERR_INVALID_PARAMETER;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /*
|
---|
1191 | * Initialize GC instance.
|
---|
1192 | */
|
---|
1193 | pLoggerRC->offScratch = 0;
|
---|
1194 | pLoggerRC->fPendingPrefix = false;
|
---|
1195 | pLoggerRC->pfnLogger = pfnLoggerRCPtr;
|
---|
1196 | pLoggerRC->pfnFlush = pfnFlushRCPtr;
|
---|
1197 | pLoggerRC->u32Magic = RTLOGGERRC_MAGIC;
|
---|
1198 | pLoggerRC->fFlags = fFlags | RTLOGFLAGS_DISABLED;
|
---|
1199 | pLoggerRC->cGroups = 1;
|
---|
1200 | pLoggerRC->afGroups[0] = 0;
|
---|
1201 |
|
---|
1202 | /*
|
---|
1203 | * Resolve defaults.
|
---|
1204 | */
|
---|
1205 | if (!pLogger)
|
---|
1206 | {
|
---|
1207 | pLogger = RTLogDefaultInstance();
|
---|
1208 | if (!pLogger)
|
---|
1209 | return VINF_SUCCESS;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | /*
|
---|
1213 | * Check if there's enough space for the groups.
|
---|
1214 | */
|
---|
1215 | if (cbLoggerRC < (size_t)RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]))
|
---|
1216 | {
|
---|
1217 | AssertMsgFailed(("%d req=%d cGroups=%d\n", cbLoggerRC, RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]), pLogger->cGroups));
|
---|
1218 | return VERR_BUFFER_OVERFLOW;
|
---|
1219 | }
|
---|
1220 | memcpy(&pLoggerRC->afGroups[0], &pLogger->afGroups[0], pLogger->cGroups * sizeof(pLoggerRC->afGroups[0]));
|
---|
1221 | pLoggerRC->cGroups = pLogger->cGroups;
|
---|
1222 |
|
---|
1223 | /*
|
---|
1224 | * Copy bits from the HC instance.
|
---|
1225 | */
|
---|
1226 | pLoggerRC->fPendingPrefix = pLogger->pInt->fPendingPrefix;
|
---|
1227 | pLoggerRC->fFlags |= pLogger->fFlags;
|
---|
1228 |
|
---|
1229 | /*
|
---|
1230 | * Check if we can remove the disabled flag.
|
---|
1231 | */
|
---|
1232 | if ( pLogger->fDestFlags
|
---|
1233 | && !((pLogger->fFlags | fFlags) & RTLOGFLAGS_DISABLED))
|
---|
1234 | pLoggerRC->fFlags &= ~RTLOGFLAGS_DISABLED;
|
---|
1235 |
|
---|
1236 | return VINF_SUCCESS;
|
---|
1237 | }
|
---|
1238 | RT_EXPORT_SYMBOL(RTLogCloneRC);
|
---|
1239 |
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Flushes a RC logger instance to a R3 logger.
|
---|
1243 | *
|
---|
1244 | *
|
---|
1245 | * @returns iprt status code.
|
---|
1246 | * @param pLogger The R3 logger instance to flush pLoggerRC to. If NULL
|
---|
1247 | * the default logger is used.
|
---|
1248 | * @param pLoggerRC The RC logger instance to flush.
|
---|
1249 | */
|
---|
1250 | RTDECL(void) RTLogFlushRC(PRTLOGGER pLogger, PRTLOGGERRC pLoggerRC)
|
---|
1251 | {
|
---|
1252 | /*
|
---|
1253 | * Resolve defaults.
|
---|
1254 | */
|
---|
1255 | if (!pLogger)
|
---|
1256 | {
|
---|
1257 | pLogger = RTLogDefaultInstance();
|
---|
1258 | if (!pLogger)
|
---|
1259 | {
|
---|
1260 | pLoggerRC->offScratch = 0;
|
---|
1261 | return;
|
---|
1262 | }
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /*
|
---|
1266 | * Any thing to flush?
|
---|
1267 | */
|
---|
1268 | if ( pLogger->offScratch
|
---|
1269 | || pLoggerRC->offScratch)
|
---|
1270 | {
|
---|
1271 | /*
|
---|
1272 | * Acquire logger instance sem.
|
---|
1273 | */
|
---|
1274 | int rc = rtlogLock(pLogger);
|
---|
1275 | if (RT_FAILURE(rc))
|
---|
1276 | return;
|
---|
1277 |
|
---|
1278 | /*
|
---|
1279 | * Write whatever the GC instance contains to the HC one, and then
|
---|
1280 | * flush the HC instance.
|
---|
1281 | */
|
---|
1282 | if (pLoggerRC->offScratch)
|
---|
1283 | {
|
---|
1284 | rtLogOutput(pLogger, pLoggerRC->achScratch, pLoggerRC->offScratch);
|
---|
1285 | rtLogOutput(pLogger, NULL, 0);
|
---|
1286 | pLoggerRC->offScratch = 0;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /*
|
---|
1290 | * Release the semaphore.
|
---|
1291 | */
|
---|
1292 | rtlogUnlock(pLogger);
|
---|
1293 | }
|
---|
1294 | }
|
---|
1295 | RT_EXPORT_SYMBOL(RTLogFlushRC);
|
---|
1296 |
|
---|
1297 | # ifdef IN_RING3
|
---|
1298 |
|
---|
1299 | RTDECL(int) RTLogCreateForR0(PRTLOGGER pLogger, size_t cbLogger,
|
---|
1300 | RTR0PTR pLoggerR0Ptr, RTR0PTR pfnLoggerR0Ptr, RTR0PTR pfnFlushR0Ptr,
|
---|
1301 | uint32_t fFlags, uint32_t fDestFlags)
|
---|
1302 | {
|
---|
1303 | /*
|
---|
1304 | * Validate input.
|
---|
1305 | */
|
---|
1306 | AssertPtrReturn(pLogger, VERR_INVALID_PARAMETER);
|
---|
1307 | size_t const cbRequired = sizeof(*pLogger) + RTLOGGERINTERNAL_R0_SIZE;
|
---|
1308 | AssertReturn(cbLogger >= cbRequired, VERR_BUFFER_OVERFLOW);
|
---|
1309 | AssertReturn(pLoggerR0Ptr != NIL_RTR0PTR, VERR_INVALID_PARAMETER);
|
---|
1310 | AssertReturn(pfnLoggerR0Ptr != NIL_RTR0PTR, VERR_INVALID_PARAMETER);
|
---|
1311 |
|
---|
1312 | /*
|
---|
1313 | * Initialize the ring-0 instance.
|
---|
1314 | */
|
---|
1315 | pLogger->achScratch[0] = 0;
|
---|
1316 | pLogger->offScratch = 0;
|
---|
1317 | pLogger->pfnLogger = (PFNRTLOGGER)pfnLoggerR0Ptr;
|
---|
1318 | pLogger->fFlags = fFlags;
|
---|
1319 | pLogger->fDestFlags = fDestFlags & ~RTLOGDEST_FILE;
|
---|
1320 | pLogger->pInt = NULL;
|
---|
1321 | pLogger->cGroups = 1;
|
---|
1322 | pLogger->afGroups[0] = 0;
|
---|
1323 |
|
---|
1324 | uint32_t cMaxGroups = (uint32_t)((cbLogger - cbRequired) / sizeof(pLogger->afGroups[0]));
|
---|
1325 | if (fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
1326 | cMaxGroups /= 2;
|
---|
1327 | PRTLOGGERINTERNAL pInt;
|
---|
1328 | for (;;)
|
---|
1329 | {
|
---|
1330 | AssertReturn(cMaxGroups > 0, VERR_BUFFER_OVERFLOW);
|
---|
1331 | pInt = (PRTLOGGERINTERNAL)&pLogger->afGroups[cMaxGroups];
|
---|
1332 | if (!((uintptr_t)pInt & (sizeof(uint64_t) - 1)))
|
---|
1333 | break;
|
---|
1334 | cMaxGroups--;
|
---|
1335 | }
|
---|
1336 | pLogger->pInt = (PRTLOGGERINTERNAL)(pLoggerR0Ptr + (uintptr_t)pInt - (uintptr_t)pLogger);
|
---|
1337 | pInt->uRevision = RTLOGGERINTERNAL_REV;
|
---|
1338 | pInt->cbSelf = RTLOGGERINTERNAL_R0_SIZE;
|
---|
1339 | pInt->hSpinMtx = NIL_RTSEMSPINMUTEX; /* Not serialized. */
|
---|
1340 | pInt->pfnFlush = (PFNRTLOGFLUSH)pfnFlushR0Ptr;
|
---|
1341 | pInt->pfnPrefix = NULL;
|
---|
1342 | pInt->pvPrefixUserArg = NULL;
|
---|
1343 | pInt->fPendingPrefix = false;
|
---|
1344 | pInt->cMaxGroups = cMaxGroups;
|
---|
1345 | pInt->papszGroups = NULL;
|
---|
1346 | pInt->cMaxEntriesPerGroup = UINT32_MAX;
|
---|
1347 | if (fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
1348 | {
|
---|
1349 | memset(pInt + 1, 0, sizeof(uint32_t) * cMaxGroups);
|
---|
1350 | pInt->pacEntriesPerGroup= (uint32_t *)(pLogger->pInt + 1);
|
---|
1351 | }
|
---|
1352 | else
|
---|
1353 | pInt->pacEntriesPerGroup= NULL;
|
---|
1354 |
|
---|
1355 | pInt->fCreated = true;
|
---|
1356 | pLogger->u32Magic = RTLOGGER_MAGIC;
|
---|
1357 | return VINF_SUCCESS;
|
---|
1358 | }
|
---|
1359 | RT_EXPORT_SYMBOL(RTLogCreateForR0);
|
---|
1360 |
|
---|
1361 |
|
---|
1362 | RTDECL(size_t) RTLogCalcSizeForR0(uint32_t cGroups, uint32_t fFlags)
|
---|
1363 | {
|
---|
1364 | size_t cb = RT_OFFSETOF(RTLOGGER, afGroups[cGroups]);
|
---|
1365 | cb = RT_ALIGN_Z(cb, sizeof(uint64_t));
|
---|
1366 | cb += sizeof(RTLOGGERINTERNAL);
|
---|
1367 | if (fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
1368 | cb += sizeof(uint32_t) * cGroups;
|
---|
1369 | return cb;
|
---|
1370 | }
|
---|
1371 | RT_EXPORT_SYMBOL(RTLogCalcSizeForR0);
|
---|
1372 |
|
---|
1373 |
|
---|
1374 | RTDECL(int) RTLogCopyGroupsAndFlagsForR0(PRTLOGGER pDstLogger, RTR0PTR pDstLoggerR0Ptr,
|
---|
1375 | PCRTLOGGER pSrcLogger, uint32_t fFlagsOr, uint32_t fFlagsAnd)
|
---|
1376 | {
|
---|
1377 | /*
|
---|
1378 | * Validate input.
|
---|
1379 | */
|
---|
1380 | AssertPtrReturn(pDstLogger, VERR_INVALID_PARAMETER);
|
---|
1381 | AssertPtrNullReturn(pSrcLogger, VERR_INVALID_PARAMETER);
|
---|
1382 |
|
---|
1383 | /*
|
---|
1384 | * Resolve defaults.
|
---|
1385 | */
|
---|
1386 | if (!pSrcLogger)
|
---|
1387 | {
|
---|
1388 | pSrcLogger = RTLogDefaultInstance();
|
---|
1389 | if (!pSrcLogger)
|
---|
1390 | {
|
---|
1391 | pDstLogger->fFlags |= RTLOGFLAGS_DISABLED | fFlagsOr;
|
---|
1392 | pDstLogger->cGroups = 1;
|
---|
1393 | pDstLogger->afGroups[0] = 0;
|
---|
1394 | return VINF_SUCCESS;
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | /*
|
---|
1399 | * Copy flags and group settings.
|
---|
1400 | */
|
---|
1401 | pDstLogger->fFlags = (pSrcLogger->fFlags & fFlagsAnd & ~RTLOGFLAGS_RESTRICT_GROUPS) | fFlagsOr;
|
---|
1402 |
|
---|
1403 | PRTLOGGERINTERNAL pDstInt = (PRTLOGGERINTERNAL)((uintptr_t)pDstLogger->pInt - pDstLoggerR0Ptr + (uintptr_t)pDstLogger);
|
---|
1404 | int rc = VINF_SUCCESS;
|
---|
1405 | uint32_t cGroups = pSrcLogger->cGroups;
|
---|
1406 | if (cGroups > pDstInt->cMaxGroups)
|
---|
1407 | {
|
---|
1408 | AssertMsgFailed(("cMaxGroups=%zd cGroups=%zd (min size %d)\n", pDstInt->cMaxGroups,
|
---|
1409 | pSrcLogger->cGroups, RT_OFFSETOF(RTLOGGER, afGroups[pSrcLogger->cGroups]) + RTLOGGERINTERNAL_R0_SIZE));
|
---|
1410 | rc = VERR_INVALID_PARAMETER;
|
---|
1411 | cGroups = pDstInt->cMaxGroups;
|
---|
1412 | }
|
---|
1413 | memcpy(&pDstLogger->afGroups[0], &pSrcLogger->afGroups[0], cGroups * sizeof(pDstLogger->afGroups[0]));
|
---|
1414 | pDstLogger->cGroups = cGroups;
|
---|
1415 |
|
---|
1416 | return rc;
|
---|
1417 | }
|
---|
1418 | RT_EXPORT_SYMBOL(RTLogCopyGroupsAndFlagsForR0);
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | RTDECL(int) RTLogSetCustomPrefixCallbackForR0(PRTLOGGER pLogger, RTR0PTR pLoggerR0Ptr,
|
---|
1422 | RTR0PTR pfnCallbackR0Ptr, RTR0PTR pvUserR0Ptr)
|
---|
1423 | {
|
---|
1424 | AssertPtrReturn(pLogger, VERR_INVALID_POINTER);
|
---|
1425 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
1426 |
|
---|
1427 | /*
|
---|
1428 | * Do the work.
|
---|
1429 | */
|
---|
1430 | PRTLOGGERINTERNAL pInt = (PRTLOGGERINTERNAL)((uintptr_t)pLogger->pInt - pLoggerR0Ptr + (uintptr_t)pLogger);
|
---|
1431 | AssertReturn(pInt->uRevision == RTLOGGERINTERNAL_REV, VERR_LOG_REVISION_MISMATCH);
|
---|
1432 | pInt->pvPrefixUserArg = (void *)pvUserR0Ptr;
|
---|
1433 | pInt->pfnPrefix = (PFNRTLOGPREFIX)pfnCallbackR0Ptr;
|
---|
1434 |
|
---|
1435 | return VINF_SUCCESS;
|
---|
1436 | }
|
---|
1437 | RT_EXPORT_SYMBOL(RTLogSetCustomPrefixCallbackForR0);
|
---|
1438 |
|
---|
1439 | RTDECL(void) RTLogFlushR0(PRTLOGGER pLogger, PRTLOGGER pLoggerR0)
|
---|
1440 | {
|
---|
1441 | /*
|
---|
1442 | * Resolve defaults.
|
---|
1443 | */
|
---|
1444 | if (!pLogger)
|
---|
1445 | {
|
---|
1446 | pLogger = RTLogDefaultInstance();
|
---|
1447 | if (!pLogger)
|
---|
1448 | {
|
---|
1449 | /* flushing to "/dev/null". */
|
---|
1450 | if (pLoggerR0->offScratch)
|
---|
1451 | pLoggerR0->offScratch = 0;
|
---|
1452 | return;
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /*
|
---|
1457 | * Anything to flush?
|
---|
1458 | */
|
---|
1459 | if ( pLoggerR0->offScratch
|
---|
1460 | || pLogger->offScratch)
|
---|
1461 | {
|
---|
1462 | /*
|
---|
1463 | * Acquire logger semaphores.
|
---|
1464 | */
|
---|
1465 | int rc = rtlogLock(pLogger);
|
---|
1466 | if (RT_FAILURE(rc))
|
---|
1467 | return;
|
---|
1468 | if (RT_SUCCESS(rc))
|
---|
1469 | {
|
---|
1470 | /*
|
---|
1471 | * Write whatever the GC instance contains to the HC one, and then
|
---|
1472 | * flush the HC instance.
|
---|
1473 | */
|
---|
1474 | if (pLoggerR0->offScratch)
|
---|
1475 | {
|
---|
1476 | rtLogOutput(pLogger, pLoggerR0->achScratch, pLoggerR0->offScratch);
|
---|
1477 | rtLogOutput(pLogger, NULL, 0);
|
---|
1478 | pLoggerR0->offScratch = 0;
|
---|
1479 | }
|
---|
1480 | }
|
---|
1481 | rtlogUnlock(pLogger);
|
---|
1482 | }
|
---|
1483 | }
|
---|
1484 | RT_EXPORT_SYMBOL(RTLogFlushR0);
|
---|
1485 |
|
---|
1486 | # endif /* IN_RING3 */
|
---|
1487 |
|
---|
1488 |
|
---|
1489 | /**
|
---|
1490 | * Flushes the buffer in one logger instance onto another logger.
|
---|
1491 | *
|
---|
1492 | * @returns iprt status code.
|
---|
1493 | *
|
---|
1494 | * @param pSrcLogger The logger instance to flush.
|
---|
1495 | * @param pDstLogger The logger instance to flush onto.
|
---|
1496 | * If NULL the default logger will be used.
|
---|
1497 | */
|
---|
1498 | RTDECL(void) RTLogFlushToLogger(PRTLOGGER pSrcLogger, PRTLOGGER pDstLogger)
|
---|
1499 | {
|
---|
1500 | /*
|
---|
1501 | * Resolve defaults.
|
---|
1502 | */
|
---|
1503 | if (!pDstLogger)
|
---|
1504 | {
|
---|
1505 | pDstLogger = RTLogDefaultInstance();
|
---|
1506 | if (!pDstLogger)
|
---|
1507 | {
|
---|
1508 | /* flushing to "/dev/null". */
|
---|
1509 | if (pSrcLogger->offScratch)
|
---|
1510 | {
|
---|
1511 | int rc = rtlogLock(pSrcLogger);
|
---|
1512 | if (RT_SUCCESS(rc))
|
---|
1513 | {
|
---|
1514 | pSrcLogger->offScratch = 0;
|
---|
1515 | rtlogUnlock(pSrcLogger);
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 | return;
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | /*
|
---|
1523 | * Any thing to flush?
|
---|
1524 | */
|
---|
1525 | if ( pSrcLogger->offScratch
|
---|
1526 | || pDstLogger->offScratch)
|
---|
1527 | {
|
---|
1528 | /*
|
---|
1529 | * Acquire logger semaphores.
|
---|
1530 | */
|
---|
1531 | int rc = rtlogLock(pDstLogger);
|
---|
1532 | if (RT_FAILURE(rc))
|
---|
1533 | return;
|
---|
1534 | rc = rtlogLock(pSrcLogger);
|
---|
1535 | if (RT_SUCCESS(rc))
|
---|
1536 | {
|
---|
1537 | /*
|
---|
1538 | * Write whatever the GC instance contains to the HC one, and then
|
---|
1539 | * flush the HC instance.
|
---|
1540 | */
|
---|
1541 | if (pSrcLogger->offScratch)
|
---|
1542 | {
|
---|
1543 | rtLogOutput(pDstLogger, pSrcLogger->achScratch, pSrcLogger->offScratch);
|
---|
1544 | rtLogOutput(pDstLogger, NULL, 0);
|
---|
1545 | pSrcLogger->offScratch = 0;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | /*
|
---|
1549 | * Release the semaphores.
|
---|
1550 | */
|
---|
1551 | rtlogUnlock(pSrcLogger);
|
---|
1552 | }
|
---|
1553 | rtlogUnlock(pDstLogger);
|
---|
1554 | }
|
---|
1555 | }
|
---|
1556 | RT_EXPORT_SYMBOL(RTLogFlushToLogger);
|
---|
1557 |
|
---|
1558 |
|
---|
1559 | /**
|
---|
1560 | * Sets the custom prefix callback.
|
---|
1561 | *
|
---|
1562 | * @returns IPRT status code.
|
---|
1563 | * @param pLogger The logger instance.
|
---|
1564 | * @param pfnCallback The callback.
|
---|
1565 | * @param pvUser The user argument for the callback.
|
---|
1566 | * */
|
---|
1567 | RTDECL(int) RTLogSetCustomPrefixCallback(PRTLOGGER pLogger, PFNRTLOGPREFIX pfnCallback, void *pvUser)
|
---|
1568 | {
|
---|
1569 | /*
|
---|
1570 | * Resolve defaults.
|
---|
1571 | */
|
---|
1572 | if (!pLogger)
|
---|
1573 | {
|
---|
1574 | pLogger = RTLogDefaultInstance();
|
---|
1575 | if (!pLogger)
|
---|
1576 | return VINF_SUCCESS;
|
---|
1577 | }
|
---|
1578 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
1579 |
|
---|
1580 | /*
|
---|
1581 | * Do the work.
|
---|
1582 | */
|
---|
1583 | rtlogLock(pLogger);
|
---|
1584 | pLogger->pInt->pvPrefixUserArg = pvUser;
|
---|
1585 | pLogger->pInt->pfnPrefix = pfnCallback;
|
---|
1586 | rtlogUnlock(pLogger);
|
---|
1587 |
|
---|
1588 | return VINF_SUCCESS;
|
---|
1589 | }
|
---|
1590 | RT_EXPORT_SYMBOL(RTLogSetCustomPrefixCallback);
|
---|
1591 |
|
---|
1592 |
|
---|
1593 | /**
|
---|
1594 | * Matches a group name with a pattern mask in an case insensitive manner (ASCII).
|
---|
1595 | *
|
---|
1596 | * @returns true if matching and *ppachMask set to the end of the pattern.
|
---|
1597 | * @returns false if no match.
|
---|
1598 | * @param pszGrp The group name.
|
---|
1599 | * @param ppachMask Pointer to the pointer to the mask. Only wildcard supported is '*'.
|
---|
1600 | * @param cchMask The length of the mask, including modifiers. The modifiers is why
|
---|
1601 | * we update *ppachMask on match.
|
---|
1602 | */
|
---|
1603 | static bool rtlogIsGroupMatching(const char *pszGrp, const char **ppachMask, size_t cchMask)
|
---|
1604 | {
|
---|
1605 | const char *pachMask;
|
---|
1606 |
|
---|
1607 | if (!pszGrp || !*pszGrp)
|
---|
1608 | return false;
|
---|
1609 | pachMask = *ppachMask;
|
---|
1610 | for (;;)
|
---|
1611 | {
|
---|
1612 | if (RT_C_TO_LOWER(*pszGrp) != RT_C_TO_LOWER(*pachMask))
|
---|
1613 | {
|
---|
1614 | const char *pszTmp;
|
---|
1615 |
|
---|
1616 | /*
|
---|
1617 | * Check for wildcard and do a minimal match if found.
|
---|
1618 | */
|
---|
1619 | if (*pachMask != '*')
|
---|
1620 | return false;
|
---|
1621 |
|
---|
1622 | /* eat '*'s. */
|
---|
1623 | do pachMask++;
|
---|
1624 | while (--cchMask && *pachMask == '*');
|
---|
1625 |
|
---|
1626 | /* is there more to match? */
|
---|
1627 | if ( !cchMask
|
---|
1628 | || *pachMask == '.'
|
---|
1629 | || *pachMask == '=')
|
---|
1630 | break; /* we're good */
|
---|
1631 |
|
---|
1632 | /* do extremely minimal matching (fixme) */
|
---|
1633 | pszTmp = strchr(pszGrp, RT_C_TO_LOWER(*pachMask));
|
---|
1634 | if (!pszTmp)
|
---|
1635 | pszTmp = strchr(pszGrp, RT_C_TO_UPPER(*pachMask));
|
---|
1636 | if (!pszTmp)
|
---|
1637 | return false;
|
---|
1638 | pszGrp = pszTmp;
|
---|
1639 | continue;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | /* done? */
|
---|
1643 | if (!*++pszGrp)
|
---|
1644 | {
|
---|
1645 | /* trailing wildcard is ok. */
|
---|
1646 | do
|
---|
1647 | {
|
---|
1648 | pachMask++;
|
---|
1649 | cchMask--;
|
---|
1650 | } while (cchMask && *pachMask == '*');
|
---|
1651 | if ( !cchMask
|
---|
1652 | || *pachMask == '.'
|
---|
1653 | || *pachMask == '=')
|
---|
1654 | break; /* we're good */
|
---|
1655 | return false;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | if (!--cchMask)
|
---|
1659 | return false;
|
---|
1660 | pachMask++;
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | /* match */
|
---|
1664 | *ppachMask = pachMask;
|
---|
1665 | return true;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 |
|
---|
1669 | /**
|
---|
1670 | * Updates the group settings for the logger instance using the specified
|
---|
1671 | * specification string.
|
---|
1672 | *
|
---|
1673 | * @returns iprt status code.
|
---|
1674 | * Failures can safely be ignored.
|
---|
1675 | * @param pLogger Logger instance.
|
---|
1676 | * @param pszValue Value to parse.
|
---|
1677 | */
|
---|
1678 | RTDECL(int) RTLogGroupSettings(PRTLOGGER pLogger, const char *pszValue)
|
---|
1679 | {
|
---|
1680 | /*
|
---|
1681 | * Resolve defaults.
|
---|
1682 | */
|
---|
1683 | if (!pLogger)
|
---|
1684 | {
|
---|
1685 | pLogger = RTLogDefaultInstance();
|
---|
1686 | if (!pLogger)
|
---|
1687 | return VINF_SUCCESS;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /*
|
---|
1691 | * Iterate the string.
|
---|
1692 | */
|
---|
1693 | while (*pszValue)
|
---|
1694 | {
|
---|
1695 | /*
|
---|
1696 | * Skip prefixes (blanks, ;, + and -).
|
---|
1697 | */
|
---|
1698 | bool fEnabled = true;
|
---|
1699 | char ch;
|
---|
1700 | const char *pszStart;
|
---|
1701 | unsigned i;
|
---|
1702 | size_t cch;
|
---|
1703 |
|
---|
1704 | while ((ch = *pszValue) == '+' || ch == '-' || ch == ' ' || ch == '\t' || ch == '\n' || ch == ';')
|
---|
1705 | {
|
---|
1706 | if (ch == '+' || ch == '-' || ch == ';')
|
---|
1707 | fEnabled = ch != '-';
|
---|
1708 | pszValue++;
|
---|
1709 | }
|
---|
1710 | if (!*pszValue)
|
---|
1711 | break;
|
---|
1712 |
|
---|
1713 | /*
|
---|
1714 | * Find end.
|
---|
1715 | */
|
---|
1716 | pszStart = pszValue;
|
---|
1717 | while ((ch = *pszValue) != '\0' && ch != '+' && ch != '-' && ch != ' ' && ch != '\t')
|
---|
1718 | pszValue++;
|
---|
1719 |
|
---|
1720 | /*
|
---|
1721 | * Find the group (ascii case insensitive search).
|
---|
1722 | * Special group 'all'.
|
---|
1723 | */
|
---|
1724 | cch = pszValue - pszStart;
|
---|
1725 | if ( cch >= 3
|
---|
1726 | && (pszStart[0] == 'a' || pszStart[0] == 'A')
|
---|
1727 | && (pszStart[1] == 'l' || pszStart[1] == 'L')
|
---|
1728 | && (pszStart[2] == 'l' || pszStart[2] == 'L')
|
---|
1729 | && (cch == 3 || pszStart[3] == '.' || pszStart[3] == '='))
|
---|
1730 | {
|
---|
1731 | /*
|
---|
1732 | * All.
|
---|
1733 | */
|
---|
1734 | unsigned fFlags = cch == 3
|
---|
1735 | ? RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1
|
---|
1736 | : rtlogGroupFlags(&pszStart[3]);
|
---|
1737 | for (i = 0; i < pLogger->cGroups; i++)
|
---|
1738 | {
|
---|
1739 | if (fEnabled)
|
---|
1740 | pLogger->afGroups[i] |= fFlags;
|
---|
1741 | else
|
---|
1742 | pLogger->afGroups[i] &= ~fFlags;
|
---|
1743 | }
|
---|
1744 | }
|
---|
1745 | else
|
---|
1746 | {
|
---|
1747 | /*
|
---|
1748 | * Specific group(s).
|
---|
1749 | */
|
---|
1750 | for (i = 0; i < pLogger->cGroups; i++)
|
---|
1751 | {
|
---|
1752 | const char *psz2 = (const char*)pszStart;
|
---|
1753 | if (rtlogIsGroupMatching(pLogger->pInt->papszGroups[i], &psz2, cch))
|
---|
1754 | {
|
---|
1755 | unsigned fFlags = RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1;
|
---|
1756 | if (*psz2 == '.' || *psz2 == '=')
|
---|
1757 | fFlags = rtlogGroupFlags(psz2);
|
---|
1758 | if (fEnabled)
|
---|
1759 | pLogger->afGroups[i] |= fFlags;
|
---|
1760 | else
|
---|
1761 | pLogger->afGroups[i] &= ~fFlags;
|
---|
1762 | }
|
---|
1763 | } /* for each group */
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | } /* parse specification */
|
---|
1767 |
|
---|
1768 | return VINF_SUCCESS;
|
---|
1769 | }
|
---|
1770 | RT_EXPORT_SYMBOL(RTLogGroupSettings);
|
---|
1771 |
|
---|
1772 |
|
---|
1773 | /**
|
---|
1774 | * Interprets the group flags suffix.
|
---|
1775 | *
|
---|
1776 | * @returns Flags specified. (0 is possible!)
|
---|
1777 | * @param psz Start of Suffix. (Either dot or equal sign.)
|
---|
1778 | */
|
---|
1779 | static unsigned rtlogGroupFlags(const char *psz)
|
---|
1780 | {
|
---|
1781 | unsigned fFlags = 0;
|
---|
1782 |
|
---|
1783 | /*
|
---|
1784 | * Literal flags.
|
---|
1785 | */
|
---|
1786 | while (*psz == '.')
|
---|
1787 | {
|
---|
1788 | static struct
|
---|
1789 | {
|
---|
1790 | const char *pszFlag; /* lowercase!! */
|
---|
1791 | unsigned fFlag;
|
---|
1792 | } aFlags[] =
|
---|
1793 | {
|
---|
1794 | { "eo", RTLOGGRPFLAGS_ENABLED },
|
---|
1795 | { "enabledonly",RTLOGGRPFLAGS_ENABLED },
|
---|
1796 | { "e", RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 | RTLOGGRPFLAGS_WARN },
|
---|
1797 | { "enabled", RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 | RTLOGGRPFLAGS_WARN },
|
---|
1798 | { "l1", RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1799 | { "level1", RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1800 | { "l", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1801 | { "l2", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1802 | { "level2", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1803 | { "l3", RTLOGGRPFLAGS_LEVEL_3 },
|
---|
1804 | { "level3", RTLOGGRPFLAGS_LEVEL_3 },
|
---|
1805 | { "l4", RTLOGGRPFLAGS_LEVEL_4 },
|
---|
1806 | { "level4", RTLOGGRPFLAGS_LEVEL_4 },
|
---|
1807 | { "l5", RTLOGGRPFLAGS_LEVEL_5 },
|
---|
1808 | { "level5", RTLOGGRPFLAGS_LEVEL_5 },
|
---|
1809 | { "l6", RTLOGGRPFLAGS_LEVEL_6 },
|
---|
1810 | { "level6", RTLOGGRPFLAGS_LEVEL_6 },
|
---|
1811 | { "l7", RTLOGGRPFLAGS_LEVEL_7 },
|
---|
1812 | { "level7", RTLOGGRPFLAGS_LEVEL_7 },
|
---|
1813 | { "l8", RTLOGGRPFLAGS_LEVEL_8 },
|
---|
1814 | { "level8", RTLOGGRPFLAGS_LEVEL_8 },
|
---|
1815 | { "l9", RTLOGGRPFLAGS_LEVEL_9 },
|
---|
1816 | { "level9", RTLOGGRPFLAGS_LEVEL_9 },
|
---|
1817 | { "l10", RTLOGGRPFLAGS_LEVEL_10 },
|
---|
1818 | { "level10", RTLOGGRPFLAGS_LEVEL_10 },
|
---|
1819 | { "l11", RTLOGGRPFLAGS_LEVEL_11 },
|
---|
1820 | { "level11", RTLOGGRPFLAGS_LEVEL_11 },
|
---|
1821 | { "l12", RTLOGGRPFLAGS_LEVEL_12 },
|
---|
1822 | { "level12", RTLOGGRPFLAGS_LEVEL_12 },
|
---|
1823 | { "f", RTLOGGRPFLAGS_FLOW },
|
---|
1824 | { "flow", RTLOGGRPFLAGS_FLOW },
|
---|
1825 | { "w", RTLOGGRPFLAGS_WARN },
|
---|
1826 | { "warn", RTLOGGRPFLAGS_WARN },
|
---|
1827 | { "warning", RTLOGGRPFLAGS_WARN },
|
---|
1828 | { "restrict", RTLOGGRPFLAGS_RESTRICT },
|
---|
1829 |
|
---|
1830 | };
|
---|
1831 | unsigned i;
|
---|
1832 | bool fFound = false;
|
---|
1833 | psz++;
|
---|
1834 | for (i = 0; i < RT_ELEMENTS(aFlags) && !fFound; i++)
|
---|
1835 | {
|
---|
1836 | const char *psz1 = aFlags[i].pszFlag;
|
---|
1837 | const char *psz2 = psz;
|
---|
1838 | while (*psz1 == RT_C_TO_LOWER(*psz2))
|
---|
1839 | {
|
---|
1840 | psz1++;
|
---|
1841 | psz2++;
|
---|
1842 | if (!*psz1)
|
---|
1843 | {
|
---|
1844 | if ( (*psz2 >= 'a' && *psz2 <= 'z')
|
---|
1845 | || (*psz2 >= 'A' && *psz2 <= 'Z')
|
---|
1846 | || (*psz2 >= '0' && *psz2 <= '9') )
|
---|
1847 | break;
|
---|
1848 | fFlags |= aFlags[i].fFlag;
|
---|
1849 | fFound = true;
|
---|
1850 | psz = psz2;
|
---|
1851 | break;
|
---|
1852 | }
|
---|
1853 | } /* strincmp */
|
---|
1854 | } /* for each flags */
|
---|
1855 | AssertMsg(fFound, ("%.15s...", psz));
|
---|
1856 | }
|
---|
1857 |
|
---|
1858 | /*
|
---|
1859 | * Flag value.
|
---|
1860 | */
|
---|
1861 | if (*psz == '=')
|
---|
1862 | {
|
---|
1863 | psz++;
|
---|
1864 | if (*psz == '~')
|
---|
1865 | fFlags = ~RTStrToInt32(psz + 1);
|
---|
1866 | else
|
---|
1867 | fFlags = RTStrToInt32(psz);
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | return fFlags;
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | /**
|
---|
1874 | * Helper for RTLogGetGroupSettings.
|
---|
1875 | */
|
---|
1876 | static int rtLogGetGroupSettingsAddOne(const char *pszName, uint32_t fGroup, char **ppszBuf, size_t *pcchBuf, bool *pfNotFirst)
|
---|
1877 | {
|
---|
1878 | # define APPEND_PSZ(psz,cch) do { memcpy(*ppszBuf, (psz), (cch)); *ppszBuf += (cch); *pcchBuf -= (cch); } while (0)
|
---|
1879 | # define APPEND_SZ(sz) APPEND_PSZ(sz, sizeof(sz) - 1)
|
---|
1880 | # define APPEND_CH(ch) do { **ppszBuf = (ch); *ppszBuf += 1; *pcchBuf -= 1; } while (0)
|
---|
1881 |
|
---|
1882 | /*
|
---|
1883 | * Add the name.
|
---|
1884 | */
|
---|
1885 | size_t cchName = strlen(pszName);
|
---|
1886 | if (cchName + 1 + *pfNotFirst > *pcchBuf)
|
---|
1887 | return VERR_BUFFER_OVERFLOW;
|
---|
1888 | if (*pfNotFirst)
|
---|
1889 | APPEND_CH(' ');
|
---|
1890 | else
|
---|
1891 | *pfNotFirst = true;
|
---|
1892 | APPEND_PSZ(pszName, cchName);
|
---|
1893 |
|
---|
1894 | /*
|
---|
1895 | * Only generate mnemonics for the simple+common bits.
|
---|
1896 | */
|
---|
1897 | if (fGroup == (RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1))
|
---|
1898 | /* nothing */;
|
---|
1899 | else if ( fGroup == (RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 | RTLOGGRPFLAGS_LEVEL_2 | RTLOGGRPFLAGS_FLOW)
|
---|
1900 | && *pcchBuf >= sizeof(".e.l.f"))
|
---|
1901 | APPEND_SZ(".e.l.f");
|
---|
1902 | else if ( fGroup == (RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 | RTLOGGRPFLAGS_FLOW)
|
---|
1903 | && *pcchBuf >= sizeof(".e.f"))
|
---|
1904 | APPEND_SZ(".e.f");
|
---|
1905 | else if (*pcchBuf >= 1 + 10 + 1)
|
---|
1906 | {
|
---|
1907 | size_t cch;
|
---|
1908 | APPEND_CH('=');
|
---|
1909 | cch = RTStrFormatNumber(*ppszBuf, fGroup, 16, 0, 0, RTSTR_F_SPECIAL | RTSTR_F_32BIT);
|
---|
1910 | *ppszBuf += cch;
|
---|
1911 | *pcchBuf -= cch;
|
---|
1912 | }
|
---|
1913 | else
|
---|
1914 | return VERR_BUFFER_OVERFLOW;
|
---|
1915 |
|
---|
1916 | # undef APPEND_PSZ
|
---|
1917 | # undef APPEND_SZ
|
---|
1918 | # undef APPEND_CH
|
---|
1919 | return VINF_SUCCESS;
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 |
|
---|
1923 | /**
|
---|
1924 | * Get the current log group settings as a string.
|
---|
1925 | *
|
---|
1926 | * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW.
|
---|
1927 | * @param pLogger Logger instance (NULL for default logger).
|
---|
1928 | * @param pszBuf The output buffer.
|
---|
1929 | * @param cchBuf The size of the output buffer. Must be greater
|
---|
1930 | * than zero.
|
---|
1931 | */
|
---|
1932 | RTDECL(int) RTLogGetGroupSettings(PRTLOGGER pLogger, char *pszBuf, size_t cchBuf)
|
---|
1933 | {
|
---|
1934 | bool fNotFirst = false;
|
---|
1935 | int rc = VINF_SUCCESS;
|
---|
1936 | uint32_t cGroups;
|
---|
1937 | uint32_t fGroup;
|
---|
1938 | uint32_t i;
|
---|
1939 |
|
---|
1940 | Assert(cchBuf);
|
---|
1941 |
|
---|
1942 | /*
|
---|
1943 | * Resolve defaults.
|
---|
1944 | */
|
---|
1945 | if (!pLogger)
|
---|
1946 | {
|
---|
1947 | pLogger = RTLogDefaultInstance();
|
---|
1948 | if (!pLogger)
|
---|
1949 | {
|
---|
1950 | *pszBuf = '\0';
|
---|
1951 | return VINF_SUCCESS;
|
---|
1952 | }
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | cGroups = pLogger->cGroups;
|
---|
1956 |
|
---|
1957 | /*
|
---|
1958 | * Check if all are the same.
|
---|
1959 | */
|
---|
1960 | fGroup = pLogger->afGroups[0];
|
---|
1961 | for (i = 1; i < cGroups; i++)
|
---|
1962 | if (pLogger->afGroups[i] != fGroup)
|
---|
1963 | break;
|
---|
1964 | if (i >= cGroups)
|
---|
1965 | rc = rtLogGetGroupSettingsAddOne("all", fGroup, &pszBuf, &cchBuf, &fNotFirst);
|
---|
1966 | else
|
---|
1967 | {
|
---|
1968 |
|
---|
1969 | /*
|
---|
1970 | * Iterate all the groups and print all that are enabled.
|
---|
1971 | */
|
---|
1972 | for (i = 0; i < cGroups; i++)
|
---|
1973 | {
|
---|
1974 | fGroup = pLogger->afGroups[i];
|
---|
1975 | if (fGroup)
|
---|
1976 | {
|
---|
1977 | const char *pszName = pLogger->pInt->papszGroups[i];
|
---|
1978 | if (pszName)
|
---|
1979 | {
|
---|
1980 | rc = rtLogGetGroupSettingsAddOne(pszName, fGroup, &pszBuf, &cchBuf, &fNotFirst);
|
---|
1981 | if (rc)
|
---|
1982 | break;
|
---|
1983 | }
|
---|
1984 | }
|
---|
1985 | }
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 | *pszBuf = '\0';
|
---|
1989 | return rc;
|
---|
1990 | }
|
---|
1991 | RT_EXPORT_SYMBOL(RTLogGetGroupSettings);
|
---|
1992 |
|
---|
1993 | #endif /* !IN_RC */
|
---|
1994 |
|
---|
1995 | /**
|
---|
1996 | * Updates the flags for the logger instance using the specified
|
---|
1997 | * specification string.
|
---|
1998 | *
|
---|
1999 | * @returns iprt status code.
|
---|
2000 | * Failures can safely be ignored.
|
---|
2001 | * @param pLogger Logger instance (NULL for default logger).
|
---|
2002 | * @param pszValue Value to parse.
|
---|
2003 | */
|
---|
2004 | RTDECL(int) RTLogFlags(PRTLOGGER pLogger, const char *pszValue)
|
---|
2005 | {
|
---|
2006 | int rc = VINF_SUCCESS;
|
---|
2007 |
|
---|
2008 | /*
|
---|
2009 | * Resolve defaults.
|
---|
2010 | */
|
---|
2011 | if (!pLogger)
|
---|
2012 | {
|
---|
2013 | pLogger = RTLogDefaultInstance();
|
---|
2014 | if (!pLogger)
|
---|
2015 | return VINF_SUCCESS;
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | /*
|
---|
2019 | * Iterate the string.
|
---|
2020 | */
|
---|
2021 | while (*pszValue)
|
---|
2022 | {
|
---|
2023 | /* check no prefix. */
|
---|
2024 | bool fNo = false;
|
---|
2025 | char ch;
|
---|
2026 | unsigned i;
|
---|
2027 |
|
---|
2028 | /* skip blanks. */
|
---|
2029 | while (RT_C_IS_SPACE(*pszValue))
|
---|
2030 | pszValue++;
|
---|
2031 | if (!*pszValue)
|
---|
2032 | return rc;
|
---|
2033 |
|
---|
2034 | while ((ch = *pszValue) != '\0')
|
---|
2035 | {
|
---|
2036 | if (ch == 'n' && pszValue[1] == 'o')
|
---|
2037 | {
|
---|
2038 | pszValue += 2;
|
---|
2039 | fNo = !fNo;
|
---|
2040 | }
|
---|
2041 | else if (ch == '+')
|
---|
2042 | {
|
---|
2043 | pszValue++;
|
---|
2044 | fNo = true;
|
---|
2045 | }
|
---|
2046 | else if (ch == '-' || ch == '!' || ch == '~')
|
---|
2047 | {
|
---|
2048 | pszValue++;
|
---|
2049 | fNo = !fNo;
|
---|
2050 | }
|
---|
2051 | else
|
---|
2052 | break;
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | /* instruction. */
|
---|
2056 | for (i = 0; i < RT_ELEMENTS(g_aLogFlags); i++)
|
---|
2057 | {
|
---|
2058 | if (!strncmp(pszValue, g_aLogFlags[i].pszInstr, g_aLogFlags[i].cchInstr))
|
---|
2059 | {
|
---|
2060 | if (fNo == g_aLogFlags[i].fInverted)
|
---|
2061 | pLogger->fFlags |= g_aLogFlags[i].fFlag;
|
---|
2062 | else
|
---|
2063 | pLogger->fFlags &= ~g_aLogFlags[i].fFlag;
|
---|
2064 | pszValue += g_aLogFlags[i].cchInstr;
|
---|
2065 | break;
|
---|
2066 | }
|
---|
2067 | }
|
---|
2068 |
|
---|
2069 | /* unknown instruction? */
|
---|
2070 | if (i >= RT_ELEMENTS(g_aLogFlags))
|
---|
2071 | {
|
---|
2072 | AssertMsgFailed(("Invalid flags! unknown instruction %.20s\n", pszValue));
|
---|
2073 | pszValue++;
|
---|
2074 | }
|
---|
2075 |
|
---|
2076 | /* skip blanks and delimiters. */
|
---|
2077 | while (RT_C_IS_SPACE(*pszValue) || *pszValue == ';')
|
---|
2078 | pszValue++;
|
---|
2079 | } /* while more environment variable value left */
|
---|
2080 |
|
---|
2081 | return rc;
|
---|
2082 | }
|
---|
2083 | RT_EXPORT_SYMBOL(RTLogFlags);
|
---|
2084 |
|
---|
2085 |
|
---|
2086 | /**
|
---|
2087 | * Changes the buffering setting of the specified logger.
|
---|
2088 | *
|
---|
2089 | * This can be used for optimizing longish logging sequences.
|
---|
2090 | *
|
---|
2091 | * @returns The old state.
|
---|
2092 | * @param pLogger The logger instance (NULL is an alias for the
|
---|
2093 | * default logger).
|
---|
2094 | * @param fBuffered The new state.
|
---|
2095 | */
|
---|
2096 | RTDECL(bool) RTLogSetBuffering(PRTLOGGER pLogger, bool fBuffered)
|
---|
2097 | {
|
---|
2098 | bool fOld;
|
---|
2099 |
|
---|
2100 | /*
|
---|
2101 | * Resolve the logger instance.
|
---|
2102 | */
|
---|
2103 | if (!pLogger)
|
---|
2104 | {
|
---|
2105 | pLogger = RTLogDefaultInstance();
|
---|
2106 | if (!pLogger)
|
---|
2107 | return false;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | rtlogLock(pLogger);
|
---|
2111 | fOld = !!(pLogger->fFlags & RTLOGFLAGS_BUFFERED);
|
---|
2112 | if (fBuffered)
|
---|
2113 | pLogger->fFlags |= RTLOGFLAGS_BUFFERED;
|
---|
2114 | else
|
---|
2115 | pLogger->fFlags &= ~RTLOGFLAGS_BUFFERED;
|
---|
2116 | rtlogUnlock(pLogger);
|
---|
2117 |
|
---|
2118 | return fOld;
|
---|
2119 | }
|
---|
2120 | RT_EXPORT_SYMBOL(RTLogSetBuffering);
|
---|
2121 |
|
---|
2122 |
|
---|
2123 | #ifdef IN_RING3
|
---|
2124 | RTDECL(uint32_t) RTLogSetGroupLimit(PRTLOGGER pLogger, uint32_t cMaxEntriesPerGroup)
|
---|
2125 | {
|
---|
2126 | /*
|
---|
2127 | * Resolve the logger instance.
|
---|
2128 | */
|
---|
2129 | if (!pLogger)
|
---|
2130 | {
|
---|
2131 | pLogger = RTLogDefaultInstance();
|
---|
2132 | if (!pLogger)
|
---|
2133 | return UINT32_MAX;
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | rtlogLock(pLogger);
|
---|
2137 | uint32_t cOld = pLogger->pInt->cMaxEntriesPerGroup;
|
---|
2138 | pLogger->pInt->cMaxEntriesPerGroup = cMaxEntriesPerGroup;
|
---|
2139 | rtlogUnlock(pLogger);
|
---|
2140 |
|
---|
2141 | return cOld;
|
---|
2142 | }
|
---|
2143 | #endif
|
---|
2144 |
|
---|
2145 | #ifndef IN_RC
|
---|
2146 |
|
---|
2147 | /**
|
---|
2148 | * Get the current log flags as a string.
|
---|
2149 | *
|
---|
2150 | * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW.
|
---|
2151 | * @param pLogger Logger instance (NULL for default logger).
|
---|
2152 | * @param pszBuf The output buffer.
|
---|
2153 | * @param cchBuf The size of the output buffer. Must be greater
|
---|
2154 | * than zero.
|
---|
2155 | */
|
---|
2156 | RTDECL(int) RTLogGetFlags(PRTLOGGER pLogger, char *pszBuf, size_t cchBuf)
|
---|
2157 | {
|
---|
2158 | bool fNotFirst = false;
|
---|
2159 | int rc = VINF_SUCCESS;
|
---|
2160 | uint32_t fFlags;
|
---|
2161 | unsigned i;
|
---|
2162 |
|
---|
2163 | Assert(cchBuf);
|
---|
2164 |
|
---|
2165 | /*
|
---|
2166 | * Resolve defaults.
|
---|
2167 | */
|
---|
2168 | if (!pLogger)
|
---|
2169 | {
|
---|
2170 | pLogger = RTLogDefaultInstance();
|
---|
2171 | if (!pLogger)
|
---|
2172 | {
|
---|
2173 | *pszBuf = '\0';
|
---|
2174 | return VINF_SUCCESS;
|
---|
2175 | }
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | /*
|
---|
2179 | * Add the flags in the list.
|
---|
2180 | */
|
---|
2181 | fFlags = pLogger->fFlags;
|
---|
2182 | for (i = 0; i < RT_ELEMENTS(g_aLogFlags); i++)
|
---|
2183 | if ( !g_aLogFlags[i].fInverted
|
---|
2184 | ? (g_aLogFlags[i].fFlag & fFlags)
|
---|
2185 | : !(g_aLogFlags[i].fFlag & fFlags))
|
---|
2186 | {
|
---|
2187 | size_t cchInstr = g_aLogFlags[i].cchInstr;
|
---|
2188 | if (cchInstr + fNotFirst + 1 > cchBuf)
|
---|
2189 | {
|
---|
2190 | rc = VERR_BUFFER_OVERFLOW;
|
---|
2191 | break;
|
---|
2192 | }
|
---|
2193 | if (fNotFirst)
|
---|
2194 | {
|
---|
2195 | *pszBuf++ = ' ';
|
---|
2196 | cchBuf--;
|
---|
2197 | }
|
---|
2198 | memcpy(pszBuf, g_aLogFlags[i].pszInstr, cchInstr);
|
---|
2199 | pszBuf += cchInstr;
|
---|
2200 | cchBuf -= cchInstr;
|
---|
2201 | fNotFirst = true;
|
---|
2202 | }
|
---|
2203 | *pszBuf = '\0';
|
---|
2204 | return rc;
|
---|
2205 | }
|
---|
2206 | RT_EXPORT_SYMBOL(RTLogGetFlags);
|
---|
2207 |
|
---|
2208 |
|
---|
2209 | /**
|
---|
2210 | * Finds the end of a destination value.
|
---|
2211 | *
|
---|
2212 | * The value ends when we counter a ';' or a free standing word (space on both
|
---|
2213 | * from the g_aLogDst table. (If this is problematic for someone, we could
|
---|
2214 | * always do quoting and escaping.)
|
---|
2215 | *
|
---|
2216 | * @returns Value length in chars.
|
---|
2217 | * @param pszValue The first char after '=' or ':'.
|
---|
2218 | */
|
---|
2219 | static size_t rtLogDestFindValueLength(const char *pszValue)
|
---|
2220 | {
|
---|
2221 | size_t off = 0;
|
---|
2222 | char ch;
|
---|
2223 | while ((ch = pszValue[off]) != '\0' && ch != ';')
|
---|
2224 | {
|
---|
2225 | if (!RT_C_IS_SPACE(ch))
|
---|
2226 | off++;
|
---|
2227 | else
|
---|
2228 | {
|
---|
2229 | unsigned i;
|
---|
2230 | size_t cchThusFar = off;
|
---|
2231 | do
|
---|
2232 | off++;
|
---|
2233 | while ((ch = pszValue[off]) != '\0' && RT_C_IS_SPACE(ch));
|
---|
2234 | if (ch == ';')
|
---|
2235 | return cchThusFar;
|
---|
2236 |
|
---|
2237 | if (ch == 'n' && pszValue[off + 1] == 'o')
|
---|
2238 | off += 2;
|
---|
2239 | for (i = 0; i < RT_ELEMENTS(g_aLogDst); i++)
|
---|
2240 | if (!strncmp(&pszValue[off], g_aLogDst[i].pszInstr, g_aLogDst[i].cchInstr))
|
---|
2241 | {
|
---|
2242 | ch = pszValue[off + g_aLogDst[i].cchInstr];
|
---|
2243 | if (ch == '\0' || RT_C_IS_SPACE(ch) || ch == '=' || ch == ':' || ch == ';')
|
---|
2244 | return cchThusFar;
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 | }
|
---|
2248 | return off;
|
---|
2249 | }
|
---|
2250 |
|
---|
2251 |
|
---|
2252 | /**
|
---|
2253 | * Updates the logger destination using the specified string.
|
---|
2254 | *
|
---|
2255 | * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW.
|
---|
2256 | * @param pLogger Logger instance (NULL for default logger).
|
---|
2257 | * @param pszValue The value to parse.
|
---|
2258 | */
|
---|
2259 | RTDECL(int) RTLogDestinations(PRTLOGGER pLogger, char const *pszValue)
|
---|
2260 | {
|
---|
2261 | /*
|
---|
2262 | * Resolve defaults.
|
---|
2263 | */
|
---|
2264 | if (!pLogger)
|
---|
2265 | {
|
---|
2266 | pLogger = RTLogDefaultInstance();
|
---|
2267 | if (!pLogger)
|
---|
2268 | return VINF_SUCCESS;
|
---|
2269 | }
|
---|
2270 |
|
---|
2271 | /*
|
---|
2272 | * Do the parsing.
|
---|
2273 | */
|
---|
2274 | while (*pszValue)
|
---|
2275 | {
|
---|
2276 | bool fNo;
|
---|
2277 | unsigned i;
|
---|
2278 |
|
---|
2279 | /* skip blanks. */
|
---|
2280 | while (RT_C_IS_SPACE(*pszValue))
|
---|
2281 | pszValue++;
|
---|
2282 | if (!*pszValue)
|
---|
2283 | break;
|
---|
2284 |
|
---|
2285 | /* check no prefix. */
|
---|
2286 | fNo = false;
|
---|
2287 | if (pszValue[0] == 'n' && pszValue[1] == 'o')
|
---|
2288 | {
|
---|
2289 | fNo = true;
|
---|
2290 | pszValue += 2;
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | /* instruction. */
|
---|
2294 | for (i = 0; i < RT_ELEMENTS(g_aLogDst); i++)
|
---|
2295 | {
|
---|
2296 | size_t cchInstr = strlen(g_aLogDst[i].pszInstr);
|
---|
2297 | if (!strncmp(pszValue, g_aLogDst[i].pszInstr, cchInstr))
|
---|
2298 | {
|
---|
2299 | if (!fNo)
|
---|
2300 | pLogger->fDestFlags |= g_aLogDst[i].fFlag;
|
---|
2301 | else
|
---|
2302 | pLogger->fDestFlags &= ~g_aLogDst[i].fFlag;
|
---|
2303 | pszValue += cchInstr;
|
---|
2304 |
|
---|
2305 | /* check for value. */
|
---|
2306 | while (RT_C_IS_SPACE(*pszValue))
|
---|
2307 | pszValue++;
|
---|
2308 | if (*pszValue == '=' || *pszValue == ':')
|
---|
2309 | {
|
---|
2310 | pszValue++;
|
---|
2311 | size_t cch = rtLogDestFindValueLength(pszValue);
|
---|
2312 | const char *pszEnd = pszValue + cch;
|
---|
2313 |
|
---|
2314 | # ifdef IN_RING3
|
---|
2315 | char szTmp[sizeof(pLogger->pInt->szFilename)];
|
---|
2316 | # else
|
---|
2317 | char szTmp[32];
|
---|
2318 | # endif
|
---|
2319 | if (0)
|
---|
2320 | { /* nothing */ }
|
---|
2321 | #ifdef IN_RING3
|
---|
2322 |
|
---|
2323 | /* log file name */
|
---|
2324 | else if (i == 0 /* file */ && !fNo)
|
---|
2325 | {
|
---|
2326 | AssertReturn(cch < sizeof(pLogger->pInt->szFilename), VERR_OUT_OF_RANGE);
|
---|
2327 | memcpy(pLogger->pInt->szFilename, pszValue, cch);
|
---|
2328 | pLogger->pInt->szFilename[cch] = '\0';
|
---|
2329 | /** @todo reopen log file if pLogger->pInt->fCreated is true ... */
|
---|
2330 | }
|
---|
2331 | /* log directory */
|
---|
2332 | else if (i == 1 /* dir */ && !fNo)
|
---|
2333 | {
|
---|
2334 | const char *pszFile = RTPathFilename(pLogger->pInt->szFilename);
|
---|
2335 | size_t cchFile = pszFile ? strlen(pszFile) : 0;
|
---|
2336 | AssertReturn(cchFile + cch + 1 < sizeof(pLogger->pInt->szFilename), VERR_OUT_OF_RANGE);
|
---|
2337 | memcpy(szTmp, cchFile ? pszFile : "", cchFile + 1);
|
---|
2338 |
|
---|
2339 | memcpy(pLogger->pInt->szFilename, pszValue, cch);
|
---|
2340 | pLogger->pInt->szFilename[cch] = '\0';
|
---|
2341 | RTPathStripTrailingSlash(pLogger->pInt->szFilename);
|
---|
2342 |
|
---|
2343 | cch = strlen(pLogger->pInt->szFilename);
|
---|
2344 | pLogger->pInt->szFilename[cch++] = '/';
|
---|
2345 | memcpy(&pLogger->pInt->szFilename[cch], szTmp, cchFile);
|
---|
2346 | pLogger->pInt->szFilename[cch + cchFile] = '\0';
|
---|
2347 | /** @todo reopen log file if pLogger->pInt->fCreated is true ... */
|
---|
2348 | }
|
---|
2349 | else if (i == 2 /* history */)
|
---|
2350 | {
|
---|
2351 | if (!fNo)
|
---|
2352 | {
|
---|
2353 | uint32_t cHistory = 0;
|
---|
2354 | int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszValue, cch);
|
---|
2355 | if (RT_SUCCESS(rc))
|
---|
2356 | rc = RTStrToUInt32Full(szTmp, 0, &cHistory);
|
---|
2357 | AssertMsgReturn(RT_SUCCESS(rc) && cHistory < _1M, ("Invalid history value %s (%Rrc)!\n", szTmp, rc), rc);
|
---|
2358 | pLogger->pInt->cHistory = cHistory;
|
---|
2359 | }
|
---|
2360 | else
|
---|
2361 | pLogger->pInt->cHistory = 0;
|
---|
2362 | }
|
---|
2363 | else if (i == 3 /* histsize */)
|
---|
2364 | {
|
---|
2365 | if (!fNo)
|
---|
2366 | {
|
---|
2367 | int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszValue, cch);
|
---|
2368 | if (RT_SUCCESS(rc))
|
---|
2369 | rc = RTStrToUInt64Full(szTmp, 0, &pLogger->pInt->cbHistoryFileMax);
|
---|
2370 | AssertMsgRCReturn(rc, ("Invalid history file size value %s (%Rrc)!\n", szTmp, rc), rc);
|
---|
2371 | if (pLogger->pInt->cbHistoryFileMax == 0)
|
---|
2372 | pLogger->pInt->cbHistoryFileMax = UINT64_MAX;
|
---|
2373 | }
|
---|
2374 | else
|
---|
2375 | pLogger->pInt->cbHistoryFileMax = UINT64_MAX;
|
---|
2376 | }
|
---|
2377 | else if (i == 4 /* histtime */)
|
---|
2378 | {
|
---|
2379 | if (!fNo)
|
---|
2380 | {
|
---|
2381 | int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszValue, cch);
|
---|
2382 | if (RT_SUCCESS(rc))
|
---|
2383 | rc = RTStrToUInt32Full(szTmp, 0, &pLogger->pInt->cSecsHistoryTimeSlot);
|
---|
2384 | AssertMsgRCReturn(rc, ("Invalid history time slot value %s (%Rrc)!\n", szTmp, rc), rc);
|
---|
2385 | if (pLogger->pInt->cSecsHistoryTimeSlot == 0)
|
---|
2386 | pLogger->pInt->cSecsHistoryTimeSlot = UINT32_MAX;
|
---|
2387 | }
|
---|
2388 | else
|
---|
2389 | pLogger->pInt->cSecsHistoryTimeSlot = UINT32_MAX;
|
---|
2390 | }
|
---|
2391 | # endif /* IN_RING3 */
|
---|
2392 | else if (i == 5 /* ringbuf */ && !fNo)
|
---|
2393 | {
|
---|
2394 | int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszValue, cch);
|
---|
2395 | uint32_t cbRingBuf = 0;
|
---|
2396 | if (RT_SUCCESS(rc))
|
---|
2397 | rc = RTStrToUInt32Full(szTmp, 0, &cbRingBuf);
|
---|
2398 | AssertMsgRCReturn(rc, ("Invalid ring buffer size value '%s' (%Rrc)!\n", szTmp, rc), rc);
|
---|
2399 |
|
---|
2400 | if (cbRingBuf == 0)
|
---|
2401 | cbRingBuf = RTLOG_RINGBUF_DEFAULT_SIZE;
|
---|
2402 | else if (cbRingBuf < RTLOG_RINGBUF_MIN_SIZE)
|
---|
2403 | cbRingBuf = RTLOG_RINGBUF_MIN_SIZE;
|
---|
2404 | else if (cbRingBuf > RTLOG_RINGBUF_MAX_SIZE)
|
---|
2405 | cbRingBuf = RTLOG_RINGBUF_MAX_SIZE;
|
---|
2406 | else
|
---|
2407 | cbRingBuf = RT_ALIGN_32(cbRingBuf, 64);
|
---|
2408 | rc = rtLogRingBufAdjust(pLogger, cbRingBuf, false /*fForce*/);
|
---|
2409 | if (RT_FAILURE(rc))
|
---|
2410 | return rc;
|
---|
2411 | }
|
---|
2412 | else
|
---|
2413 | AssertMsgFailedReturn(("Invalid destination value! %s%s doesn't take a value!\n",
|
---|
2414 | fNo ? "no" : "", g_aLogDst[i].pszInstr),
|
---|
2415 | VERR_INVALID_PARAMETER);
|
---|
2416 |
|
---|
2417 | pszValue = pszEnd + (*pszEnd != '\0');
|
---|
2418 | }
|
---|
2419 | else if (i == 5 /* ringbuf */ && !fNo && !pLogger->pInt->pszRingBuf)
|
---|
2420 | {
|
---|
2421 | int rc = rtLogRingBufAdjust(pLogger, pLogger->pInt->cbRingBuf, false /*fForce*/);
|
---|
2422 | if (RT_FAILURE(rc))
|
---|
2423 | return rc;
|
---|
2424 | }
|
---|
2425 | break;
|
---|
2426 | }
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | /* assert known instruction */
|
---|
2430 | AssertMsgReturn(i < RT_ELEMENTS(g_aLogDst),
|
---|
2431 | ("Invalid destination value! unknown instruction %.20s\n", pszValue),
|
---|
2432 | VERR_INVALID_PARAMETER);
|
---|
2433 |
|
---|
2434 | /* skip blanks and delimiters. */
|
---|
2435 | while (RT_C_IS_SPACE(*pszValue) || *pszValue == ';')
|
---|
2436 | pszValue++;
|
---|
2437 | } /* while more environment variable value left */
|
---|
2438 |
|
---|
2439 | return VINF_SUCCESS;
|
---|
2440 | }
|
---|
2441 | RT_EXPORT_SYMBOL(RTLogDestinations);
|
---|
2442 |
|
---|
2443 |
|
---|
2444 | /**
|
---|
2445 | * Get the current log destinations as a string.
|
---|
2446 | *
|
---|
2447 | * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW.
|
---|
2448 | * @param pLogger Logger instance (NULL for default logger).
|
---|
2449 | * @param pszBuf The output buffer.
|
---|
2450 | * @param cchBuf The size of the output buffer. Must be greater
|
---|
2451 | * than 0.
|
---|
2452 | */
|
---|
2453 | RTDECL(int) RTLogGetDestinations(PRTLOGGER pLogger, char *pszBuf, size_t cchBuf)
|
---|
2454 | {
|
---|
2455 | bool fNotFirst = false;
|
---|
2456 | int rc = VINF_SUCCESS;
|
---|
2457 | uint32_t fDestFlags;
|
---|
2458 | unsigned i;
|
---|
2459 |
|
---|
2460 | AssertReturn(cchBuf, VERR_INVALID_PARAMETER);
|
---|
2461 | *pszBuf = '\0';
|
---|
2462 |
|
---|
2463 | /*
|
---|
2464 | * Resolve defaults.
|
---|
2465 | */
|
---|
2466 | if (!pLogger)
|
---|
2467 | {
|
---|
2468 | pLogger = RTLogDefaultInstance();
|
---|
2469 | if (!pLogger)
|
---|
2470 | return VINF_SUCCESS;
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | /*
|
---|
2474 | * Add the flags in the list.
|
---|
2475 | */
|
---|
2476 | fDestFlags = pLogger->fDestFlags;
|
---|
2477 | for (i = 6; i < RT_ELEMENTS(g_aLogDst); i++)
|
---|
2478 | if (g_aLogDst[i].fFlag & fDestFlags)
|
---|
2479 | {
|
---|
2480 | if (fNotFirst)
|
---|
2481 | {
|
---|
2482 | rc = RTStrCopyP(&pszBuf, &cchBuf, " ");
|
---|
2483 | if (RT_FAILURE(rc))
|
---|
2484 | return rc;
|
---|
2485 | }
|
---|
2486 | rc = RTStrCopyP(&pszBuf, &cchBuf, g_aLogDst[i].pszInstr);
|
---|
2487 | if (RT_FAILURE(rc))
|
---|
2488 | return rc;
|
---|
2489 | fNotFirst = true;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | char szNum[32];
|
---|
2493 |
|
---|
2494 | # ifdef IN_RING3
|
---|
2495 | /*
|
---|
2496 | * Add the filename.
|
---|
2497 | */
|
---|
2498 | if (fDestFlags & RTLOGDEST_FILE)
|
---|
2499 | {
|
---|
2500 | rc = RTStrCopyP(&pszBuf, &cchBuf, fNotFirst ? " file=" : "file=");
|
---|
2501 | if (RT_FAILURE(rc))
|
---|
2502 | return rc;
|
---|
2503 | rc = RTStrCopyP(&pszBuf, &cchBuf, pLogger->pInt->szFilename);
|
---|
2504 | if (RT_FAILURE(rc))
|
---|
2505 | return rc;
|
---|
2506 | fNotFirst = true;
|
---|
2507 |
|
---|
2508 | if (pLogger->pInt->cHistory)
|
---|
2509 | {
|
---|
2510 | RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? " history=%u" : "history=%u", pLogger->pInt->cHistory);
|
---|
2511 | rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
|
---|
2512 | if (RT_FAILURE(rc))
|
---|
2513 | return rc;
|
---|
2514 | fNotFirst = true;
|
---|
2515 | }
|
---|
2516 | if (pLogger->pInt->cbHistoryFileMax != UINT64_MAX)
|
---|
2517 | {
|
---|
2518 | RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? " histsize=%llu" : "histsize=%llu", pLogger->pInt->cbHistoryFileMax);
|
---|
2519 | rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
|
---|
2520 | if (RT_FAILURE(rc))
|
---|
2521 | return rc;
|
---|
2522 | fNotFirst = true;
|
---|
2523 | }
|
---|
2524 | if (pLogger->pInt->cSecsHistoryTimeSlot != UINT32_MAX)
|
---|
2525 | {
|
---|
2526 | RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? " histtime=%llu" : "histtime=%llu", pLogger->pInt->cSecsHistoryTimeSlot);
|
---|
2527 | rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
|
---|
2528 | if (RT_FAILURE(rc))
|
---|
2529 | return rc;
|
---|
2530 | fNotFirst = true;
|
---|
2531 | }
|
---|
2532 | }
|
---|
2533 | # endif /* IN_RING3 */
|
---|
2534 |
|
---|
2535 | /*
|
---|
2536 | * Add the ring buffer.
|
---|
2537 | */
|
---|
2538 | if (fDestFlags & RTLOGDEST_RINGBUF)
|
---|
2539 | {
|
---|
2540 | if (pLogger->pInt->cbRingBuf == RTLOG_RINGBUF_DEFAULT_SIZE)
|
---|
2541 | rc = RTStrCopyP(&pszBuf, &cchBuf, fNotFirst ? " ringbuf" : "ringbuf");
|
---|
2542 | else
|
---|
2543 | {
|
---|
2544 | RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? " ringbuf=%#x" : "ringbuf=%#x", pLogger->pInt->cbRingBuf);
|
---|
2545 | rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
|
---|
2546 | }
|
---|
2547 | if (RT_FAILURE(rc))
|
---|
2548 | return rc;
|
---|
2549 | fNotFirst = true;
|
---|
2550 | }
|
---|
2551 |
|
---|
2552 | return VINF_SUCCESS;
|
---|
2553 | }
|
---|
2554 | RT_EXPORT_SYMBOL(RTLogGetDestinations);
|
---|
2555 |
|
---|
2556 | #endif /* !IN_RC */
|
---|
2557 |
|
---|
2558 | /**
|
---|
2559 | * Flushes the specified logger.
|
---|
2560 | *
|
---|
2561 | * @param pLogger The logger instance to flush.
|
---|
2562 | * If NULL the default instance is used. The default instance
|
---|
2563 | * will not be initialized by this call.
|
---|
2564 | */
|
---|
2565 | RTDECL(void) RTLogFlush(PRTLOGGER pLogger)
|
---|
2566 | {
|
---|
2567 | /*
|
---|
2568 | * Resolve defaults.
|
---|
2569 | */
|
---|
2570 | if (!pLogger)
|
---|
2571 | {
|
---|
2572 | #ifdef IN_RC
|
---|
2573 | pLogger = &g_Logger;
|
---|
2574 | #else
|
---|
2575 | pLogger = g_pLogger;
|
---|
2576 | #endif
|
---|
2577 | if (!pLogger)
|
---|
2578 | return;
|
---|
2579 | }
|
---|
2580 |
|
---|
2581 | /*
|
---|
2582 | * Any thing to flush?
|
---|
2583 | */
|
---|
2584 | if ( pLogger->offScratch
|
---|
2585 | #ifndef IN_RC
|
---|
2586 | || (pLogger->fDestFlags & RTLOGDEST_RINGBUF)
|
---|
2587 | #endif
|
---|
2588 | )
|
---|
2589 | {
|
---|
2590 | #ifndef IN_RC
|
---|
2591 | /*
|
---|
2592 | * Acquire logger instance sem.
|
---|
2593 | */
|
---|
2594 | int rc = rtlogLock(pLogger);
|
---|
2595 | if (RT_FAILURE(rc))
|
---|
2596 | return;
|
---|
2597 | #endif
|
---|
2598 | /*
|
---|
2599 | * Call worker.
|
---|
2600 | */
|
---|
2601 | rtlogFlush(pLogger);
|
---|
2602 |
|
---|
2603 | #ifndef IN_RC
|
---|
2604 | /*
|
---|
2605 | * Since this is an explicit flush call, the ring buffer content should
|
---|
2606 | * be flushed to the other destinations if active.
|
---|
2607 | */
|
---|
2608 | if ( (pLogger->fDestFlags & RTLOGDEST_RINGBUF)
|
---|
2609 | && pLogger->pInt->pszRingBuf /* paranoia */)
|
---|
2610 | rtLogRingBufFlush(pLogger);
|
---|
2611 |
|
---|
2612 | /*
|
---|
2613 | * Release the semaphore.
|
---|
2614 | */
|
---|
2615 | rtlogUnlock(pLogger);
|
---|
2616 | #endif
|
---|
2617 | }
|
---|
2618 | }
|
---|
2619 | RT_EXPORT_SYMBOL(RTLogFlush);
|
---|
2620 |
|
---|
2621 |
|
---|
2622 | /**
|
---|
2623 | * Common worker for RTLogDefaultInstance and RTLogDefaultInstanceEx.
|
---|
2624 | */
|
---|
2625 | DECL_FORCE_INLINE(PRTLOGGER) rtLogDefaultInstanceCommon(void)
|
---|
2626 | {
|
---|
2627 | #ifdef IN_RC
|
---|
2628 | return &g_Logger;
|
---|
2629 |
|
---|
2630 | #else /* !IN_RC */
|
---|
2631 | # ifdef IN_RING0
|
---|
2632 | /*
|
---|
2633 | * Check per thread loggers first.
|
---|
2634 | */
|
---|
2635 | if (g_cPerThreadLoggers)
|
---|
2636 | {
|
---|
2637 | const RTNATIVETHREAD Self = RTThreadNativeSelf();
|
---|
2638 | int32_t i = RT_ELEMENTS(g_aPerThreadLoggers);
|
---|
2639 | while (i-- > 0)
|
---|
2640 | if (g_aPerThreadLoggers[i].NativeThread == Self)
|
---|
2641 | return g_aPerThreadLoggers[i].pLogger;
|
---|
2642 | }
|
---|
2643 | # endif /* IN_RING0 */
|
---|
2644 |
|
---|
2645 | /*
|
---|
2646 | * If no per thread logger, use the default one.
|
---|
2647 | */
|
---|
2648 | if (!g_pLogger)
|
---|
2649 | g_pLogger = RTLogDefaultInit();
|
---|
2650 | return g_pLogger;
|
---|
2651 | #endif /* !IN_RC */
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 |
|
---|
2655 | RTDECL(PRTLOGGER) RTLogDefaultInstance(void)
|
---|
2656 | {
|
---|
2657 | return rtLogDefaultInstanceCommon();
|
---|
2658 | }
|
---|
2659 | RT_EXPORT_SYMBOL(RTLogDefaultInstance);
|
---|
2660 |
|
---|
2661 |
|
---|
2662 | RTDECL(PRTLOGGER) RTLogDefaultInstanceEx(uint32_t fFlagsAndGroup)
|
---|
2663 | {
|
---|
2664 | PRTLOGGER pLogger = rtLogDefaultInstanceCommon();
|
---|
2665 | if (pLogger)
|
---|
2666 | {
|
---|
2667 | if (pLogger->fFlags & RTLOGFLAGS_DISABLED)
|
---|
2668 | pLogger = NULL;
|
---|
2669 | else
|
---|
2670 | {
|
---|
2671 | uint16_t const fFlags = RT_LO_U16(fFlagsAndGroup);
|
---|
2672 | uint16_t const iGroup = RT_HI_U16(fFlagsAndGroup);
|
---|
2673 | if ( iGroup != UINT16_MAX
|
---|
2674 | && ( (pLogger->afGroups[iGroup < pLogger->cGroups ? iGroup : 0] & (fFlags | (uint32_t)RTLOGGRPFLAGS_ENABLED))
|
---|
2675 | != (fFlags | (uint32_t)RTLOGGRPFLAGS_ENABLED)))
|
---|
2676 | pLogger = NULL;
|
---|
2677 | }
|
---|
2678 | }
|
---|
2679 | return pLogger;
|
---|
2680 | }
|
---|
2681 | RT_EXPORT_SYMBOL(RTLogDefaultInstanceEx);
|
---|
2682 |
|
---|
2683 |
|
---|
2684 | /**
|
---|
2685 | * Common worker for RTLogGetDefaultInstance and RTLogGetDefaultInstanceEx.
|
---|
2686 | */
|
---|
2687 | DECL_FORCE_INLINE(PRTLOGGER) rtLogGetDefaultInstanceCommon(void)
|
---|
2688 | {
|
---|
2689 | #ifdef IN_RC
|
---|
2690 | return &g_Logger;
|
---|
2691 | #else
|
---|
2692 | # ifdef IN_RING0
|
---|
2693 | /*
|
---|
2694 | * Check per thread loggers first.
|
---|
2695 | */
|
---|
2696 | if (g_cPerThreadLoggers)
|
---|
2697 | {
|
---|
2698 | const RTNATIVETHREAD Self = RTThreadNativeSelf();
|
---|
2699 | int32_t i = RT_ELEMENTS(g_aPerThreadLoggers);
|
---|
2700 | while (i-- > 0)
|
---|
2701 | if (g_aPerThreadLoggers[i].NativeThread == Self)
|
---|
2702 | return g_aPerThreadLoggers[i].pLogger;
|
---|
2703 | }
|
---|
2704 | # endif /* IN_RING0 */
|
---|
2705 |
|
---|
2706 | return g_pLogger;
|
---|
2707 | #endif
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 |
|
---|
2711 | RTDECL(PRTLOGGER) RTLogGetDefaultInstance(void)
|
---|
2712 | {
|
---|
2713 | return rtLogGetDefaultInstanceCommon();
|
---|
2714 | }
|
---|
2715 | RT_EXPORT_SYMBOL(RTLogGetDefaultInstance);
|
---|
2716 |
|
---|
2717 |
|
---|
2718 | RTDECL(PRTLOGGER) RTLogGetDefaultInstanceEx(uint32_t fFlagsAndGroup)
|
---|
2719 | {
|
---|
2720 | PRTLOGGER pLogger = rtLogGetDefaultInstanceCommon();
|
---|
2721 | if (pLogger)
|
---|
2722 | {
|
---|
2723 | if (pLogger->fFlags & RTLOGFLAGS_DISABLED)
|
---|
2724 | pLogger = NULL;
|
---|
2725 | else
|
---|
2726 | {
|
---|
2727 | uint32_t const fFlags = RT_LO_U16(fFlagsAndGroup);
|
---|
2728 | uint16_t const iGroup = RT_HI_U16(fFlagsAndGroup);
|
---|
2729 | if ( iGroup != UINT16_MAX
|
---|
2730 | && ( (pLogger->afGroups[iGroup < pLogger->cGroups ? iGroup : 0] & (fFlags | RTLOGGRPFLAGS_ENABLED))
|
---|
2731 | != (fFlags | RTLOGGRPFLAGS_ENABLED)))
|
---|
2732 | pLogger = NULL;
|
---|
2733 | }
|
---|
2734 | }
|
---|
2735 | return pLogger;
|
---|
2736 | }
|
---|
2737 | RT_EXPORT_SYMBOL(RTLogGetDefaultInstanceEx);
|
---|
2738 |
|
---|
2739 |
|
---|
2740 | #ifndef IN_RC
|
---|
2741 | /**
|
---|
2742 | * Sets the default logger instance.
|
---|
2743 | *
|
---|
2744 | * @returns iprt status code.
|
---|
2745 | * @param pLogger The new default logger instance.
|
---|
2746 | */
|
---|
2747 | RTDECL(PRTLOGGER) RTLogSetDefaultInstance(PRTLOGGER pLogger)
|
---|
2748 | {
|
---|
2749 | return ASMAtomicXchgPtrT(&g_pLogger, pLogger, PRTLOGGER);
|
---|
2750 | }
|
---|
2751 | RT_EXPORT_SYMBOL(RTLogSetDefaultInstance);
|
---|
2752 | #endif /* !IN_RC */
|
---|
2753 |
|
---|
2754 |
|
---|
2755 | #ifdef IN_RING0
|
---|
2756 | /**
|
---|
2757 | * Changes the default logger instance for the current thread.
|
---|
2758 | *
|
---|
2759 | * @returns IPRT status code.
|
---|
2760 | * @param pLogger The logger instance. Pass NULL for deregistration.
|
---|
2761 | * @param uKey Associated key for cleanup purposes. If pLogger is NULL,
|
---|
2762 | * all instances with this key will be deregistered. So in
|
---|
2763 | * order to only deregister the instance associated with the
|
---|
2764 | * current thread use 0.
|
---|
2765 | */
|
---|
2766 | RTDECL(int) RTLogSetDefaultInstanceThread(PRTLOGGER pLogger, uintptr_t uKey)
|
---|
2767 | {
|
---|
2768 | int rc;
|
---|
2769 | RTNATIVETHREAD Self = RTThreadNativeSelf();
|
---|
2770 | if (pLogger)
|
---|
2771 | {
|
---|
2772 | int32_t i;
|
---|
2773 | unsigned j;
|
---|
2774 |
|
---|
2775 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
2776 |
|
---|
2777 | /*
|
---|
2778 | * Iterate the table to see if there is already an entry for this thread.
|
---|
2779 | */
|
---|
2780 | i = RT_ELEMENTS(g_aPerThreadLoggers);
|
---|
2781 | while (i-- > 0)
|
---|
2782 | if (g_aPerThreadLoggers[i].NativeThread == Self)
|
---|
2783 | {
|
---|
2784 | ASMAtomicWritePtr((void * volatile *)&g_aPerThreadLoggers[i].uKey, (void *)uKey);
|
---|
2785 | g_aPerThreadLoggers[i].pLogger = pLogger;
|
---|
2786 | return VINF_SUCCESS;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | /*
|
---|
2790 | * Allocate a new table entry.
|
---|
2791 | */
|
---|
2792 | i = ASMAtomicIncS32(&g_cPerThreadLoggers);
|
---|
2793 | if (i > (int32_t)RT_ELEMENTS(g_aPerThreadLoggers))
|
---|
2794 | {
|
---|
2795 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
2796 | return VERR_BUFFER_OVERFLOW; /* horrible error code! */
|
---|
2797 | }
|
---|
2798 |
|
---|
2799 | for (j = 0; j < 10; j++)
|
---|
2800 | {
|
---|
2801 | i = RT_ELEMENTS(g_aPerThreadLoggers);
|
---|
2802 | while (i-- > 0)
|
---|
2803 | {
|
---|
2804 | AssertCompile(sizeof(RTNATIVETHREAD) == sizeof(void*));
|
---|
2805 | if ( g_aPerThreadLoggers[i].NativeThread == NIL_RTNATIVETHREAD
|
---|
2806 | && ASMAtomicCmpXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].NativeThread, (void *)Self, (void *)NIL_RTNATIVETHREAD))
|
---|
2807 | {
|
---|
2808 | ASMAtomicWritePtr((void * volatile *)&g_aPerThreadLoggers[i].uKey, (void *)uKey);
|
---|
2809 | ASMAtomicWritePtr(&g_aPerThreadLoggers[i].pLogger, pLogger);
|
---|
2810 | return VINF_SUCCESS;
|
---|
2811 | }
|
---|
2812 | }
|
---|
2813 | }
|
---|
2814 |
|
---|
2815 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
2816 | rc = VERR_INTERNAL_ERROR;
|
---|
2817 | }
|
---|
2818 | else
|
---|
2819 | {
|
---|
2820 | /*
|
---|
2821 | * Search the array for the current thread.
|
---|
2822 | */
|
---|
2823 | int32_t i = RT_ELEMENTS(g_aPerThreadLoggers);
|
---|
2824 | while (i-- > 0)
|
---|
2825 | if ( g_aPerThreadLoggers[i].NativeThread == Self
|
---|
2826 | || g_aPerThreadLoggers[i].uKey == uKey)
|
---|
2827 | {
|
---|
2828 | ASMAtomicWriteNullPtr((void * volatile *)&g_aPerThreadLoggers[i].uKey);
|
---|
2829 | ASMAtomicWriteNullPtr(&g_aPerThreadLoggers[i].pLogger);
|
---|
2830 | ASMAtomicWriteHandle(&g_aPerThreadLoggers[i].NativeThread, NIL_RTNATIVETHREAD);
|
---|
2831 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
2832 | }
|
---|
2833 |
|
---|
2834 | rc = VINF_SUCCESS;
|
---|
2835 | }
|
---|
2836 | return rc;
|
---|
2837 | }
|
---|
2838 | RT_EXPORT_SYMBOL(RTLogSetDefaultInstanceThread);
|
---|
2839 | #endif /* IN_RING0 */
|
---|
2840 |
|
---|
2841 |
|
---|
2842 | /**
|
---|
2843 | * Write to a logger instance.
|
---|
2844 | *
|
---|
2845 | * @param pLogger Pointer to logger instance.
|
---|
2846 | * @param pszFormat Format string.
|
---|
2847 | * @param args Format arguments.
|
---|
2848 | */
|
---|
2849 | RTDECL(void) RTLogLoggerV(PRTLOGGER pLogger, const char *pszFormat, va_list args)
|
---|
2850 | {
|
---|
2851 | RTLogLoggerExV(pLogger, 0, ~0U, pszFormat, args);
|
---|
2852 | }
|
---|
2853 | RT_EXPORT_SYMBOL(RTLogLoggerV);
|
---|
2854 |
|
---|
2855 |
|
---|
2856 | /**
|
---|
2857 | * Write to a logger instance.
|
---|
2858 | *
|
---|
2859 | * This function will check whether the instance, group and flags makes up a
|
---|
2860 | * logging kind which is currently enabled before writing anything to the log.
|
---|
2861 | *
|
---|
2862 | * @param pLogger Pointer to logger instance. If NULL the default logger instance will be attempted.
|
---|
2863 | * @param fFlags The logging flags.
|
---|
2864 | * @param iGroup The group.
|
---|
2865 | * The value ~0U is reserved for compatibility with RTLogLogger[V] and is
|
---|
2866 | * only for internal usage!
|
---|
2867 | * @param pszFormat Format string.
|
---|
2868 | * @param args Format arguments.
|
---|
2869 | */
|
---|
2870 | RTDECL(void) RTLogLoggerExV(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
|
---|
2871 | {
|
---|
2872 | int rc;
|
---|
2873 |
|
---|
2874 | /*
|
---|
2875 | * A NULL logger means default instance.
|
---|
2876 | */
|
---|
2877 | if (!pLogger)
|
---|
2878 | {
|
---|
2879 | pLogger = RTLogDefaultInstance();
|
---|
2880 | if (!pLogger)
|
---|
2881 | return;
|
---|
2882 | }
|
---|
2883 |
|
---|
2884 | /*
|
---|
2885 | * Validate and correct iGroup.
|
---|
2886 | */
|
---|
2887 | if (iGroup != ~0U && iGroup >= pLogger->cGroups)
|
---|
2888 | iGroup = 0;
|
---|
2889 |
|
---|
2890 | /*
|
---|
2891 | * If no output, then just skip it.
|
---|
2892 | */
|
---|
2893 | if ( (pLogger->fFlags & RTLOGFLAGS_DISABLED)
|
---|
2894 | #ifndef IN_RC
|
---|
2895 | || !pLogger->fDestFlags
|
---|
2896 | #endif
|
---|
2897 | || !pszFormat || !*pszFormat)
|
---|
2898 | return;
|
---|
2899 | if ( iGroup != ~0U
|
---|
2900 | && (pLogger->afGroups[iGroup] & (fFlags | RTLOGGRPFLAGS_ENABLED)) != (fFlags | RTLOGGRPFLAGS_ENABLED))
|
---|
2901 | return;
|
---|
2902 |
|
---|
2903 | /*
|
---|
2904 | * Acquire logger instance sem.
|
---|
2905 | */
|
---|
2906 | rc = rtlogLock(pLogger);
|
---|
2907 | if (RT_FAILURE(rc))
|
---|
2908 | {
|
---|
2909 | #ifdef IN_RING0
|
---|
2910 | if (pLogger->fDestFlags & ~RTLOGDEST_FILE)
|
---|
2911 | rtR0LogLoggerExFallback(pLogger->fDestFlags, pLogger->fFlags, pLogger->pInt, pszFormat, args);
|
---|
2912 | #endif
|
---|
2913 | return;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | /*
|
---|
2917 | * Check restrictions and call worker.
|
---|
2918 | */
|
---|
2919 | #ifndef IN_RC
|
---|
2920 | if (RT_UNLIKELY( (pLogger->fFlags & RTLOGFLAGS_RESTRICT_GROUPS)
|
---|
2921 | && iGroup < pLogger->cGroups
|
---|
2922 | && (pLogger->afGroups[iGroup] & RTLOGGRPFLAGS_RESTRICT)
|
---|
2923 | && ++pLogger->pInt->pacEntriesPerGroup[iGroup] >= pLogger->pInt->cMaxEntriesPerGroup ))
|
---|
2924 | {
|
---|
2925 | uint32_t cEntries = pLogger->pInt->pacEntriesPerGroup[iGroup];
|
---|
2926 | if (cEntries > pLogger->pInt->cMaxEntriesPerGroup)
|
---|
2927 | pLogger->pInt->pacEntriesPerGroup[iGroup] = cEntries - 1;
|
---|
2928 | else
|
---|
2929 | {
|
---|
2930 | rtlogLoggerExVLocked(pLogger, fFlags, iGroup, pszFormat, args);
|
---|
2931 | if ( pLogger->pInt->papszGroups
|
---|
2932 | && pLogger->pInt->papszGroups[iGroup])
|
---|
2933 | rtlogLoggerExFLocked(pLogger, fFlags, iGroup, "%u messages from group %s (#%u), muting it.\n",
|
---|
2934 | cEntries, pLogger->pInt->papszGroups[iGroup], iGroup);
|
---|
2935 | else
|
---|
2936 | rtlogLoggerExFLocked(pLogger, fFlags, iGroup, "%u messages from group #%u, muting it.\n",
|
---|
2937 | cEntries, iGroup);
|
---|
2938 | }
|
---|
2939 | }
|
---|
2940 | else
|
---|
2941 | #endif
|
---|
2942 | rtlogLoggerExVLocked(pLogger, fFlags, iGroup, pszFormat, args);
|
---|
2943 |
|
---|
2944 | /*
|
---|
2945 | * Release the semaphore.
|
---|
2946 | */
|
---|
2947 | rtlogUnlock(pLogger);
|
---|
2948 | }
|
---|
2949 | RT_EXPORT_SYMBOL(RTLogLoggerExV);
|
---|
2950 |
|
---|
2951 |
|
---|
2952 | #ifdef IN_RING0
|
---|
2953 | /**
|
---|
2954 | * For rtR0LogLoggerExFallbackOutput and rtR0LogLoggerExFallbackFlush.
|
---|
2955 | */
|
---|
2956 | typedef struct RTR0LOGLOGGERFALLBACK
|
---|
2957 | {
|
---|
2958 | /** The current scratch buffer offset. */
|
---|
2959 | uint32_t offScratch;
|
---|
2960 | /** The destination flags. */
|
---|
2961 | uint32_t fDestFlags;
|
---|
2962 | /** For ring buffer output. */
|
---|
2963 | PRTLOGGERINTERNAL pInt;
|
---|
2964 | /** The scratch buffer. */
|
---|
2965 | char achScratch[80];
|
---|
2966 | } RTR0LOGLOGGERFALLBACK;
|
---|
2967 | /** Pointer to RTR0LOGLOGGERFALLBACK which is used by
|
---|
2968 | * rtR0LogLoggerExFallbackOutput. */
|
---|
2969 | typedef RTR0LOGLOGGERFALLBACK *PRTR0LOGLOGGERFALLBACK;
|
---|
2970 |
|
---|
2971 |
|
---|
2972 | /**
|
---|
2973 | * Flushes the fallback buffer.
|
---|
2974 | *
|
---|
2975 | * @param pThis The scratch buffer.
|
---|
2976 | */
|
---|
2977 | static void rtR0LogLoggerExFallbackFlush(PRTR0LOGLOGGERFALLBACK pThis)
|
---|
2978 | {
|
---|
2979 | if (!pThis->offScratch)
|
---|
2980 | return;
|
---|
2981 |
|
---|
2982 | if ( (pThis->fDestFlags & RTLOGDEST_RINGBUF)
|
---|
2983 | && pThis->pInt
|
---|
2984 | && pThis->pInt->pszRingBuf /* paranoia */)
|
---|
2985 | rtLogRingBufWrite(pThis->pInt, pThis->achScratch, pThis->offScratch);
|
---|
2986 | else
|
---|
2987 | {
|
---|
2988 | if (pThis->fDestFlags & RTLOGDEST_USER)
|
---|
2989 | RTLogWriteUser(pThis->achScratch, pThis->offScratch);
|
---|
2990 |
|
---|
2991 | if (pThis->fDestFlags & RTLOGDEST_DEBUGGER)
|
---|
2992 | RTLogWriteDebugger(pThis->achScratch, pThis->offScratch);
|
---|
2993 |
|
---|
2994 | if (pThis->fDestFlags & RTLOGDEST_STDOUT)
|
---|
2995 | RTLogWriteStdOut(pThis->achScratch, pThis->offScratch);
|
---|
2996 |
|
---|
2997 | if (pThis->fDestFlags & RTLOGDEST_STDERR)
|
---|
2998 | RTLogWriteStdErr(pThis->achScratch, pThis->offScratch);
|
---|
2999 |
|
---|
3000 | # ifndef LOG_NO_COM
|
---|
3001 | if (pThis->fDestFlags & RTLOGDEST_COM)
|
---|
3002 | RTLogWriteCom(pThis->achScratch, pThis->offScratch);
|
---|
3003 | # endif
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | /* empty the buffer. */
|
---|
3007 | pThis->offScratch = 0;
|
---|
3008 | }
|
---|
3009 |
|
---|
3010 |
|
---|
3011 | /**
|
---|
3012 | * Callback for RTLogFormatV used by rtR0LogLoggerExFallback.
|
---|
3013 | * See PFNLOGOUTPUT() for details.
|
---|
3014 | */
|
---|
3015 | static DECLCALLBACK(size_t) rtR0LogLoggerExFallbackOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
3016 | {
|
---|
3017 | PRTR0LOGLOGGERFALLBACK pThis = (PRTR0LOGLOGGERFALLBACK)pv;
|
---|
3018 | if (cbChars)
|
---|
3019 | {
|
---|
3020 | size_t cbRet = 0;
|
---|
3021 | for (;;)
|
---|
3022 | {
|
---|
3023 | /* how much */
|
---|
3024 | uint32_t cb = sizeof(pThis->achScratch) - pThis->offScratch - 1; /* minus 1 - for the string terminator. */
|
---|
3025 | if (cb > cbChars)
|
---|
3026 | cb = (uint32_t)cbChars;
|
---|
3027 |
|
---|
3028 | /* copy */
|
---|
3029 | memcpy(&pThis->achScratch[pThis->offScratch], pachChars, cb);
|
---|
3030 |
|
---|
3031 | /* advance */
|
---|
3032 | pThis->offScratch += cb;
|
---|
3033 | cbRet += cb;
|
---|
3034 | cbChars -= cb;
|
---|
3035 |
|
---|
3036 | /* done? */
|
---|
3037 | if (cbChars <= 0)
|
---|
3038 | return cbRet;
|
---|
3039 |
|
---|
3040 | pachChars += cb;
|
---|
3041 |
|
---|
3042 | /* flush */
|
---|
3043 | pThis->achScratch[pThis->offScratch] = '\0';
|
---|
3044 | rtR0LogLoggerExFallbackFlush(pThis);
|
---|
3045 | }
|
---|
3046 |
|
---|
3047 | /* won't ever get here! */
|
---|
3048 | }
|
---|
3049 | else
|
---|
3050 | {
|
---|
3051 | /*
|
---|
3052 | * Termination call, flush the log.
|
---|
3053 | */
|
---|
3054 | pThis->achScratch[pThis->offScratch] = '\0';
|
---|
3055 | rtR0LogLoggerExFallbackFlush(pThis);
|
---|
3056 | return 0;
|
---|
3057 | }
|
---|
3058 | }
|
---|
3059 |
|
---|
3060 |
|
---|
3061 | /**
|
---|
3062 | * Ring-0 fallback for cases where we're unable to grab the lock.
|
---|
3063 | *
|
---|
3064 | * This will happen when we're at a too high IRQL on Windows for instance and
|
---|
3065 | * needs to be dealt with or we'll drop a lot of log output. This fallback will
|
---|
3066 | * only output to some of the log destinations as a few of them may be doing
|
---|
3067 | * dangerous things. We won't be doing any prefixing here either, at least not
|
---|
3068 | * for the present, because it's too much hassle.
|
---|
3069 | *
|
---|
3070 | * @param fDestFlags The destination flags.
|
---|
3071 | * @param fFlags The logger flags.
|
---|
3072 | * @param pInt The internal logger data, for ring buffer output.
|
---|
3073 | * @param pszFormat The format string.
|
---|
3074 | * @param va The format arguments.
|
---|
3075 | */
|
---|
3076 | static void rtR0LogLoggerExFallback(uint32_t fDestFlags, uint32_t fFlags, PRTLOGGERINTERNAL pInt,
|
---|
3077 | const char *pszFormat, va_list va)
|
---|
3078 | {
|
---|
3079 | RTR0LOGLOGGERFALLBACK This;
|
---|
3080 | This.fDestFlags = fDestFlags;
|
---|
3081 | This.pInt = pInt;
|
---|
3082 |
|
---|
3083 | /* fallback indicator. */
|
---|
3084 | This.offScratch = 2;
|
---|
3085 | This.achScratch[0] = '[';
|
---|
3086 | This.achScratch[1] = 'F';
|
---|
3087 |
|
---|
3088 | /* selected prefixes */
|
---|
3089 | if (fFlags & RTLOGFLAGS_PREFIX_PID)
|
---|
3090 | {
|
---|
3091 | RTPROCESS Process = RTProcSelf();
|
---|
3092 | This.achScratch[This.offScratch++] = ' ';
|
---|
3093 | This.offScratch += RTStrFormatNumber(&This.achScratch[This.offScratch], Process, 16, sizeof(RTPROCESS) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
3094 | }
|
---|
3095 | if (fFlags & RTLOGFLAGS_PREFIX_TID)
|
---|
3096 | {
|
---|
3097 | RTNATIVETHREAD Thread = RTThreadNativeSelf();
|
---|
3098 | This.achScratch[This.offScratch++] = ' ';
|
---|
3099 | This.offScratch += RTStrFormatNumber(&This.achScratch[This.offScratch], Thread, 16, sizeof(RTNATIVETHREAD) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
3100 | }
|
---|
3101 |
|
---|
3102 | This.achScratch[This.offScratch++] = ']';
|
---|
3103 | This.achScratch[This.offScratch++] = ' ';
|
---|
3104 |
|
---|
3105 | RTLogFormatV(rtR0LogLoggerExFallbackOutput, &This, pszFormat, va);
|
---|
3106 | }
|
---|
3107 | #endif /* IN_RING0 */
|
---|
3108 |
|
---|
3109 |
|
---|
3110 | /**
|
---|
3111 | * vprintf like function for writing to the default log.
|
---|
3112 | *
|
---|
3113 | * @param pszFormat Printf like format string.
|
---|
3114 | * @param va Optional arguments as specified in pszFormat.
|
---|
3115 | *
|
---|
3116 | * @remark The API doesn't support formatting of floating point numbers at the moment.
|
---|
3117 | */
|
---|
3118 | RTDECL(void) RTLogPrintfV(const char *pszFormat, va_list va)
|
---|
3119 | {
|
---|
3120 | RTLogLoggerV(NULL, pszFormat, va);
|
---|
3121 | }
|
---|
3122 | RT_EXPORT_SYMBOL(RTLogPrintfV);
|
---|
3123 |
|
---|
3124 |
|
---|
3125 | /**
|
---|
3126 | * Dumper vprintf-like function outputting to a logger.
|
---|
3127 | *
|
---|
3128 | * @param pvUser Pointer to the logger instance to use, NULL for
|
---|
3129 | * default instance.
|
---|
3130 | * @param pszFormat Format string.
|
---|
3131 | * @param va Format arguments.
|
---|
3132 | */
|
---|
3133 | RTDECL(void) RTLogDumpPrintfV(void *pvUser, const char *pszFormat, va_list va)
|
---|
3134 | {
|
---|
3135 | RTLogLoggerV((PRTLOGGER)pvUser, pszFormat, va);
|
---|
3136 | }
|
---|
3137 | RT_EXPORT_SYMBOL(RTLogDumpPrintfV);
|
---|
3138 |
|
---|
3139 |
|
---|
3140 | #ifdef IN_RING3
|
---|
3141 |
|
---|
3142 | /**
|
---|
3143 | * Opens/creates the log file.
|
---|
3144 | *
|
---|
3145 | * @param pLogger The logger instance to update. NULL is not allowed!
|
---|
3146 | * @param pszErrorMsg A buffer which is filled with an error message if
|
---|
3147 | * something fails. May be NULL.
|
---|
3148 | * @param cchErrorMsg The size of the error message buffer.
|
---|
3149 | */
|
---|
3150 | static int rtlogFileOpen(PRTLOGGER pLogger, char *pszErrorMsg, size_t cchErrorMsg)
|
---|
3151 | {
|
---|
3152 | uint32_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_NONE;
|
---|
3153 | if (pLogger->fFlags & RTLOGFLAGS_APPEND)
|
---|
3154 | fOpen |= RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND;
|
---|
3155 | else
|
---|
3156 | fOpen |= RTFILE_O_CREATE_REPLACE;
|
---|
3157 | if (pLogger->fFlags & RTLOGFLAGS_WRITE_THROUGH)
|
---|
3158 | fOpen |= RTFILE_O_WRITE_THROUGH;
|
---|
3159 |
|
---|
3160 | unsigned cBackoff = 0;
|
---|
3161 | int rc = RTFileOpen(&pLogger->pInt->hFile, pLogger->pInt->szFilename, fOpen);
|
---|
3162 | while ( rc == VERR_SHARING_VIOLATION
|
---|
3163 | && cBackoff < RT_ELEMENTS(g_acMsLogBackoff))
|
---|
3164 | {
|
---|
3165 | RTThreadSleep(g_acMsLogBackoff[cBackoff++]);
|
---|
3166 | rc = RTFileOpen(&pLogger->pInt->hFile, pLogger->pInt->szFilename, fOpen);
|
---|
3167 | }
|
---|
3168 | if (RT_SUCCESS(rc))
|
---|
3169 | {
|
---|
3170 | rc = RTFileGetSize(pLogger->pInt->hFile, &pLogger->pInt->cbHistoryFileWritten);
|
---|
3171 | if (RT_FAILURE(rc))
|
---|
3172 | {
|
---|
3173 | /* Don't complain if this fails, assume the file is empty. */
|
---|
3174 | pLogger->pInt->cbHistoryFileWritten = 0;
|
---|
3175 | rc = VINF_SUCCESS;
|
---|
3176 | }
|
---|
3177 | }
|
---|
3178 | else
|
---|
3179 | {
|
---|
3180 | pLogger->pInt->hFile = NIL_RTFILE;
|
---|
3181 | if (pszErrorMsg)
|
---|
3182 | RTStrPrintf(pszErrorMsg, cchErrorMsg, N_("could not open file '%s' (fOpen=%#x)"), pLogger->pInt->szFilename, fOpen);
|
---|
3183 | }
|
---|
3184 | return rc;
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 |
|
---|
3188 | /**
|
---|
3189 | * Closes, rotates and opens the log files if necessary.
|
---|
3190 | *
|
---|
3191 | * Used by the rtlogFlush() function as well as RTLogCreateExV.
|
---|
3192 | *
|
---|
3193 | * @param pLogger The logger instance to update. NULL is not allowed!
|
---|
3194 | * @param uTimeSlot Current time slot (for tikme based rotation).
|
---|
3195 | * @param fFirst Flag whether this is the beginning of logging, i.e.
|
---|
3196 | * called from RTLogCreateExV. Prevents pfnPhase from
|
---|
3197 | * being called.
|
---|
3198 | */
|
---|
3199 | static void rtlogRotate(PRTLOGGER pLogger, uint32_t uTimeSlot, bool fFirst)
|
---|
3200 | {
|
---|
3201 | /* Suppress rotating empty log files simply because the time elapsed. */
|
---|
3202 | if (RT_UNLIKELY(!pLogger->pInt->cbHistoryFileWritten))
|
---|
3203 | pLogger->pInt->uHistoryTimeSlotStart = uTimeSlot;
|
---|
3204 |
|
---|
3205 | /* Check rotation condition: file still small enough and not too old? */
|
---|
3206 | if (RT_LIKELY( pLogger->pInt->cbHistoryFileWritten < pLogger->pInt->cbHistoryFileMax
|
---|
3207 | && uTimeSlot == pLogger->pInt->uHistoryTimeSlotStart))
|
---|
3208 | return;
|
---|
3209 |
|
---|
3210 | /*
|
---|
3211 | * Save "disabled" log flag and make sure logging is disabled.
|
---|
3212 | * The logging in the functions called during log file history
|
---|
3213 | * rotation would cause severe trouble otherwise.
|
---|
3214 | */
|
---|
3215 | uint32_t const fSavedFlags = pLogger->fFlags;
|
---|
3216 | pLogger->fFlags |= RTLOGFLAGS_DISABLED;
|
---|
3217 |
|
---|
3218 | /*
|
---|
3219 | * Disable log rotation temporarily, otherwise with extreme settings and
|
---|
3220 | * chatty phase logging we could run into endless rotation.
|
---|
3221 | */
|
---|
3222 | uint32_t const cSavedHistory = pLogger->pInt->cHistory;
|
---|
3223 | pLogger->pInt->cHistory = 0;
|
---|
3224 |
|
---|
3225 | /*
|
---|
3226 | * Close the old log file.
|
---|
3227 | */
|
---|
3228 | if (pLogger->pInt->hFile != NIL_RTFILE)
|
---|
3229 | {
|
---|
3230 | /* Use the callback to generate some final log contents, but only if
|
---|
3231 | * this is a rotation with a fully set up logger. Leave the other case
|
---|
3232 | * to the RTLogCreateExV function. */
|
---|
3233 | if (pLogger->pInt->pfnPhase && !fFirst)
|
---|
3234 | {
|
---|
3235 | uint32_t fODestFlags = pLogger->fDestFlags;
|
---|
3236 | pLogger->fDestFlags &= RTLOGDEST_FILE;
|
---|
3237 | pLogger->pInt->pfnPhase(pLogger, RTLOGPHASE_PREROTATE, rtlogPhaseMsgLocked);
|
---|
3238 | pLogger->fDestFlags = fODestFlags;
|
---|
3239 | }
|
---|
3240 | RTFileClose(pLogger->pInt->hFile);
|
---|
3241 | pLogger->pInt->hFile = NIL_RTFILE;
|
---|
3242 | }
|
---|
3243 |
|
---|
3244 | if (cSavedHistory)
|
---|
3245 | {
|
---|
3246 | /*
|
---|
3247 | * Rotate the log files.
|
---|
3248 | */
|
---|
3249 | for (uint32_t i = cSavedHistory - 1; i + 1 > 0; i--)
|
---|
3250 | {
|
---|
3251 | char szOldName[sizeof(pLogger->pInt->szFilename) + 32];
|
---|
3252 | if (i > 0)
|
---|
3253 | RTStrPrintf(szOldName, sizeof(szOldName), "%s.%u", pLogger->pInt->szFilename, i);
|
---|
3254 | else
|
---|
3255 | RTStrCopy(szOldName, sizeof(szOldName), pLogger->pInt->szFilename);
|
---|
3256 |
|
---|
3257 | char szNewName[sizeof(pLogger->pInt->szFilename) + 32];
|
---|
3258 | RTStrPrintf(szNewName, sizeof(szNewName), "%s.%u", pLogger->pInt->szFilename, i + 1);
|
---|
3259 |
|
---|
3260 | unsigned cBackoff = 0;
|
---|
3261 | int rc = RTFileRename(szOldName, szNewName, RTFILEMOVE_FLAGS_REPLACE);
|
---|
3262 | while ( rc == VERR_SHARING_VIOLATION
|
---|
3263 | && cBackoff < RT_ELEMENTS(g_acMsLogBackoff))
|
---|
3264 | {
|
---|
3265 | RTThreadSleep(g_acMsLogBackoff[cBackoff++]);
|
---|
3266 | rc = RTFileRename(szOldName, szNewName, RTFILEMOVE_FLAGS_REPLACE);
|
---|
3267 | }
|
---|
3268 |
|
---|
3269 | if (rc == VERR_FILE_NOT_FOUND)
|
---|
3270 | RTFileDelete(szNewName);
|
---|
3271 | }
|
---|
3272 |
|
---|
3273 | /*
|
---|
3274 | * Delete excess log files.
|
---|
3275 | */
|
---|
3276 | for (uint32_t i = cSavedHistory + 1; ; i++)
|
---|
3277 | {
|
---|
3278 | char szExcessName[sizeof(pLogger->pInt->szFilename) + 32];
|
---|
3279 | RTStrPrintf(szExcessName, sizeof(szExcessName), "%s.%u", pLogger->pInt->szFilename, i);
|
---|
3280 | int rc = RTFileDelete(szExcessName);
|
---|
3281 | if (RT_FAILURE(rc))
|
---|
3282 | break;
|
---|
3283 | }
|
---|
3284 | }
|
---|
3285 |
|
---|
3286 | /*
|
---|
3287 | * Update logger state and create new log file.
|
---|
3288 | */
|
---|
3289 | pLogger->pInt->cbHistoryFileWritten = 0;
|
---|
3290 | pLogger->pInt->uHistoryTimeSlotStart = uTimeSlot;
|
---|
3291 | rtlogFileOpen(pLogger, NULL, 0);
|
---|
3292 |
|
---|
3293 | /*
|
---|
3294 | * Use the callback to generate some initial log contents, but only if this
|
---|
3295 | * is a rotation with a fully set up logger. Leave the other case to the
|
---|
3296 | * RTLogCreateExV function.
|
---|
3297 | */
|
---|
3298 | if (pLogger->pInt->pfnPhase && !fFirst)
|
---|
3299 | {
|
---|
3300 | uint32_t const fSavedDestFlags = pLogger->fDestFlags;
|
---|
3301 | pLogger->fDestFlags &= RTLOGDEST_FILE;
|
---|
3302 | pLogger->pInt->pfnPhase(pLogger, RTLOGPHASE_POSTROTATE, rtlogPhaseMsgLocked);
|
---|
3303 | pLogger->fDestFlags = fSavedDestFlags;
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | /* Restore saved values. */
|
---|
3307 | pLogger->pInt->cHistory = cSavedHistory;
|
---|
3308 | pLogger->fFlags = fSavedFlags;
|
---|
3309 | }
|
---|
3310 |
|
---|
3311 | #endif /* IN_RING3 */
|
---|
3312 |
|
---|
3313 |
|
---|
3314 | /**
|
---|
3315 | * Writes the buffer to the given log device without checking for buffered
|
---|
3316 | * data or anything.
|
---|
3317 | * Used by the RTLogFlush() function.
|
---|
3318 | *
|
---|
3319 | * @param pLogger The logger instance to write to. NULL is not allowed!
|
---|
3320 | */
|
---|
3321 | static void rtlogFlush(PRTLOGGER pLogger)
|
---|
3322 | {
|
---|
3323 | uint32_t const cchScratch = pLogger->offScratch;
|
---|
3324 | if (cchScratch == 0)
|
---|
3325 | return; /* nothing to flush. */
|
---|
3326 |
|
---|
3327 | #ifndef IN_RC
|
---|
3328 | /*
|
---|
3329 | * If the ring buffer is active, the other destinations are only written
|
---|
3330 | * to when the ring buffer is flushed by RTLogFlush().
|
---|
3331 | */
|
---|
3332 | if ( (pLogger->fDestFlags & RTLOGDEST_RINGBUF)
|
---|
3333 | && pLogger->pInt
|
---|
3334 | && pLogger->pInt->pszRingBuf /* paraoia */)
|
---|
3335 | {
|
---|
3336 | rtLogRingBufWrite(pLogger->pInt, pLogger->achScratch, pLogger->offScratch);
|
---|
3337 | pLogger->offScratch = 0; /* empty the buffer. */
|
---|
3338 | }
|
---|
3339 | else
|
---|
3340 | #endif
|
---|
3341 | {
|
---|
3342 | /* Make sure the string is terminated. On Windows, RTLogWriteDebugger
|
---|
3343 | will get upset if it isn't. */
|
---|
3344 | if (RT_LIKELY(cchScratch < sizeof(pLogger->achScratch)))
|
---|
3345 | pLogger->achScratch[cchScratch] = '\0';
|
---|
3346 | else
|
---|
3347 | AssertFailed();
|
---|
3348 |
|
---|
3349 | #ifndef IN_RC
|
---|
3350 | if (pLogger->fDestFlags & RTLOGDEST_USER)
|
---|
3351 | RTLogWriteUser(pLogger->achScratch, cchScratch);
|
---|
3352 |
|
---|
3353 | if (pLogger->fDestFlags & RTLOGDEST_DEBUGGER)
|
---|
3354 | RTLogWriteDebugger(pLogger->achScratch, cchScratch);
|
---|
3355 |
|
---|
3356 | # ifdef IN_RING3
|
---|
3357 | if ((pLogger->fDestFlags & (RTLOGDEST_FILE | RTLOGDEST_RINGBUF)) == RTLOGDEST_FILE)
|
---|
3358 | {
|
---|
3359 | if (pLogger->pInt->hFile != NIL_RTFILE)
|
---|
3360 | {
|
---|
3361 | RTFileWrite(pLogger->pInt->hFile, pLogger->achScratch, cchScratch, NULL);
|
---|
3362 | if (pLogger->fFlags & RTLOGFLAGS_FLUSH)
|
---|
3363 | RTFileFlush(pLogger->pInt->hFile);
|
---|
3364 | }
|
---|
3365 | if (pLogger->pInt->cHistory)
|
---|
3366 | pLogger->pInt->cbHistoryFileWritten += cchScratch;
|
---|
3367 | }
|
---|
3368 | # endif
|
---|
3369 |
|
---|
3370 | if (pLogger->fDestFlags & RTLOGDEST_STDOUT)
|
---|
3371 | RTLogWriteStdOut(pLogger->achScratch, cchScratch);
|
---|
3372 |
|
---|
3373 | if (pLogger->fDestFlags & RTLOGDEST_STDERR)
|
---|
3374 | RTLogWriteStdErr(pLogger->achScratch, cchScratch);
|
---|
3375 |
|
---|
3376 | # if (defined(IN_RING0) || defined(IN_RC)) && !defined(LOG_NO_COM)
|
---|
3377 | if (pLogger->fDestFlags & RTLOGDEST_COM)
|
---|
3378 | RTLogWriteCom(pLogger->achScratch, cchScratch);
|
---|
3379 | # endif
|
---|
3380 | #endif /* !IN_RC */
|
---|
3381 |
|
---|
3382 | #ifdef IN_RC
|
---|
3383 | if (pLogger->pfnFlush)
|
---|
3384 | pLogger->pfnFlush(pLogger);
|
---|
3385 | #else
|
---|
3386 | if (pLogger->pInt->pfnFlush)
|
---|
3387 | pLogger->pInt->pfnFlush(pLogger);
|
---|
3388 | #endif
|
---|
3389 |
|
---|
3390 | /* empty the buffer. */
|
---|
3391 | pLogger->offScratch = 0;
|
---|
3392 |
|
---|
3393 | #ifdef IN_RING3
|
---|
3394 | /*
|
---|
3395 | * Rotate the log file if configured. Must be done after everything is
|
---|
3396 | * flushed, since this will also use logging/flushing to write the header
|
---|
3397 | * and footer messages.
|
---|
3398 | */
|
---|
3399 | if ( (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
3400 | && pLogger->pInt->cHistory)
|
---|
3401 | rtlogRotate(pLogger, RTTimeProgramSecTS() / pLogger->pInt->cSecsHistoryTimeSlot, false /* fFirst */);
|
---|
3402 | #endif
|
---|
3403 | }
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 |
|
---|
3407 | /**
|
---|
3408 | * Callback for RTLogFormatV which writes to the com port.
|
---|
3409 | * See PFNLOGOUTPUT() for details.
|
---|
3410 | */
|
---|
3411 | static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
3412 | {
|
---|
3413 | PRTLOGGER pLogger = (PRTLOGGER)pv;
|
---|
3414 | if (cbChars)
|
---|
3415 | {
|
---|
3416 | size_t cbRet = 0;
|
---|
3417 | for (;;)
|
---|
3418 | {
|
---|
3419 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
3420 | /* sanity */
|
---|
3421 | if (pLogger->offScratch >= sizeof(pLogger->achScratch))
|
---|
3422 | {
|
---|
3423 | fprintf(stderr, "pLogger->offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
3424 | pLogger->offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
3425 | AssertBreakpoint(); AssertBreakpoint();
|
---|
3426 | }
|
---|
3427 | #endif
|
---|
3428 |
|
---|
3429 | /* how much */
|
---|
3430 | size_t cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
3431 | if (cb > cbChars)
|
---|
3432 | cb = cbChars;
|
---|
3433 |
|
---|
3434 | /* copy */
|
---|
3435 | memcpy(&pLogger->achScratch[pLogger->offScratch], pachChars, cb);
|
---|
3436 |
|
---|
3437 | /* advance */
|
---|
3438 | pLogger->offScratch += (uint32_t)cb;
|
---|
3439 | cbRet += cb;
|
---|
3440 | cbChars -= cb;
|
---|
3441 |
|
---|
3442 | /* done? */
|
---|
3443 | if (cbChars <= 0)
|
---|
3444 | return cbRet;
|
---|
3445 |
|
---|
3446 | pachChars += cb;
|
---|
3447 |
|
---|
3448 | /* flush */
|
---|
3449 | rtlogFlush(pLogger);
|
---|
3450 | }
|
---|
3451 |
|
---|
3452 | /* won't ever get here! */
|
---|
3453 | }
|
---|
3454 | else
|
---|
3455 | {
|
---|
3456 | /*
|
---|
3457 | * Termination call.
|
---|
3458 | * There's always space for a terminator, and it's not counted.
|
---|
3459 | */
|
---|
3460 | pLogger->achScratch[pLogger->offScratch] = '\0';
|
---|
3461 | return 0;
|
---|
3462 | }
|
---|
3463 | }
|
---|
3464 |
|
---|
3465 |
|
---|
3466 | /**
|
---|
3467 | * stpncpy implementation for use in rtLogOutputPrefixed w/ padding.
|
---|
3468 | *
|
---|
3469 | * @returns Pointer to the destination buffer byte following the copied string.
|
---|
3470 | * @param pszDst The destination buffer.
|
---|
3471 | * @param pszSrc The source string.
|
---|
3472 | * @param cchSrcMax The maximum number of characters to copy from
|
---|
3473 | * the string.
|
---|
3474 | * @param cchMinWidth The minimum field with, padd with spaces to
|
---|
3475 | * reach this.
|
---|
3476 | */
|
---|
3477 | DECLINLINE(char *) rtLogStPNCpyPad(char *pszDst, const char *pszSrc, size_t cchSrcMax, size_t cchMinWidth)
|
---|
3478 | {
|
---|
3479 | size_t cchSrc = 0;
|
---|
3480 | if (pszSrc)
|
---|
3481 | {
|
---|
3482 | cchSrc = strlen(pszSrc);
|
---|
3483 | if (cchSrc > cchSrcMax)
|
---|
3484 | cchSrc = cchSrcMax;
|
---|
3485 |
|
---|
3486 | memcpy(pszDst, pszSrc, cchSrc);
|
---|
3487 | pszDst += cchSrc;
|
---|
3488 | }
|
---|
3489 | do
|
---|
3490 | *pszDst++ = ' ';
|
---|
3491 | while (cchSrc++ < cchMinWidth);
|
---|
3492 |
|
---|
3493 | return pszDst;
|
---|
3494 | }
|
---|
3495 |
|
---|
3496 |
|
---|
3497 |
|
---|
3498 | /**
|
---|
3499 | * Callback for RTLogFormatV which writes to the logger instance.
|
---|
3500 | * This version supports prefixes.
|
---|
3501 | *
|
---|
3502 | * See PFNLOGOUTPUT() for details.
|
---|
3503 | */
|
---|
3504 | static DECLCALLBACK(size_t) rtLogOutputPrefixed(void *pv, const char *pachChars, size_t cbChars)
|
---|
3505 | {
|
---|
3506 | PRTLOGOUTPUTPREFIXEDARGS pArgs = (PRTLOGOUTPUTPREFIXEDARGS)pv;
|
---|
3507 | PRTLOGGER pLogger = pArgs->pLogger;
|
---|
3508 | if (cbChars)
|
---|
3509 | {
|
---|
3510 | size_t cbRet = 0;
|
---|
3511 | for (;;)
|
---|
3512 | {
|
---|
3513 | uint32_t offScratch = pLogger->offScratch;
|
---|
3514 | size_t cb = sizeof(pLogger->achScratch) - offScratch - 1;
|
---|
3515 | const char *pszNewLine;
|
---|
3516 | char *psz;
|
---|
3517 | #ifdef IN_RC
|
---|
3518 | bool *pfPendingPrefix = &pLogger->fPendingPrefix;
|
---|
3519 | #else
|
---|
3520 | bool *pfPendingPrefix = &pLogger->pInt->fPendingPrefix;
|
---|
3521 | #endif
|
---|
3522 |
|
---|
3523 | /*
|
---|
3524 | * Pending prefix?
|
---|
3525 | */
|
---|
3526 | if (*pfPendingPrefix)
|
---|
3527 | {
|
---|
3528 | *pfPendingPrefix = false;
|
---|
3529 |
|
---|
3530 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
3531 | /* sanity */
|
---|
3532 | if (offScratch >= sizeof(pLogger->achScratch))
|
---|
3533 | {
|
---|
3534 | fprintf(stderr, "offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
3535 | offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
3536 | AssertBreakpoint(); AssertBreakpoint();
|
---|
3537 | }
|
---|
3538 | #endif
|
---|
3539 |
|
---|
3540 | /*
|
---|
3541 | * Flush the buffer if there isn't enough room for the maximum prefix config.
|
---|
3542 | * Max is 256, add a couple of extra bytes. See CCH_PREFIX check way below.
|
---|
3543 | */
|
---|
3544 | if (cb < 256 + 16)
|
---|
3545 | {
|
---|
3546 | rtlogFlush(pLogger);
|
---|
3547 | offScratch = pLogger->offScratch;
|
---|
3548 | cb = sizeof(pLogger->achScratch) - offScratch - 1;
|
---|
3549 | }
|
---|
3550 |
|
---|
3551 | /*
|
---|
3552 | * Write the prefixes.
|
---|
3553 | * psz is pointing to the current position.
|
---|
3554 | */
|
---|
3555 | psz = &pLogger->achScratch[offScratch];
|
---|
3556 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TS)
|
---|
3557 | {
|
---|
3558 | uint64_t u64 = RTTimeNanoTS();
|
---|
3559 | int iBase = 16;
|
---|
3560 | unsigned int fFlags = RTSTR_F_ZEROPAD;
|
---|
3561 | if (pLogger->fFlags & RTLOGFLAGS_DECIMAL_TS)
|
---|
3562 | {
|
---|
3563 | iBase = 10;
|
---|
3564 | fFlags = 0;
|
---|
3565 | }
|
---|
3566 | if (pLogger->fFlags & RTLOGFLAGS_REL_TS)
|
---|
3567 | {
|
---|
3568 | static volatile uint64_t s_u64LastTs;
|
---|
3569 | uint64_t u64DiffTs = u64 - s_u64LastTs;
|
---|
3570 | s_u64LastTs = u64;
|
---|
3571 | /* We could have been preempted just before reading of s_u64LastTs by
|
---|
3572 | * another thread which wrote s_u64LastTs. In that case the difference
|
---|
3573 | * is negative which we simply ignore. */
|
---|
3574 | u64 = (int64_t)u64DiffTs < 0 ? 0 : u64DiffTs;
|
---|
3575 | }
|
---|
3576 | /* 1E15 nanoseconds = 11 days */
|
---|
3577 | psz += RTStrFormatNumber(psz, u64, iBase, 16, 0, fFlags);
|
---|
3578 | *psz++ = ' ';
|
---|
3579 | }
|
---|
3580 | #define CCH_PREFIX_01 0 + 17
|
---|
3581 |
|
---|
3582 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TSC)
|
---|
3583 | {
|
---|
3584 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
3585 | uint64_t u64 = ASMReadTSC();
|
---|
3586 | #else
|
---|
3587 | uint64_t u64 = RTTimeNanoTS();
|
---|
3588 | #endif
|
---|
3589 | int iBase = 16;
|
---|
3590 | unsigned int fFlags = RTSTR_F_ZEROPAD;
|
---|
3591 | if (pLogger->fFlags & RTLOGFLAGS_DECIMAL_TS)
|
---|
3592 | {
|
---|
3593 | iBase = 10;
|
---|
3594 | fFlags = 0;
|
---|
3595 | }
|
---|
3596 | if (pLogger->fFlags & RTLOGFLAGS_REL_TS)
|
---|
3597 | {
|
---|
3598 | static volatile uint64_t s_u64LastTsc;
|
---|
3599 | int64_t i64DiffTsc = u64 - s_u64LastTsc;
|
---|
3600 | s_u64LastTsc = u64;
|
---|
3601 | /* We could have been preempted just before reading of s_u64LastTsc by
|
---|
3602 | * another thread which wrote s_u64LastTsc. In that case the difference
|
---|
3603 | * is negative which we simply ignore. */
|
---|
3604 | u64 = i64DiffTsc < 0 ? 0 : i64DiffTsc;
|
---|
3605 | }
|
---|
3606 | /* 1E15 ticks at 4GHz = 69 hours */
|
---|
3607 | psz += RTStrFormatNumber(psz, u64, iBase, 16, 0, fFlags);
|
---|
3608 | *psz++ = ' ';
|
---|
3609 | }
|
---|
3610 | #define CCH_PREFIX_02 CCH_PREFIX_01 + 17
|
---|
3611 |
|
---|
3612 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_MS_PROG)
|
---|
3613 | {
|
---|
3614 | #if defined(IN_RING3) || defined(IN_RC)
|
---|
3615 | uint64_t u64 = RTTimeProgramMilliTS();
|
---|
3616 | #else
|
---|
3617 | uint64_t u64 = 0;
|
---|
3618 | #endif
|
---|
3619 | /* 1E8 milliseconds = 27 hours */
|
---|
3620 | psz += RTStrFormatNumber(psz, u64, 10, 9, 0, RTSTR_F_ZEROPAD);
|
---|
3621 | *psz++ = ' ';
|
---|
3622 | }
|
---|
3623 | #define CCH_PREFIX_03 CCH_PREFIX_02 + 21
|
---|
3624 |
|
---|
3625 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TIME)
|
---|
3626 | {
|
---|
3627 | #if defined(IN_RING3) || defined(IN_RING0)
|
---|
3628 | RTTIMESPEC TimeSpec;
|
---|
3629 | RTTIME Time;
|
---|
3630 | RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
|
---|
3631 | psz += RTStrFormatNumber(psz, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3632 | *psz++ = ':';
|
---|
3633 | psz += RTStrFormatNumber(psz, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3634 | *psz++ = ':';
|
---|
3635 | psz += RTStrFormatNumber(psz, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3636 | *psz++ = '.';
|
---|
3637 | psz += RTStrFormatNumber(psz, Time.u32Nanosecond / 1000, 10, 6, 0, RTSTR_F_ZEROPAD);
|
---|
3638 | *psz++ = ' ';
|
---|
3639 | #else
|
---|
3640 | memset(psz, ' ', 16);
|
---|
3641 | psz += 16;
|
---|
3642 | #endif
|
---|
3643 | }
|
---|
3644 | #define CCH_PREFIX_04 CCH_PREFIX_03 + (3+1+3+1+3+1+7+1)
|
---|
3645 |
|
---|
3646 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TIME_PROG)
|
---|
3647 | {
|
---|
3648 |
|
---|
3649 | #if defined(IN_RING3) || defined(IN_RC)
|
---|
3650 | uint64_t u64 = RTTimeProgramMicroTS();
|
---|
3651 | psz += RTStrFormatNumber(psz, (uint32_t)(u64 / RT_US_1HOUR), 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3652 | *psz++ = ':';
|
---|
3653 | uint32_t u32 = (uint32_t)(u64 % RT_US_1HOUR);
|
---|
3654 | psz += RTStrFormatNumber(psz, u32 / RT_US_1MIN, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3655 | *psz++ = ':';
|
---|
3656 | u32 %= RT_US_1MIN;
|
---|
3657 |
|
---|
3658 | psz += RTStrFormatNumber(psz, u32 / RT_US_1SEC, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
3659 | *psz++ = '.';
|
---|
3660 | psz += RTStrFormatNumber(psz, u32 % RT_US_1SEC, 10, 6, 0, RTSTR_F_ZEROPAD);
|
---|
3661 | *psz++ = ' ';
|
---|
3662 | #else
|
---|
3663 | memset(psz, ' ', 16);
|
---|
3664 | psz += 16;
|
---|
3665 | #endif
|
---|
3666 | }
|
---|
3667 | #define CCH_PREFIX_05 CCH_PREFIX_04 + (9+1+2+1+2+1+6+1)
|
---|
3668 |
|
---|
3669 | # if 0
|
---|
3670 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_DATETIME)
|
---|
3671 | {
|
---|
3672 | char szDate[32];
|
---|
3673 | RTTIMESPEC Time;
|
---|
3674 | RTTimeSpecToString(RTTimeNow(&Time), szDate, sizeof(szDate));
|
---|
3675 | size_t cch = strlen(szDate);
|
---|
3676 | memcpy(psz, szDate, cch);
|
---|
3677 | psz += cch;
|
---|
3678 | *psz++ = ' ';
|
---|
3679 | }
|
---|
3680 | # define CCH_PREFIX_06 CCH_PREFIX_05 + 32
|
---|
3681 | # else
|
---|
3682 | # define CCH_PREFIX_06 CCH_PREFIX_05 + 0
|
---|
3683 | # endif
|
---|
3684 |
|
---|
3685 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_PID)
|
---|
3686 | {
|
---|
3687 | #ifndef IN_RC
|
---|
3688 | RTPROCESS Process = RTProcSelf();
|
---|
3689 | #else
|
---|
3690 | RTPROCESS Process = NIL_RTPROCESS;
|
---|
3691 | #endif
|
---|
3692 | psz += RTStrFormatNumber(psz, Process, 16, sizeof(RTPROCESS) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
3693 | *psz++ = ' ';
|
---|
3694 | }
|
---|
3695 | #define CCH_PREFIX_07 CCH_PREFIX_06 + 9
|
---|
3696 |
|
---|
3697 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TID)
|
---|
3698 | {
|
---|
3699 | #ifndef IN_RC
|
---|
3700 | RTNATIVETHREAD Thread = RTThreadNativeSelf();
|
---|
3701 | #else
|
---|
3702 | RTNATIVETHREAD Thread = NIL_RTNATIVETHREAD;
|
---|
3703 | #endif
|
---|
3704 | psz += RTStrFormatNumber(psz, Thread, 16, sizeof(RTNATIVETHREAD) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
3705 | *psz++ = ' ';
|
---|
3706 | }
|
---|
3707 | #define CCH_PREFIX_08 CCH_PREFIX_07 + 17
|
---|
3708 |
|
---|
3709 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_THREAD)
|
---|
3710 | {
|
---|
3711 | #ifdef IN_RING3
|
---|
3712 | const char *pszName = RTThreadSelfName();
|
---|
3713 | #elif defined IN_RC
|
---|
3714 | const char *pszName = "EMT-RC";
|
---|
3715 | #else
|
---|
3716 | const char *pszName = "R0";
|
---|
3717 | #endif
|
---|
3718 | psz = rtLogStPNCpyPad(psz, pszName, 16, 8);
|
---|
3719 | }
|
---|
3720 | #define CCH_PREFIX_09 CCH_PREFIX_08 + 17
|
---|
3721 |
|
---|
3722 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_CPUID)
|
---|
3723 | {
|
---|
3724 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
3725 | const uint8_t idCpu = ASMGetApicId();
|
---|
3726 | #else
|
---|
3727 | const RTCPUID idCpu = RTMpCpuId();
|
---|
3728 | #endif
|
---|
3729 | psz += RTStrFormatNumber(psz, idCpu, 16, sizeof(idCpu) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
3730 | *psz++ = ' ';
|
---|
3731 | }
|
---|
3732 | #define CCH_PREFIX_10 CCH_PREFIX_09 + 17
|
---|
3733 |
|
---|
3734 | #ifndef IN_RC
|
---|
3735 | if ( (pLogger->fFlags & RTLOGFLAGS_PREFIX_CUSTOM)
|
---|
3736 | && pLogger->pInt->pfnPrefix)
|
---|
3737 | {
|
---|
3738 | psz += pLogger->pInt->pfnPrefix(pLogger, psz, 31, pLogger->pInt->pvPrefixUserArg);
|
---|
3739 | *psz++ = ' '; /* +32 */
|
---|
3740 | }
|
---|
3741 | #endif
|
---|
3742 | #define CCH_PREFIX_11 CCH_PREFIX_10 + 32
|
---|
3743 |
|
---|
3744 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_LOCK_COUNTS)
|
---|
3745 | {
|
---|
3746 | #ifdef IN_RING3 /** @todo implement these counters in ring-0 too? */
|
---|
3747 | RTTHREAD Thread = RTThreadSelf();
|
---|
3748 | if (Thread != NIL_RTTHREAD)
|
---|
3749 | {
|
---|
3750 | uint32_t cReadLocks = RTLockValidatorReadLockGetCount(Thread);
|
---|
3751 | uint32_t cWriteLocks = RTLockValidatorWriteLockGetCount(Thread) - g_cLoggerLockCount;
|
---|
3752 | cReadLocks = RT_MIN(0xfff, cReadLocks);
|
---|
3753 | cWriteLocks = RT_MIN(0xfff, cWriteLocks);
|
---|
3754 | psz += RTStrFormatNumber(psz, cReadLocks, 16, 1, 0, RTSTR_F_ZEROPAD);
|
---|
3755 | *psz++ = '/';
|
---|
3756 | psz += RTStrFormatNumber(psz, cWriteLocks, 16, 1, 0, RTSTR_F_ZEROPAD);
|
---|
3757 | }
|
---|
3758 | else
|
---|
3759 | #endif
|
---|
3760 | {
|
---|
3761 | *psz++ = '?';
|
---|
3762 | *psz++ = '/';
|
---|
3763 | *psz++ = '?';
|
---|
3764 | }
|
---|
3765 | *psz++ = ' ';
|
---|
3766 | }
|
---|
3767 | #define CCH_PREFIX_12 CCH_PREFIX_11 + 8
|
---|
3768 |
|
---|
3769 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_FLAG_NO)
|
---|
3770 | {
|
---|
3771 | psz += RTStrFormatNumber(psz, pArgs->fFlags, 16, 8, 0, RTSTR_F_ZEROPAD);
|
---|
3772 | *psz++ = ' ';
|
---|
3773 | }
|
---|
3774 | #define CCH_PREFIX_13 CCH_PREFIX_12 + 9
|
---|
3775 |
|
---|
3776 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_FLAG)
|
---|
3777 | {
|
---|
3778 | #ifdef IN_RING3
|
---|
3779 | const char *pszGroup = pArgs->iGroup != ~0U ? pLogger->pInt->papszGroups[pArgs->iGroup] : NULL;
|
---|
3780 | #else
|
---|
3781 | const char *pszGroup = NULL;
|
---|
3782 | #endif
|
---|
3783 | psz = rtLogStPNCpyPad(psz, pszGroup, 16, 8);
|
---|
3784 | }
|
---|
3785 | #define CCH_PREFIX_14 CCH_PREFIX_13 + 17
|
---|
3786 |
|
---|
3787 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_GROUP_NO)
|
---|
3788 | {
|
---|
3789 | if (pArgs->iGroup != ~0U)
|
---|
3790 | {
|
---|
3791 | psz += RTStrFormatNumber(psz, pArgs->iGroup, 16, 3, 0, RTSTR_F_ZEROPAD);
|
---|
3792 | *psz++ = ' ';
|
---|
3793 | }
|
---|
3794 | else
|
---|
3795 | {
|
---|
3796 | memcpy(psz, "-1 ", sizeof("-1 ") - 1);
|
---|
3797 | psz += sizeof("-1 ") - 1;
|
---|
3798 | } /* +9 */
|
---|
3799 | }
|
---|
3800 | #define CCH_PREFIX_15 CCH_PREFIX_14 + 9
|
---|
3801 |
|
---|
3802 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_GROUP)
|
---|
3803 | {
|
---|
3804 | const unsigned fGrp = pLogger->afGroups[pArgs->iGroup != ~0U ? pArgs->iGroup : 0];
|
---|
3805 | const char *pszGroup;
|
---|
3806 | size_t cch;
|
---|
3807 | switch (pArgs->fFlags & fGrp)
|
---|
3808 | {
|
---|
3809 | case 0: pszGroup = "--------"; cch = sizeof("--------") - 1; break;
|
---|
3810 | case RTLOGGRPFLAGS_ENABLED: pszGroup = "enabled" ; cch = sizeof("enabled" ) - 1; break;
|
---|
3811 | case RTLOGGRPFLAGS_LEVEL_1: pszGroup = "level 1" ; cch = sizeof("level 1" ) - 1; break;
|
---|
3812 | case RTLOGGRPFLAGS_LEVEL_2: pszGroup = "level 2" ; cch = sizeof("level 2" ) - 1; break;
|
---|
3813 | case RTLOGGRPFLAGS_LEVEL_3: pszGroup = "level 3" ; cch = sizeof("level 3" ) - 1; break;
|
---|
3814 | case RTLOGGRPFLAGS_LEVEL_4: pszGroup = "level 4" ; cch = sizeof("level 4" ) - 1; break;
|
---|
3815 | case RTLOGGRPFLAGS_LEVEL_5: pszGroup = "level 5" ; cch = sizeof("level 5" ) - 1; break;
|
---|
3816 | case RTLOGGRPFLAGS_LEVEL_6: pszGroup = "level 6" ; cch = sizeof("level 6" ) - 1; break;
|
---|
3817 | case RTLOGGRPFLAGS_LEVEL_7: pszGroup = "level 7" ; cch = sizeof("level 7" ) - 1; break;
|
---|
3818 | case RTLOGGRPFLAGS_LEVEL_8: pszGroup = "level 8" ; cch = sizeof("level 8" ) - 1; break;
|
---|
3819 | case RTLOGGRPFLAGS_LEVEL_9: pszGroup = "level 9" ; cch = sizeof("level 9" ) - 1; break;
|
---|
3820 | case RTLOGGRPFLAGS_LEVEL_10: pszGroup = "level 10"; cch = sizeof("level 10") - 1; break;
|
---|
3821 | case RTLOGGRPFLAGS_LEVEL_11: pszGroup = "level 11"; cch = sizeof("level 11") - 1; break;
|
---|
3822 | case RTLOGGRPFLAGS_LEVEL_12: pszGroup = "level 12"; cch = sizeof("level 12") - 1; break;
|
---|
3823 | case RTLOGGRPFLAGS_FLOW: pszGroup = "flow" ; cch = sizeof("flow" ) - 1; break;
|
---|
3824 | case RTLOGGRPFLAGS_WARN: pszGroup = "warn" ; cch = sizeof("warn" ) - 1; break;
|
---|
3825 | default: pszGroup = "????????"; cch = sizeof("????????") - 1; break;
|
---|
3826 | }
|
---|
3827 | psz = rtLogStPNCpyPad(psz, pszGroup, 16, 8);
|
---|
3828 | }
|
---|
3829 | #define CCH_PREFIX_16 CCH_PREFIX_15 + 17
|
---|
3830 |
|
---|
3831 | #define CCH_PREFIX ( CCH_PREFIX_16 )
|
---|
3832 | { AssertCompile(CCH_PREFIX < 256); }
|
---|
3833 |
|
---|
3834 | /*
|
---|
3835 | * Done, figure what we've used and advance the buffer and free size.
|
---|
3836 | */
|
---|
3837 | cb = psz - &pLogger->achScratch[offScratch];
|
---|
3838 | AssertMsg(cb <= 223, ("%#zx (%zd) - fFlags=%#x\n", cb, cb, pLogger->fFlags));
|
---|
3839 | pLogger->offScratch = offScratch += (uint32_t)cb;
|
---|
3840 | cb = sizeof(pLogger->achScratch) - offScratch - 1;
|
---|
3841 | }
|
---|
3842 | else if (cb <= 0)
|
---|
3843 | {
|
---|
3844 | rtlogFlush(pLogger);
|
---|
3845 | offScratch = pLogger->offScratch;
|
---|
3846 | cb = sizeof(pLogger->achScratch) - offScratch - 1;
|
---|
3847 | }
|
---|
3848 |
|
---|
3849 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
3850 | /* sanity */
|
---|
3851 | if (offScratch >= sizeof(pLogger->achScratch))
|
---|
3852 | {
|
---|
3853 | fprintf(stderr, "offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
3854 | offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
3855 | AssertBreakpoint(); AssertBreakpoint();
|
---|
3856 | }
|
---|
3857 | #endif
|
---|
3858 |
|
---|
3859 | /* how much */
|
---|
3860 | if (cb > cbChars)
|
---|
3861 | cb = cbChars;
|
---|
3862 |
|
---|
3863 | /* have newline? */
|
---|
3864 | pszNewLine = (const char *)memchr(pachChars, '\n', cb);
|
---|
3865 | if (pszNewLine)
|
---|
3866 | {
|
---|
3867 | if (pLogger->fFlags & RTLOGFLAGS_USECRLF)
|
---|
3868 | cb = pszNewLine - pachChars;
|
---|
3869 | else
|
---|
3870 | {
|
---|
3871 | cb = pszNewLine - pachChars + 1;
|
---|
3872 | *pfPendingPrefix = true;
|
---|
3873 | }
|
---|
3874 | }
|
---|
3875 |
|
---|
3876 | /* copy */
|
---|
3877 | memcpy(&pLogger->achScratch[offScratch], pachChars, cb);
|
---|
3878 |
|
---|
3879 | /* advance */
|
---|
3880 | pLogger->offScratch = offScratch += (uint32_t)cb;
|
---|
3881 | cbRet += cb;
|
---|
3882 | cbChars -= cb;
|
---|
3883 |
|
---|
3884 | if ( pszNewLine
|
---|
3885 | && (pLogger->fFlags & RTLOGFLAGS_USECRLF)
|
---|
3886 | && offScratch + 2 < sizeof(pLogger->achScratch))
|
---|
3887 | {
|
---|
3888 | memcpy(&pLogger->achScratch[offScratch], "\r\n", 2);
|
---|
3889 | pLogger->offScratch = offScratch += 2;
|
---|
3890 | cbRet++;
|
---|
3891 | cbChars--;
|
---|
3892 | cb++;
|
---|
3893 | *pfPendingPrefix = true;
|
---|
3894 | }
|
---|
3895 |
|
---|
3896 | /* done? */
|
---|
3897 | if (cbChars <= 0)
|
---|
3898 | return cbRet;
|
---|
3899 | pachChars += cb;
|
---|
3900 | }
|
---|
3901 |
|
---|
3902 | /* won't ever get here! */
|
---|
3903 | }
|
---|
3904 | else
|
---|
3905 | {
|
---|
3906 | /*
|
---|
3907 | * Termination call.
|
---|
3908 | * There's always space for a terminator, and it's not counted.
|
---|
3909 | */
|
---|
3910 | pLogger->achScratch[pLogger->offScratch] = '\0';
|
---|
3911 | return 0;
|
---|
3912 | }
|
---|
3913 | }
|
---|
3914 |
|
---|
3915 |
|
---|
3916 | /**
|
---|
3917 | * Write to a logger instance (worker function).
|
---|
3918 | *
|
---|
3919 | * This function will check whether the instance, group and flags makes up a
|
---|
3920 | * logging kind which is currently enabled before writing anything to the log.
|
---|
3921 | *
|
---|
3922 | * @param pLogger Pointer to logger instance. Must be non-NULL.
|
---|
3923 | * @param fFlags The logging flags.
|
---|
3924 | * @param iGroup The group.
|
---|
3925 | * The value ~0U is reserved for compatibility with RTLogLogger[V] and is
|
---|
3926 | * only for internal usage!
|
---|
3927 | * @param pszFormat Format string.
|
---|
3928 | * @param args Format arguments.
|
---|
3929 | */
|
---|
3930 | static void rtlogLoggerExVLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
|
---|
3931 | {
|
---|
3932 | /*
|
---|
3933 | * Format the message and perhaps flush it.
|
---|
3934 | */
|
---|
3935 | if (pLogger->fFlags & (RTLOGFLAGS_PREFIX_MASK | RTLOGFLAGS_USECRLF))
|
---|
3936 | {
|
---|
3937 | RTLOGOUTPUTPREFIXEDARGS OutputArgs;
|
---|
3938 | OutputArgs.pLogger = pLogger;
|
---|
3939 | OutputArgs.iGroup = iGroup;
|
---|
3940 | OutputArgs.fFlags = fFlags;
|
---|
3941 | RTLogFormatV(rtLogOutputPrefixed, &OutputArgs, pszFormat, args);
|
---|
3942 | }
|
---|
3943 | else
|
---|
3944 | RTLogFormatV(rtLogOutput, pLogger, pszFormat, args);
|
---|
3945 | if ( !(pLogger->fFlags & RTLOGFLAGS_BUFFERED)
|
---|
3946 | && pLogger->offScratch)
|
---|
3947 | rtlogFlush(pLogger);
|
---|
3948 | }
|
---|
3949 |
|
---|
3950 |
|
---|
3951 | #ifndef IN_RC
|
---|
3952 | /**
|
---|
3953 | * For calling rtlogLoggerExVLocked.
|
---|
3954 | *
|
---|
3955 | * @param pLogger The logger.
|
---|
3956 | * @param fFlags The logging flags.
|
---|
3957 | * @param iGroup The group.
|
---|
3958 | * The value ~0U is reserved for compatibility with RTLogLogger[V] and is
|
---|
3959 | * only for internal usage!
|
---|
3960 | * @param pszFormat Format string.
|
---|
3961 | * @param ... Format arguments.
|
---|
3962 | */
|
---|
3963 | static void rtlogLoggerExFLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, ...)
|
---|
3964 | {
|
---|
3965 | va_list va;
|
---|
3966 | va_start(va, pszFormat);
|
---|
3967 | rtlogLoggerExVLocked(pLogger, fFlags, iGroup, pszFormat, va);
|
---|
3968 | va_end(va);
|
---|
3969 | }
|
---|
3970 | #endif /* !IN_RC */
|
---|
3971 |
|
---|