Skip to content

Commit f89a840

Browse files
committed
log: Test case for log levels
Signed-off-by: Diogo Behrens <[email protected]>
1 parent ea9afd5 commit f89a840

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

include/dice/log.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ DICE_WEAK caslock_t log_lock;
3737
#define LOG_SUFFIX "\n"
3838
#endif
3939

40+
#define LOG_PASTE(LEVEL) LOG_LEVEL##_##LEVEL
41+
#define LOG_EXPAND(LEVEL) LOG_PASTE(LEVEL)
42+
#define LOG_LEVEL_ LOG_EXPAND(LOG_LEVEL)
43+
4044
#define LOG_MAX_LEN 1024
4145
#define log_printf(fmt, ...) \
4246
do { \
@@ -48,10 +52,8 @@ DICE_WEAK caslock_t log_lock;
4852
} \
4953
} while (0)
5054

51-
#define LOG_PASTE(LEVEL) LOG_LEVEL##_##LEVEL
52-
#define LOG_EXPAND(LEVEL) LOG_PASTE(LEVEL)
5355

54-
#if LOG_EXPAND(LOG_LEVEL) >= LOG_LEVEL_DEBUG
56+
#if LOG_LEVEL_ >= LOG_LEVEL_DEBUG
5557
#define log_debug(fmt, ...) \
5658
do { \
5759
LOG_LOCK_ACQUIRE; \
@@ -64,7 +66,7 @@ DICE_WEAK caslock_t log_lock;
6466
#define log_debug log_none
6567
#endif
6668

67-
#if LOG_EXPAND(LOG_LEVEL) >= LOG_LEVEL_INFO
69+
#if LOG_LEVEL_ >= LOG_LEVEL_INFO
6870
#define log_info(fmt, ...) \
6971
do { \
7072
LOG_LOCK_ACQUIRE; \

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ add_custom_target(expand-tests)
5252
add_subdirectory(interpose)
5353
add_subdirectory(tsan)
5454
add_subdirectory(order)
55+
add_subdirectory(log)

test/log/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
2+
# SPDX-License-Identifier: MIT
3+
4+
add_executable(log-fatal-test log_levels.c)
5+
target_link_libraries(log-fatal-test PRIVATE dice.h)
6+
target_compile_options(log-fatal-test PRIVATE -DLOG_LEVEL=FATAL)
7+
add_test(NAME log-fatal-test COMMAND log-fatal-test)
8+
9+
add_executable(log-info-test log_levels.c)
10+
target_link_libraries(log-info-test PRIVATE dice.h)
11+
target_compile_options(log-info-test PRIVATE -DLOG_LEVEL=INFO)
12+
add_test(NAME log-info-test COMMAND log-info-test)
13+
14+
add_executable(log-debug-test log_levels.c)
15+
target_link_libraries(log-debug-test PRIVATE dice.h)
16+
target_compile_options(log-debug-test PRIVATE -DLOG_LEVEL=DEBUG)
17+
add_test(NAME log-debug-test COMMAND log-debug-test)

test/log/log_levels.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
#include <dice/interpose.h>
7+
#include <dice/log.h>
8+
#include <string.h>
9+
#include <assert.h>
10+
#include <stdbool.h>
11+
12+
#define MAX_EXP 16
13+
static char *strings[MAX_EXP] = {0};
14+
static char **head = &strings[0];
15+
static char **tail = &strings[0];
16+
17+
INTERPOSE(ssize_t, write, int fd, const void *buf, size_t count) {
18+
static int nest = 0;
19+
if (nest == 1) {
20+
return REAL(write, fd, buf, count);
21+
}
22+
assert(nest == 0);
23+
nest++;
24+
assert(*tail);
25+
assert(tail < head);
26+
if (strncmp((char*) buf, *tail, count) != 0) {
27+
log_printf("exp: %s\n", *tail);
28+
log_printf("buf: %s\n", (char*)buf);
29+
abort();
30+
}
31+
tail++;
32+
nest--;
33+
return count;
34+
}
35+
36+
static void expect(char *e) {
37+
assert(head < strings + MAX_EXP);
38+
*head = e;
39+
head++;
40+
}
41+
static bool empty(void) {
42+
return head == tail;
43+
}
44+
45+
46+
int
47+
main()
48+
{
49+
// this should always work
50+
expect("print");
51+
log_printf("print");
52+
assert(empty());
53+
54+
// this should always work, but we remove the abort to about actually
55+
// aborting
56+
printf("level >= fatal\n");
57+
expect("dice: ");
58+
expect("fatal");
59+
expect("\n");
60+
#define abort()
61+
log_fatal("fatal");
62+
#undef abort
63+
assert(empty());
64+
65+
#if LOG_LEVEL_ >= LOG_LEVEL_INFO
66+
printf("level >= info\n");
67+
expect("dice: ");
68+
expect("info");
69+
expect("\n");
70+
log_info("info");
71+
assert(empty());
72+
#endif
73+
74+
#if LOG_LEVEL_ >= LOG_LEVEL_DEBUG
75+
printf("level >= debug \n");
76+
expect("dice: ");
77+
expect("debug");
78+
expect("\n");
79+
log_debug("debug");
80+
assert(empty());
81+
#endif
82+
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)