2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2021-12-28 02:32:22 +00:00
|
|
|
#include <cstdarg>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdio>
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
2013-01-25 23:34:20 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2022-01-24 02:44:43 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2021-12-28 15:08:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_level __tr_message_level = TR_LOG_ERROR;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool myQueueEnabled = false;
|
2021-09-15 00:18:09 +00:00
|
|
|
static tr_log_message* myQueue = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_log_message** myQueueTail = &myQueue;
|
|
|
|
static int myQueueLength = 0;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* make null versions of these win32 functions */
|
2022-01-24 19:07:55 +00:00
|
|
|
static inline bool IsDebuggerPresent()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
tr_log_level tr_logGetLevel()
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return __tr_message_level;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
static std::recursive_mutex message_mutex_;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-01-24 19:07:55 +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:
|
2021-09-15 00:18:09 +00:00
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_OUT, nullptr);
|
2013-01-25 23:34:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 2:
|
2021-09-15 00:18:09 +00:00
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR, nullptr);
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_logSetLevel(tr_log_level level)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
|
|
|
__tr_message_level = level;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_logSetQueueEnabled(bool isEnabled)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myQueueEnabled = isEnabled;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
bool tr_logGetQueueEnabled()
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myQueueEnabled;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
tr_log_message* tr_logGetQueue()
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = std::lock_guard(message_mutex_);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
auto* const ret = myQueue;
|
2021-09-15 00:18:09 +00:00
|
|
|
myQueue = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
myQueueTail = &myQueue;
|
|
|
|
myQueueLength = 0;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_logFreeQueue(tr_log_message* list)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
while (list != nullptr)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
tr_log_message* next = list->next;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(list->message);
|
|
|
|
tr_free(list->name);
|
|
|
|
tr_free(list);
|
|
|
|
list = next;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_logGetTimeStr(char* buf, size_t buflen)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct timeval tv;
|
|
|
|
tr_gettimeofday(&tv);
|
2020-11-05 22:46:21 +00:00
|
|
|
time_t const seconds = tv.tv_sec;
|
2021-12-17 05:47:51 +00:00
|
|
|
auto const milliseconds = int(tv.tv_usec / 1000);
|
2020-11-05 22:46:21 +00:00
|
|
|
char msec_str[8];
|
|
|
|
tr_snprintf(msec_str, sizeof msec_str, "%03d", milliseconds);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
struct tm now_tm;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_localtime_r(&seconds, &now_tm);
|
2020-11-05 22:46:21 +00:00
|
|
|
char date_str[32];
|
|
|
|
strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S", &now_tm);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_snprintf(buf, buflen, "%s.%s", date_str, msec_str);
|
2017-04-19 12:04:45 +00:00
|
|
|
return buf;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
bool tr_logGetDeepEnabled()
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static int8_t deepLoggingIsActive = -1;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (deepLoggingIsActive < 0)
|
|
|
|
{
|
2019-07-14 12:40:41 +00:00
|
|
|
deepLoggingIsActive = (int8_t)(IsDebuggerPresent() || tr_logGetFile() != TR_BAD_SYS_FILE);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return deepLoggingIsActive != 0;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_logAddDeep(char const* file, int line, char const* name, char const* fmt, ...)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sys_file_t const fp = tr_logGetFile();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (fp != TR_BAD_SYS_FILE || IsDebuggerPresent())
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* buf = evbuffer_new();
|
2021-09-15 00:18:09 +00:00
|
|
|
char* base = tr_sys_path_basename(file, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
char timestr[64];
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(buf, "[%s] ", tr_logGetTimeStr(timestr, sizeof(timestr)));
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (name != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "%s ", name);
|
|
|
|
}
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
va_list args;
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
evbuffer_add_vprintf(buf, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
evbuffer_add_printf(buf, " (%s:%d)" TR_NATIVE_EOL_STR, base, line);
|
2020-09-13 21:43:29 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const message = evbuffer_free_to_str(buf);
|
2020-09-13 21:43:29 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-12-27 22:47:25 +00:00
|
|
|
OutputDebugStringA(message.c_str());
|
2020-09-13 21:43:29 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (fp != TR_BAD_SYS_FILE)
|
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
tr_sys_file_write(fp, std::data(message), std::size(message), nullptr, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(base);
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_logAddMessage(char const* file, int line, tr_log_level level, char const* name, char const* fmt, ...)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = errno; /* message logging shouldn't affect errno */
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[1024];
|
|
|
|
va_list ap;
|
2021-11-20 21:20:45 +00:00
|
|
|
|
|
|
|
auto const lock = std::lock_guard(message_mutex_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* build the text message */
|
|
|
|
*buf = '\0';
|
|
|
|
va_start(ap, fmt);
|
2021-10-23 15:43:15 +00:00
|
|
|
int const buf_len = evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
|
2017-04-19 12:04:45 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (buf_len < 0)
|
|
|
|
{
|
2021-12-28 15:08:04 +00:00
|
|
|
errno = err;
|
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-12-13 10:34:53 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if ((size_t)buf_len < sizeof(buf) - 3)
|
2015-12-13 10:34:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
buf[buf_len + 0] = '\r';
|
|
|
|
buf[buf_len + 1] = '\n';
|
|
|
|
buf[buf_len + 2] = '\0';
|
|
|
|
OutputDebugStringA(buf);
|
|
|
|
buf[buf_len + 0] = '\0';
|
2015-12-13 10:34:53 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-12-13 10:34:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
OutputDebugStringA(buf);
|
2015-12-13 10:34:53 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-24 02:44:43 +00:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
|
|
|
|
int prio;
|
|
|
|
|
2022-01-24 03:53:49 +00:00
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case TR_LOG_ERROR:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_ERROR;
|
|
|
|
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-01-24 03:53:49 +00:00
|
|
|
default:
|
2022-01-24 02:44:43 +00:00
|
|
|
prio = ANDROID_LOG_VERBOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
__android_log_print(prio, "transmission", "%s", buf);
|
|
|
|
#else
|
|
|
|
__android_log_print(prio, "transmission", "[%s:%d] %s", file, line, buf);
|
2015-12-13 10:34:53 +00:00
|
|
|
#endif
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2022-01-24 02:44:43 +00:00
|
|
|
#else
|
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(buf))
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logGetQueueEnabled())
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
auto* const newmsg = tr_new0(tr_log_message, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
newmsg->level = level;
|
|
|
|
newmsg->when = tr_time();
|
2021-11-04 01:36:32 +00:00
|
|
|
newmsg->message = tr_strndup(buf, buf_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
newmsg->file = file;
|
|
|
|
newmsg->line = line;
|
|
|
|
newmsg->name = tr_strdup(name);
|
|
|
|
|
|
|
|
*myQueueTail = newmsg;
|
|
|
|
myQueueTail = &newmsg->next;
|
|
|
|
++myQueueLength;
|
|
|
|
|
|
|
|
if (myQueueLength > TR_LOG_MAX_QUEUE_LENGTH)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* old = myQueue;
|
|
|
|
myQueue = old->next;
|
2021-09-15 00:18:09 +00:00
|
|
|
old->next = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logFreeQueue(old);
|
|
|
|
--myQueueLength;
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(myQueueLength == TR_LOG_MAX_QUEUE_LENGTH);
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char timestr[64];
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
tr_sys_file_t fp = tr_logGetFile();
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fp == TR_BAD_SYS_FILE)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
fp = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_logGetTimeStr(timestr, sizeof(timestr));
|
|
|
|
|
2021-12-28 15:08:04 +00:00
|
|
|
auto const out = name != nullptr ? tr_strvJoin("["sv, timestr, "] "sv, name, ": "sv, buf) :
|
|
|
|
tr_strvJoin("["sv, timestr, "] "sv, buf);
|
|
|
|
tr_sys_file_write_line(fp, out, nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_flush(fp, nullptr);
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 02:44:43 +00:00
|
|
|
#endif
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = err;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|