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