1 | /* $Id: log.cpp 12159 2008-09-05 23:03:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Runtime VBox - Logger.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/log.h>
|
---|
36 | #ifndef IN_GC
|
---|
37 | # include <iprt/alloc.h>
|
---|
38 | # include <iprt/process.h>
|
---|
39 | # include <iprt/semaphore.h>
|
---|
40 | # include <iprt/thread.h>
|
---|
41 | # include <iprt/mp.h>
|
---|
42 | #endif
|
---|
43 | #ifdef IN_RING3
|
---|
44 | # include <iprt/file.h>
|
---|
45 | # include <iprt/path.h>
|
---|
46 | #endif
|
---|
47 | #include <iprt/time.h>
|
---|
48 | #include <iprt/asm.h>
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/err.h>
|
---|
51 | #include <iprt/param.h>
|
---|
52 |
|
---|
53 | #include <iprt/stdarg.h>
|
---|
54 | #include <iprt/string.h>
|
---|
55 | #include <iprt/ctype.h>
|
---|
56 | #ifdef IN_RING3
|
---|
57 | # include <iprt/alloca.h>
|
---|
58 | # include <stdio.h>
|
---|
59 | #endif
|
---|
60 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Structures and Typedefs *
|
---|
64 | *******************************************************************************/
|
---|
65 | /**
|
---|
66 | * Arguments passed to the output function.
|
---|
67 | */
|
---|
68 | typedef struct RTLOGOUTPUTPREFIXEDARGS
|
---|
69 | {
|
---|
70 | /** The logger instance. */
|
---|
71 | PRTLOGGER pLogger;
|
---|
72 | /** The flags. (used for prefixing.) */
|
---|
73 | unsigned fFlags;
|
---|
74 | /** The group. (used for prefixing.) */
|
---|
75 | unsigned iGroup;
|
---|
76 | } RTLOGOUTPUTPREFIXEDARGS, *PRTLOGOUTPUTPREFIXEDARGS;
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Internal Functions *
|
---|
81 | *******************************************************************************/
|
---|
82 | #ifndef IN_GC
|
---|
83 | static unsigned rtlogGroupFlags(const char *psz);
|
---|
84 | #endif
|
---|
85 | static void rtlogFlush(PRTLOGGER pLogger);
|
---|
86 | static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars);
|
---|
87 | static DECLCALLBACK(size_t) rtLogOutputPrefixed(void *pv, const char *pachChars, size_t cbChars);
|
---|
88 |
|
---|
89 |
|
---|
90 | /*******************************************************************************
|
---|
91 | * Global Variables *
|
---|
92 | *******************************************************************************/
|
---|
93 | #ifdef IN_GC
|
---|
94 | /** Default logger instance. */
|
---|
95 | extern "C" DECLIMPORT(RTLOGGERRC) g_Logger;
|
---|
96 | #else /* !IN_GC */
|
---|
97 | /** Default logger instance. */
|
---|
98 | static PRTLOGGER g_pLogger;
|
---|
99 | #endif /* !IN_GC */
|
---|
100 | #ifdef IN_RING3
|
---|
101 | /** The RTThreadGetWriteLockCount() change caused by the logger mutex semaphore. */
|
---|
102 | static uint32_t volatile g_cLoggerLockCount;
|
---|
103 | #endif
|
---|
104 | #ifdef IN_RING0
|
---|
105 | /** Number of per-thread loggers. */
|
---|
106 | static int32_t volatile g_cPerThreadLoggers;
|
---|
107 | /** Per-thread loggers.
|
---|
108 | * This is just a quick TLS hack suitable for debug logging only.
|
---|
109 | * If we run out of entries, just unload and reload the driver. */
|
---|
110 | static struct RTLOGGERPERTHREAD
|
---|
111 | {
|
---|
112 | /** The thread. */
|
---|
113 | RTNATIVETHREAD volatile NativeThread;
|
---|
114 | /** The (process / session) key. */
|
---|
115 | uintptr_t volatile uKey;
|
---|
116 | /** The logger instance.*/
|
---|
117 | PRTLOGGER volatile pLogger;
|
---|
118 | } g_aPerThreadLoggers[8] =
|
---|
119 | { { NIL_RTNATIVETHREAD, 0, 0},
|
---|
120 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
121 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
122 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
123 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
124 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
125 | { NIL_RTNATIVETHREAD, 0, 0},
|
---|
126 | { NIL_RTNATIVETHREAD, 0, 0}
|
---|
127 | };
|
---|
128 | #endif /* IN_RING0 */
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Locks the logger instance.
|
---|
133 | *
|
---|
134 | * @returns See RTSemFastMutexRequest().
|
---|
135 | * @param pLogger The logger instance.
|
---|
136 | */
|
---|
137 | DECLINLINE(int) rtlogLock(PRTLOGGER pLogger)
|
---|
138 | {
|
---|
139 | #ifndef IN_GC
|
---|
140 | if (pLogger->MutexSem != NIL_RTSEMFASTMUTEX)
|
---|
141 | {
|
---|
142 | int rc = RTSemFastMutexRequest(pLogger->MutexSem);
|
---|
143 | if (RT_FAILURE(rc))
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 | #endif
|
---|
147 | return VINF_SUCCESS;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Unlocks the logger instance.
|
---|
153 | * @param pLogger The logger instance.
|
---|
154 | */
|
---|
155 | DECLINLINE(void) rtlogUnlock(PRTLOGGER pLogger)
|
---|
156 | {
|
---|
157 | #ifndef IN_GC
|
---|
158 | if (pLogger->MutexSem != NIL_RTSEMFASTMUTEX)
|
---|
159 | RTSemFastMutexRelease(pLogger->MutexSem);
|
---|
160 | #endif
|
---|
161 | return;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | #ifndef IN_GC
|
---|
166 | /**
|
---|
167 | * Create a logger instance, comprehensive version.
|
---|
168 | *
|
---|
169 | * @returns iprt status code.
|
---|
170 | *
|
---|
171 | * @param ppLogger Where to store the logger instance.
|
---|
172 | * @param fFlags Logger instance flags, a combination of the RTLOGFLAGS_* values.
|
---|
173 | * @param pszGroupSettings The initial group settings.
|
---|
174 | * @param pszEnvVarBase Base name for the environment variables for this instance.
|
---|
175 | * @param cGroups Number of groups in the array.
|
---|
176 | * @param papszGroups Pointer to array of groups. This must stick around for the life of the
|
---|
177 | * logger instance.
|
---|
178 | * @param fDestFlags The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
|
---|
179 | * @param pszErrorMsg A buffer which is filled with an error message if something fails. May be NULL.
|
---|
180 | * @param cchErrorMsg The size of the error message buffer.
|
---|
181 | * @param pszFilenameFmt Log filename format string. Standard RTStrFormat().
|
---|
182 | * @param ... Format arguments.
|
---|
183 | */
|
---|
184 | RTDECL(int) RTLogCreateExV(PRTLOGGER *ppLogger, RTUINT fFlags, const char *pszGroupSettings,
|
---|
185 | const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
|
---|
186 | RTUINT fDestFlags, char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, va_list args)
|
---|
187 | {
|
---|
188 | int rc;
|
---|
189 | size_t cb;
|
---|
190 | PRTLOGGER pLogger;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Validate input.
|
---|
194 | */
|
---|
195 | if ( (cGroups && !papszGroups)
|
---|
196 | || !VALID_PTR(ppLogger)
|
---|
197 | )
|
---|
198 | {
|
---|
199 | AssertMsgFailed(("Invalid parameters!\n"));
|
---|
200 | return VERR_INVALID_PARAMETER;
|
---|
201 | }
|
---|
202 | *ppLogger = NULL;
|
---|
203 |
|
---|
204 | if (pszErrorMsg)
|
---|
205 | RTStrPrintf(pszErrorMsg, cchErrorMsg, "unknown error");
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Allocate a logger instance.
|
---|
209 | */
|
---|
210 | cb = RT_OFFSETOF(RTLOGGER, afGroups[cGroups + 1]) + RTPATH_MAX;
|
---|
211 | pLogger = (PRTLOGGER)RTMemAllocZ(cb);
|
---|
212 | if (pLogger)
|
---|
213 | {
|
---|
214 | uint8_t *pu8Code;
|
---|
215 |
|
---|
216 | pLogger->u32Magic = RTLOGGER_MAGIC;
|
---|
217 | pLogger->papszGroups = papszGroups;
|
---|
218 | pLogger->cMaxGroups = cGroups;
|
---|
219 | pLogger->cGroups = cGroups;
|
---|
220 | pLogger->pszFilename = (char *)&pLogger->afGroups[cGroups + 1];
|
---|
221 | pLogger->File = NIL_RTFILE;
|
---|
222 | pLogger->fFlags = fFlags;
|
---|
223 | pLogger->fDestFlags = fDestFlags;
|
---|
224 | pLogger->fPendingPrefix = true;
|
---|
225 | if (pszGroupSettings)
|
---|
226 | RTLogGroupSettings(pLogger, pszGroupSettings);
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Emit wrapper code.
|
---|
230 | */
|
---|
231 | pu8Code = (uint8_t *)RTMemExecAlloc(64);
|
---|
232 | if (pu8Code)
|
---|
233 | {
|
---|
234 | pLogger->pfnLogger = *(PFNRTLOGGER*)&pu8Code;
|
---|
235 | #ifdef RT_ARCH_AMD64
|
---|
236 | /* this wrapper will not be used on AMD64, we will be requiring C99 compilers there. */
|
---|
237 | *pu8Code++ = 0xcc;
|
---|
238 | #else
|
---|
239 | *pu8Code++ = 0x68; /* push imm32 */
|
---|
240 | *(void **)pu8Code = pLogger;
|
---|
241 | pu8Code += sizeof(void *);
|
---|
242 | *pu8Code++ = 0xe8; /* call rel32 */
|
---|
243 | *(uint32_t *)pu8Code = (uintptr_t)RTLogLogger - ((uintptr_t)pu8Code + sizeof(uint32_t));
|
---|
244 | pu8Code += sizeof(uint32_t);
|
---|
245 | *pu8Code++ = 0x8d; /* lea esp, [esp + 4] */
|
---|
246 | *pu8Code++ = 0x64;
|
---|
247 | *pu8Code++ = 0x24;
|
---|
248 | *pu8Code++ = 0x04;
|
---|
249 | *pu8Code++ = 0xc3; /* ret near */
|
---|
250 | #endif
|
---|
251 | AssertMsg((uintptr_t)pu8Code - (uintptr_t)pLogger->pfnLogger <= 64,
|
---|
252 | ("Wrapper assembly is too big! %d bytes\n", (uintptr_t)pu8Code - (uintptr_t)pLogger->pfnLogger));
|
---|
253 |
|
---|
254 |
|
---|
255 | #ifdef IN_RING3 /* files and env.vars. are only accessible when in R3 at the present time. */
|
---|
256 | /*
|
---|
257 | * Format the filename.
|
---|
258 | */
|
---|
259 | if (pszFilenameFmt)
|
---|
260 | {
|
---|
261 | RTStrPrintfV(pLogger->pszFilename, RTPATH_MAX, pszFilenameFmt, args);
|
---|
262 | pLogger->fDestFlags |= RTLOGDEST_FILE;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Parse the environment variables.
|
---|
267 | */
|
---|
268 | if (pszEnvVarBase)
|
---|
269 | {
|
---|
270 | /* make temp copy of environment variable base. */
|
---|
271 | size_t cchEnvVarBase = strlen(pszEnvVarBase);
|
---|
272 | char *pszEnvVar = (char *)alloca(cchEnvVarBase + 16);
|
---|
273 | memcpy(pszEnvVar, pszEnvVarBase, cchEnvVarBase);
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Destination.
|
---|
277 | */
|
---|
278 | strcpy(pszEnvVar + cchEnvVarBase, "_DEST");
|
---|
279 | const char *pszVar = getenv(pszEnvVar);
|
---|
280 | if (pszVar)
|
---|
281 | {
|
---|
282 | while (*pszVar)
|
---|
283 | {
|
---|
284 | /* skip blanks. */
|
---|
285 | while (RT_C_IS_SPACE(*pszVar))
|
---|
286 | pszVar++;
|
---|
287 | if (!*pszVar)
|
---|
288 | break;
|
---|
289 |
|
---|
290 | /* parse instruction. */
|
---|
291 | static struct
|
---|
292 | {
|
---|
293 | const char *pszInstr;
|
---|
294 | unsigned fFlag;
|
---|
295 | } const aDest[] =
|
---|
296 | {
|
---|
297 | { "file", RTLOGDEST_FILE }, /* Must be 1st! */
|
---|
298 | { "dir", RTLOGDEST_FILE }, /* Must be 2nd! */
|
---|
299 | { "stdout", RTLOGDEST_STDOUT },
|
---|
300 | { "stderr", RTLOGDEST_STDERR },
|
---|
301 | { "debugger", RTLOGDEST_DEBUGGER },
|
---|
302 | { "com", RTLOGDEST_COM },
|
---|
303 | { "user", RTLOGDEST_USER },
|
---|
304 | };
|
---|
305 |
|
---|
306 | /* check no prefix. */
|
---|
307 | bool fNo = false;
|
---|
308 | if (pszVar[0] == 'n' && pszVar[1] == 'o')
|
---|
309 | {
|
---|
310 | fNo = true;
|
---|
311 | pszVar += 2;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* instruction. */
|
---|
315 | unsigned i;
|
---|
316 | for (i = 0; i < ELEMENTS(aDest); i++)
|
---|
317 | {
|
---|
318 | size_t cchInstr = strlen(aDest[i].pszInstr);
|
---|
319 | if (!strncmp(pszVar, aDest[i].pszInstr, cchInstr))
|
---|
320 | {
|
---|
321 | if (!fNo)
|
---|
322 | pLogger->fDestFlags |= aDest[i].fFlag;
|
---|
323 | else
|
---|
324 | pLogger->fDestFlags &= ~aDest[i].fFlag;
|
---|
325 | pszVar += cchInstr;
|
---|
326 |
|
---|
327 | /* check for value. */
|
---|
328 | while (RT_C_IS_SPACE(*pszVar))
|
---|
329 | pszVar++;
|
---|
330 | if (*pszVar == '=' || *pszVar == ':')
|
---|
331 | {
|
---|
332 | pszVar++;
|
---|
333 | const char *pszEnd = strchr(pszVar, ';');
|
---|
334 | if (!pszEnd)
|
---|
335 | pszEnd = strchr(pszVar, '\0');
|
---|
336 |
|
---|
337 | /* log file name */
|
---|
338 | size_t cch = pszEnd - pszVar;
|
---|
339 | if (i == 0 /* file */ && !fNo)
|
---|
340 | {
|
---|
341 | memcpy(pLogger->pszFilename, pszVar, cch);
|
---|
342 | pLogger->pszFilename[cch] = '\0';
|
---|
343 | }
|
---|
344 | /* log directory */
|
---|
345 | else if (i == 1 /* dir */ && !fNo)
|
---|
346 | {
|
---|
347 | char szTmp[RTPATH_MAX];
|
---|
348 | const char *pszFile = RTPathFilename(pLogger->pszFilename);
|
---|
349 | if (pszFile)
|
---|
350 | strcpy(szTmp, pszFile);
|
---|
351 | else
|
---|
352 | pszFile = ""; /* you've screwed up, sir. */
|
---|
353 |
|
---|
354 | memcpy(pLogger->pszFilename, pszVar, cch);
|
---|
355 | pLogger->pszFilename[cch] = '\0';
|
---|
356 | RTPathStripTrailingSlash(pLogger->pszFilename);
|
---|
357 |
|
---|
358 | cch = strlen(pLogger->pszFilename);
|
---|
359 | pLogger->pszFilename[cch++] = '/';
|
---|
360 | strcpy(&pLogger->pszFilename[cch], szTmp);
|
---|
361 | }
|
---|
362 | else
|
---|
363 | AssertMsgFailed(("Invalid %s_DEST! %s%s doesn't take a value!\n", pszEnvVarBase, fNo ? "no" : "", aDest[i].pszInstr));
|
---|
364 | pszVar = pszEnd + (*pszEnd != '\0');
|
---|
365 | }
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | /* unknown instruction? */
|
---|
370 | if (i >= ELEMENTS(aDest))
|
---|
371 | {
|
---|
372 | AssertMsgFailed(("Invalid %s_DEST! unknown instruction %.20s\n", pszEnvVarBase, pszVar));
|
---|
373 | pszVar++;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* skip blanks and delimiters. */
|
---|
377 | while (RT_C_IS_SPACE(*pszVar) || *pszVar == ';')
|
---|
378 | pszVar++;
|
---|
379 | } /* while more environment variable value left */
|
---|
380 | }
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * The flags.
|
---|
384 | */
|
---|
385 | strcpy(pszEnvVar + cchEnvVarBase, "_FLAGS");
|
---|
386 | pszVar = getenv(pszEnvVar);
|
---|
387 | if (pszVar)
|
---|
388 | RTLogFlags(pLogger, pszVar);
|
---|
389 |
|
---|
390 | /*
|
---|
391 | * The group settings.
|
---|
392 | */
|
---|
393 | pszEnvVar[cchEnvVarBase] = '\0';
|
---|
394 | pszVar = getenv(pszEnvVar);
|
---|
395 | if (pszVar)
|
---|
396 | RTLogGroupSettings(pLogger, pszVar);
|
---|
397 | }
|
---|
398 | #endif /* IN_RING3 */
|
---|
399 |
|
---|
400 | /*
|
---|
401 | * Open the destination(s).
|
---|
402 | */
|
---|
403 | rc = VINF_SUCCESS;
|
---|
404 | #ifdef IN_RING3
|
---|
405 | if (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
406 | {
|
---|
407 | if (!(pLogger->fFlags & RTLOGFLAGS_APPEND))
|
---|
408 | rc = RTFileOpen(&pLogger->File, pLogger->pszFilename,
|
---|
409 | RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE);
|
---|
410 | else
|
---|
411 | {
|
---|
412 | /** @todo RTFILE_O_APPEND. */
|
---|
413 | rc = RTFileOpen(&pLogger->File, pLogger->pszFilename,
|
---|
414 | RTFILE_O_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE);
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | {
|
---|
417 | rc = RTFileSeek(pLogger->File, 0, RTFILE_SEEK_END, NULL);
|
---|
418 | if (RT_FAILURE(rc))
|
---|
419 | {
|
---|
420 | RTFileClose(pLogger->File);
|
---|
421 | pLogger->File = NIL_RTFILE;
|
---|
422 | }
|
---|
423 | }
|
---|
424 | }
|
---|
425 | if (RT_FAILURE(rc) && pszErrorMsg)
|
---|
426 | RTStrPrintf(pszErrorMsg, cchErrorMsg, "could not open file '%s'", pLogger->pszFilename);
|
---|
427 | }
|
---|
428 | #endif /* IN_RING3 */
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Create mutex and check how much it counts when entering the lock
|
---|
432 | * so that we can report the values for RTLOGFLAGS_PREFIX_LOCK_COUNTS.
|
---|
433 | */
|
---|
434 | if (RT_SUCCESS(rc))
|
---|
435 | {
|
---|
436 | rc = RTSemFastMutexCreate(&pLogger->MutexSem);
|
---|
437 | if (RT_SUCCESS(rc))
|
---|
438 | {
|
---|
439 | #ifdef IN_RING3 /** @todo do counters in ring-0 too? */
|
---|
440 | RTTHREAD Thread = RTThreadSelf();
|
---|
441 | if (Thread != NIL_RTTHREAD)
|
---|
442 | {
|
---|
443 | int32_t c = RTThreadGetWriteLockCount(Thread);
|
---|
444 | RTSemFastMutexRequest(pLogger->MutexSem);
|
---|
445 | c = RTThreadGetWriteLockCount(Thread) - c;
|
---|
446 | RTSemFastMutexRelease(pLogger->MutexSem);
|
---|
447 | ASMAtomicWriteU32(&g_cLoggerLockCount, c);
|
---|
448 | }
|
---|
449 | #endif
|
---|
450 | *ppLogger = pLogger;
|
---|
451 | return VINF_SUCCESS;
|
---|
452 | }
|
---|
453 |
|
---|
454 | if (pszErrorMsg)
|
---|
455 | RTStrPrintf(pszErrorMsg, cchErrorMsg, "failed to create sempahore");
|
---|
456 | }
|
---|
457 | #ifdef IN_RING3
|
---|
458 | RTFileClose(pLogger->File);
|
---|
459 | #endif
|
---|
460 | RTMemExecFree(*(void **)&pLogger->pfnLogger);
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | #ifdef RT_OS_LINUX
|
---|
465 | /*
|
---|
466 | * RTMemAlloc() succeeded but RTMemExecAlloc() failed -- most probably an SELinux problem.
|
---|
467 | */
|
---|
468 | if (pszErrorMsg)
|
---|
469 | RTStrPrintf(pszErrorMsg, cchErrorMsg, "mmap(PROT_WRITE | PROT_EXEC) failed -- SELinux?");
|
---|
470 | #endif /* RT_OS_LINUX */
|
---|
471 | rc = VERR_NO_MEMORY;
|
---|
472 | }
|
---|
473 | RTMemFree(pLogger);
|
---|
474 | }
|
---|
475 | else
|
---|
476 | rc = VERR_NO_MEMORY;
|
---|
477 |
|
---|
478 | return rc;
|
---|
479 | }
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Create a logger instance.
|
---|
483 | *
|
---|
484 | * @returns iprt status code.
|
---|
485 | *
|
---|
486 | * @param ppLogger Where to store the logger instance.
|
---|
487 | * @param fFlags Logger instance flags, a combination of the RTLOGFLAGS_* values.
|
---|
488 | * @param pszGroupSettings The initial group settings.
|
---|
489 | * @param pszEnvVarBase Base name for the environment variables for this instance.
|
---|
490 | * @param cGroups Number of groups in the array.
|
---|
491 | * @param papszGroups Pointer to array of groups. This must stick around for the life of the
|
---|
492 | * logger instance.
|
---|
493 | * @param fDestFlags The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
|
---|
494 | * @param pszFilenameFmt Log filename format string. Standard RTStrFormat().
|
---|
495 | * @param ... Format arguments.
|
---|
496 | */
|
---|
497 | RTDECL(int) RTLogCreate(PRTLOGGER *ppLogger, RTUINT fFlags, const char *pszGroupSettings,
|
---|
498 | const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
|
---|
499 | RTUINT fDestFlags, const char *pszFilenameFmt, ...)
|
---|
500 | {
|
---|
501 | va_list args;
|
---|
502 | int rc;
|
---|
503 |
|
---|
504 | va_start(args, pszFilenameFmt);
|
---|
505 | rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups, fDestFlags, NULL, 0, pszFilenameFmt, args);
|
---|
506 | va_end(args);
|
---|
507 | return rc;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Create a logger instance.
|
---|
512 | *
|
---|
513 | * @returns iprt status code.
|
---|
514 | *
|
---|
515 | * @param ppLogger Where to store the logger instance.
|
---|
516 | * @param fFlags Logger instance flags, a combination of the RTLOGFLAGS_* values.
|
---|
517 | * @param pszGroupSettings The initial group settings.
|
---|
518 | * @param pszEnvVarBase Base name for the environment variables for this instance.
|
---|
519 | * @param cGroups Number of groups in the array.
|
---|
520 | * @param papszGroups Pointer to array of groups. This must stick around for the life of the
|
---|
521 | * logger instance.
|
---|
522 | * @param fDestFlags The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
|
---|
523 | * @param pszErrorMsg A buffer which is filled with an error message if something fails. May be NULL.
|
---|
524 | * @param cchErrorMsg The size of the error message buffer.
|
---|
525 | * @param pszFilenameFmt Log filename format string. Standard RTStrFormat().
|
---|
526 | * @param ... Format arguments.
|
---|
527 | */
|
---|
528 | RTDECL(int) RTLogCreateEx(PRTLOGGER *ppLogger, RTUINT fFlags, const char *pszGroupSettings,
|
---|
529 | const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
|
---|
530 | RTUINT fDestFlags, char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, ...)
|
---|
531 | {
|
---|
532 | va_list args;
|
---|
533 | int rc;
|
---|
534 |
|
---|
535 | va_start(args, pszFilenameFmt);
|
---|
536 | rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups, fDestFlags, pszErrorMsg, cchErrorMsg, pszFilenameFmt, args);
|
---|
537 | va_end(args);
|
---|
538 | return rc;
|
---|
539 | }
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Destroys a logger instance.
|
---|
543 | *
|
---|
544 | * The instance is flushed and all output destinations closed (where applicable).
|
---|
545 | *
|
---|
546 | * @returns iprt status code.
|
---|
547 | * @param pLogger The logger instance which close destroyed. NULL is fine.
|
---|
548 | */
|
---|
549 | RTDECL(int) RTLogDestroy(PRTLOGGER pLogger)
|
---|
550 | {
|
---|
551 | int rc;
|
---|
552 | RTUINT iGroup;
|
---|
553 | RTSEMFASTMUTEX MutexSem;
|
---|
554 |
|
---|
555 | /*
|
---|
556 | * Validate input.
|
---|
557 | */
|
---|
558 | if (!pLogger)
|
---|
559 | return VINF_SUCCESS;
|
---|
560 | AssertReturn(VALID_PTR(pLogger), VERR_INVALID_POINTER);
|
---|
561 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Acquire logger instance sem and disable all logging. (paranoia)
|
---|
565 | */
|
---|
566 | rc = rtlogLock(pLogger);
|
---|
567 | if (RT_FAILURE(rc))
|
---|
568 | return rc;
|
---|
569 |
|
---|
570 | pLogger->fFlags |= RTLOGFLAGS_DISABLED;
|
---|
571 | iGroup = pLogger->cGroups;
|
---|
572 | while (iGroup-- > 0)
|
---|
573 | pLogger->afGroups[iGroup] = 0;
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Flush it.
|
---|
577 | */
|
---|
578 | RTLogFlush(pLogger);
|
---|
579 |
|
---|
580 | /*
|
---|
581 | * Close output stuffs.
|
---|
582 | */
|
---|
583 | #ifdef IN_RING3
|
---|
584 | if (pLogger->File != NIL_RTFILE)
|
---|
585 | {
|
---|
586 | int rc2 = RTFileClose(pLogger->File);
|
---|
587 | AssertRC(rc2);
|
---|
588 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
589 | rc = rc2;
|
---|
590 | pLogger->File = NIL_RTFILE;
|
---|
591 | }
|
---|
592 | #endif
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * Free the mutex and the instance memory.
|
---|
596 | */
|
---|
597 | MutexSem = pLogger->MutexSem;
|
---|
598 | pLogger->MutexSem = NIL_RTSEMFASTMUTEX;
|
---|
599 | if (MutexSem != NIL_RTSEMFASTMUTEX)
|
---|
600 | {
|
---|
601 | int rc2;
|
---|
602 | RTSemFastMutexRelease(MutexSem);
|
---|
603 | rc2 = RTSemFastMutexDestroy(MutexSem);
|
---|
604 | AssertRC(rc2);
|
---|
605 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
606 | rc = rc2;
|
---|
607 | }
|
---|
608 |
|
---|
609 | RTMemFree(pLogger);
|
---|
610 |
|
---|
611 | return rc;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Create a logger instance clone for RC usage.
|
---|
617 | *
|
---|
618 | * @returns iprt status code.
|
---|
619 | *
|
---|
620 | * @param pLogger The logger instance to be cloned.
|
---|
621 | * @param pLoggerGC Where to create the GC logger instance.
|
---|
622 | * @param cbLoggerGC Amount of memory allocated to for the GC logger instance clone.
|
---|
623 | * @param pfnLoggerGCPtr Pointer to logger wrapper function for this instance (GC Ptr).
|
---|
624 | * @param pfnFlushGCPtr Pointer to flush function (GC Ptr).
|
---|
625 | * @param fFlags Logger instance flags, a combination of the RTLOGFLAGS_* values.
|
---|
626 | */
|
---|
627 | RTDECL(int) RTLogCloneRC(PRTLOGGER pLogger, PRTLOGGERRC pLoggerGC, size_t cbLoggerGC,
|
---|
628 | RTRCPTR pfnLoggerGCPtr, RTRCPTR pfnFlushGCPtr, RTUINT fFlags)
|
---|
629 | {
|
---|
630 | /*
|
---|
631 | * Validate input.
|
---|
632 | */
|
---|
633 | if ( !pLoggerGC
|
---|
634 | || !pfnFlushGCPtr
|
---|
635 | || !pfnLoggerGCPtr)
|
---|
636 | {
|
---|
637 | AssertMsgFailed(("Invalid parameters!\n"));
|
---|
638 | return VERR_INVALID_PARAMETER;
|
---|
639 | }
|
---|
640 | if (cbLoggerGC < sizeof(*pLoggerGC))
|
---|
641 | {
|
---|
642 | AssertMsgFailed(("%d min=%d\n", cbLoggerGC, sizeof(*pLoggerGC)));
|
---|
643 | return VERR_INVALID_PARAMETER;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Initialize GC instance.
|
---|
648 | */
|
---|
649 | pLoggerGC->offScratch = 0;
|
---|
650 | pLoggerGC->fPendingPrefix = false;
|
---|
651 | pLoggerGC->pfnLogger = pfnLoggerGCPtr;
|
---|
652 | pLoggerGC->pfnFlush = pfnFlushGCPtr;
|
---|
653 | pLoggerGC->u32Magic = RTLOGGERRC_MAGIC;
|
---|
654 | pLoggerGC->fFlags = fFlags | RTLOGFLAGS_DISABLED;
|
---|
655 | pLoggerGC->cGroups = 1;
|
---|
656 | pLoggerGC->afGroups[0] = 0;
|
---|
657 |
|
---|
658 | /*
|
---|
659 | * Resolve defaults.
|
---|
660 | */
|
---|
661 | if (!pLogger)
|
---|
662 | {
|
---|
663 | pLogger = RTLogDefaultInstance();
|
---|
664 | if (!pLogger)
|
---|
665 | return VINF_SUCCESS;
|
---|
666 | }
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Check if there's enough space for the groups.
|
---|
670 | */
|
---|
671 | if (cbLoggerGC < (size_t)RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]))
|
---|
672 | {
|
---|
673 | AssertMsgFailed(("%d req=%d cGroups=%d\n", cbLoggerGC, RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]), pLogger->cGroups));
|
---|
674 | return VERR_INVALID_PARAMETER;
|
---|
675 | }
|
---|
676 | memcpy(&pLoggerGC->afGroups[0], &pLogger->afGroups[0], pLogger->cGroups * sizeof(pLoggerGC->afGroups[0]));
|
---|
677 | pLoggerGC->cGroups = pLogger->cGroups;
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Copy bits from the HC instance.
|
---|
681 | */
|
---|
682 | pLoggerGC->fPendingPrefix = pLogger->fPendingPrefix;
|
---|
683 | pLoggerGC->fFlags |= pLogger->fFlags;
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * Check if we can remove the disabled flag.
|
---|
687 | */
|
---|
688 | if ( pLogger->fDestFlags
|
---|
689 | && !((pLogger->fFlags | fFlags) & RTLOGFLAGS_DISABLED))
|
---|
690 | pLoggerGC->fFlags &= ~RTLOGFLAGS_DISABLED;
|
---|
691 |
|
---|
692 | return VINF_SUCCESS;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Flushes a GC logger instance to a HC logger.
|
---|
698 | *
|
---|
699 | *
|
---|
700 | * @returns iprt status code.
|
---|
701 | * @param pLogger The HC logger instance to flush pLoggerGC to.
|
---|
702 | * If NULL the default logger is used.
|
---|
703 | * @param pLoggerGC The GC logger instance to flush.
|
---|
704 | */
|
---|
705 | RTDECL(void) RTLogFlushGC(PRTLOGGER pLogger, PRTLOGGERRC pLoggerGC)
|
---|
706 | {
|
---|
707 | /*
|
---|
708 | * Resolve defaults.
|
---|
709 | */
|
---|
710 | if (!pLogger)
|
---|
711 | {
|
---|
712 | pLogger = RTLogDefaultInstance();
|
---|
713 | if (!pLogger)
|
---|
714 | {
|
---|
715 | pLoggerGC->offScratch = 0;
|
---|
716 | return;
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Any thing to flush?
|
---|
722 | */
|
---|
723 | if ( pLogger->offScratch
|
---|
724 | || pLoggerGC->offScratch)
|
---|
725 | {
|
---|
726 | /*
|
---|
727 | * Acquire logger instance sem.
|
---|
728 | */
|
---|
729 | int rc = rtlogLock(pLogger);
|
---|
730 | if (RT_FAILURE(rc))
|
---|
731 | return;
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * Write whatever the GC instance contains to the HC one, and then
|
---|
735 | * flush the HC instance.
|
---|
736 | */
|
---|
737 | if (pLoggerGC->offScratch)
|
---|
738 | {
|
---|
739 | rtLogOutput(pLogger, pLoggerGC->achScratch, pLoggerGC->offScratch);
|
---|
740 | rtLogOutput(pLogger, NULL, 0);
|
---|
741 | pLoggerGC->offScratch = 0;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /*
|
---|
745 | * Release the semaphore.
|
---|
746 | */
|
---|
747 | rtlogUnlock(pLogger);
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | #ifdef IN_RING3
|
---|
753 | /**
|
---|
754 | * Create a logger instance for singled threaded ring-0 usage.
|
---|
755 | *
|
---|
756 | * @returns iprt status code.
|
---|
757 | *
|
---|
758 | * @param pLogger Where to create the logger instance.
|
---|
759 | * @param cbLogger The amount of memory available for the logger instance.
|
---|
760 | * @param pfnLogger Pointer to logger wrapper function for the clone.
|
---|
761 | * @param pfnFlush Pointer to flush function for the clone.
|
---|
762 | * @param fFlags Logger instance flags for the clone, a combination of the RTLOGFLAGS_* values.
|
---|
763 | * @param fDestFlags The destination flags.
|
---|
764 | */
|
---|
765 | RTDECL(int) RTLogCreateForR0(PRTLOGGER pLogger, size_t cbLogger, PFNRTLOGGER pfnLogger, PFNRTLOGFLUSH pfnFlush, RTUINT fFlags, RTUINT fDestFlags)
|
---|
766 | {
|
---|
767 | /*
|
---|
768 | * Validate input.
|
---|
769 | */
|
---|
770 | AssertPtrReturn(pLogger, VERR_INVALID_PARAMETER);
|
---|
771 | AssertReturn(cbLogger >= sizeof(*pLogger), VERR_INVALID_PARAMETER);
|
---|
772 | AssertReturn(pfnLogger, VERR_INVALID_PARAMETER);
|
---|
773 | AssertReturn(pfnFlush, VERR_INVALID_PARAMETER);
|
---|
774 |
|
---|
775 | /*
|
---|
776 | * Initialize the ring-0 instance.
|
---|
777 | */
|
---|
778 | pLogger->offScratch = 0;
|
---|
779 | pLogger->fPendingPrefix = false;
|
---|
780 | pLogger->pfnLogger = pfnLogger;
|
---|
781 | pLogger->pfnFlush = pfnFlush;
|
---|
782 | pLogger->MutexSem = NIL_RTSEMFASTMUTEX; /* Not serialized. */
|
---|
783 | pLogger->u32Magic = RTLOGGER_MAGIC;
|
---|
784 | pLogger->fFlags = fFlags;
|
---|
785 | pLogger->fDestFlags = fDestFlags & ~RTLOGDEST_FILE;
|
---|
786 | pLogger->File = NIL_RTFILE;
|
---|
787 | pLogger->pszFilename = NULL;
|
---|
788 | pLogger->papszGroups = NULL;
|
---|
789 | pLogger->cMaxGroups = (cbLogger - RT_OFFSETOF(RTLOGGER, afGroups[0])) / sizeof(pLogger->afGroups[0]);
|
---|
790 | pLogger->cGroups = 1;
|
---|
791 | pLogger->afGroups[0] = 0;
|
---|
792 | return VINF_SUCCESS;
|
---|
793 | }
|
---|
794 | #endif /* IN_RING3 */
|
---|
795 |
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Copies the group settings and flags from logger instance to another.
|
---|
799 | *
|
---|
800 | * @returns IPRT status code.
|
---|
801 | * @param pDstLogger The destination logger instance.
|
---|
802 | * @param pSrcLogger The source logger instance. If NULL the default one is used.
|
---|
803 | * @param fFlagsOr OR mask for the flags.
|
---|
804 | * @param fFlagsAnd AND mask for the flags.
|
---|
805 | */
|
---|
806 | RTDECL(int) RTLogCopyGroupsAndFlags(PRTLOGGER pDstLogger, PCRTLOGGER pSrcLogger, unsigned fFlagsOr, unsigned fFlagsAnd)
|
---|
807 | {
|
---|
808 | int rc;
|
---|
809 | unsigned cGroups;
|
---|
810 |
|
---|
811 | /*
|
---|
812 | * Validate input.
|
---|
813 | */
|
---|
814 | AssertPtrReturn(pDstLogger, VERR_INVALID_PARAMETER);
|
---|
815 | AssertPtrNullReturn(pSrcLogger, VERR_INVALID_PARAMETER);
|
---|
816 |
|
---|
817 | /*
|
---|
818 | * Resolve defaults.
|
---|
819 | */
|
---|
820 | if (!pSrcLogger)
|
---|
821 | {
|
---|
822 | pSrcLogger = RTLogDefaultInstance();
|
---|
823 | if (!pSrcLogger)
|
---|
824 | {
|
---|
825 | pDstLogger->fFlags |= RTLOGFLAGS_DISABLED;
|
---|
826 | pDstLogger->cGroups = 1;
|
---|
827 | pDstLogger->afGroups[0] = 0;
|
---|
828 | return VINF_SUCCESS;
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | /*
|
---|
833 | * Copy flags and group settings.
|
---|
834 | */
|
---|
835 | pDstLogger->fFlags = (pSrcLogger->fFlags & fFlagsAnd) | fFlagsOr;
|
---|
836 |
|
---|
837 | rc = VINF_SUCCESS;
|
---|
838 | cGroups = pSrcLogger->cGroups;
|
---|
839 | if (cGroups < pDstLogger->cMaxGroups)
|
---|
840 | {
|
---|
841 | AssertMsgFailed(("cMaxGroups=%zd cGroups=%zd (min size %d)\n", pDstLogger->cMaxGroups,
|
---|
842 | pSrcLogger->cGroups, RT_OFFSETOF(RTLOGGER, afGroups[pSrcLogger->cGroups])));
|
---|
843 | rc = VERR_INVALID_PARAMETER;
|
---|
844 | cGroups = pDstLogger->cMaxGroups;
|
---|
845 | }
|
---|
846 | memcpy(&pDstLogger->afGroups[0], &pSrcLogger->afGroups[0], cGroups * sizeof(pDstLogger->afGroups[0]));
|
---|
847 | pDstLogger->cGroups = cGroups;
|
---|
848 |
|
---|
849 | return rc;
|
---|
850 | }
|
---|
851 |
|
---|
852 |
|
---|
853 | /**
|
---|
854 | * Flushes the buffer in one logger instance onto another logger.
|
---|
855 | *
|
---|
856 | * @returns iprt status code.
|
---|
857 | *
|
---|
858 | * @param pSrcLogger The logger instance to flush.
|
---|
859 | * @param pDstLogger The logger instance to flush onto.
|
---|
860 | * If NULL the default logger will be used.
|
---|
861 | */
|
---|
862 | RTDECL(void) RTLogFlushToLogger(PRTLOGGER pSrcLogger, PRTLOGGER pDstLogger)
|
---|
863 | {
|
---|
864 | /*
|
---|
865 | * Resolve defaults.
|
---|
866 | */
|
---|
867 | if (!pDstLogger)
|
---|
868 | {
|
---|
869 | pDstLogger = RTLogDefaultInstance();
|
---|
870 | if (!pDstLogger)
|
---|
871 | {
|
---|
872 | /* flushing to "/dev/null". */
|
---|
873 | if (pSrcLogger->offScratch)
|
---|
874 | {
|
---|
875 | int rc = rtlogLock(pSrcLogger);
|
---|
876 | if (RT_SUCCESS(rc))
|
---|
877 | {
|
---|
878 | pSrcLogger->offScratch = 0;
|
---|
879 | rtlogLock(pSrcLogger);
|
---|
880 | }
|
---|
881 | }
|
---|
882 | return;
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | /*
|
---|
887 | * Any thing to flush?
|
---|
888 | */
|
---|
889 | if ( pSrcLogger->offScratch
|
---|
890 | || pDstLogger->offScratch)
|
---|
891 | {
|
---|
892 | /*
|
---|
893 | * Acquire logger semaphores.
|
---|
894 | */
|
---|
895 | int rc = rtlogLock(pDstLogger);
|
---|
896 | if (RT_FAILURE(rc))
|
---|
897 | return;
|
---|
898 | rc = rtlogLock(pSrcLogger);
|
---|
899 | if (RT_SUCCESS(rc))
|
---|
900 | {
|
---|
901 | /*
|
---|
902 | * Write whatever the GC instance contains to the HC one, and then
|
---|
903 | * flush the HC instance.
|
---|
904 | */
|
---|
905 | if (pSrcLogger->offScratch)
|
---|
906 | {
|
---|
907 | rtLogOutput(pDstLogger, pSrcLogger->achScratch, pSrcLogger->offScratch);
|
---|
908 | rtLogOutput(pDstLogger, NULL, 0);
|
---|
909 | pSrcLogger->offScratch = 0;
|
---|
910 | }
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * Release the semaphores.
|
---|
914 | */
|
---|
915 | rtlogUnlock(pSrcLogger);
|
---|
916 | }
|
---|
917 | rtlogUnlock(pDstLogger);
|
---|
918 | }
|
---|
919 | }
|
---|
920 |
|
---|
921 |
|
---|
922 | /**
|
---|
923 | * Matches a group name with a pattern mask in an case insensitive manner (ASCII).
|
---|
924 | *
|
---|
925 | * @returns true if matching and *ppachMask set to the end of the pattern.
|
---|
926 | * @returns false if no match.
|
---|
927 | * @param pszGrp The group name.
|
---|
928 | * @param ppachMask Pointer to the pointer to the mask. Only wildcard supported is '*'.
|
---|
929 | * @param cchMask The length of the mask, including modifiers. The modifiers is why
|
---|
930 | * we update *ppachMask on match.
|
---|
931 | */
|
---|
932 | static bool rtlogIsGroupMatching(const char *pszGrp, const char **ppachMask, size_t cchMask)
|
---|
933 | {
|
---|
934 | const char *pachMask;
|
---|
935 |
|
---|
936 | if (!pszGrp || !*pszGrp)
|
---|
937 | return false;
|
---|
938 | pachMask = *ppachMask;
|
---|
939 | for (;;)
|
---|
940 | {
|
---|
941 | if (RT_C_TO_LOWER(*pszGrp) != RT_C_TO_LOWER(*pachMask))
|
---|
942 | {
|
---|
943 | const char *pszTmp;
|
---|
944 |
|
---|
945 | /*
|
---|
946 | * Check for wildcard and do a minimal match if found.
|
---|
947 | */
|
---|
948 | if (*pachMask != '*')
|
---|
949 | return false;
|
---|
950 |
|
---|
951 | /* eat '*'s. */
|
---|
952 | do pachMask++;
|
---|
953 | while (--cchMask && *pachMask == '*');
|
---|
954 |
|
---|
955 | /* is there more to match? */
|
---|
956 | if ( !cchMask
|
---|
957 | || *pachMask == '.'
|
---|
958 | || *pachMask == '=')
|
---|
959 | break; /* we're good */
|
---|
960 |
|
---|
961 | /* do extremely minimal matching (fixme) */
|
---|
962 | pszTmp = strchr(pszGrp, RT_C_TO_LOWER(*pachMask));
|
---|
963 | if (!pszTmp)
|
---|
964 | pszTmp = strchr(pszGrp, RT_C_TO_UPPER(*pachMask));
|
---|
965 | if (!pszTmp)
|
---|
966 | return false;
|
---|
967 | pszGrp = pszTmp;
|
---|
968 | continue;
|
---|
969 | }
|
---|
970 |
|
---|
971 | /* done? */
|
---|
972 | if (!*++pszGrp)
|
---|
973 | {
|
---|
974 | /* trailing wildcard is ok. */
|
---|
975 | do
|
---|
976 | {
|
---|
977 | pachMask++;
|
---|
978 | cchMask--;
|
---|
979 | } while (cchMask && *pachMask == '*');
|
---|
980 | if ( !cchMask
|
---|
981 | || *pachMask == '.'
|
---|
982 | || *pachMask == '=')
|
---|
983 | break; /* we're good */
|
---|
984 | return false;
|
---|
985 | }
|
---|
986 |
|
---|
987 | if (!--cchMask)
|
---|
988 | return false;
|
---|
989 | pachMask++;
|
---|
990 | }
|
---|
991 |
|
---|
992 | /* match */
|
---|
993 | *ppachMask = pachMask;
|
---|
994 | return true;
|
---|
995 | }
|
---|
996 |
|
---|
997 |
|
---|
998 | /**
|
---|
999 | * Updates the group settings for the logger instance using the specified
|
---|
1000 | * specification string.
|
---|
1001 | *
|
---|
1002 | * @returns iprt status code.
|
---|
1003 | * Failures can safely be ignored.
|
---|
1004 | * @param pLogger Logger instance.
|
---|
1005 | * @param pszVar Value to parse.
|
---|
1006 | */
|
---|
1007 | RTDECL(int) RTLogGroupSettings(PRTLOGGER pLogger, const char *pszVar)
|
---|
1008 | {
|
---|
1009 | /*
|
---|
1010 | * Resolve defaults.
|
---|
1011 | */
|
---|
1012 | if (!pLogger)
|
---|
1013 | {
|
---|
1014 | pLogger = RTLogDefaultInstance();
|
---|
1015 | if (!pLogger)
|
---|
1016 | return VINF_SUCCESS;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Iterate the string.
|
---|
1021 | */
|
---|
1022 | while (*pszVar)
|
---|
1023 | {
|
---|
1024 | /*
|
---|
1025 | * Skip prefixes (blanks, ;, + and -).
|
---|
1026 | */
|
---|
1027 | bool fEnabled = true;
|
---|
1028 | char ch;
|
---|
1029 | const char *pszStart;
|
---|
1030 | unsigned i;
|
---|
1031 | size_t cch;
|
---|
1032 |
|
---|
1033 | while ((ch = *pszVar) == '+' || ch == '-' || ch == ' ' || ch == '\t' || ch == '\n' || ch == ';')
|
---|
1034 | {
|
---|
1035 | if (ch == '+' || ch == '-' || ';')
|
---|
1036 | fEnabled = ch != '-';
|
---|
1037 | pszVar++;
|
---|
1038 | }
|
---|
1039 | if (!*pszVar)
|
---|
1040 | break;
|
---|
1041 |
|
---|
1042 | /*
|
---|
1043 | * Find end.
|
---|
1044 | */
|
---|
1045 | pszStart = pszVar;
|
---|
1046 | while ((ch = *pszVar) != '\0' && ch != '+' && ch != '-' && ch != ' ' && ch != '\t')
|
---|
1047 | pszVar++;
|
---|
1048 |
|
---|
1049 | /*
|
---|
1050 | * Find the group (ascii case insensitive search).
|
---|
1051 | * Special group 'all'.
|
---|
1052 | */
|
---|
1053 | cch = pszVar - pszStart;
|
---|
1054 | if ( cch >= 3
|
---|
1055 | && (pszStart[0] == 'a' || pszStart[0] == 'A')
|
---|
1056 | && (pszStart[1] == 'l' || pszStart[1] == 'L')
|
---|
1057 | && (pszStart[2] == 'l' || pszStart[2] == 'L')
|
---|
1058 | && (cch == 3 || pszStart[3] == '.' || pszStart[3] == '='))
|
---|
1059 | {
|
---|
1060 | /*
|
---|
1061 | * All.
|
---|
1062 | */
|
---|
1063 | unsigned fFlags = cch == 3
|
---|
1064 | ? RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1
|
---|
1065 | : rtlogGroupFlags(&pszStart[3]);
|
---|
1066 | for (i = 0; i < pLogger->cGroups; i++)
|
---|
1067 | {
|
---|
1068 | if (fEnabled)
|
---|
1069 | pLogger->afGroups[i] |= fFlags;
|
---|
1070 | else
|
---|
1071 | pLogger->afGroups[i] &= ~fFlags;
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | else
|
---|
1075 | {
|
---|
1076 | /*
|
---|
1077 | * Specific group(s).
|
---|
1078 | */
|
---|
1079 | for (i = 0; i < pLogger->cGroups; i++)
|
---|
1080 | {
|
---|
1081 | const char *psz2 = (const char*)pszStart;
|
---|
1082 | if (rtlogIsGroupMatching(pLogger->papszGroups[i], &psz2, cch))
|
---|
1083 | {
|
---|
1084 | unsigned fFlags = RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1;
|
---|
1085 | if (*psz2 == '.' || *psz2 == '=')
|
---|
1086 | fFlags = rtlogGroupFlags(psz2);
|
---|
1087 | if (fEnabled)
|
---|
1088 | pLogger->afGroups[i] |= fFlags;
|
---|
1089 | else
|
---|
1090 | pLogger->afGroups[i] &= ~fFlags;
|
---|
1091 | }
|
---|
1092 | } /* for each group */
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | } /* parse specification */
|
---|
1096 |
|
---|
1097 | return VINF_SUCCESS;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Interprets the group flags suffix.
|
---|
1103 | *
|
---|
1104 | * @returns Flags specified. (0 is possible!)
|
---|
1105 | * @param psz Start of Suffix. (Either dot or equal sign.)
|
---|
1106 | */
|
---|
1107 | static unsigned rtlogGroupFlags(const char *psz)
|
---|
1108 | {
|
---|
1109 | unsigned fFlags = 0;
|
---|
1110 |
|
---|
1111 | /*
|
---|
1112 | * Litteral flags.
|
---|
1113 | */
|
---|
1114 | while (*psz == '.')
|
---|
1115 | {
|
---|
1116 | static struct
|
---|
1117 | {
|
---|
1118 | const char *pszFlag; /* lowercase!! */
|
---|
1119 | unsigned fFlag;
|
---|
1120 | } aFlags[] =
|
---|
1121 | {
|
---|
1122 | { "eo", RTLOGGRPFLAGS_ENABLED },
|
---|
1123 | { "enabledonly",RTLOGGRPFLAGS_ENABLED },
|
---|
1124 | { "e", RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1125 | { "enabled", RTLOGGRPFLAGS_ENABLED | RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1126 | { "l1", RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1127 | { "level1", RTLOGGRPFLAGS_LEVEL_1 },
|
---|
1128 | { "l", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1129 | { "l2", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1130 | { "level2", RTLOGGRPFLAGS_LEVEL_2 },
|
---|
1131 | { "l3", RTLOGGRPFLAGS_LEVEL_3 },
|
---|
1132 | { "level3", RTLOGGRPFLAGS_LEVEL_3 },
|
---|
1133 | { "l4", RTLOGGRPFLAGS_LEVEL_4 },
|
---|
1134 | { "level4", RTLOGGRPFLAGS_LEVEL_4 },
|
---|
1135 | { "l5", RTLOGGRPFLAGS_LEVEL_5 },
|
---|
1136 | { "level5", RTLOGGRPFLAGS_LEVEL_5 },
|
---|
1137 | { "l6", RTLOGGRPFLAGS_LEVEL_6 },
|
---|
1138 | { "level6", RTLOGGRPFLAGS_LEVEL_6 },
|
---|
1139 | { "f", RTLOGGRPFLAGS_FLOW },
|
---|
1140 | { "flow", RTLOGGRPFLAGS_FLOW },
|
---|
1141 |
|
---|
1142 | { "lelik", RTLOGGRPFLAGS_LELIK },
|
---|
1143 | { "michael", RTLOGGRPFLAGS_MICHAEL },
|
---|
1144 | { "dmik", RTLOGGRPFLAGS_DMIK },
|
---|
1145 | { "sunlover", RTLOGGRPFLAGS_SUNLOVER },
|
---|
1146 | { "achim", RTLOGGRPFLAGS_ACHIM },
|
---|
1147 | { "achimha", RTLOGGRPFLAGS_ACHIM },
|
---|
1148 | { "s", RTLOGGRPFLAGS_SANDER },
|
---|
1149 | { "sander", RTLOGGRPFLAGS_SANDER },
|
---|
1150 | { "sandervl", RTLOGGRPFLAGS_SANDER },
|
---|
1151 | { "klaus", RTLOGGRPFLAGS_KLAUS },
|
---|
1152 | { "frank", RTLOGGRPFLAGS_FRANK },
|
---|
1153 | { "b", RTLOGGRPFLAGS_BIRD },
|
---|
1154 | { "bird", RTLOGGRPFLAGS_BIRD },
|
---|
1155 | { "aleksey", RTLOGGRPFLAGS_ALEKSEY },
|
---|
1156 | { "n", RTLOGGRPFLAGS_NONAME },
|
---|
1157 | { "noname", RTLOGGRPFLAGS_NONAME }
|
---|
1158 | };
|
---|
1159 | unsigned i;
|
---|
1160 | bool fFound = false;
|
---|
1161 | psz++;
|
---|
1162 | for (i = 0; i < ELEMENTS(aFlags) && !fFound; i++)
|
---|
1163 | {
|
---|
1164 | const char *psz1 = aFlags[i].pszFlag;
|
---|
1165 | const char *psz2 = psz;
|
---|
1166 | while (*psz1 == RT_C_TO_LOWER(*psz2))
|
---|
1167 | {
|
---|
1168 | psz1++;
|
---|
1169 | psz2++;
|
---|
1170 | if (!*psz1)
|
---|
1171 | {
|
---|
1172 | if ( (*psz2 >= 'a' && *psz2 <= 'z')
|
---|
1173 | || (*psz2 >= 'A' && *psz2 <= 'Z')
|
---|
1174 | || (*psz2 >= '0' && *psz2 <= '9') )
|
---|
1175 | break;
|
---|
1176 | fFlags |= aFlags[i].fFlag;
|
---|
1177 | fFound = true;
|
---|
1178 | psz = psz2;
|
---|
1179 | break;
|
---|
1180 | }
|
---|
1181 | } /* strincmp */
|
---|
1182 | } /* for each flags */
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | /*
|
---|
1186 | * Flag value.
|
---|
1187 | */
|
---|
1188 | if (*psz == '=')
|
---|
1189 | {
|
---|
1190 | psz++;
|
---|
1191 | if (*psz == '~')
|
---|
1192 | fFlags = ~RTStrToInt32(psz + 1);
|
---|
1193 | else
|
---|
1194 | fFlags = RTStrToInt32(psz);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | return fFlags;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | #endif /* !IN_GC */
|
---|
1201 |
|
---|
1202 |
|
---|
1203 | /**
|
---|
1204 | * Updates the flags for the logger instance using the specified
|
---|
1205 | * specification string.
|
---|
1206 | *
|
---|
1207 | * @returns iprt status code.
|
---|
1208 | * Failures can safely be ignored.
|
---|
1209 | * @param pLogger Logger instance (NULL for default logger).
|
---|
1210 | * @param pszVar Value to parse.
|
---|
1211 | */
|
---|
1212 | RTDECL(int) RTLogFlags(PRTLOGGER pLogger, const char *pszVar)
|
---|
1213 | {
|
---|
1214 | int rc = VINF_SUCCESS;
|
---|
1215 |
|
---|
1216 | /*
|
---|
1217 | * Resolve defaults.
|
---|
1218 | */
|
---|
1219 | if (!pLogger)
|
---|
1220 | {
|
---|
1221 | pLogger = RTLogDefaultInstance();
|
---|
1222 | if (!pLogger)
|
---|
1223 | return VINF_SUCCESS;
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | /*
|
---|
1227 | * Iterate the string.
|
---|
1228 | */
|
---|
1229 | while (*pszVar)
|
---|
1230 | {
|
---|
1231 | /* parse instruction. */
|
---|
1232 | static struct
|
---|
1233 | {
|
---|
1234 | const char *pszInstr;
|
---|
1235 | size_t cchInstr;
|
---|
1236 | unsigned fFlag;
|
---|
1237 | bool fInverted;
|
---|
1238 | } const aDest[] =
|
---|
1239 | {
|
---|
1240 | { "disabled", sizeof("disabled" ) - 1, RTLOGFLAGS_DISABLED, false },
|
---|
1241 | { "enabled", sizeof("enabled" ) - 1, RTLOGFLAGS_DISABLED, true },
|
---|
1242 | { "buffered", sizeof("buffered" ) - 1, RTLOGFLAGS_BUFFERED, false },
|
---|
1243 | { "unbuffered", sizeof("unbuffered" ) - 1, RTLOGFLAGS_BUFFERED, true },
|
---|
1244 | { "usecrlf", sizeof("usecrlf" ) - 1, RTLOGFLAGS_USECRLF, true },
|
---|
1245 | { "uself", sizeof("uself" ) - 1, RTLOGFLAGS_USECRLF, false },
|
---|
1246 | { "append", sizeof("append" ) - 1, RTLOGFLAGS_APPEND, false },
|
---|
1247 | { "overwrite", sizeof("overwrite" ) - 1, RTLOGFLAGS_APPEND, true },
|
---|
1248 | { "rel", sizeof("rel" ) - 1, RTLOGFLAGS_REL_TS, false },
|
---|
1249 | { "abs", sizeof("abs" ) - 1, RTLOGFLAGS_REL_TS, true },
|
---|
1250 | { "dec", sizeof("dec" ) - 1, RTLOGFLAGS_DECIMAL_TS, false },
|
---|
1251 | { "hex", sizeof("hex" ) - 1, RTLOGFLAGS_DECIMAL_TS, true },
|
---|
1252 | { "lockcnts", sizeof("lockcnts" ) - 1, RTLOGFLAGS_PREFIX_LOCK_COUNTS, false },
|
---|
1253 | { "cpuid", sizeof("cpuid" ) - 1, RTLOGFLAGS_PREFIX_CPUID, false },
|
---|
1254 | { "pid", sizeof("pid" ) - 1, RTLOGFLAGS_PREFIX_PID, false },
|
---|
1255 | { "flagno", sizeof("flagno" ) - 1, RTLOGFLAGS_PREFIX_FLAG_NO, false },
|
---|
1256 | { "flag", sizeof("flag" ) - 1, RTLOGFLAGS_PREFIX_FLAG, false },
|
---|
1257 | { "groupno", sizeof("groupno" ) - 1, RTLOGFLAGS_PREFIX_GROUP_NO, false },
|
---|
1258 | { "group", sizeof("group" ) - 1, RTLOGFLAGS_PREFIX_GROUP, false },
|
---|
1259 | { "tid", sizeof("tid" ) - 1, RTLOGFLAGS_PREFIX_TID, false },
|
---|
1260 | { "thread", sizeof("thread" ) - 1, RTLOGFLAGS_PREFIX_THREAD, false },
|
---|
1261 | { "timeprog", sizeof("timeprog" ) - 1, RTLOGFLAGS_PREFIX_TIME_PROG, false },
|
---|
1262 | { "time", sizeof("time" ) - 1, RTLOGFLAGS_PREFIX_TIME, false },
|
---|
1263 | { "msprog", sizeof("msprog" ) - 1, RTLOGFLAGS_PREFIX_MS_PROG, false },
|
---|
1264 | { "tsc", sizeof("tsc" ) - 1, RTLOGFLAGS_PREFIX_TSC, false }, /* before ts! */
|
---|
1265 | { "ts", sizeof("ts" ) - 1, RTLOGFLAGS_PREFIX_TS, false },
|
---|
1266 | };
|
---|
1267 |
|
---|
1268 | /* check no prefix. */
|
---|
1269 | bool fNo = false;
|
---|
1270 | char ch;
|
---|
1271 | unsigned i;
|
---|
1272 |
|
---|
1273 | /* skip blanks. */
|
---|
1274 | while (RT_C_IS_SPACE(*pszVar))
|
---|
1275 | pszVar++;
|
---|
1276 | if (!*pszVar)
|
---|
1277 | return rc;
|
---|
1278 |
|
---|
1279 | while ((ch = *pszVar) != '\0')
|
---|
1280 | {
|
---|
1281 | if (ch == 'n' && pszVar[1] == 'o')
|
---|
1282 | {
|
---|
1283 | pszVar += 2;
|
---|
1284 | fNo = !fNo;
|
---|
1285 | }
|
---|
1286 | else if (ch == '+')
|
---|
1287 | {
|
---|
1288 | pszVar++;
|
---|
1289 | fNo = true;
|
---|
1290 | }
|
---|
1291 | else if (ch == '-' || ch == '!' || ch == '~')
|
---|
1292 | {
|
---|
1293 | pszVar++;
|
---|
1294 | fNo = !fNo;
|
---|
1295 | }
|
---|
1296 | else
|
---|
1297 | break;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | /* instruction. */
|
---|
1301 | for (i = 0; i < ELEMENTS(aDest); i++)
|
---|
1302 | {
|
---|
1303 | if (!strncmp(pszVar, aDest[i].pszInstr, aDest[i].cchInstr))
|
---|
1304 | {
|
---|
1305 | if (fNo == aDest[i].fInverted)
|
---|
1306 | pLogger->fFlags |= aDest[i].fFlag;
|
---|
1307 | else
|
---|
1308 | pLogger->fFlags &= ~aDest[i].fFlag;
|
---|
1309 | pszVar += aDest[i].cchInstr;
|
---|
1310 | break;
|
---|
1311 | }
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | /* unknown instruction? */
|
---|
1315 | if (i >= ELEMENTS(aDest))
|
---|
1316 | {
|
---|
1317 | AssertMsgFailed(("Invalid flags! unknown instruction %.20s\n", pszVar));
|
---|
1318 | pszVar++;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /* skip blanks and delimiters. */
|
---|
1322 | while (RT_C_IS_SPACE(*pszVar) || *pszVar == ';')
|
---|
1323 | pszVar++;
|
---|
1324 | } /* while more environment variable value left */
|
---|
1325 |
|
---|
1326 | return rc;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 |
|
---|
1330 | /**
|
---|
1331 | * Flushes the specified logger.
|
---|
1332 | *
|
---|
1333 | * @param pLogger The logger instance to flush.
|
---|
1334 | * If NULL the default instance is used. The default instance
|
---|
1335 | * will not be initialized by this call.
|
---|
1336 | */
|
---|
1337 | RTDECL(void) RTLogFlush(PRTLOGGER pLogger)
|
---|
1338 | {
|
---|
1339 | /*
|
---|
1340 | * Resolve defaults.
|
---|
1341 | */
|
---|
1342 | if (!pLogger)
|
---|
1343 | {
|
---|
1344 | #ifdef IN_GC
|
---|
1345 | pLogger = &g_Logger;
|
---|
1346 | #else
|
---|
1347 | pLogger = g_pLogger;
|
---|
1348 | #endif
|
---|
1349 | if (!pLogger)
|
---|
1350 | return;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | /*
|
---|
1354 | * Any thing to flush?
|
---|
1355 | */
|
---|
1356 | if (pLogger->offScratch)
|
---|
1357 | {
|
---|
1358 | #ifndef IN_GC
|
---|
1359 | /*
|
---|
1360 | * Acquire logger instance sem.
|
---|
1361 | */
|
---|
1362 | int rc = rtlogLock(pLogger);
|
---|
1363 | if (RT_FAILURE(rc))
|
---|
1364 | return;
|
---|
1365 | #endif
|
---|
1366 | /*
|
---|
1367 | * Call worker.
|
---|
1368 | */
|
---|
1369 | rtlogFlush(pLogger);
|
---|
1370 |
|
---|
1371 | #ifndef IN_GC
|
---|
1372 | /*
|
---|
1373 | * Release the semaphore.
|
---|
1374 | */
|
---|
1375 | rtlogUnlock(pLogger);
|
---|
1376 | #endif
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 |
|
---|
1381 | /**
|
---|
1382 | * Gets the default logger instance.
|
---|
1383 | *
|
---|
1384 | * @returns Pointer to default logger instance.
|
---|
1385 | * @returns NULL if no default logger instance available.
|
---|
1386 | */
|
---|
1387 | RTDECL(PRTLOGGER) RTLogDefaultInstance(void)
|
---|
1388 | {
|
---|
1389 | #ifdef IN_GC
|
---|
1390 | return &g_Logger;
|
---|
1391 |
|
---|
1392 | #else /* !IN_GC */
|
---|
1393 | # ifdef IN_RING0
|
---|
1394 | /*
|
---|
1395 | * Check per thread loggers first.
|
---|
1396 | */
|
---|
1397 | if (g_cPerThreadLoggers)
|
---|
1398 | {
|
---|
1399 | const RTNATIVETHREAD Self = RTThreadNativeSelf();
|
---|
1400 | int32_t i = ELEMENTS(g_aPerThreadLoggers);
|
---|
1401 | while (i-- > 0)
|
---|
1402 | if (g_aPerThreadLoggers[i].NativeThread == Self)
|
---|
1403 | return g_aPerThreadLoggers[i].pLogger;
|
---|
1404 | }
|
---|
1405 | # endif /* IN_RING0 */
|
---|
1406 |
|
---|
1407 | /*
|
---|
1408 | * If no per thread logger, use the default one.
|
---|
1409 | */
|
---|
1410 | if (!g_pLogger)
|
---|
1411 | g_pLogger = RTLogDefaultInit();
|
---|
1412 | return g_pLogger;
|
---|
1413 | #endif /* !IN_GC */
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 |
|
---|
1417 | #ifndef IN_GC
|
---|
1418 | /**
|
---|
1419 | * Sets the default logger instance.
|
---|
1420 | *
|
---|
1421 | * @returns iprt status code.
|
---|
1422 | * @param pLogger The new default logger instance.
|
---|
1423 | */
|
---|
1424 | RTDECL(PRTLOGGER) RTLogSetDefaultInstance(PRTLOGGER pLogger)
|
---|
1425 | {
|
---|
1426 | return (PRTLOGGER)ASMAtomicXchgPtr((void * volatile *)&g_pLogger, pLogger);
|
---|
1427 | }
|
---|
1428 | #endif /* !IN_GC */
|
---|
1429 |
|
---|
1430 |
|
---|
1431 | #ifdef IN_RING0
|
---|
1432 | /**
|
---|
1433 | * Changes the default logger instance for the current thread.
|
---|
1434 | *
|
---|
1435 | * @returns IPRT status code.
|
---|
1436 | * @param pLogger The logger instance. Pass NULL for deregistration.
|
---|
1437 | * @param uKey Associated key for cleanup purposes. If pLogger is NULL,
|
---|
1438 | * all instances with this key will be deregistered. So in
|
---|
1439 | * order to only deregister the instance associated with the
|
---|
1440 | * current thread use 0.
|
---|
1441 | */
|
---|
1442 | RTDECL(int) RTLogSetDefaultInstanceThread(PRTLOGGER pLogger, uintptr_t uKey)
|
---|
1443 | {
|
---|
1444 | int rc;
|
---|
1445 | RTNATIVETHREAD Self = RTThreadNativeSelf();
|
---|
1446 | if (pLogger)
|
---|
1447 | {
|
---|
1448 | int32_t i;
|
---|
1449 | unsigned j;
|
---|
1450 |
|
---|
1451 | AssertReturn(pLogger->u32Magic == RTLOGGER_MAGIC, VERR_INVALID_MAGIC);
|
---|
1452 |
|
---|
1453 | /*
|
---|
1454 | * Iterate the table to see if there is already an entry for this thread.
|
---|
1455 | */
|
---|
1456 | i = ELEMENTS(g_aPerThreadLoggers);
|
---|
1457 | while (i-- > 0)
|
---|
1458 | if (g_aPerThreadLoggers[i].NativeThread == Self)
|
---|
1459 | {
|
---|
1460 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].uKey, (void *)uKey);
|
---|
1461 | g_aPerThreadLoggers[i].pLogger = pLogger;
|
---|
1462 | return VINF_SUCCESS;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | /*
|
---|
1466 | * Allocate a new table entry.
|
---|
1467 | */
|
---|
1468 | i = ASMAtomicIncS32(&g_cPerThreadLoggers);
|
---|
1469 | if (i > (int32_t)ELEMENTS(g_aPerThreadLoggers))
|
---|
1470 | {
|
---|
1471 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
1472 | return VERR_BUFFER_OVERFLOW; /* horrible error code! */
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | for (j = 0; j < 10; j++)
|
---|
1476 | {
|
---|
1477 | i = ELEMENTS(g_aPerThreadLoggers);
|
---|
1478 | while (i-- > 0)
|
---|
1479 | {
|
---|
1480 | AssertCompile(sizeof(RTNATIVETHREAD) == sizeof(void*));
|
---|
1481 | if ( g_aPerThreadLoggers[i].NativeThread == NIL_RTNATIVETHREAD
|
---|
1482 | && ASMAtomicCmpXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].NativeThread, (void *)Self, (void *)NIL_RTNATIVETHREAD))
|
---|
1483 | {
|
---|
1484 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].uKey, (void *)uKey);
|
---|
1485 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].pLogger, pLogger);
|
---|
1486 | return VINF_SUCCESS;
|
---|
1487 | }
|
---|
1488 | }
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
1492 | rc = VERR_INTERNAL_ERROR;
|
---|
1493 | }
|
---|
1494 | else
|
---|
1495 | {
|
---|
1496 | /*
|
---|
1497 | * Search the array for the current thread.
|
---|
1498 | */
|
---|
1499 | int32_t i = ELEMENTS(g_aPerThreadLoggers);
|
---|
1500 | while (i-- > 0)
|
---|
1501 | if ( g_aPerThreadLoggers[i].NativeThread == Self
|
---|
1502 | || g_aPerThreadLoggers[i].uKey == uKey)
|
---|
1503 | {
|
---|
1504 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].uKey, NULL);
|
---|
1505 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].pLogger, NULL);
|
---|
1506 | ASMAtomicXchgPtr((void * volatile *)&g_aPerThreadLoggers[i].NativeThread, (void *)NIL_RTNATIVETHREAD);
|
---|
1507 | ASMAtomicDecS32(&g_cPerThreadLoggers);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | rc = VINF_SUCCESS;
|
---|
1511 | }
|
---|
1512 | return rc;
|
---|
1513 | }
|
---|
1514 | #endif
|
---|
1515 |
|
---|
1516 |
|
---|
1517 | /**
|
---|
1518 | * Write to a logger instance.
|
---|
1519 | *
|
---|
1520 | * @param pLogger Pointer to logger instance.
|
---|
1521 | * @param pszFormat Format string.
|
---|
1522 | * @param args Format arguments.
|
---|
1523 | */
|
---|
1524 | RTDECL(void) RTLogLoggerV(PRTLOGGER pLogger, const char *pszFormat, va_list args)
|
---|
1525 | {
|
---|
1526 | RTLogLoggerExV(pLogger, 0, ~0U, pszFormat, args);
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 |
|
---|
1530 | /**
|
---|
1531 | * Write to a logger instance.
|
---|
1532 | *
|
---|
1533 | * This function will check whether the instance, group and flags makes up a
|
---|
1534 | * logging kind which is currently enabled before writing anything to the log.
|
---|
1535 | *
|
---|
1536 | * @param pLogger Pointer to logger instance. If NULL the default logger instance will be attempted.
|
---|
1537 | * @param fFlags The logging flags.
|
---|
1538 | * @param iGroup The group.
|
---|
1539 | * The value ~0U is reserved for compatability with RTLogLogger[V] and is
|
---|
1540 | * only for internal usage!
|
---|
1541 | * @param pszFormat Format string.
|
---|
1542 | * @param args Format arguments.
|
---|
1543 | */
|
---|
1544 | RTDECL(void) RTLogLoggerExV(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
|
---|
1545 | {
|
---|
1546 | int rc;
|
---|
1547 |
|
---|
1548 | /*
|
---|
1549 | * A NULL logger means default instance.
|
---|
1550 | */
|
---|
1551 | if (!pLogger)
|
---|
1552 | {
|
---|
1553 | pLogger = RTLogDefaultInstance();
|
---|
1554 | if (!pLogger)
|
---|
1555 | return;
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | /*
|
---|
1559 | * Validate and correct iGroup.
|
---|
1560 | */
|
---|
1561 | if (iGroup != ~0U && iGroup >= pLogger->cGroups)
|
---|
1562 | iGroup = 0;
|
---|
1563 |
|
---|
1564 | /*
|
---|
1565 | * If no output, then just skip it.
|
---|
1566 | */
|
---|
1567 | if ( (pLogger->fFlags & RTLOGFLAGS_DISABLED)
|
---|
1568 | #ifndef IN_GC
|
---|
1569 | || !pLogger->fDestFlags
|
---|
1570 | #endif
|
---|
1571 | || !pszFormat || !*pszFormat)
|
---|
1572 | return;
|
---|
1573 | if ( iGroup != ~0U
|
---|
1574 | && (pLogger->afGroups[iGroup] & (fFlags | RTLOGGRPFLAGS_ENABLED)) != (fFlags | RTLOGGRPFLAGS_ENABLED))
|
---|
1575 | return;
|
---|
1576 |
|
---|
1577 | /*
|
---|
1578 | * Acquire logger instance sem.
|
---|
1579 | */
|
---|
1580 | rc = rtlogLock(pLogger);
|
---|
1581 | if (RT_FAILURE(rc))
|
---|
1582 | return;
|
---|
1583 |
|
---|
1584 | /*
|
---|
1585 | * Format the message and perhaps flush it.
|
---|
1586 | */
|
---|
1587 | if (pLogger->fFlags & (RTLOGFLAGS_PREFIX_MASK | RTLOGFLAGS_USECRLF))
|
---|
1588 | {
|
---|
1589 | RTLOGOUTPUTPREFIXEDARGS OutputArgs;
|
---|
1590 | OutputArgs.pLogger = pLogger;
|
---|
1591 | OutputArgs.iGroup = iGroup;
|
---|
1592 | OutputArgs.fFlags = fFlags;
|
---|
1593 | RTLogFormatV(rtLogOutputPrefixed, &OutputArgs, pszFormat, args);
|
---|
1594 | }
|
---|
1595 | else
|
---|
1596 | RTLogFormatV(rtLogOutput, pLogger, pszFormat, args);
|
---|
1597 | if ( !(pLogger->fFlags & RTLOGFLAGS_BUFFERED)
|
---|
1598 | && pLogger->offScratch)
|
---|
1599 | rtlogFlush(pLogger);
|
---|
1600 |
|
---|
1601 | /*
|
---|
1602 | * Release the semaphore.
|
---|
1603 | */
|
---|
1604 | rtlogUnlock(pLogger);
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 |
|
---|
1608 | /**
|
---|
1609 | * vprintf like function for writing to the default log.
|
---|
1610 | *
|
---|
1611 | * @param pszFormat Printf like format string.
|
---|
1612 | * @param args Optional arguments as specified in pszFormat.
|
---|
1613 | *
|
---|
1614 | * @remark The API doesn't support formatting of floating point numbers at the moment.
|
---|
1615 | */
|
---|
1616 | RTDECL(void) RTLogPrintfV(const char *pszFormat, va_list args)
|
---|
1617 | {
|
---|
1618 | RTLogLoggerV(NULL, pszFormat, args);
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 |
|
---|
1622 | /**
|
---|
1623 | * Writes the buffer to the given log device without checking for buffered
|
---|
1624 | * data or anything.
|
---|
1625 | * Used by the RTLogFlush() function.
|
---|
1626 | *
|
---|
1627 | * @param pLogger The logger instance to write to. NULL is not allowed!
|
---|
1628 | */
|
---|
1629 | static void rtlogFlush(PRTLOGGER pLogger)
|
---|
1630 | {
|
---|
1631 | #ifndef IN_GC
|
---|
1632 | if (pLogger->fDestFlags & RTLOGDEST_USER)
|
---|
1633 | RTLogWriteUser(pLogger->achScratch, pLogger->offScratch);
|
---|
1634 |
|
---|
1635 | if (pLogger->fDestFlags & RTLOGDEST_DEBUGGER)
|
---|
1636 | RTLogWriteDebugger(pLogger->achScratch, pLogger->offScratch);
|
---|
1637 |
|
---|
1638 | # ifdef IN_RING3
|
---|
1639 | if (pLogger->fDestFlags & RTLOGDEST_FILE)
|
---|
1640 | RTFileWrite(pLogger->File, pLogger->achScratch, pLogger->offScratch, NULL);
|
---|
1641 | # endif
|
---|
1642 |
|
---|
1643 | if (pLogger->fDestFlags & RTLOGDEST_STDOUT)
|
---|
1644 | RTLogWriteStdOut(pLogger->achScratch, pLogger->offScratch);
|
---|
1645 |
|
---|
1646 | if (pLogger->fDestFlags & RTLOGDEST_STDERR)
|
---|
1647 | RTLogWriteStdErr(pLogger->achScratch, pLogger->offScratch);
|
---|
1648 |
|
---|
1649 | # if (defined(IN_RING0) || defined(IN_GC)) && !defined(LOG_NO_COM)
|
---|
1650 | if (pLogger->fDestFlags & RTLOGDEST_COM)
|
---|
1651 | RTLogWriteCom(pLogger->achScratch, pLogger->offScratch);
|
---|
1652 | # endif
|
---|
1653 | #endif /* !IN_GC */
|
---|
1654 |
|
---|
1655 | if (pLogger->pfnFlush)
|
---|
1656 | pLogger->pfnFlush(pLogger);
|
---|
1657 |
|
---|
1658 | /* empty the buffer. */
|
---|
1659 | pLogger->offScratch = 0;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 |
|
---|
1663 | /**
|
---|
1664 | * Callback for RTLogFormatV which writes to the com port.
|
---|
1665 | * See PFNLOGOUTPUT() for details.
|
---|
1666 | */
|
---|
1667 | static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
1668 | {
|
---|
1669 | PRTLOGGER pLogger = (PRTLOGGER)pv;
|
---|
1670 | if (cbChars)
|
---|
1671 | {
|
---|
1672 | size_t cbRet = 0;
|
---|
1673 | for (;;)
|
---|
1674 | {
|
---|
1675 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
1676 | /* sanity */
|
---|
1677 | if (pLogger->offScratch >= sizeof(pLogger->achScratch))
|
---|
1678 | {
|
---|
1679 | fprintf(stderr, "pLogger->offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
1680 | pLogger->offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
1681 | AssertBreakpoint(); AssertBreakpoint();
|
---|
1682 | }
|
---|
1683 | #endif
|
---|
1684 |
|
---|
1685 | /* how much */
|
---|
1686 | size_t cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
1687 | if (cb > cbChars)
|
---|
1688 | cb = cbChars;
|
---|
1689 |
|
---|
1690 | /* copy */
|
---|
1691 | memcpy(&pLogger->achScratch[pLogger->offScratch], pachChars, cb);
|
---|
1692 |
|
---|
1693 | /* advance */
|
---|
1694 | pLogger->offScratch += cb;
|
---|
1695 | cbRet += cb;
|
---|
1696 | cbChars -= cb;
|
---|
1697 |
|
---|
1698 | /* done? */
|
---|
1699 | if (cbChars <= 0)
|
---|
1700 | return cbRet;
|
---|
1701 |
|
---|
1702 | pachChars += cb;
|
---|
1703 |
|
---|
1704 | /* flush */
|
---|
1705 | rtlogFlush(pLogger);
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | /* won't ever get here! */
|
---|
1709 | }
|
---|
1710 | else
|
---|
1711 | {
|
---|
1712 | /*
|
---|
1713 | * Termination call.
|
---|
1714 | * There's always space for a terminator, and it's not counted.
|
---|
1715 | */
|
---|
1716 | pLogger->achScratch[pLogger->offScratch] = '\0';
|
---|
1717 | return 0;
|
---|
1718 | }
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 |
|
---|
1722 |
|
---|
1723 | /**
|
---|
1724 | * Callback for RTLogFormatV which writes to the logger instance.
|
---|
1725 | * This version supports prefixes.
|
---|
1726 | *
|
---|
1727 | * See PFNLOGOUTPUT() for details.
|
---|
1728 | */
|
---|
1729 | static DECLCALLBACK(size_t) rtLogOutputPrefixed(void *pv, const char *pachChars, size_t cbChars)
|
---|
1730 | {
|
---|
1731 | PRTLOGOUTPUTPREFIXEDARGS pArgs = (PRTLOGOUTPUTPREFIXEDARGS)pv;
|
---|
1732 | PRTLOGGER pLogger = pArgs->pLogger;
|
---|
1733 | if (cbChars)
|
---|
1734 | {
|
---|
1735 | size_t cbRet = 0;
|
---|
1736 | for (;;)
|
---|
1737 | {
|
---|
1738 | size_t cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
1739 | char *psz;
|
---|
1740 | const char *pszNewLine;
|
---|
1741 |
|
---|
1742 | /*
|
---|
1743 | * Pending prefix?
|
---|
1744 | */
|
---|
1745 | if (pLogger->fPendingPrefix)
|
---|
1746 | {
|
---|
1747 | pLogger->fPendingPrefix = false;
|
---|
1748 |
|
---|
1749 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
1750 | /* sanity */
|
---|
1751 | if (pLogger->offScratch >= sizeof(pLogger->achScratch))
|
---|
1752 | {
|
---|
1753 | fprintf(stderr, "pLogger->offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
1754 | pLogger->offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
1755 | AssertBreakpoint(); AssertBreakpoint();
|
---|
1756 | }
|
---|
1757 | #endif
|
---|
1758 |
|
---|
1759 | /*
|
---|
1760 | * Flush the buffer if there isn't enough room for the maximum prefix config.
|
---|
1761 | * Max is 224, add a couple of extra bytes.
|
---|
1762 | */
|
---|
1763 | if (cb < 224 + 16)
|
---|
1764 | {
|
---|
1765 | rtlogFlush(pLogger);
|
---|
1766 | cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | /*
|
---|
1770 | * Write the prefixes.
|
---|
1771 | * psz is pointing to the current position.
|
---|
1772 | */
|
---|
1773 | psz = &pLogger->achScratch[pLogger->offScratch];
|
---|
1774 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TS)
|
---|
1775 | {
|
---|
1776 | #if defined(IN_RING3) || defined(IN_GC)
|
---|
1777 | uint64_t u64 = RTTimeNanoTS();
|
---|
1778 | #else
|
---|
1779 | uint64_t u64 = ~0;
|
---|
1780 | #endif
|
---|
1781 | int iBase = 16;
|
---|
1782 | unsigned int fFlags = RTSTR_F_ZEROPAD;
|
---|
1783 | if (pLogger->fFlags & RTLOGFLAGS_DECIMAL_TS)
|
---|
1784 | {
|
---|
1785 | iBase = 10;
|
---|
1786 | fFlags = 0;
|
---|
1787 | }
|
---|
1788 | if (pLogger->fFlags & RTLOGFLAGS_REL_TS)
|
---|
1789 | {
|
---|
1790 | static volatile uint64_t s_u64LastTs;
|
---|
1791 | uint64_t u64DiffTs = u64 - s_u64LastTs;
|
---|
1792 | s_u64LastTs = u64;
|
---|
1793 | /* We could have been preempted just before reading of s_u64LastTs by
|
---|
1794 | * another thread which wrote s_u64LastTs. In that case the difference
|
---|
1795 | * is negative which we simply ignore. */
|
---|
1796 | u64 = (int64_t)u64DiffTs < 0 ? 0 : u64DiffTs;
|
---|
1797 | }
|
---|
1798 | /* 1E15 nanoseconds = 11 days */
|
---|
1799 | psz += RTStrFormatNumber(psz, u64, iBase, 16, 0, fFlags); /* +17 */
|
---|
1800 | *psz++ = ' ';
|
---|
1801 | }
|
---|
1802 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TSC)
|
---|
1803 | {
|
---|
1804 | uint64_t u64 = ASMReadTSC();
|
---|
1805 | int iBase = 16;
|
---|
1806 | unsigned int fFlags = RTSTR_F_ZEROPAD;
|
---|
1807 | if (pLogger->fFlags & RTLOGFLAGS_DECIMAL_TS)
|
---|
1808 | {
|
---|
1809 | iBase = 10;
|
---|
1810 | fFlags = 0;
|
---|
1811 | }
|
---|
1812 | if (pLogger->fFlags & RTLOGFLAGS_REL_TS)
|
---|
1813 | {
|
---|
1814 | static volatile uint64_t s_u64LastTsc;
|
---|
1815 | uint64_t u64DiffTsc = u64 - s_u64LastTsc;
|
---|
1816 | s_u64LastTsc = u64;
|
---|
1817 | /* We could have been preempted just before reading of s_u64LastTsc by
|
---|
1818 | * another thread which wrote s_u64LastTsc. In that case the difference
|
---|
1819 | * is negative which we simply ignore. */
|
---|
1820 | u64 = u64DiffTsc < 0 ? 0 : u64DiffTsc;
|
---|
1821 | }
|
---|
1822 | /* 1E15 ticks at 4GHz = 69 hours */
|
---|
1823 | psz += RTStrFormatNumber(psz, u64, iBase, 16, 0, fFlags); /* +17 */
|
---|
1824 | *psz++ = ' ';
|
---|
1825 | }
|
---|
1826 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_MS_PROG)
|
---|
1827 | {
|
---|
1828 | #if defined(IN_RING3) || defined(IN_GC)
|
---|
1829 | uint64_t u64 = RTTimeProgramMilliTS();
|
---|
1830 | #else
|
---|
1831 | uint64_t u64 = 0;
|
---|
1832 | #endif
|
---|
1833 | /* 1E8 milliseconds = 27 hours */
|
---|
1834 | psz += RTStrFormatNumber(psz, u64, 10, 9, 0, RTSTR_F_ZEROPAD);
|
---|
1835 | *psz++ = ' ';
|
---|
1836 | }
|
---|
1837 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TIME)
|
---|
1838 | {
|
---|
1839 | #ifdef IN_RING3
|
---|
1840 | RTTIMESPEC TimeSpec;
|
---|
1841 | RTTIME Time;
|
---|
1842 | RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
|
---|
1843 | psz += RTStrFormatNumber(psz, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1844 | *psz++ = ':';
|
---|
1845 | psz += RTStrFormatNumber(psz, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1846 | *psz++ = ':';
|
---|
1847 | psz += RTStrFormatNumber(psz, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1848 | *psz++ = '.';
|
---|
1849 | psz += RTStrFormatNumber(psz, Time.u32Nanosecond / 1000000, 10, 3, 0, RTSTR_F_ZEROPAD);
|
---|
1850 | *psz++ = ' '; /* +17 (3+1+3+1+3+1+4+1) */
|
---|
1851 | #else
|
---|
1852 | memset(psz, ' ', 13);
|
---|
1853 | psz += 13;
|
---|
1854 | #endif
|
---|
1855 | }
|
---|
1856 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TIME_PROG)
|
---|
1857 | {
|
---|
1858 | #ifdef IN_RING3
|
---|
1859 | uint64_t u64 = RTTimeProgramMilliTS();
|
---|
1860 | psz += RTStrFormatNumber(psz, (uint32_t)(u64 / (60 * 60 * 1000)), 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1861 | *psz++ = ':';
|
---|
1862 | uint32_t u32 = (uint32_t)(u64 % (60 * 60 * 1000));
|
---|
1863 | psz += RTStrFormatNumber(psz, u32 / (60 * 1000), 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1864 | *psz++ = ':';
|
---|
1865 | u32 %= 60 * 1000;
|
---|
1866 | psz += RTStrFormatNumber(psz, u32 / 1000, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
1867 | *psz++ = '.';
|
---|
1868 | psz += RTStrFormatNumber(psz, u32 % 1000, 10, 3, 0, RTSTR_F_ZEROPAD);
|
---|
1869 | *psz++ = ' '; /* +20 (9+1+2+1+2+1+3+1) */
|
---|
1870 | #else
|
---|
1871 | memset(psz, ' ', 13);
|
---|
1872 | psz += 13;
|
---|
1873 | #endif
|
---|
1874 | }
|
---|
1875 | # if 0
|
---|
1876 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_DATETIME)
|
---|
1877 | {
|
---|
1878 | char szDate[32];
|
---|
1879 | RTTIMESPEC Time;
|
---|
1880 | RTTimeSpecToString(RTTimeNow(&Time), szDate, sizeof(szDate));
|
---|
1881 | size_t cch = strlen(szDate);
|
---|
1882 | memcpy(psz, szDate, cch);
|
---|
1883 | psz += cch;
|
---|
1884 | *psz++ = ' '; /* +32 */
|
---|
1885 | }
|
---|
1886 | # endif
|
---|
1887 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_PID)
|
---|
1888 | {
|
---|
1889 | #ifndef IN_GC
|
---|
1890 | RTPROCESS Process = RTProcSelf();
|
---|
1891 | #else
|
---|
1892 | RTPROCESS Process = NIL_RTPROCESS;
|
---|
1893 | #endif
|
---|
1894 | psz += RTStrFormatNumber(psz, Process, 16, sizeof(RTPROCESS) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
1895 | *psz++ = ' '; /* +9 */
|
---|
1896 | }
|
---|
1897 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_TID)
|
---|
1898 | {
|
---|
1899 | #ifndef IN_GC
|
---|
1900 | RTNATIVETHREAD Thread = RTThreadNativeSelf();
|
---|
1901 | #else
|
---|
1902 | RTNATIVETHREAD Thread = NIL_RTNATIVETHREAD;
|
---|
1903 | #endif
|
---|
1904 | psz += RTStrFormatNumber(psz, Thread, 16, sizeof(RTNATIVETHREAD) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
1905 | *psz++ = ' '; /* +17 */
|
---|
1906 | }
|
---|
1907 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_THREAD)
|
---|
1908 | {
|
---|
1909 | #ifdef IN_RING3
|
---|
1910 | const char *pszName = RTThreadSelfName();
|
---|
1911 | #elif defined IN_GC
|
---|
1912 | const char *pszName = "EMT-GC";
|
---|
1913 | #else
|
---|
1914 | const char *pszName = "EMT-R0";
|
---|
1915 | #endif
|
---|
1916 | size_t cch = 0;
|
---|
1917 | if (pszName)
|
---|
1918 | {
|
---|
1919 | cch = strlen(pszName);
|
---|
1920 | cch = RT_MIN(cch, 16);
|
---|
1921 | memcpy(psz, pszName, cch);
|
---|
1922 | psz += cch;
|
---|
1923 | }
|
---|
1924 | do
|
---|
1925 | *psz++ = ' ';
|
---|
1926 | while (cch++ < 8); /* +17 */
|
---|
1927 | }
|
---|
1928 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_CPUID)
|
---|
1929 | {
|
---|
1930 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
1931 | const uint8_t idCpu = ASMGetApicId();
|
---|
1932 | #else
|
---|
1933 | const RTCPUID idCpu = RTMpCpuId();
|
---|
1934 | #endif
|
---|
1935 | psz += RTStrFormatNumber(psz, idCpu, 16, sizeof(idCpu) * 2, 0, RTSTR_F_ZEROPAD);
|
---|
1936 | *psz++ = ' '; /* +17 */
|
---|
1937 | }
|
---|
1938 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_LOCK_COUNTS)
|
---|
1939 | {
|
---|
1940 | #ifdef IN_RING3 /** @todo implement these counters in ring-0 too? */
|
---|
1941 | RTTHREAD Thread = RTThreadSelf();
|
---|
1942 | if (Thread != NIL_RTTHREAD)
|
---|
1943 | {
|
---|
1944 | uint32_t cReadLocks = RTThreadGetReadLockCount(Thread);
|
---|
1945 | uint32_t cWriteLocks = RTThreadGetWriteLockCount(Thread) - g_cLoggerLockCount;
|
---|
1946 | cReadLocks = RT_MIN(0xfff, cReadLocks);
|
---|
1947 | cWriteLocks = RT_MIN(0xfff, cWriteLocks);
|
---|
1948 | psz += RTStrFormatNumber(psz, cReadLocks, 16, 1, 0, RTSTR_F_ZEROPAD);
|
---|
1949 | *psz++ = '/';
|
---|
1950 | psz += RTStrFormatNumber(psz, cWriteLocks, 16, 1, 0, RTSTR_F_ZEROPAD);
|
---|
1951 | }
|
---|
1952 | else
|
---|
1953 | #endif
|
---|
1954 | {
|
---|
1955 | *psz++ = '?';
|
---|
1956 | *psz++ = '/';
|
---|
1957 | *psz++ = '?';
|
---|
1958 | }
|
---|
1959 | *psz++ = ' '; /* +8 */
|
---|
1960 | }
|
---|
1961 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_FLAG_NO)
|
---|
1962 | {
|
---|
1963 | psz += RTStrFormatNumber(psz, pArgs->fFlags, 16, 8, 0, RTSTR_F_ZEROPAD);
|
---|
1964 | *psz++ = ' '; /* +9 */
|
---|
1965 | }
|
---|
1966 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_FLAG)
|
---|
1967 | {
|
---|
1968 | #ifdef IN_RING3
|
---|
1969 | const char *pszGroup = pArgs->iGroup != ~0U ? pLogger->papszGroups[pArgs->iGroup] : NULL;
|
---|
1970 | #else
|
---|
1971 | const char *pszGroup = NULL;
|
---|
1972 | #endif
|
---|
1973 | size_t cch = 0;
|
---|
1974 | if (pszGroup)
|
---|
1975 | {
|
---|
1976 | cch = strlen(pszGroup);
|
---|
1977 | cch = RT_MIN(cch, 16);
|
---|
1978 | memcpy(psz, pszGroup, cch);
|
---|
1979 | psz += cch;
|
---|
1980 | }
|
---|
1981 | do
|
---|
1982 | *psz++ = ' ';
|
---|
1983 | while (cch++ < 8); /* +17 */
|
---|
1984 | }
|
---|
1985 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_GROUP_NO)
|
---|
1986 | {
|
---|
1987 | if (pArgs->iGroup != ~0U)
|
---|
1988 | {
|
---|
1989 | psz += RTStrFormatNumber(psz, pArgs->iGroup, 16, 3, 0, RTSTR_F_ZEROPAD);
|
---|
1990 | *psz++ = ' ';
|
---|
1991 | }
|
---|
1992 | else
|
---|
1993 | {
|
---|
1994 | memcpy(psz, "-1 ", sizeof("-1 ") - 1);
|
---|
1995 | psz += sizeof("-1 ") - 1;
|
---|
1996 | } /* +9 */
|
---|
1997 | }
|
---|
1998 | if (pLogger->fFlags & RTLOGFLAGS_PREFIX_GROUP)
|
---|
1999 | {
|
---|
2000 | const unsigned fGrp = pLogger->afGroups[pArgs->iGroup != ~0U ? pArgs->iGroup : 0];
|
---|
2001 | const char *pszGroup;
|
---|
2002 | size_t cch;
|
---|
2003 | switch (pArgs->fFlags & fGrp)
|
---|
2004 | {
|
---|
2005 | case 0: pszGroup = "--------"; cch = sizeof("--------") - 1; break;
|
---|
2006 | case RTLOGGRPFLAGS_ENABLED: pszGroup = "enabled" ; cch = sizeof("enabled" ) - 1; break;
|
---|
2007 | case RTLOGGRPFLAGS_LEVEL_1: pszGroup = "level 1" ; cch = sizeof("level 1" ) - 1; break;
|
---|
2008 | case RTLOGGRPFLAGS_LEVEL_2: pszGroup = "level 2" ; cch = sizeof("level 2" ) - 1; break;
|
---|
2009 | case RTLOGGRPFLAGS_LEVEL_3: pszGroup = "level 3" ; cch = sizeof("level 3" ) - 1; break;
|
---|
2010 | case RTLOGGRPFLAGS_LEVEL_4: pszGroup = "level 4" ; cch = sizeof("level 4" ) - 1; break;
|
---|
2011 | case RTLOGGRPFLAGS_LEVEL_5: pszGroup = "level 5" ; cch = sizeof("level 5" ) - 1; break;
|
---|
2012 | case RTLOGGRPFLAGS_LEVEL_6: pszGroup = "level 6" ; cch = sizeof("level 6" ) - 1; break;
|
---|
2013 | case RTLOGGRPFLAGS_FLOW: pszGroup = "flow" ; cch = sizeof("flow" ) - 1; break;
|
---|
2014 |
|
---|
2015 | /* personal groups */
|
---|
2016 | case RTLOGGRPFLAGS_LELIK: pszGroup = "lelik" ; cch = sizeof("lelik" ) - 1; break;
|
---|
2017 | case RTLOGGRPFLAGS_MICHAEL: pszGroup = "Michael" ; cch = sizeof("Michael" ) - 1; break;
|
---|
2018 | case RTLOGGRPFLAGS_DMIK: pszGroup = "dmik" ; cch = sizeof("dmik" ) - 1; break;
|
---|
2019 | case RTLOGGRPFLAGS_SUNLOVER: pszGroup = "sunlover"; cch = sizeof("sunlover") - 1; break;
|
---|
2020 | case RTLOGGRPFLAGS_ACHIM: pszGroup = "Achim" ; cch = sizeof("Achim" ) - 1; break;
|
---|
2021 | case RTLOGGRPFLAGS_SANDER: pszGroup = "Sander" ; cch = sizeof("Sander" ) - 1; break;
|
---|
2022 | case RTLOGGRPFLAGS_KLAUS: pszGroup = "Klaus" ; cch = sizeof("Klaus" ) - 1; break;
|
---|
2023 | case RTLOGGRPFLAGS_FRANK: pszGroup = "Frank" ; cch = sizeof("Frank" ) - 1; break;
|
---|
2024 | case RTLOGGRPFLAGS_BIRD: pszGroup = "bird" ; cch = sizeof("bird" ) - 1; break;
|
---|
2025 | case RTLOGGRPFLAGS_NONAME: pszGroup = "noname" ; cch = sizeof("noname" ) - 1; break;
|
---|
2026 | default: pszGroup = "????????"; cch = sizeof("????????") - 1; break;
|
---|
2027 | }
|
---|
2028 | if (pszGroup)
|
---|
2029 | {
|
---|
2030 | cch = RT_MIN(cch, 16);
|
---|
2031 | memcpy(psz, pszGroup, cch);
|
---|
2032 | psz += cch;
|
---|
2033 | }
|
---|
2034 | do
|
---|
2035 | *psz++ = ' ';
|
---|
2036 | while (cch++ < 8); /* +17 */
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | /*
|
---|
2040 | * Done, figure what we've used and advance the buffer and free size.
|
---|
2041 | */
|
---|
2042 | cb = psz - &pLogger->achScratch[pLogger->offScratch];
|
---|
2043 | Assert(cb <= 198);
|
---|
2044 | pLogger->offScratch += cb;
|
---|
2045 | cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
2046 | }
|
---|
2047 | else if (cb <= 0)
|
---|
2048 | {
|
---|
2049 | rtlogFlush(pLogger);
|
---|
2050 | cb = sizeof(pLogger->achScratch) - pLogger->offScratch - 1;
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | #if defined(DEBUG) && defined(IN_RING3)
|
---|
2054 | /* sanity */
|
---|
2055 | if (pLogger->offScratch >= sizeof(pLogger->achScratch))
|
---|
2056 | {
|
---|
2057 | fprintf(stderr, "pLogger->offScratch >= sizeof(pLogger->achScratch) (%#x >= %#x)\n",
|
---|
2058 | pLogger->offScratch, (unsigned)sizeof(pLogger->achScratch));
|
---|
2059 | AssertBreakpoint(); AssertBreakpoint();
|
---|
2060 | }
|
---|
2061 | #endif
|
---|
2062 |
|
---|
2063 | /* how much */
|
---|
2064 | if (cb > cbChars)
|
---|
2065 | cb = cbChars;
|
---|
2066 |
|
---|
2067 | /* have newline? */
|
---|
2068 | pszNewLine = (const char *)memchr(pachChars, '\n', cb);
|
---|
2069 | if (pszNewLine)
|
---|
2070 | {
|
---|
2071 | if (pLogger->fFlags & RTLOGFLAGS_USECRLF)
|
---|
2072 | cb = pszNewLine - pachChars;
|
---|
2073 | else
|
---|
2074 | {
|
---|
2075 | cb = pszNewLine - pachChars + 1;
|
---|
2076 | pLogger->fPendingPrefix = true;
|
---|
2077 | }
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | /* copy */
|
---|
2081 | memcpy(&pLogger->achScratch[pLogger->offScratch], pachChars, cb);
|
---|
2082 |
|
---|
2083 | /* advance */
|
---|
2084 | pLogger->offScratch += cb;
|
---|
2085 | cbRet += cb;
|
---|
2086 | cbChars -= cb;
|
---|
2087 |
|
---|
2088 | if ( pszNewLine
|
---|
2089 | && (pLogger->fFlags & RTLOGFLAGS_USECRLF)
|
---|
2090 | && pLogger->offScratch + 2 < sizeof(pLogger->achScratch))
|
---|
2091 | {
|
---|
2092 | memcpy(&pLogger->achScratch[pLogger->offScratch], "\r\n", 2);
|
---|
2093 | pLogger->offScratch += 2;
|
---|
2094 | cbRet++;
|
---|
2095 | cbChars--;
|
---|
2096 | cb++;
|
---|
2097 | pLogger->fPendingPrefix = true;
|
---|
2098 | }
|
---|
2099 |
|
---|
2100 | /* done? */
|
---|
2101 | if (cbChars <= 0)
|
---|
2102 | return cbRet;
|
---|
2103 | pachChars += cb;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | /* won't ever get here! */
|
---|
2107 | }
|
---|
2108 | else
|
---|
2109 | {
|
---|
2110 | /*
|
---|
2111 | * Termination call.
|
---|
2112 | * There's always space for a terminator, and it's not counted.
|
---|
2113 | */
|
---|
2114 | pLogger->achScratch[pLogger->offScratch] = '\0';
|
---|
2115 | return 0;
|
---|
2116 | }
|
---|
2117 | }
|
---|
2118 |
|
---|