Skip to content

Commit

Permalink
feat(liblogging): Add permission check of logdir in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hparzych committed Oct 10, 2023
1 parent 1fceaf0 commit 90f4fd7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions liblogging/src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <filesystem>
#include <memory>
#include <string>
#include <unistd.h>
#include <vector>

namespace logging {
Expand Down Expand Up @@ -61,10 +62,17 @@ void Logger::setup(std::string name, bool logToStdout, std::filesystem::path log
}

if (!logDir.empty()) {
if (!fs::exists(logDir)) {
throw std::runtime_error("Log directory doesn't exist");
}

if (!fs::is_directory(logDir)) {
throw std::runtime_error("Log directory doesn't exist or is not a directory");
throw std::runtime_error("Log directory is not a directory");
}

if (access(logDir.c_str(), R_OK | W_OK) != 0) {
throw std::runtime_error("Couldn't access log directory for reading and writing");
}
// TODO: Check for permissions

fs::path logFile{logDir / (name + ".log")};
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, max_size, max_files));
Expand Down Expand Up @@ -117,6 +125,8 @@ void Logger::logLine(enum LogLevel level, const char* buf, size_t len) {
} else {
str.assign(buf, len);
}

log(level, "{}", str);
}

} // namespace logging

0 comments on commit 90f4fd7

Please sign in to comment.