mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
handle new log levels in daemon cli invocation
This commit is contained in:
@@ -4,10 +4,12 @@
|
|||||||
// License text can be found in the licenses/ folder.
|
// License text can be found in the licenses/ folder.
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <errno.h>
|
#include <cerrno>
|
||||||
#include <stdio.h> /* printf */
|
#include <cstdio> /* printf */
|
||||||
#include <stdlib.h> /* atoi */
|
#include <cstdlib> /* atoi */
|
||||||
|
#include <iostream>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#ifdef HAVE_SYSLOG
|
#ifdef HAVE_SYSLOG
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
@@ -24,11 +26,12 @@
|
|||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
#include <libtransmission/transmission.h>
|
#include <libtransmission/transmission.h>
|
||||||
|
|
||||||
#include <libtransmission/error.h>
|
#include <libtransmission/error.h>
|
||||||
#include <libtransmission/file.h>
|
#include <libtransmission/file.h>
|
||||||
|
#include <libtransmission/log.h>
|
||||||
#include <libtransmission/tr-getopt.h>
|
#include <libtransmission/tr-getopt.h>
|
||||||
#include <libtransmission/tr-macros.h>
|
#include <libtransmission/tr-macros.h>
|
||||||
#include <libtransmission/log.h>
|
|
||||||
#include <libtransmission/utils.h>
|
#include <libtransmission/utils.h>
|
||||||
#include <libtransmission/variant.h>
|
#include <libtransmission/variant.h>
|
||||||
#include <libtransmission/version.h>
|
#include <libtransmission/version.h>
|
||||||
@@ -100,7 +103,7 @@ static struct event_base* ev_base = nullptr;
|
|||||||
**** Config File
|
**** 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>" },
|
{ { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", true, "<list>" },
|
||||||
{ 'b', "blocklist", "Enable peer blocklists", "b", false, nullptr },
|
{ 'b', "blocklist", "Enable peer blocklists", "b", false, nullptr },
|
||||||
{ 'B', "no-blocklist", "Disable 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>" },
|
{ 'u', "username", "Set username for authentication", "u", true, "<username>" },
|
||||||
{ 'v', "password", "Set password for authentication", "v", true, "<password>" },
|
{ 'v', "password", "Set password for authentication", "v", true, "<password>" },
|
||||||
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
||||||
{ 810, "log-error", "Show error messages", nullptr, false, nullptr },
|
{ 810, "log-level", "Must be 'critical', 'error', 'warn', 'info', 'debug', or 'trace'.", nullptr, true, "<level>" },
|
||||||
{ 811, "log-info", "Show error and info messages", nullptr, false, nullptr },
|
{ 811, "log-error", "Deprecated. Use --log-level=error", nullptr, false, nullptr },
|
||||||
{ 812, "log-debug", "Show error, info, and debug messages", 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>" },
|
{ 'w', "download-dir", "Where to save downloaded data", "w", true, "<path>" },
|
||||||
{ 800, "paused", "Pause all torrents on startup", nullptr, false, nullptr },
|
{ 800, "paused", "Pause all torrents on startup", nullptr, false, nullptr },
|
||||||
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", false, nullptr },
|
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", false, nullptr },
|
||||||
@@ -582,14 +586,28 @@ static bool parse_args(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 810:
|
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;
|
break;
|
||||||
|
|
||||||
case 811:
|
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;
|
break;
|
||||||
|
|
||||||
case 812:
|
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);
|
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_DEBUG);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,13 @@
|
|||||||
// or any future license endorsed by Mnemosyne LLC.
|
// or any future license endorsed by Mnemosyne LLC.
|
||||||
// License text can be found in the licenses/ folder.
|
// License text can be found in the licenses/ folder.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <string_view>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include <event2/buffer.h>
|
#include <event2/buffer.h>
|
||||||
|
|
||||||
@@ -289,3 +292,53 @@ void tr_logAddMessage(char const* file, int line, tr_log_level level, std::strin
|
|||||||
|
|
||||||
errno = err;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,8 +6,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <optional>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
///
|
||||||
|
|
||||||
enum tr_log_level
|
enum tr_log_level
|
||||||
{
|
{
|
||||||
// No logging at all
|
// No logging at all
|
||||||
@@ -35,6 +38,12 @@ enum tr_log_level
|
|||||||
TR_LOG_TRACE
|
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
|
struct tr_log_message
|
||||||
{
|
{
|
||||||
tr_log_level level;
|
tr_log_level level;
|
||||||
|
|||||||
Reference in New Issue
Block a user