forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
140 lines (117 loc) · 5.18 KB
/
logger.cpp
File metadata and controls
140 lines (117 loc) · 5.18 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
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <iostream>
#include <regex>
#include <sstream>
#include "openvino/core/log.hpp"
#include "openvino/core/log_util.hpp"
#include "openvino/util/log.hpp"
namespace ov::test {
using namespace ov::util;
// Capturing std::cout streambuffer doesn't work in a CI job on Windows, so disabled. Tested locally on Ubuntu.
static constexpr bool enable_logging_to_std_cout_test = false;
using LogEntries = std::tuple<LOG_TYPE, const char*, int, const char*>;
class TestLogHelper : public testing::TestWithParam<LogEntries> {
std::ostream* const actual_out_stream = &std::cout;
std::streambuf* const actual_out_buf = actual_out_stream->rdbuf();
// LogEntries
LOG_TYPE m_log_type;
const char* m_log_path;
int m_log_line;
const char* m_log_message;
protected:
void SetUp() override {
reset_log_callback();
if (enable_logging_to_std_cout_test) {
actual_out_stream->flush();
actual_out_stream->rdbuf(m_mock_out_stream.rdbuf());
}
std::tie(m_log_type, m_log_path, m_log_line, m_log_message) = GetParam();
}
void TearDown() override {
if (enable_logging_to_std_cout_test) {
actual_out_stream->rdbuf(actual_out_buf);
}
reset_log_callback();
}
auto log_test_params() {
LogHelper{m_log_type, m_log_path, m_log_line}.stream() << m_log_message;
}
auto get_log_regex() const {
static auto log_prefix_pattern =
std::map<LOG_TYPE, const std::string>{{LOG_TYPE::_LOG_TYPE_ERROR, R"(\[ERROR\])"},
{LOG_TYPE::_LOG_TYPE_WARNING, R"(\[WARNING\])"},
{LOG_TYPE::_LOG_TYPE_INFO, R"(\[INFO\])"},
{LOG_TYPE::_LOG_TYPE_DEBUG, R"(\[DEBUG\])"}};
std::stringstream log_regex;
if (LOG_TYPE::_LOG_TYPE_DEBUG_EMPTY != m_log_type) {
log_regex << log_prefix_pattern[m_log_type] << ".*" << m_log_path << ".*" << m_log_line << ".*";
}
log_regex << m_log_message;
return std::regex{log_regex.str()};
}
auto are_params_logged_to(const std::string& buf) {
return std::regex_search(buf, get_log_regex());
}
std::stringstream m_mock_out_stream;
std::string m_callback_message;
const LogCallback m_log_callback{[this](std::string_view msg) {
m_callback_message = msg;
}};
};
TEST_P(TestLogHelper, set_callback) {
if (enable_logging_to_std_cout_test) {
log_test_params();
EXPECT_TRUE(are_params_logged_to(m_mock_out_stream.str()))
<< "Mock cout got: '" << m_mock_out_stream.str() << "'\n";
m_mock_out_stream.str("");
m_mock_out_stream.clear();
}
set_log_callback(m_log_callback);
log_test_params();
if (enable_logging_to_std_cout_test) {
EXPECT_TRUE(m_mock_out_stream.str().empty()) << "Expected no cout. Got: '" << m_mock_out_stream.str() << "'\n";
}
EXPECT_TRUE(are_params_logged_to(m_callback_message)) << "Callback got: '" << m_callback_message << "'\n";
}
TEST_P(TestLogHelper, toggle_callbacks) {
set_log_callback(m_log_callback);
log_test_params();
EXPECT_TRUE(are_params_logged_to(m_callback_message)) << "1st callback got: '" << m_callback_message << "'\n";
m_callback_message.clear();
std::string aux_callback_msg;
const LogCallback aux_callback = [&aux_callback_msg](std::string_view msg) {
aux_callback_msg = msg;
};
set_log_callback(aux_callback);
log_test_params();
EXPECT_TRUE(are_params_logged_to(aux_callback_msg)) << "2st callback got: '" << aux_callback_msg << "'\n";
EXPECT_TRUE(m_callback_message.empty()) << "Expected no 1st callback. Got: '" << m_callback_message << "'\n";
}
TEST_P(TestLogHelper, reset) {
set_log_callback(m_log_callback);
reset_log_callback();
log_test_params();
if (enable_logging_to_std_cout_test) {
EXPECT_TRUE(are_params_logged_to(m_mock_out_stream.str()))
<< "Mock cout got: '" << m_mock_out_stream.str() << "'\n";
}
EXPECT_TRUE(m_callback_message.empty()) << "Expected no callback. Got: '" << m_callback_message << "'\n";
}
TEST_P(TestLogHelper, no_log) {
set_log_callback(m_log_callback);
const auto empty_callback = LogCallback{};
set_log_callback(empty_callback);
ASSERT_NO_THROW(log_test_params());
EXPECT_TRUE(m_callback_message.empty()) << "Expected no callback. Got: '" << m_callback_message << "'\n";
}
INSTANTIATE_TEST_SUITE_P(Log_callback,
TestLogHelper,
::testing::Values(LogEntries{LOG_TYPE::_LOG_TYPE_ERROR, "path_1", 1, "text 1"},
LogEntries{LOG_TYPE::_LOG_TYPE_WARNING, "path_2", 2, "text 2"},
LogEntries{LOG_TYPE::_LOG_TYPE_INFO, "path_3", 3, "text 3"},
LogEntries{LOG_TYPE::_LOG_TYPE_DEBUG, "path_4", 4, "text 4"},
LogEntries{LOG_TYPE::_LOG_TYPE_DEBUG_EMPTY, "path_5", 5, "text 5"}));
} // namespace ov::test