1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include <stdio.h>
#include <string.h>
#include "../testing/nuts.h"
#ifdef NNG_PLATFORM_POSIX
#include <stdlib.h>
#endif
void
test_log_stderr(void)
{
nng_log_set_logger(nng_stderr_logger);
nng_log_set_level(NNG_LOG_DEBUG);
nng_log_info(NULL, "something wicked");
nng_log_err(NULL, "This is an error message");
nng_log_warn(NULL, "This is a warning message");
nng_log_notice(NULL, "This is a notice message");
nng_log_info(NULL, "This is an info message");
nng_log_debug(NULL, "This is a debug message");
nng_log_notice("TESTMSG", "This notice has a msg id");
#ifdef NNG_PLATFORM_POSIX
setenv("NO_COLOR", "", 1);
nng_log_err("MONO", "Uncolored messages");
unsetenv("NO_COLOR");
setenv("NNG_LOG_NO_COLOR", "", 1);
nng_log_err("MONO", "Also uncolored messages");
#endif
// these are intentionally unreasonably large
nng_log_set_level((nng_log_level) 100);
nng_log_auth(99, "WTF", "This should be NONE");
}
typedef struct test_log_entry {
nng_log_level level;
nng_log_facility facility;
const char *msgid;
char msg[128];
} test_log_entry;
typedef struct {
test_log_entry entries[16];
int count;
} test_logs;
void
custom_logger_base(test_logs *logs, nng_log_level level,
nng_log_facility facility, const char *msgid, const char *msg)
{
test_log_entry *entry;
if (logs->count >= 16) {
return;
}
entry = &logs->entries[logs->count++];
entry->level = level;
entry->facility = facility;
entry->msgid = msgid; // ok for constant strings
snprintf(entry->msg, sizeof(entry->msg), "%s", msg);
}
static test_logs test_logs_priority;
void
test_log_priority_logger(nng_log_level level, nng_log_facility facility,
const char *msgid, const char *msg)
{
custom_logger_base(&test_logs_priority, level, facility, msgid, msg);
}
void
test_log_priority(void)
{
nng_log_set_logger(test_log_priority_logger);
nng_log_set_level(NNG_LOG_WARN);
nng_log_debug(NULL, "This should be filtered");
nng_log_err("ERR", "This gets through");
nng_log_notice("NOT", "This gets filtered");
nng_log_warn("WRN", "This makes it");
nng_log_info("INF", "Filtered!");
nng_log_err("ERR", "Another error message");
nng_log_auth(NNG_LOG_ERR, "AUTH", "authentication err sample message");
nng_log_set_level(NNG_LOG_NONE);
nng_log_err("ERR", "Yet Another error message - filtered");
NUTS_ASSERT(test_logs_priority.count == 4);
NUTS_ASSERT(strcmp(test_logs_priority.entries[0].msgid, "ERR") == 0);
NUTS_ASSERT(test_logs_priority.entries[0].level == NNG_LOG_ERR);
NUTS_ASSERT(strcmp(test_logs_priority.entries[1].msgid, "WRN") == 0);
NUTS_ASSERT(test_logs_priority.entries[1].level == NNG_LOG_WARN);
NUTS_ASSERT(strcmp(test_logs_priority.entries[2].msgid, "ERR") == 0);
NUTS_ASSERT(test_logs_priority.entries[2].level == NNG_LOG_ERR);
NUTS_ASSERT(strcmp(test_logs_priority.entries[3].msgid, "AUTH") == 0);
NUTS_ASSERT(test_logs_priority.entries[3].level == NNG_LOG_ERR);
NUTS_ASSERT(test_logs_priority.entries[3].facility == NNG_LOG_AUTH);
}
static test_logs test_logs_facility;
void
test_log_facility_logger(nng_log_level level, nng_log_facility facility,
const char *msgid, const char *msg)
{
custom_logger_base(&test_logs_facility, level, facility, msgid, msg);
}
void
test_log_facility(void)
{
nng_log_set_logger(test_log_facility_logger);
nng_log_set_facility(NNG_LOG_LOCAL2);
nng_log_set_level(NNG_LOG_WARN);
nng_log_debug(NULL, "This should be filtered");
nng_log_err("001", "This is local2");
nng_log_set_facility(NNG_LOG_DAEMON);
nng_log_warn("002", "This is Daemon");
NUTS_ASSERT(test_logs_facility.count == 2);
NUTS_ASSERT(strcmp(test_logs_facility.entries[0].msgid, "001") == 0);
NUTS_ASSERT(test_logs_facility.entries[0].level == NNG_LOG_ERR);
NUTS_ASSERT(test_logs_facility.entries[0].facility == NNG_LOG_LOCAL2);
NUTS_ASSERT(strcmp(test_logs_facility.entries[1].msgid, "002") == 0);
NUTS_ASSERT(test_logs_facility.entries[1].facility == NNG_LOG_DAEMON);
NUTS_ASSERT(test_logs_facility.entries[1].level == NNG_LOG_WARN);
}
void
test_log_null_logger(void)
{
nng_log_set_logger(nng_null_logger);
nng_log_set_level(NNG_LOG_DEBUG);
nng_log_debug(NULL, "This should be dropped");
nng_log_err("001", "This is local2");
nng_log_warn("002", "This is also dropped");
// Lets also try setting it to NULL
nng_log_set_logger(nng_null_logger);
nng_log_warn("003", "This is also dropped");
}
void
test_log_system_logger(void)
{
nng_log_set_logger(nng_system_logger);
nng_log_set_level(NNG_LOG_DEBUG);
nng_log_debug(NULL, "This is a test message, ignore me");
nng_log_set_facility(NNG_LOG_DAEMON);
nng_log_debug(NULL, "This is a test message (DAEMON), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL0);
nng_log_debug(NULL, "This is a test message (LOCAL0), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL1);
nng_log_debug(NULL, "This is a test message (LOCAL1), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL2);
nng_log_debug(NULL, "This is a test message (LOCAL2), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL3);
nng_log_debug(NULL, "This is a test message (LOCAL3), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL4);
nng_log_debug(NULL, "This is a test message (LOCAL4), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL5);
nng_log_debug(NULL, "This is a test message (LOCAL5), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL6);
nng_log_debug(NULL, "This is a test message (LOCAL6), ignore me");
nng_log_set_facility(NNG_LOG_LOCAL7);
nng_log_debug(NULL, "This is a test message (LOCAL7), ignore me");
nng_log_set_facility(NNG_LOG_USER);
nng_log_debug(NULL, "This is a test message (LOCAL7), ignore me");
nng_log_err("TEST", "This is only a test (ERR). Ignore me.");
nng_log_warn("TEST", "This is only a test (WARN). Ignore me.");
nng_log_notice("TEST", "This is only a test (NOTICE). Ignore me.");
nng_log_info("TEST", "This is only a test (INFO). Ignore me.");
}
TEST_LIST = {
{ "log stderr", test_log_stderr },
{ "log priority", test_log_priority },
{ "log facility", test_log_facility },
{ "log null logger", test_log_null_logger },
{ "log system logger", test_log_system_logger },
{ NULL, NULL },
};
|