VirtualBox

source: vbox/trunk/src/VBox/Runtime/log.cpp@ 61

Last change on this file since 61 was 1, checked in by vboxsync, 55 years ago

import

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

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