fixup! refactor: remove deep logging (#2749) (#2785)

handle new log levels in daemon cli invocation
This commit is contained in:
Charles Kerr
2022-03-18 14:15:43 -05:00
committed by GitHub
parent 68646df4e3
commit 7b837bca1f
3 changed files with 90 additions and 10 deletions

View File

@@ -4,10 +4,12 @@
// License text can be found in the licenses/ folder.
#include <array>
#include <errno.h>
#include <stdio.h> /* printf */
#include <stdlib.h> /* atoi */
#include <cerrno>
#include <cstdio> /* printf */
#include <cstdlib> /* atoi */
#include <iostream>
#include <string_view>
#include <utility>
#ifdef HAVE_SYSLOG
#include <syslog.h>
@@ -24,11 +26,12 @@
#include <fmt/core.h>
#include <libtransmission/transmission.h>
#include <libtransmission/error.h>
#include <libtransmission/file.h>
#include <libtransmission/log.h>
#include <libtransmission/tr-getopt.h>
#include <libtransmission/tr-macros.h>
#include <libtransmission/log.h>
#include <libtransmission/utils.h>
#include <libtransmission/variant.h>
#include <libtransmission/version.h>
@@ -100,7 +103,7 @@ static struct event_base* ev_base = nullptr;
**** Config File
***/
static auto constexpr Options = std::array<tr_option, 44>{
static auto constexpr Options = std::array<tr_option, 45>{
{ { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", true, "<list>" },
{ 'b', "blocklist", "Enable peer blocklists", "b", false, nullptr },
{ 'B', "no-blocklist", "Disable peer blocklists", "B", false, nullptr },
@@ -119,9 +122,10 @@ static auto constexpr Options = std::array<tr_option, 44>{
{ 'u', "username", "Set username for authentication", "u", true, "<username>" },
{ 'v', "password", "Set password for authentication", "v", true, "<password>" },
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
{ 810, "log-error", "Show error messages", nullptr, false, nullptr },
{ 811, "log-info", "Show error and info messages", nullptr, false, nullptr },
{ 812, "log-debug", "Show error, info, and debug messages", nullptr, false, nullptr },
{ 810, "log-level", "Must be 'critical', 'error', 'warn', 'info', 'debug', or 'trace'.", nullptr, true, "<level>" },
{ 811, "log-error", "Deprecated. Use --log-level=error", nullptr, false, nullptr },
{ 812, "log-info", "Deprecated. Use --log-level=info", nullptr, false, nullptr },
{ 813, "log-debug", "Deprecated. Use --log-level=debug", nullptr, false, nullptr },
{ 'w', "download-dir", "Where to save downloaded data", "w", true, "<path>" },
{ 800, "paused", "Pause all torrents on startup", nullptr, false, nullptr },
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", false, nullptr },
@@ -582,14 +586,28 @@ static bool parse_args(
break;
case 810:
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_ERROR);
if (auto const level = tr_logGetLevelFromKey(optstr); level)
{
tr_variantDictAddInt(settings, TR_KEY_message_level, *level);
}
else
{
std::cerr << fmt::format(_("Couldn't parse log level '{level}'"), fmt::arg("level", optstr)) << std::endl;
}
break;
case 811:
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_INFO);
std::cerr << "WARN: --log-error is deprecated. Use --log-level=error" << std::endl;
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_ERROR);
break;
case 812:
std::cerr << "WARN: --log-info is deprecated. Use --log-level=info" << std::endl;
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_INFO);
break;
case 813:
std::cerr << "WARN: --log-debug is deprecated. Use --log-level=debug" << std::endl;
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_DEBUG);
break;

View File

@@ -3,10 +3,13 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <array>
#include <cerrno>
#include <cstdio>
#include <map>
#include <mutex>
#include <string_view>
#include <utility>
#include <event2/buffer.h>
@@ -289,3 +292,53 @@ void tr_logAddMessage(char const* file, int line, tr_log_level level, std::strin
errno = err;
}
///
namespace
{
auto constexpr LogKeys = std::array<std::pair<std::string_view, tr_log_level>, 7>{ { { "off", TR_LOG_OFF },
{ "critical", TR_LOG_CRITICAL },
{ "error", TR_LOG_ERROR },
{ "warn", TR_LOG_WARN },
{ "info", TR_LOG_INFO },
{ "debug", TR_LOG_DEBUG },
{ "trace", TR_LOG_TRACE } } };
bool constexpr keysAreOrdered()
{
for (size_t i = 0, n = std::size(LogKeys); i < n; ++i)
{
if (LogKeys[i].second != i)
{
return false;
}
}
return true;
}
static_assert(keysAreOrdered());
} // unnamed namespace
std::optional<tr_log_level> tr_logGetLevelFromKey(std::string_view key_in)
{
auto const key = tr_strlower(tr_strvStrip(key_in));
for (auto const& [name, level] : LogKeys)
{
if (key == name)
{
return level;
}
}
return std::nullopt;
}
std::string_view tr_logLevelToKey(tr_log_level key)
{
return LogKeys[key].first;
}

View File

@@ -6,8 +6,11 @@
#pragma once
#include <ctime>
#include <optional>
#include <string_view>
///
enum tr_log_level
{
// No logging at all
@@ -35,6 +38,12 @@ enum tr_log_level
TR_LOG_TRACE
};
std::optional<tr_log_level> tr_logGetLevelFromKey(std::string_view key);
std::string_view tr_logLevelToKey(tr_log_level);
///
struct tr_log_message
{
tr_log_level level;