forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_test.cpp
More file actions
315 lines (272 loc) · 11.3 KB
/
Copy pathlog_test.cpp
File metadata and controls
315 lines (272 loc) · 11.3 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <bthread/bthread.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <cstring>
#include <fstream>
#include <random>
#include <regex>
#include <sstream>
#include <thread>
#include "common/config.h"
#include "common/logging.h"
using doris::cloud::AnnotateTag;
int main(int argc, char** argv) {
if (!doris::cloud::init_glog("log_test")) {
std::cerr << "failed to init glog" << std::endl;
return -1;
}
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
TEST(LogTest, ConstructionTest) {
// Heap allocation is disabled.
// new AnnotateTag();
// Arithmetics
{
char c = 0;
bool b = false;
int8_t i8 = 0;
uint8_t u8 = 0;
int16_t i16 = 0;
uint16_t u16 = 0;
int32_t i32 = 0;
uint32_t u32 = 0;
int64_t i64 = 0;
uint64_t u64 = 0;
AnnotateTag tag_char("char", c);
AnnotateTag tag_bool("bool", b);
AnnotateTag tag_i8("i8", i8);
AnnotateTag tag_u8("u8", u8);
AnnotateTag tag_i16("i16", i16);
AnnotateTag tag_u16("u16", u16);
AnnotateTag tag_i32("i32", i32);
AnnotateTag tag_u32("u32", u32);
AnnotateTag tag_i64("i64", i64);
AnnotateTag tag_u64("u64", u64);
LOG_INFO("hello");
}
// String literals.
{
const char* text = "hello";
AnnotateTag tag_text("hello", text);
LOG_INFO("hello");
}
// String view.
{
std::string test("abc");
AnnotateTag tag_text("hello", std::string_view(test));
LOG_INFO("hello");
}
// Const string.
{
const std::string test("abc");
AnnotateTag tag_text("hello", test);
LOG_INFO("hello");
}
}
TEST(LogTest, ThreadTest) {
// In pthread.
{
ASSERT_EQ(bthread_self(), 0);
AnnotateTag tag("run_in_bthread", true);
LOG_INFO("thread test");
}
// In bthread.
{
auto fn = +[](void*) -> void* {
EXPECT_NE(bthread_self(), 0);
AnnotateTag tag("run_in_bthread", true);
LOG_INFO("thread test");
return nullptr;
};
bthread_t tid;
ASSERT_EQ(bthread_start_background(&tid, nullptr, fn, nullptr), 0);
ASSERT_EQ(bthread_join(tid, nullptr), 0);
}
}
// Test StdoutLogSink format output
TEST(LogTest, StdoutLogSinkFormatTest) {
// Capture stdout
testing::internal::CaptureStdout();
// Create a test log sink and send a test message
struct TestLogSink : google::LogSink {
std::stringstream captured_output;
void send(google::LogSeverity severity, const char* /*full_filename*/,
const char* base_filename, int line, const google::LogMessageTime& time,
const char* message, std::size_t message_len) override {
char severity_char;
switch (severity) {
case google::GLOG_INFO:
severity_char = 'I';
break;
case google::GLOG_WARNING:
severity_char = 'W';
break;
case google::GLOG_ERROR:
severity_char = 'E';
break;
case google::GLOG_FATAL:
severity_char = 'F';
break;
default:
severity_char = '?';
break;
}
captured_output << std::setfill('0');
captured_output << severity_char;
captured_output << std::setw(4) << (time.year() + 1900) << std::setw(2)
<< std::setfill('0') << (time.month() + 1) << std::setw(2)
<< std::setfill('0') << time.day();
captured_output << " " << std::setw(2) << std::setfill('0') << time.hour() << ":"
<< std::setw(2) << std::setfill('0') << time.min() << ":"
<< std::setw(2) << std::setfill('0') << time.sec() << "."
<< std::setw(6) << std::setfill('0') << time.usec();
captured_output << " " << std::setfill(' ') << std::setw(5) << getpid()
<< std::setfill('0');
captured_output << " " << base_filename << ":" << line << "] ";
captured_output.write(message, message_len);
}
} test_sink;
// Simulate a log message using current time
google::LogMessageTime test_time;
const char* test_message = "Test log message";
test_sink.send(google::GLOG_INFO, "test_file.cpp", "test_file.cpp", 123, test_time,
test_message, strlen(test_message));
std::string output = test_sink.captured_output.str();
// Verify format pattern: I<YYYYMMDD> <HH:MM:SS>.<usec> <pid> test_file.cpp:123] Test log message
// Check severity character
EXPECT_EQ(output[0], 'I') << "Severity character incorrect: " << output;
// Check date format (8 digits after 'I')
std::regex date_pattern(R"(I\d{8}\s)");
EXPECT_TRUE(std::regex_search(output, date_pattern)) << "Date format incorrect: " << output;
// Check time format (HH:MM:SS.ffffff)
std::regex time_pattern(R"(\d{2}:\d{2}:\d{2}\.\d{6})");
EXPECT_TRUE(std::regex_search(output, time_pattern)) << "Time format incorrect: " << output;
// Check file:line format
EXPECT_TRUE(output.find("test_file.cpp:123]") != std::string::npos)
<< "File:line format incorrect: " << output;
// Check message content
EXPECT_TRUE(output.find("Test log message") != std::string::npos)
<< "Message not found: " << output;
// Verify process ID is present (at least 1 digit followed by space and filename)
std::regex pid_pattern(R"(\d+\s+test_file\.cpp)");
EXPECT_TRUE(std::regex_search(output, pid_pattern)) << "Process ID not found: " << output;
std::string captured = testing::internal::GetCapturedStdout();
}
// Test log initialization with different configurations
TEST(LogTest, LogInitializationTest) {
// Test 1: Verify init_glog can be called multiple times safely
EXPECT_TRUE(doris::cloud::init_glog("test_logger"));
EXPECT_TRUE(doris::cloud::init_glog("test_logger")); // Should return true on second call
// Test 2: Verify log level configuration
// This test verifies that different log levels can be set
std::string original_level = doris::cloud::config::log_level;
// Note: We can't easily test the actual behavior without modifying global state,
// but we can verify the configuration exists and init succeeds
doris::cloud::config::log_level = "INFO";
EXPECT_TRUE(doris::cloud::init_glog("test_info"));
doris::cloud::config::log_level = "WARNING";
EXPECT_TRUE(doris::cloud::init_glog("test_warn"));
// Restore original level
doris::cloud::config::log_level = original_level;
}
// Test log output with different severity levels
TEST(LogTest, LogSeverityTest) {
// Test different log severity characters
struct SeverityTestSink : google::LogSink {
std::map<google::LogSeverity, char> severity_chars;
void send(google::LogSeverity severity, const char* /*full_filename*/,
const char* /*base_filename*/, int /*line*/,
const google::LogMessageTime& /*time*/, const char* /*message*/,
std::size_t /*message_len*/) override {
char severity_char;
switch (severity) {
case google::GLOG_INFO:
severity_char = 'I';
break;
case google::GLOG_WARNING:
severity_char = 'W';
break;
case google::GLOG_ERROR:
severity_char = 'E';
break;
case google::GLOG_FATAL:
severity_char = 'F';
break;
default:
severity_char = '?';
break;
}
severity_chars[severity] = severity_char;
}
} severity_sink;
google::LogMessageTime test_time;
severity_sink.send(google::GLOG_INFO, "", "", 1, test_time, "test", 4);
severity_sink.send(google::GLOG_WARNING, "", "", 1, test_time, "test", 4);
severity_sink.send(google::GLOG_ERROR, "", "", 1, test_time, "test", 4);
EXPECT_EQ(severity_sink.severity_chars[google::GLOG_INFO], 'I');
EXPECT_EQ(severity_sink.severity_chars[google::GLOG_WARNING], 'W');
EXPECT_EQ(severity_sink.severity_chars[google::GLOG_ERROR], 'E');
}
// Test that log format matches expected pattern
TEST(LogTest, LogFormatPatternTest) {
struct PatternTestSink : google::LogSink {
std::string last_output;
void send(google::LogSeverity severity, const char* /*full_filename*/,
const char* base_filename, int line, const google::LogMessageTime& time,
const char* message, std::size_t message_len) override {
std::stringstream ss;
char severity_char = 'I';
switch (severity) {
case google::GLOG_INFO:
severity_char = 'I';
break;
case google::GLOG_WARNING:
severity_char = 'W';
break;
case google::GLOG_ERROR:
severity_char = 'E';
break;
case google::GLOG_FATAL:
severity_char = 'F';
break;
default:
severity_char = '?';
break;
}
ss << severity_char;
ss << std::setw(4) << std::setfill('0') << (time.year() + 1900) << std::setw(2)
<< (time.month() + 1) << std::setw(2) << time.day();
ss << " " << std::setw(2) << std::setfill('0') << time.hour() << ":" << std::setw(2)
<< time.min() << ":" << std::setw(2) << time.sec() << "." << std::setw(6)
<< time.usec();
ss << " " << std::setfill(' ') << std::setw(5) << getpid() << std::setfill('0');
ss << " " << base_filename << ":" << line << "] ";
ss.write(message, message_len);
last_output = ss.str();
}
} pattern_sink;
google::LogMessageTime test_time;
pattern_sink.send(google::GLOG_INFO, "test.cpp", "test.cpp", 100, test_time, "Test message",
12);
// Verify the pattern: I<YYYYMMDD> <HH:MM:SS>.<usec> <pid> test.cpp:100] Test message
std::regex pattern(R"(I\d{8} \d{2}:\d{2}:\d{2}\.\d{6}\s+\d+\s+test\.cpp:100\]\s+Test message)");
EXPECT_TRUE(std::regex_search(pattern_sink.last_output, pattern))
<< "Log format pattern mismatch. Got: " << pattern_sink.last_output;
}