summaryrefslogtreecommitdiff
path: root/test/buildtool/logging/logger.test.cpp
blob: deee74774e24cf2863b86706cc834acf1612a0a1 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed 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 "src/buildtool/logging/logger.hpp"

#include <atomic>
#include <string>
#include <unordered_map>
#include <vector>

#include "catch2/catch_test_macros.hpp"
#include "src/buildtool/logging/log_config.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/log_sink.hpp"

// Stores prints from test sink instances
class TestPrints {
    struct PrintData {
        std::atomic<int> counter;
        std::unordered_map<int, std::vector<std::string>> prints;
    };

  public:
    static void Print(int sink_id, std::string const& print) noexcept {
        Data().prints[sink_id].push_back(print);
    }
    [[nodiscard]] static auto Read(int sink_id) noexcept
        -> std::vector<std::string> {
        return Data().prints[sink_id];
    }

    static void Clear() noexcept {
        Data().prints.clear();
        Data().counter = 0;
    }

    static auto GetId() noexcept -> int { return Data().counter++; }

  private:
    [[nodiscard]] static auto Data() noexcept -> PrintData& {
        static PrintData instance{};
        return instance;
    }
};

// Test sink, prints to TestPrints depending on its own instance id.
class LogSinkTest : public ILogSink {
  public:
    static auto CreateFactory() -> LogSinkFactory {
        return [] { return std::make_shared<LogSinkTest>(); };
    }

    LogSinkTest() noexcept : id_{TestPrints::GetId()} {}

    void Emit(Logger const* logger,
              LogLevel level,
              std::string const& msg) const noexcept final {
        auto prefix = LogLevelToString(level);

        if (logger != nullptr) {
            prefix += " (" + logger->Name() + ")";
        }

        TestPrints::Print(id_, prefix + ": " + msg);
    }

  private:
    int id_{};
};

class OneGlobalSinkFixture {
  public:
    OneGlobalSinkFixture() {
        TestPrints::Clear();
        LogConfig::SetLogLimit(LogLevel::Info);
        LogConfig::SetSinks({LogSinkTest::CreateFactory()});
    }
};

class TwoGlobalSinksFixture : public OneGlobalSinkFixture {
  public:
    TwoGlobalSinksFixture() {
        LogConfig::AddSink(LogSinkTest::CreateFactory());
    }
};

TEST_CASE_METHOD(OneGlobalSinkFixture,
                 "Global static logger with one sink",
                 "[logger]") {
    // logs should be forwarded to sink instance: 0
    int instance = 0;

    // create log outside of log limit
    Logger::Log(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance).empty());

    SECTION("create log within log limit") {
        Logger::Log(LogLevel::Info, "second");
        auto prints = TestPrints::Read(instance);
        REQUIRE(prints.size() == 1);
        CHECK(prints[0] == "INFO: second");

        SECTION("increase log limit create log within log limit") {
            LogConfig::SetLogLimit(LogLevel::Trace);
            Logger::Log(LogLevel::Trace, "third");
            auto prints = TestPrints::Read(instance);
            REQUIRE(prints.size() == 2);
            CHECK(prints[1] == "TRACE: third");

            SECTION("log via lambda function") {
                Logger::Log(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints = TestPrints::Read(instance);
                REQUIRE(prints.size() == 3);
                CHECK(prints[2] == "TRACE: forth");
            }
        }
    }
}

TEST_CASE_METHOD(OneGlobalSinkFixture,
                 "Local named logger using one global sink",
                 "[logger]") {
    // create logger with sink instances from global LogConfig
    Logger logger("TestLogger");

    // logs should be forwarded to same sink instance as before: 0
    int instance = 0;

    // create log outside of log limit
    logger.Emit(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance).empty());

    SECTION("create log within log limit") {
        logger.Emit(LogLevel::Info, "second");
        auto prints = TestPrints::Read(instance);
        REQUIRE(prints.size() == 1);
        CHECK(prints[0] == "INFO (TestLogger): second");

        SECTION("increase log limit create log within log limit") {
            logger.SetLogLimit(LogLevel::Trace);
            logger.Emit(LogLevel::Trace, "third");
            auto prints = TestPrints::Read(instance);
            REQUIRE(prints.size() == 2);
            CHECK(prints[1] == "TRACE (TestLogger): third");

            SECTION("log via lambda function") {
                logger.Emit(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints = TestPrints::Read(instance);
                REQUIRE(prints.size() == 3);
                CHECK(prints[2] == "TRACE (TestLogger): forth");
            }
        }
    }
}

TEST_CASE_METHOD(OneGlobalSinkFixture,
                 "Local named logger with its own sink instance"
                 "[logger]") {
    // create logger with separate sink instance
    Logger logger("OwnSinkLogger", {LogSinkTest::CreateFactory()});

    // logs should be forwarded to new sink instance: 1
    int instance = 1;

    // create log outside of log limit
    logger.Emit(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance).empty());

    SECTION("create log within log limit") {
        logger.Emit(LogLevel::Info, "second");
        auto prints = TestPrints::Read(instance);
        REQUIRE(prints.size() == 1);
        CHECK(prints[0] == "INFO (OwnSinkLogger): second");

        SECTION("increase log limit create log within log limit") {
            logger.SetLogLimit(LogLevel::Trace);
            logger.Emit(LogLevel::Trace, "third");
            auto prints = TestPrints::Read(instance);
            REQUIRE(prints.size() == 2);
            CHECK(prints[1] == "TRACE (OwnSinkLogger): third");

            SECTION("log via lambda function") {
                logger.Emit(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints = TestPrints::Read(instance);
                REQUIRE(prints.size() == 3);
                CHECK(prints[2] == "TRACE (OwnSinkLogger): forth");
            }
        }
    }
}

TEST_CASE_METHOD(TwoGlobalSinksFixture,
                 "Global static logger with two sinks",
                 "[logger]") {
    // logs should be forwarded to sink instances: 0 and 1
    int instance1 = 0;
    int instance2 = 1;

    // create log outside of log limit
    Logger::Log(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance1).empty());
    CHECK(TestPrints::Read(instance2).empty());

    SECTION("create log within log limit") {
        Logger::Log(LogLevel::Info, "second");
        auto prints1 = TestPrints::Read(instance1);
        auto prints2 = TestPrints::Read(instance2);
        REQUIRE(prints1.size() == 1);
        REQUIRE(prints2.size() == 1);
        CHECK(prints1[0] == "INFO: second");
        CHECK(prints2[0] == "INFO: second");

        SECTION("increase log limit create log within log limit") {
            LogConfig::SetLogLimit(LogLevel::Trace);
            Logger::Log(LogLevel::Trace, "third");
            auto prints1 = TestPrints::Read(instance1);
            auto prints2 = TestPrints::Read(instance2);
            REQUIRE(prints1.size() == 2);
            REQUIRE(prints2.size() == 2);
            CHECK(prints1[1] == "TRACE: third");
            CHECK(prints2[1] == "TRACE: third");

            SECTION("log via lambda function") {
                Logger::Log(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints1 = TestPrints::Read(instance1);
                auto prints2 = TestPrints::Read(instance2);
                REQUIRE(prints1.size() == 3);
                REQUIRE(prints2.size() == 3);
                CHECK(prints1[2] == "TRACE: forth");
                CHECK(prints2[2] == "TRACE: forth");
            }
        }
    }
}

TEST_CASE_METHOD(TwoGlobalSinksFixture,
                 "Local named logger using two global sinks",
                 "[logger]") {
    // create logger with sink instances from global LogConfig
    Logger logger("TestLogger");

    // logs should be forwarded to same sink instances: 0 and 1
    int instance1 = 0;
    int instance2 = 1;

    // create log outside of log limit
    logger.Emit(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance1).empty());
    CHECK(TestPrints::Read(instance2).empty());

    SECTION("create log within log limit") {
        logger.Emit(LogLevel::Info, "second");
        auto prints1 = TestPrints::Read(instance1);
        auto prints2 = TestPrints::Read(instance2);
        REQUIRE(prints1.size() == 1);
        REQUIRE(prints2.size() == 1);
        CHECK(prints1[0] == "INFO (TestLogger): second");
        CHECK(prints2[0] == "INFO (TestLogger): second");

        SECTION("increase log limit create log within log limit") {
            logger.SetLogLimit(LogLevel::Trace);
            logger.Emit(LogLevel::Trace, "third");
            auto prints1 = TestPrints::Read(instance1);
            auto prints2 = TestPrints::Read(instance2);
            REQUIRE(prints1.size() == 2);
            REQUIRE(prints2.size() == 2);
            CHECK(prints1[1] == "TRACE (TestLogger): third");
            CHECK(prints2[1] == "TRACE (TestLogger): third");

            SECTION("log via lambda function") {
                logger.Emit(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints1 = TestPrints::Read(instance1);
                auto prints2 = TestPrints::Read(instance2);
                REQUIRE(prints1.size() == 3);
                REQUIRE(prints2.size() == 3);
                CHECK(prints1[2] == "TRACE (TestLogger): forth");
                CHECK(prints2[2] == "TRACE (TestLogger): forth");
            }
        }
    }
}

TEST_CASE_METHOD(TwoGlobalSinksFixture,
                 "Local named logger with its own two sink instances",
                 "[logger]") {
    // create logger with separate sink instances
    Logger logger("OwnSinkLogger",
                  {LogSinkTest::CreateFactory(), LogSinkTest::CreateFactory()});

    // logs should be forwarded to new sink instances: 2 and 3
    int instance1 = 2;
    int instance2 = 3;

    // create log outside of log limit
    logger.Emit(LogLevel::Trace, "first");
    CHECK(TestPrints::Read(instance1).empty());
    CHECK(TestPrints::Read(instance2).empty());

    SECTION("create log within log limit") {
        logger.Emit(LogLevel::Info, "second");
        auto prints1 = TestPrints::Read(instance1);
        auto prints2 = TestPrints::Read(instance2);
        REQUIRE(prints1.size() == 1);
        REQUIRE(prints2.size() == 1);
        CHECK(prints1[0] == "INFO (OwnSinkLogger): second");
        CHECK(prints2[0] == "INFO (OwnSinkLogger): second");

        SECTION("increase log limit create log within log limit") {
            logger.SetLogLimit(LogLevel::Trace);
            logger.Emit(LogLevel::Trace, "third");
            auto prints1 = TestPrints::Read(instance1);
            auto prints2 = TestPrints::Read(instance2);
            REQUIRE(prints1.size() == 2);
            REQUIRE(prints2.size() == 2);
            CHECK(prints1[1] == "TRACE (OwnSinkLogger): third");
            CHECK(prints2[1] == "TRACE (OwnSinkLogger): third");

            SECTION("log via lambda function") {
                logger.Emit(LogLevel::Trace,
                            [] { return std::string{"forth"}; });
                auto prints1 = TestPrints::Read(instance1);
                auto prints2 = TestPrints::Read(instance2);
                REQUIRE(prints1.size() == 3);
                REQUIRE(prints2.size() == 3);
                CHECK(prints1[2] == "TRACE (OwnSinkLogger): forth");
                CHECK(prints2[2] == "TRACE (OwnSinkLogger): forth");
            }
        }
    }
}

TEST_CASE_METHOD(OneGlobalSinkFixture,
                 "Common interface for global and local named loggers"
                 "[logger]") {
    // global logs will be forwarded to instance: 0
    int global_instance = 0;

    // create local logger with separate sink instance
    Logger logger("OwnSinkLogger", {LogSinkTest::CreateFactory()});
    // local logs should be forwarded to new sink instance: 1
    int local_instance = 1;

    SECTION("global instance") {
        // create log outside of log limit
        Logger::Log(nullptr, LogLevel::Trace, "first");
        CHECK(TestPrints::Read(global_instance).empty());

        SECTION("create log within log limit") {
            Logger::Log(nullptr, LogLevel::Info, "second");
            auto prints = TestPrints::Read(global_instance);
            REQUIRE(prints.size() == 1);
            CHECK(prints[0] == "INFO: second");

            SECTION("increase log limit create log within log limit") {
                LogConfig::SetLogLimit(LogLevel::Trace);
                Logger::Log(nullptr, LogLevel::Trace, "third");
                auto prints = TestPrints::Read(global_instance);
                REQUIRE(prints.size() == 2);
                CHECK(prints[1] == "TRACE: third");

                SECTION("log via lambda function") {
                    Logger::Log(nullptr, LogLevel::Trace, [] {
                        return std::string{"forth"};
                    });
                    auto prints = TestPrints::Read(global_instance);
                    REQUIRE(prints.size() == 3);
                    CHECK(prints[2] == "TRACE: forth");
                }
            }
        }
    }

    SECTION("named instance") {
        // create log outside of log limit
        Logger::Log(&logger, LogLevel::Trace, "first");
        CHECK(TestPrints::Read(local_instance).empty());

        SECTION("create log within log limit") {
            Logger::Log(&logger, LogLevel::Info, "second");
            auto prints = TestPrints::Read(local_instance);
            REQUIRE(prints.size() == 1);
            CHECK(prints[0] == "INFO (OwnSinkLogger): second");

            SECTION("increase log limit create log within log limit") {
                logger.SetLogLimit(LogLevel::Trace);
                Logger::Log(&logger, LogLevel::Trace, "third");
                auto prints = TestPrints::Read(local_instance);
                REQUIRE(prints.size() == 2);
                CHECK(prints[1] == "TRACE (OwnSinkLogger): third");

                SECTION("log via lambda function") {
                    Logger::Log(&logger, LogLevel::Trace, [] {
                        return std::string{"forth"};
                    });
                    auto prints = TestPrints::Read(local_instance);
                    REQUIRE(prints.size() == 3);
                    CHECK(prints[2] == "TRACE (OwnSinkLogger): forth");
                }
            }
        }
    }
}