2013-01-25 23:34:20 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2013-01-25 23:34:20 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2013-01-25 23:34:20 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "platform.h" /* tr_lock */
|
|
|
|
#include "utils.h"
|
|
|
|
|
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;
|
|
|
|
static tr_log_message* myQueue = NULL;
|
|
|
|
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 */
|
|
|
|
static inline int IsDebuggerPresent(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline void OutputDebugStringA(void const* unused UNUSED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_level tr_logGetLevel(void)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_lock* getMessageLock(void)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_lock* l = NULL;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (l == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
l = tr_lockNew();
|
|
|
|
}
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return l;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t tr_logGetFile(void)
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const fd = tr_env_get_int("TR_DEBUG_FD", 0);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (fd)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 1:
|
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_OUT, NULL);
|
2013-01-25 23:34:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 2:
|
|
|
|
file = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR, NULL);
|
2013-01-25 23:34:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_logGetQueueEnabled(void)
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* tr_logGetQueue(void)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* ret;
|
|
|
|
tr_lockLock(getMessageLock());
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = myQueue;
|
|
|
|
myQueue = NULL;
|
|
|
|
myQueueTail = &myQueue;
|
|
|
|
myQueueLength = 0;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(getMessageLock());
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* next;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (list != NULL)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
next = list->next;
|
|
|
|
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
|
|
|
char tmp[64];
|
|
|
|
struct tm now_tm;
|
|
|
|
struct timeval tv;
|
|
|
|
time_t seconds;
|
|
|
|
int milliseconds;
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_gettimeofday(&tv);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
seconds = tv.tv_sec;
|
|
|
|
tr_localtime_r(&seconds, &now_tm);
|
|
|
|
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S.%%03d", &now_tm);
|
|
|
|
milliseconds = tv.tv_usec / 1000;
|
|
|
|
tr_snprintf(buf, buflen, tmp, milliseconds);
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return buf;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_logGetDeepEnabled(void)
|
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)
|
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
deepLoggingIsActive = 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
|
|
|
va_list args;
|
|
|
|
char timestr[64];
|
|
|
|
char* message;
|
|
|
|
size_t message_len;
|
|
|
|
struct evbuffer* buf = evbuffer_new();
|
|
|
|
char* base = tr_sys_path_basename(file, NULL);
|
|
|
|
|
|
|
|
evbuffer_add_printf(buf, "[%s] ", tr_logGetTimeStr(timestr, sizeof(timestr)));
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (name != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "%s ", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
/* FIXME (libevent2) ifdef this out for nonwindows platforms */
|
|
|
|
message = evbuffer_free_to_str(buf, &message_len);
|
|
|
|
OutputDebugStringA(message);
|
|
|
|
|
|
|
|
if (fp != TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
tr_sys_file_write(fp, message, message_len, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(message);
|
|
|
|
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];
|
|
|
|
int buf_len;
|
|
|
|
va_list ap;
|
|
|
|
tr_lockLock(getMessageLock());
|
|
|
|
|
|
|
|
/* build the text message */
|
|
|
|
*buf = '\0';
|
|
|
|
va_start(ap, fmt);
|
|
|
|
buf_len = evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (buf_len < 0)
|
|
|
|
{
|
2017-05-16 18:37:00 +00:00
|
|
|
goto finish;
|
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
|
|
|
|
2015-12-13 10:34:53 +00:00
|
|
|
#endif
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*buf != '\0')
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* newmsg;
|
|
|
|
newmsg = tr_new0(tr_log_message, 1);
|
|
|
|
newmsg->level = level;
|
|
|
|
newmsg->when = tr_time();
|
|
|
|
newmsg->message = tr_strdup(buf);
|
|
|
|
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;
|
|
|
|
old->next = NULL;
|
|
|
|
tr_logFreeQueue(old);
|
|
|
|
--myQueueLength;
|
|
|
|
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
|
|
|
tr_sys_file_t fp;
|
|
|
|
char timestr[64];
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
fp = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_logGetTimeStr(timestr, sizeof(timestr));
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (name != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_write_fmt(fp, "[%s] %s: %s" TR_NATIVE_EOL_STR, NULL, timestr, name, buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_sys_file_write_fmt(fp, "[%s] %s" TR_NATIVE_EOL_STR, NULL, timestr, buf);
|
|
|
|
}
|
2013-01-25 23:34:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_flush(fp, NULL);
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:37:00 +00:00
|
|
|
finish:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(getMessageLock());
|
|
|
|
errno = err;
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|