1 | /* $Id: tstLog.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Log Groups.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/log.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/test.h>
|
---|
44 | #ifdef VBOX
|
---|
45 | # include <VBox/log.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 |
|
---|
49 | /** Differs from normal strcmp in that '_' is considered smaller than
|
---|
50 | * alphanumerical characters. */
|
---|
51 | static int CompareLogGroups(const char *psz1, const char *psz2)
|
---|
52 | {
|
---|
53 | for (;;)
|
---|
54 | {
|
---|
55 | char ch1 = *psz1++;
|
---|
56 | char ch2 = *psz2++;
|
---|
57 | if (ch1 != ch2)
|
---|
58 | {
|
---|
59 | if (ch1 == 0)
|
---|
60 | return -1;
|
---|
61 | if (ch2 == 0)
|
---|
62 | return 1;
|
---|
63 | if (ch1 == '_')
|
---|
64 | ch1 = 1;
|
---|
65 | if (ch2 == '_')
|
---|
66 | ch2 = 1;
|
---|
67 | return ch1 < ch2 ? -1 : 1;
|
---|
68 | }
|
---|
69 | if (ch1 == 0)
|
---|
70 | return 0;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | int main()
|
---|
75 | {
|
---|
76 | RTTEST hTest;
|
---|
77 | RTEXITCODE rcExit = RTTestInitAndCreate("tstLog", &hTest);
|
---|
78 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
79 | {
|
---|
80 | #if 0 /* Old tests: */
|
---|
81 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Requires manual inspection of the log output!\n");
|
---|
82 | RTLogPrintf("%%Rrc %d: %Rrc\n", VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER);
|
---|
83 | RTLogPrintf("%%Rrs %d: %Rrs\n", VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER);
|
---|
84 | RTLogPrintf("%%Rrf %d: %Rrf\n", VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER);
|
---|
85 | RTLogPrintf("%%Rra %d: %Rra\n", VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER);
|
---|
86 |
|
---|
87 | static uint8_t au8Hex[256];
|
---|
88 | for (unsigned iHex = 0; iHex < sizeof(au8Hex); iHex++)
|
---|
89 | au8Hex[iHex] = (uint8_t)iHex;
|
---|
90 | RTLogPrintf("%%Rhxs : %Rhxs\n", &au8Hex[0]);
|
---|
91 | RTLogPrintf("%%.32Rhxs: %.32Rhxs\n", &au8Hex[0]);
|
---|
92 |
|
---|
93 | RTLogPrintf("%%Rhxd :\n%Rhxd\n", &au8Hex[0]);
|
---|
94 | RTLogPrintf("%%.64Rhxd:\n%.64Rhxd\n", &au8Hex[0]);
|
---|
95 | RTLogPrintf("%%.*Rhxd:\n%.*Rhxd\n", 64, &au8Hex[0]);
|
---|
96 | RTLogPrintf("%%32.256Rhxd : \n%32.256Rhxd\n", &au8Hex[0]);
|
---|
97 | RTLogPrintf("%%32.*Rhxd : \n%32.*Rhxd\n", 256, &au8Hex[0]);
|
---|
98 | RTLogPrintf("%%7.32Rhxd : \n%7.32Rhxd\n", &au8Hex[0]);
|
---|
99 | RTLogPrintf("%%7.*Rhxd : \n%7.*Rhxd\n", 32, &au8Hex[0]);
|
---|
100 | RTLogPrintf("%%*.*Rhxd : \n%*.*Rhxd\n", 7, 32, &au8Hex[0]);
|
---|
101 |
|
---|
102 | RTLogPrintf("%%RGp: %RGp\n", (RTGCPHYS)0x87654321);
|
---|
103 | RTLogPrintf("%%RGv: %RGv\n", (RTGCPTR)0x87654321);
|
---|
104 | RTLogPrintf("%%RHp: %RHp\n", (RTGCPHYS)0x87654321);
|
---|
105 | RTLogPrintf("%%RHv: %RHv\n", (RTGCPTR)0x87654321);
|
---|
106 |
|
---|
107 | RTLogPrintf("%%RI8 : %RI8\n", (uint8_t)88);
|
---|
108 | RTLogPrintf("%%RI16: %RI16\n", (uint16_t)16016);
|
---|
109 | RTLogPrintf("%%RI32: %RI32\n", _1G);
|
---|
110 | RTLogPrintf("%%RI64: %RI64\n", _1E);
|
---|
111 |
|
---|
112 | RTLogPrintf("%%RU8 : %RU8\n", (uint8_t)88);
|
---|
113 | RTLogPrintf("%%RU16: %RU16\n", (uint16_t)16016);
|
---|
114 | RTLogPrintf("%%RU32: %RU32\n", _2G32);
|
---|
115 | RTLogPrintf("%%RU64: %RU64\n", _2E);
|
---|
116 |
|
---|
117 | RTLogPrintf("%%RX8 : %RX8 %#RX8\n", (uint8_t)88, (uint8_t)88);
|
---|
118 | RTLogPrintf("%%RX16: %RX16 %#RX16\n", (uint16_t)16016, (uint16_t)16016);
|
---|
119 | RTLogPrintf("%%RX32: %RX32 %#RX32\n", _2G32, _2G32);
|
---|
120 | RTLogPrintf("%%RX64: %RX64 %#RX64\n", _2E, _2E);
|
---|
121 |
|
---|
122 | RTLogFlush(NULL);
|
---|
123 |
|
---|
124 | /* Flush tests (assumes _4K log buffer). */
|
---|
125 | uint32_t const cbLogBuf = _4K;
|
---|
126 | static char s_szBuf[cbLogBuf * 4];
|
---|
127 | RTLogChangeFlags(NULL, RTLOGFLAGS_USECRLF, 0);
|
---|
128 | for (uint32_t i = cbLogBuf - 512; i < cbLogBuf + 512; i++)
|
---|
129 | {
|
---|
130 | memset(s_szBuf, '0' + (i % 10), i);
|
---|
131 | s_szBuf[i] = '\n';
|
---|
132 | s_szBuf[i + 1] = '\0';
|
---|
133 | RTLogPrintf("i=%#08x: %s", i, s_szBuf);
|
---|
134 | RTLogFlush(NULL);
|
---|
135 | }
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Check the groups.
|
---|
140 | */
|
---|
141 | #ifdef VBOX
|
---|
142 | static const char *s_apszGroups[] = VBOX_LOGGROUP_NAMES;
|
---|
143 | static const struct { uint16_t idGroup; const char *pszGroup; } s_aGroupEnumValues[] =
|
---|
144 | {
|
---|
145 | # include "tstLogGroups.h"
|
---|
146 | };
|
---|
147 |
|
---|
148 | for (size_t iVal = 0, iGrp = RTLOGGROUP_FIRST_USER + 1; iVal < RT_ELEMENTS(s_aGroupEnumValues); iVal++, iGrp++)
|
---|
149 | {
|
---|
150 | if (iGrp >= RT_ELEMENTS(s_apszGroups))
|
---|
151 | {
|
---|
152 | RTTestIFailed("iGrp=%zu >= RT_ELEMENTS(s_apszGroups)=%zu\n", iGrp, RT_ELEMENTS(s_apszGroups));
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | if (strcmp(s_apszGroups[iGrp], s_aGroupEnumValues[iVal].pszGroup))
|
---|
156 | RTTestIFailed("iGrp=%zu mismatch: %s vs %s\n", iGrp, s_apszGroups[iGrp], s_aGroupEnumValues[iVal].pszGroup);
|
---|
157 | if ( iVal > 0
|
---|
158 | && CompareLogGroups(s_aGroupEnumValues[iVal].pszGroup, s_aGroupEnumValues[iVal - 1].pszGroup) <= 0)
|
---|
159 | RTTestIFailed("iGrp=%zu wrong order: %s, prev %s\n",
|
---|
160 | iGrp, s_aGroupEnumValues[iVal].pszGroup, s_aGroupEnumValues[iVal - 1].pszGroup);
|
---|
161 | if ( iVal > 0
|
---|
162 | && s_aGroupEnumValues[iVal - 1].idGroup + 1 != s_aGroupEnumValues[iVal].idGroup)
|
---|
163 | RTTestIFailed("Enum values jumped - bad log.h sed: %u -> %u; %s and %s\n",
|
---|
164 | s_aGroupEnumValues[iVal - 1].idGroup, s_aGroupEnumValues[iVal].idGroup,
|
---|
165 | s_aGroupEnumValues[iVal - 1].pszGroup, s_aGroupEnumValues[iVal].pszGroup);
|
---|
166 | }
|
---|
167 | #endif
|
---|
168 | rcExit = RTTestSummaryAndDestroy(hTest);
|
---|
169 | }
|
---|
170 | return rcExit;
|
---|
171 | }
|
---|
172 |
|
---|