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

Improvements to uRaft #136

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
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
62 changes: 58 additions & 4 deletions src/slogger/slogger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
#include "common/platform.h"
#include "slogger/slogger.h"

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <boost/algorithm/string/case_conv.hpp>
#include <cassert>
#include <cerrno>
#include <cstdarg>
#include <cstdio>
#include <expected>
#include <format>
#include <stdexcept>
#include <string>

#include "common/cfg.h"
#include "errors/sfserr.h"

static safs::log_level::LogLevel log_level_from_syslog(int priority) {
Expand All @@ -44,6 +48,56 @@ static safs::log_level::LogLevel log_level_from_syslog(int priority) {
return kSyslogToLevel[std::min<int>(priority, kSyslogToLevel.size())];
}

std::expected<safs::log_level::LogLevel, std::string>
safs::log_level_from_string(const std::string &level) {
const auto lower_level = boost::algorithm::to_lower_copy(level);
if (lower_level == "trace") {
return safs::log_level::trace;
}
if (lower_level == "debug") {
return safs::log_level::debug;
}
if (lower_level == "info") {
return safs::log_level::info;
}
if (lower_level == "warn" || lower_level == "warning") {
return safs::log_level::warn;
}
if (lower_level == "err" || lower_level == "error") {
return safs::log_level::err;
}
if (lower_level == "crit" || lower_level == "critical") {
return safs::log_level::critical;
}
if (lower_level == "off") {
return safs::log_level::off;
}
return std::unexpected<std::string>("Invalid log level: " + level);
}

std::string safs::log_level_to_string(safs::log_level::LogLevel level) {
using safs::log_level::LogLevel;
switch (level) {
case LogLevel::trace:
return "trace";
case LogLevel::debug:
return "debug";
case LogLevel::info:
return "info";
case LogLevel::warn:
return "warn";
case LogLevel::err:
return "err";
case LogLevel::critical:
return "crit";
case LogLevel::off:
break;
uristdwarf marked this conversation as resolved.
Show resolved Hide resolved
default:
throw std::logic_error(std::format("invalid log_level: {}", uint64_t(level)));
}
return "off";
};

bool safs_add_log_file(const char *path, int priority, int max_file_size, int max_file_count) {
return safs::add_log_file(path, log_level_from_syslog(priority), max_file_size, max_file_count);
}
Expand Down
10 changes: 10 additions & 0 deletions src/slogger/slogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "common/platform.h"

#include <expected>

#include "common/syslog_defs.h"

#ifndef _WIN32
Expand Down Expand Up @@ -98,6 +100,14 @@ void drop_all_logs();
bool add_log_syslog();
bool add_log_stderr(log_level::LogLevel level);

// Returns the appropriate log level from the level string, or an error message
// indicating that it's not a valid level.
std::expected<log_level::LogLevel, std::string> log_level_from_string(const std::string &level);

// Opposite of the log_level_from_string, returns the string representation of
// safs::log_level::LogLevel
std::string log_level_to_string(log_level::LogLevel level);

} // namespace safs

// NOTICE(sarna) Old interface, don't use unless extern-C is needed
Expand Down
3 changes: 2 additions & 1 deletion src/uraft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(URAFT_SOURCES ${URAFT_SOURCES} ${CMAKE_SOURCE_DIR}/src/common/time_utils.cc)

add_executable(saunafs-uraft ${URAFT_MAIN} ${URAFT_SOURCES})

target_link_libraries(saunafs-uraft ${Boost_LIBRARIES} ${RT_LIBRARY} pthread)
target_link_libraries(saunafs-uraft ${Boost_LIBRARIES} ${RT_LIBRARY} pthread
slogger)

install(TARGETS saunafs-uraft RUNTIME DESTINATION ${SBIN_SUBDIR})
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/saunafs-uraft-helper DESTINATION ${SBIN_SUBDIR})
Loading