2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2010-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-18 19:15:43 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2022-09-21 23:34:18 +00:00
|
|
|
#include <chrono>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdio>
|
2022-03-11 22:06:58 +00:00
|
|
|
#include <map>
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string>
|
2022-03-18 19:15:43 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <utility>
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-02 14:06:02 +00:00
|
|
|
#include <fmt/chrono.h>
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2022-01-24 02:44:43 +00:00
|
|
|
|
2021-12-28 15:08:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
namespace
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
class tr_log_state
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
[[nodiscard]] auto unique_lock()
|
|
|
|
{
|
|
|
|
return std::unique_lock(message_mutex_);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_log_level level = TR_LOG_ERROR;
|
|
|
|
|
|
|
|
bool queue_enabled_ = false;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
tr_log_message* queue_ = nullptr;
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
tr_log_message** queue_tail_ = &queue_;
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
int queue_length_ = 0;
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
std::recursive_mutex message_mutex_;
|
|
|
|
};
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
auto log_state = tr_log_state{};
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_sys_file_t tr_logGetFile()
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool initialized = false;
|
|
|
|
static tr_sys_file_t file = TR_BAD_SYS_FILE;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!initialized)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
switch (tr_env_get_int("TR_DEBUG_FD", 0))
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 1:
|
2022-03-27 17:37:29 +00:00
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_OUT);
|
2013-01-25 23:34:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 2:
|
2022-03-27 17:37:29 +00:00
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR);
|
2013-01-25 23:34:20 +00:00
|
|
|
break;
|
2021-12-17 05:47:51 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
file = TR_BAD_SYS_FILE;
|
|
|
|
break;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
initialized = true;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return file;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 22:06:58 +00:00
|
|
|
void logAddImpl(
|
2023-02-27 16:02:58 +00:00
|
|
|
[[maybe_unused]] std::string_view file,
|
2022-12-18 16:23:44 +00:00
|
|
|
[[maybe_unused]] long line,
|
2022-04-02 14:06:02 +00:00
|
|
|
[[maybe_unused]] tr_log_level level,
|
2022-03-17 22:39:06 +00:00
|
|
|
std::string_view msg,
|
|
|
|
[[maybe_unused]] std::string_view name)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2022-03-11 22:06:58 +00:00
|
|
|
if (std::empty(msg))
|
2022-03-10 05:51:14 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
auto const lock = log_state.unique_lock();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-04 05:34:39 +00:00
|
|
|
#if defined(__ANDROID__)
|
2022-01-24 02:44:43 +00:00
|
|
|
|
|
|
|
int prio;
|
|
|
|
|
2022-01-24 03:53:49 +00:00
|
|
|
switch (level)
|
|
|
|
{
|
2022-03-11 22:06:58 +00:00
|
|
|
case TR_LOG_CRITICAL:
|
|
|
|
prio = ANDROID_LOG_FATAL;
|
|
|
|
break;
|
2022-01-24 03:53:49 +00:00
|
|
|
case TR_LOG_ERROR:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
2022-03-11 22:06:58 +00:00
|
|
|
case TR_LOG_WARN:
|
|
|
|
prio = ANDROID_LOG_WARN;
|
|
|
|
break;
|
2022-01-24 03:53:49 +00:00
|
|
|
case TR_LOG_INFO:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_INFO;
|
|
|
|
break;
|
2022-01-24 03:53:49 +00:00
|
|
|
case TR_LOG_DEBUG:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
2022-03-11 22:06:58 +00:00
|
|
|
case TR_LOG_TRACE:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_VERBOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
2022-09-23 19:36:37 +00:00
|
|
|
auto const szmsg = fmt::format("{:s}", msg);
|
2022-01-24 02:44:43 +00:00
|
|
|
#else
|
2022-09-23 19:36:37 +00:00
|
|
|
auto const szmsg = fmt::format("[{:s}:{:d}] {:s}", file, line, msg);
|
2015-12-13 10:34:53 +00:00
|
|
|
#endif
|
2022-09-23 19:36:37 +00:00
|
|
|
__android_log_write(prio, "transmission", szmsg.c_str());
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-01-24 02:44:43 +00:00
|
|
|
#else
|
|
|
|
|
2022-03-11 22:06:58 +00:00
|
|
|
if (tr_logGetQueueEnabled())
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
auto* const newmsg = new tr_log_message{};
|
2022-03-11 22:06:58 +00:00
|
|
|
newmsg->level = level;
|
|
|
|
newmsg->when = tr_time();
|
2022-08-12 00:59:58 +00:00
|
|
|
newmsg->message = msg;
|
2022-03-11 22:06:58 +00:00
|
|
|
newmsg->file = file;
|
|
|
|
newmsg->line = line;
|
2022-08-12 00:59:58 +00:00
|
|
|
newmsg->name = name;
|
2022-03-11 22:06:58 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
*log_state.queue_tail_ = newmsg;
|
|
|
|
log_state.queue_tail_ = &newmsg->next;
|
|
|
|
++log_state.queue_length_;
|
2022-03-11 22:06:58 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
if (log_state.queue_length_ > TR_LOG_MAX_QUEUE_LENGTH)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2022-03-25 05:24:04 +00:00
|
|
|
tr_log_message* old = log_state.queue_;
|
|
|
|
log_state.queue_ = old->next;
|
2022-03-11 22:06:58 +00:00
|
|
|
old->next = nullptr;
|
|
|
|
tr_logFreeQueue(old);
|
2022-03-25 05:24:04 +00:00
|
|
|
--log_state.queue_length_;
|
|
|
|
TR_ASSERT(log_state.queue_length_ == TR_LOG_MAX_QUEUE_LENGTH);
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
2022-03-11 22:06:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_sys_file_t fp = tr_logGetFile();
|
|
|
|
|
|
|
|
if (fp == TR_BAD_SYS_FILE)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
fp = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR);
|
2022-03-11 22:06:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
auto timestr = std::array<char, 64>{};
|
|
|
|
tr_logGetTimeStr(std::data(timestr), std::size(timestr));
|
2022-03-30 19:59:13 +00:00
|
|
|
tr_sys_file_write_line(
|
|
|
|
fp,
|
2022-08-25 01:19:21 +00:00
|
|
|
!std::empty(name) ? fmt::format(FMT_STRING("[{:s}] {:s}: {:s}"), std::data(timestr), name, msg) :
|
|
|
|
fmt::format(FMT_STRING("[{:s}] {:s}"), std::data(timestr), msg));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_flush(fp);
|
2022-03-11 22:06:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
tr_log_level tr_logGetLevel()
|
|
|
|
{
|
2022-03-25 05:24:04 +00:00
|
|
|
return log_state.level;
|
2022-03-17 22:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_logLevelIsActive(tr_log_level level)
|
|
|
|
{
|
|
|
|
return tr_logGetLevel() >= level;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_logSetLevel(tr_log_level level)
|
|
|
|
{
|
2022-03-25 05:24:04 +00:00
|
|
|
log_state.level = level;
|
2022-03-17 22:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_logSetQueueEnabled(bool is_enabled)
|
2022-03-17 22:39:06 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
log_state.queue_enabled_ = is_enabled;
|
2022-03-17 22:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_logGetQueueEnabled()
|
|
|
|
{
|
2022-03-25 05:24:04 +00:00
|
|
|
return log_state.queue_enabled_;
|
2022-03-17 22:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_log_message* tr_logGetQueue()
|
|
|
|
{
|
2022-03-25 05:24:04 +00:00
|
|
|
auto const lock = log_state.unique_lock();
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
auto* const ret = log_state.queue_;
|
|
|
|
log_state.queue_ = nullptr;
|
|
|
|
log_state.queue_tail_ = &log_state.queue_;
|
|
|
|
log_state.queue_length_ = 0;
|
2022-03-17 22:39:06 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_logFreeQueue(tr_log_message* freeme)
|
2022-03-11 22:06:58 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
while (freeme != nullptr)
|
2022-03-17 22:39:06 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
auto* const next = freeme->next;
|
2022-08-12 00:59:58 +00:00
|
|
|
delete freeme;
|
2022-08-03 06:15:37 +00:00
|
|
|
freeme = next;
|
2022-03-17 22:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-03-17 22:39:06 +00:00
|
|
|
|
|
|
|
char* tr_logGetTimeStr(char* buf, size_t buflen)
|
|
|
|
{
|
2022-04-02 14:06:02 +00:00
|
|
|
auto const a = std::chrono::system_clock::now();
|
|
|
|
auto const [out, len] = fmt::format_to_n(
|
|
|
|
buf,
|
|
|
|
buflen - 1,
|
2022-04-14 04:19:18 +00:00
|
|
|
"{0:%F %H:%M:}{1:%S}",
|
2022-04-02 14:06:02 +00:00
|
|
|
a,
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(a.time_since_epoch()));
|
|
|
|
*out = '\0';
|
2022-03-17 22:39:06 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:23:44 +00:00
|
|
|
void tr_logAddMessage(char const* file, long line, tr_log_level level, std::string_view msg, std::string_view name)
|
2022-03-17 22:39:06 +00:00
|
|
|
{
|
|
|
|
TR_ASSERT(!std::empty(msg));
|
|
|
|
|
2023-02-27 16:02:58 +00:00
|
|
|
// strip source path to only include the filename
|
|
|
|
auto filename = tr_sys_path_basename(file);
|
|
|
|
if (std::empty(filename))
|
|
|
|
{
|
|
|
|
filename = "?"sv;
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
auto name_fallback = std::string{};
|
|
|
|
if (std::empty(name))
|
|
|
|
{
|
2023-02-27 16:02:58 +00:00
|
|
|
name_fallback = fmt::format(FMT_STRING("{}:{}"), filename, line);
|
2022-03-17 22:39:06 +00:00
|
|
|
name = name_fallback;
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:06:58 +00:00
|
|
|
// message logging shouldn't affect errno
|
|
|
|
int const err = errno;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-03-11 22:06:58 +00:00
|
|
|
// skip unwanted messages
|
|
|
|
if (!tr_logLevelIsActive(level))
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
auto const lock = log_state.unique_lock();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-11 22:06:58 +00:00
|
|
|
// don't log the same warning ad infinitum.
|
|
|
|
// it's not useful after some point.
|
|
|
|
bool last_one = false;
|
|
|
|
if (level == TR_LOG_CRITICAL || level == TR_LOG_ERROR || level == TR_LOG_WARN)
|
|
|
|
{
|
|
|
|
static auto constexpr MaxRepeat = size_t{ 30 };
|
2023-02-27 16:02:58 +00:00
|
|
|
static auto counts = new std::map<std::pair<std::string_view, int>, size_t>{};
|
2022-03-11 22:06:58 +00:00
|
|
|
|
2023-02-27 16:02:58 +00:00
|
|
|
auto& count = (*counts)[std::make_pair(filename, line)];
|
2022-03-11 22:06:58 +00:00
|
|
|
++count;
|
|
|
|
last_one = count == MaxRepeat;
|
|
|
|
if (count > MaxRepeat)
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
return;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 22:06:58 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
// log the messages
|
2023-02-27 16:02:58 +00:00
|
|
|
logAddImpl(filename, line, level, msg, name);
|
2022-03-14 04:43:35 +00:00
|
|
|
if (last_one)
|
|
|
|
{
|
2023-02-27 16:02:58 +00:00
|
|
|
logAddImpl(
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
level,
|
|
|
|
_("Too many messages like this! I won't log this message anymore this session."),
|
|
|
|
name);
|
2022-03-14 04:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = err;
|
|
|
|
}
|
2022-03-18 19:15:43 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-03-18 19:15:43 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-04-02 14:06:02 +00:00
|
|
|
if (LogKeys[i].second != static_cast<tr_log_level>(i))
|
2022-03-18 19:15:43 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|