Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger initialization fixes #34

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions bpf_generic/src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <filesystem>
#include <system_error>
#include <unistd.h>
#include <vector>

Expand All @@ -11,22 +11,23 @@ namespace logging {
constexpr std::size_t max_size = 10 * 1024 * 1024;
constexpr std::size_t max_files = 3;

void setUpLogger(const std::string& logDir, bool logToStdout) {
namespace fs = std::filesystem;
namespace fs = std::filesystem;

void setUpLogger(const fs::path logDir, bool logToStdout) {
std::vector<spdlog::sink_ptr> sinks;

if (logToStdout){
sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
}

if (!logDir.empty()) {
fs::path logPath;
if (!fs::is_directory(logDir)) {
spdlog::warn("{} doesn't exist or is not a directory.", logDir);
}
else {
std::string logFile = fmt::format("{}/oneagent_nettracer_{:d}.log", logDir, getpid());
sinks.emplace_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, max_size, max_files));
std::error_code ec;
bool dirExists = fs::exists(logDir, ec);
if (!dirExists || ec || !fs::is_directory(logDir)) {
exit(5);
} else {
auto logFile = logDir / fmt::format("oneagent_nettracer_{:d}.log", getpid());
sinks.emplace_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile.string(), max_size, max_files));
}
}

Expand Down
3 changes: 2 additions & 1 deletion bpf_generic/src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include <spdlog/spdlog.h>
#include <filesystem>
#include <memory>
#include <string>

Expand All @@ -13,7 +14,7 @@ inline std::shared_ptr<spdlog::logger> getLogger() {
return spdlog::get(LOGGER_NAME);
}

void setUpLogger(const std::string& logDir, bool logToStdout);
void setUpLogger(const std::filesystem::path logDir, bool logToStdout);

} // namespace logging

Expand Down
3 changes: 0 additions & 3 deletions nettracersrv/unified_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ bool setUpLogging(const boost::program_options::variables_map& vm) {
bool noStdoutLog = vm.count("no_stdout_log");
bool noFileLog = logger_path.empty();

if (!noFileLog) {
std::filesystem::create_directory(logger_path);
}
logging::setUpLogger(logger_path, !noStdoutLog);
if (areDebugLogsEnabled(vm)) {
logging::getLogger()->set_level(spdlog::level::debug);
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.1.9
version=1.1.10
Loading