Minor changes to logging

This commit is contained in:
Bartosz Wieczorek 2025-07-18 14:13:06 +02:00
parent 92c1c040c8
commit ffb3202320
4 changed files with 29 additions and 25 deletions

View File

@ -122,7 +122,7 @@ class ConfigurationReader {
std::pmr::string val{&memoryResource}; std::pmr::string val{&memoryResource};
val = it->second; val = it->second;
std::transform(val.begin(), val.end(), val.begin(), [](unsigned char c) { return static_cast< char >(std::tolower(c)); }); std::transform(val.begin(), val.end(), val.begin(), [](auto c) { return std::tolower(c); });
if(val == "1" || val == "true" || val == "yes" || val == "on") if(val == "1" || val == "true" || val == "yes" || val == "on")
return true; return true;
@ -136,6 +136,7 @@ class ConfigurationReader {
if(it == keyValues.end()) if(it == keyValues.end())
return std::nullopt; return std::nullopt;
auto val = it->second; auto val = it->second;
std::transform(val.begin(), val.end(), val.begin(), [](auto c) { return std::tolower(c); });
if(val == "bypass") if(val == "bypass")
return FailMode::bypass; return FailMode::bypass;
if(val == "deny") if(val == "deny")
@ -200,10 +201,9 @@ class ConfigurationReader {
return true; return true;
}; };
auto toLowerCaseOpt = [](auto str) { auto toLowerCaseOpt = [](auto & str) {
if(str) if(str)
std::transform(str->cbegin(), str->cend(), str->begin(), [](auto c) { return std::tolower(c); }); std::transform(str->cbegin(), str->cend(), str->begin(), [](auto c) { return std::tolower(c); });
return str;
}; };
/// NOTE: /// NOTE:
@ -246,8 +246,8 @@ class ConfigurationReader {
// reading proxy configuration // reading proxy configuration
config.proxyEnabled = getBool("proxyEnabled").value_or(false); config.proxyEnabled = getBool("proxyEnabled").value_or(false);
config.proxyType = toLowerCaseOpt(getStringOpt("proxyType")); config.proxyType = getStringOpt("proxyType");
config.proxyHost = toLowerCaseOpt(getStringOpt("proxyHost")); config.proxyHost = getStringOpt("proxyHost");
// Apply fallback if no config is set // Apply fallback if no config is set
if(config.proxyEnabled && (!config.proxyType || config.proxyType->empty()) && (!config.proxyHost || config.proxyHost->empty())) { if(config.proxyEnabled && (!config.proxyType || config.proxyType->empty()) && (!config.proxyHost || config.proxyHost->empty())) {
@ -289,16 +289,13 @@ class ConfigurationReader {
} }
} }
auto defaultProxyPort = [&]() -> int { toLowerCaseOpt(config.proxyType);
memory::MonotonicStackResource< 32 > memoryResource; toLowerCaseOpt(config.proxyHost);
if(config.proxyType) { auto defaultProxyPort = [&]() -> int {
std::pmr::string val{*config.proxyType, &memoryResource}; if(config.proxyType.value_or("").find("socks") != std::pmr::string::npos) {
std::transform(val.begin(), val.end(), val.begin(), [](auto c) { return std::tolower(c); });
if(val.find("socks") != std::pmr::string::npos) {
return 1080; return 1080;
} }
}
return 8080; return 8080;
}; };

View File

@ -44,7 +44,7 @@ class ErrorHandler {
} }
if(error.is< ConnectionError >()) { if(error.is< ConnectionError >()) {
if(config.failMode != FailMode::deny) { if(config.failMode == FailMode::bypass) {
pam.print("Incorrect response from the Rublon API, user bypassed"); pam.print("Incorrect response from the Rublon API, user bypassed");
return AuthenticationStatus::Action::Bypass; return AuthenticationStatus::Action::Bypass;
} else { } else {

View File

@ -7,8 +7,9 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include <string> #include <string>
#include <utility> #include <string_view>
#include <tl/expected.hpp> #include <tl/expected.hpp>
#include <utility>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -84,12 +85,18 @@ namespace details {
return logPath(); return logPath();
} }
inline void doLog(LogLevel level, const char * line) noexcept { inline void doLog(LogLevel level, std::string_view line) noexcept {
auto fp = std::unique_ptr< FILE, int (*)(FILE *) >(fopen(initLog(), "a+"), fclose); auto fp = std::unique_ptr< FILE, int (*)(FILE *) >(fopen(initLog(), "a+"), fclose);
if(fp) { if(fp) {
auto newl = line.back() == '\n' ? "" : "\n";
/// TODO add transaction ID /// TODO add transaction ID
fprintf( fprintf(fp.get(),
fp.get(), "%s %s[%s] %s\n", dateStr().c_str(), application == nullptr ? "" : application, LogLevelNames[( int ) level], line); "%s %s[%s] %s%s",
dateStr().c_str(),
application == nullptr ? "" : application,
LogLevelNames[( int ) level],
line.data(),
newl);
if(syncLogFile) if(syncLogFile)
sync(); sync();
} }
@ -111,8 +118,8 @@ void log(LogLevel level, const char * fmt, Ti &&... ti) noexcept {
return; return;
constexpr auto maxEntryLength = 1000; constexpr auto maxEntryLength = 1000;
std::array< char, maxEntryLength > line; std::array< char, maxEntryLength > line;
snprintf(line.data(), maxEntryLength, fmt, std::forward< Ti >(ti)...); auto len = snprintf(line.data(), maxEntryLength, fmt, std::forward< Ti >(ti)...);
details::doLog(level, line.data()); details::doLog(level, {line.data(), len});
} }
class PrintUser { class PrintUser {

View File

@ -64,7 +64,7 @@ class WebSocket {
}; };
if(_config.get().logging) { if(_config.get().logging) {
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG | LLL_HEADER, lws_log_emit); lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG, lws_log_emit);
} else { } else {
lws_set_log_level(LLL_ERR | LLL_WARN, lws_log_emit); lws_set_log_level(LLL_ERR | LLL_WARN, lws_log_emit);
} }