Debian Codegraph @

src/tests/kylog-gtest/kylog-gtest.cpp

raw
1 /*
2 * Copyright (C) 2024-2025, KylinSoft Co., Ltd.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this library. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Authors: Yunhe Liu <[email protected]>
18 *
19 */
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <gtest/gtest.h>
24 #include "../cpp_stub/src/stub.h"
25 extern "C"
26 {
27 #include "../../log/libkylog.h"
28 #include "../../log/core.h"
29 #include "../../log/klog_mqueue.h"
30 }
31
32 TEST(TestSuite1, kylog_1)
33 {
34 assert(kdk_logger_init("logtest.conf") == 0);
35
36 klog_calltrace();
37 klog_debug("Debug");
38 klog_info("Info");
39 klog_notice("Notice");
40 klog_warning("Warning");
41 klog_err("Error");
42 klog_crit("Crit");
43 klog_alert("Alert");
44 klog_emerg("Emerg");
45
46 emptyMessageQueue();
47
48 destroyKLogger();
49 }
50
51 TEST(TestSuite2, kylog_2)
52 {
53 assert(kdk_logger_init(NULL) == 0);
54 klog_calltrace();
55 klog_debug("Debug");
56 klog_info("Info");
57 klog_notice("Notice");
58 klog_warning("Warning");
59 klog_err("Error");
60 klog_crit("Crit");
61 klog_alert("Alert");
62 klog_emerg("Emerg");
63 }
64
65 TEST(TestSuite3, kylog_autowrap)
66 {
67 // assert(kdk_logger_init("../log/logtest.conf") == 0);
68
69 kdk_logger_set_autowrap(1);
70
71 klog_calltrace();
72 klog_debug("Debug");
73 klog_info("Info");
74 klog_notice("Notice");
75 klog_warning("Warning");
76 klog_err("Error");
77 klog_crit("Crit");
78 klog_alert("Alert");
79 klog_emerg("Emerg");
80 }
81
82 TEST(TestSuite4, kylog_setdir)
83 {
84 // assert(kdk_logger_init("../log/logtest.conf") == 0);
85 assert(kdk_logger_setdir("/tmp/kylog") == 0);
86
87 klog_calltrace();
88 klog_debug("Debug");
89 klog_info("Info");
90 klog_notice("Notice");
91 klog_warning("Warning");
92 klog_err("Error");
93 klog_crit("Crit");
94 klog_alert("Alert");
95 klog_emerg("Emerg");
96 }
97
98 TEST(TestSuite5, levelBasedStorage)
99 {
100 logger->levelBasedStorage = 1;
101
102 klog_calltrace();
103 klog_debug("Debug");
104 klog_info("Info");
105 klog_notice("Notice");
106 klog_warning("Warning");
107 klog_err("Error");
108 klog_crit("Crit");
109 klog_alert("Alert");
110 klog_emerg("Emerg");
111 logger->levelBasedStorage = 0;
112 }
113
114 TEST(TestSuite6, async)
115 {
116 logger->stype = ASYNC;
117 // assert(kdk_logger_init(NULL) == 0);
118
119 klog_calltrace();
120 klog_debug("Debug");
121 klog_info("Info");
122 klog_notice("Notice");
123 klog_warning("Warning");
124 klog_err("Error");
125 klog_crit("Crit");
126 klog_alert("Alert");
127 klog_emerg("Emerg");
128 logger->stype = SYNC;
129 }
130
131 TEST(TestSuite7, otype_stdout)
132 {
133 logger->otype = OUT_STDOUT;
134
135 klog_calltrace();
136 klog_debug("Debug");
137 klog_info("Info");
138 klog_notice("Notice");
139 klog_warning("Warning");
140 klog_err("Error");
141 klog_crit("Crit");
142 klog_alert("Alert");
143 klog_emerg("Emerg");
144 logger->otype = OUT_SPECFILE;
145 }
146
147 TEST(TestSuite8, otype_syslog)
148 {
149 logger->otype = OUT_SYSLOG;
150
151 klog_calltrace();
152 klog_debug("Debug");
153 klog_info("Info");
154 klog_notice("Notice");
155 klog_warning("Warning");
156 klog_err("Error");
157 klog_crit("Crit");
158 klog_alert("Alert");
159 klog_emerg("Emerg");
160 logger->otype = OUT_SPECFILE;
161 }
162
163 TEST(TestSuite9, commonfp_null)
164 {
165 FILE *fp = logger->fp.commonfp;
166 logger->fp.commonfp = NULL;
167
168 klog_calltrace();
169 klog_debug("Debug");
170 klog_info("Info");
171 klog_notice("Notice");
172 klog_warning("Warning");
173 klog_err("Error");
174 klog_crit("Crit");
175 klog_alert("Alert");
176 klog_emerg("Emerg");
177 logger->fp.commonfp = fp;
178 }
179