-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpcdaemon: add support for rotating file logs of external interfaces
infra: add default settings in logging initialization conan: add spdlog dependency
- Loading branch information
Showing
7 changed files
with
228 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2024 The Silkworm Authors | ||
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 "interface_log.hpp" | ||
|
||
#include <spdlog/sinks/rotating_file_sink.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include <silkworm/core/common/base.hpp> | ||
#include <silkworm/infra/common/ensure.hpp> | ||
|
||
namespace silkworm::log { | ||
|
||
class InterfaceLogImpl final { | ||
public: | ||
explicit InterfaceLogImpl(std::string_view name, std::string_view folder, bool auto_flush); | ||
~InterfaceLogImpl() { | ||
flush(); | ||
} | ||
|
||
[[nodiscard]] std::filesystem::path path() const { | ||
return file_path_; | ||
} | ||
|
||
template <typename... Args> | ||
void log(spdlog::format_string_t<Args...> fmt, Args&&... args) { | ||
rotating_logger_->info(fmt, std::forward<Args>(args)...); | ||
} | ||
|
||
void log(std::string_view msg) { | ||
rotating_logger_->info(msg); | ||
if (auto_flush_) { | ||
rotating_logger_->flush(); | ||
} | ||
} | ||
|
||
void flush() { | ||
rotating_logger_->flush(); | ||
} | ||
|
||
private: | ||
std::string name_; | ||
bool auto_flush_; | ||
std::filesystem::path file_path_; | ||
std::size_t max_file_size_{1 * kMebi}; | ||
std::size_t max_files_{10}; | ||
std::shared_ptr<spdlog::logger> rotating_logger_; | ||
}; | ||
|
||
InterfaceLogImpl::InterfaceLogImpl(std::string_view name, std::string_view folder, bool auto_flush) | ||
: name_{name}, | ||
auto_flush_{auto_flush}, | ||
file_path_{folder / std::filesystem::path{name_ + ".log"}}, | ||
rotating_logger_{spdlog::rotating_logger_mt(name_, file_path_.string(), max_file_size_, max_files_)} { | ||
ensure(!name_.empty(), "InterfaceLogImpl: name is empty"); | ||
|
||
// Hard-code log level because we want all-or-nothing in interface log | ||
rotating_logger_->set_level(spdlog::level::info); | ||
|
||
// Customize log pattern to avoid unnecessary fields (log level, logger name) | ||
rotating_logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v"); | ||
} | ||
|
||
InterfaceLog::InterfaceLog(std::string_view name, std::string_view folder, bool auto_flush) | ||
: p_impl_{std::make_unique<InterfaceLogImpl>(name, folder, auto_flush)} { | ||
} | ||
|
||
// An explicit destructor is needed to avoid error: | ||
// invalid application of 'sizeof' to an incomplete type 'silkworm::log::InterfaceLogImpl' | ||
InterfaceLog::~InterfaceLog() { | ||
p_impl_->flush(); | ||
} | ||
|
||
std::filesystem::path InterfaceLog::path() const { | ||
return p_impl_->path(); | ||
} | ||
|
||
void InterfaceLog::log_req(std::string_view msg) { | ||
p_impl_->log("REQ -> {}", msg); | ||
} | ||
|
||
void InterfaceLog::log_rsp(std::string_view msg) { | ||
p_impl_->log("RSP <- {}", msg); | ||
} | ||
|
||
void InterfaceLog::flush() { | ||
p_impl_->flush(); | ||
} | ||
|
||
} // namespace silkworm::log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2024 The Silkworm Authors | ||
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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <memory> | ||
#include <string_view> | ||
|
||
namespace silkworm::log { | ||
|
||
class InterfaceLogImpl; | ||
|
||
class InterfaceLog final { | ||
public: | ||
explicit InterfaceLog(std::string_view name, std::string_view folder = "logs/", bool auto_flush = false); | ||
~InterfaceLog(); | ||
|
||
[[nodiscard]] std::filesystem::path path() const; | ||
|
||
void log_req(std::string_view msg); | ||
void log_rsp(std::string_view msg); | ||
|
||
void flush(); | ||
|
||
private: | ||
std::unique_ptr<InterfaceLogImpl> p_impl_; | ||
}; | ||
|
||
} // namespace silkworm::log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2024 The Silkworm Authors | ||
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 "interface_log.hpp" | ||
|
||
#include <fstream> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include <absl/strings/match.h> | ||
#include <catch2/catch.hpp> | ||
|
||
#include <silkworm/infra/common/directories.hpp> | ||
|
||
namespace silkworm::log { | ||
|
||
TEST_CASE("InterfaceLog basic", "[infra][common][log]") { | ||
const auto tmp_dir{TemporaryDirectory::get_unique_temporary_path()}; | ||
static int count{0}; | ||
auto ifc_log{std::make_unique<InterfaceLog>("eth_rpc" + std::to_string(++count), tmp_dir.string())}; | ||
REQUIRE(!ifc_log->path().empty()); | ||
ifc_log->log_req(R"({"json":"2.0"})"); | ||
ifc_log->log_rsp(R"({"json":"2.0"})"); | ||
std::ifstream log_ifstream{ifc_log->path().string()}; | ||
|
||
// Log file must be empty before flushing | ||
CHECK(log_ifstream.get() == -1); | ||
CHECK(log_ifstream.eof()); | ||
log_ifstream.clear(); | ||
log_ifstream.seekg(std::ios::beg); | ||
|
||
SECTION("explicit flush") { | ||
// InterfaceLog instance gets flushed here but remains alive until the end | ||
ifc_log->flush(); | ||
} | ||
|
||
SECTION("implicit flush") { | ||
// InterfaceLog instance gets destroyed here and implicitly flushed | ||
ifc_log.reset(); | ||
} | ||
|
||
// First line must be the request | ||
std::string content; | ||
std::getline(log_ifstream, content); | ||
CHECK(absl::StrContains(content, R"(REQ -> {"json":"2.0"})")); | ||
// Second line must be the response | ||
std::getline(log_ifstream, content); | ||
CHECK(absl::StrContains(content, R"(RSP <- {"json":"2.0"})")); | ||
// No other content is present | ||
CHECK(log_ifstream.get() == -1); | ||
CHECK(log_ifstream.eof()); | ||
} | ||
|
||
} // namespace silkworm::log |