add new subsystem interface, Logger

This commit is contained in:
falsycat 2023-08-13 11:35:43 +09:00
parent d0f0955c47
commit e6d9d2205d
3 changed files with 101 additions and 0 deletions

View File

@ -21,6 +21,7 @@ target_sources(nf7_iface
data/interface.hh
subsys/concurrency.hh
subsys/interface.hh
subsys/logger.hh
subsys/parallelism.hh
env.hh
file.hh
@ -40,6 +41,7 @@ target_sources(nf7_iface_test
common/task_test.cc
common/task_test.hh
common/value_test.cc
subsys/logger_test.hh
lambda_test.cc
lambda_test.hh
)

80
iface/subsys/logger.hh Normal file
View File

@ -0,0 +1,80 @@
// No copyright
#pragma once
#include <source_location>
#include <string>
#include <string_view>
#include "iface/subsys/interface.hh"
namespace nf7::subsys {
class Logger : public Interface {
public:
using SrcLoc = std::source_location;
enum Level {
kTrace,
kInfo,
kWarn,
kError,
};
class Item final {
public:
Item() = delete;
Item(Level level,
std::string_view contents,
std::source_location srcloc = std::source_location::current()) noexcept
: level_(level), contents_(contents), srcloc_(srcloc) { }
Item(const Item&) = default;
Item(Item&&) = default;
Item& operator=(const Item&) = default;
Item& operator=(Item&&) = default;
Level level() const noexcept { return level_; }
const std::string& contents() const noexcept { return contents_; }
const std::source_location& srcloc() const noexcept { return srcloc_; }
private:
Level level_;
std::string contents_;
std::source_location srcloc_;
};
public:
using Interface::Interface;
public:
// THREAD-SAFE
virtual void Push(const Item&) noexcept = 0;
// THREAD-SAFE
void Trace(std::string_view contents,
SrcLoc srcloc = SrcLoc::current()) noexcept {
Push(Item {kTrace, contents, srcloc});
}
// THREAD-SAFE
void Info(std::string_view contents,
SrcLoc srcloc = SrcLoc::current()) noexcept {
Push(Item {kInfo, contents, srcloc});
}
// THREAD-SAFE
void Warn(std::string_view contents,
SrcLoc srcloc = SrcLoc::current()) noexcept {
Push(Item {kWarn, contents, srcloc});
}
// THREAD-SAFE
void Error(std::string_view contents,
SrcLoc srcloc = SrcLoc::current()) noexcept {
Push(Item {kError, contents, srcloc});
}
};
} // namespace nf7::subsys

View File

@ -0,0 +1,19 @@
// No copyright
#pragma once
#include "iface/subsys/logger.hh"
#include <gmock/gmock.h>
namespace nf7::subsys::test {
class LoggerMock : public Logger {
public:
explicit LoggerMock(const char* name = "LoggerMock") noexcept
: Logger(name) {}
MOCK_METHOD(void, Push, (const Item&), (noexcept));
};
} // namespace nf7::subsys::test