2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
|
|
|
#include <util/logging/logger.h>
|
|
|
|
#include <util/logging/ostream_logger.hpp>
|
2020-04-02 17:45:33 +00:00
|
|
|
#include <util/logging/logger_syslog.hpp>
|
|
|
|
#include <util/logging/file_logger.hpp>
|
|
|
|
#include <util/logging/json_logger.hpp>
|
2019-04-11 12:58:23 +00:00
|
|
|
#if defined(_WIN32)
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/win32_logger.hpp>
|
2019-04-11 12:58:23 +00:00
|
|
|
#endif
|
|
|
|
#if defined(ANDROID)
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/android_logger.hpp>
|
2019-04-11 12:58:23 +00:00
|
|
|
#endif
|
2018-05-27 13:42:55 +00:00
|
|
|
|
2020-04-02 17:45:33 +00:00
|
|
|
#include <util/str.hpp>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-05-27 13:42:55 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
using Stream_t = Win32LogStream;
|
|
|
|
#define _LOGSTREAM_INIT std::cout
|
|
|
|
#else
|
|
|
|
#if defined(ANDROID)
|
|
|
|
using Stream_t = AndroidLogStream;
|
|
|
|
#define _LOGSTREAM_INIT
|
|
|
|
#else
|
|
|
|
using Stream_t = OStreamLogStream;
|
2019-09-16 19:40:31 +00:00
|
|
|
#define _LOGSTREAM_INIT true, std::cout
|
2019-04-11 12:58:23 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-04-02 17:45:33 +00:00
|
|
|
LogType
|
|
|
|
LogTypeFromString(const std::string& str)
|
|
|
|
{
|
2020-04-07 20:41:11 +00:00
|
|
|
if (str == "unknown")
|
|
|
|
return LogType::Unknown;
|
|
|
|
else if (str == "file")
|
|
|
|
return LogType::File;
|
|
|
|
else if (str == "json")
|
|
|
|
return LogType::Json;
|
|
|
|
else if (str == "syslog")
|
|
|
|
return LogType::Syslog;
|
2020-04-02 17:45:33 +00:00
|
|
|
|
|
|
|
return LogType::Unknown;
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
LogContext::LogContext()
|
2020-04-07 18:38:56 +00:00
|
|
|
: logStream(std::make_unique<Stream_t>(_LOGSTREAM_INIT)), started(llarp::time_now_ms())
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LogContext&
|
|
|
|
LogContext::Instance()
|
|
|
|
{
|
|
|
|
static LogContext ctx;
|
|
|
|
return ctx;
|
|
|
|
}
|
2018-05-28 13:49:44 +00:00
|
|
|
|
2019-12-09 13:08:30 +00:00
|
|
|
void
|
|
|
|
LogContext::DropToRuntimeLevel()
|
|
|
|
{
|
|
|
|
curLevel = runtimeLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LogContext::RevertRuntimeLevel()
|
|
|
|
{
|
|
|
|
curLevel = startupLevel;
|
|
|
|
}
|
|
|
|
|
2019-06-10 13:20:48 +00:00
|
|
|
log_timestamp::log_timestamp() : log_timestamp("%c %Z")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
log_timestamp::log_timestamp(const char* fmt)
|
|
|
|
: format(fmt)
|
|
|
|
, now(llarp::time_now_ms())
|
|
|
|
, delta(llarp::time_now_ms() - LogContext::Instance().started)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
void
|
|
|
|
SetLogLevel(LogLevel lvl)
|
|
|
|
{
|
2019-12-09 13:08:30 +00:00
|
|
|
LogContext::Instance().curLevel = lvl;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (lvl == eLogDebug)
|
2019-12-10 15:21:52 +00:00
|
|
|
{
|
|
|
|
LogContext::Instance().runtimeLevel = lvl;
|
|
|
|
}
|
2018-05-28 13:49:44 +00:00
|
|
|
}
|
2020-04-02 16:47:56 +00:00
|
|
|
|
2020-06-08 12:42:10 +00:00
|
|
|
LogLevel
|
|
|
|
GetLogLevel()
|
|
|
|
{
|
|
|
|
return LogContext::Instance().curLevel;
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:47:56 +00:00
|
|
|
void
|
|
|
|
LogContext::ImmediateFlush()
|
|
|
|
{
|
|
|
|
logStream->ImmediateFlush();
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:45:33 +00:00
|
|
|
void
|
2020-04-07 20:41:11 +00:00
|
|
|
LogContext::Initialize(
|
|
|
|
LogLevel level,
|
|
|
|
LogType type,
|
|
|
|
const std::string& file,
|
|
|
|
const std::string& nickname,
|
|
|
|
std::shared_ptr<thread::ThreadPool> threadpool)
|
2020-04-02 17:45:33 +00:00
|
|
|
{
|
|
|
|
SetLogLevel(level);
|
|
|
|
nodeName = nickname;
|
|
|
|
|
|
|
|
FILE* logfile = nullptr;
|
2020-04-07 23:52:14 +00:00
|
|
|
if (file == "stdout" or file.empty())
|
2020-04-02 17:45:33 +00:00
|
|
|
{
|
|
|
|
logfile = stdout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logfile = ::fopen(file.c_str(), "a");
|
|
|
|
if (not logfile)
|
|
|
|
{
|
2020-04-07 20:41:11 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
stringify("could not open logfile ", file, ", errno: ", strerror(errno)));
|
2020-04-02 17:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case LogType::Unknown:
|
2020-04-07 23:52:14 +00:00
|
|
|
// tolerate as fallback to LogType::File
|
2020-04-02 17:45:33 +00:00
|
|
|
|
|
|
|
case LogType::File:
|
|
|
|
if (logfile != stdout)
|
|
|
|
{
|
|
|
|
LogInfo("Switching logger to file ", file);
|
|
|
|
std::cout << std::flush;
|
|
|
|
|
|
|
|
LogContext::Instance().logStream =
|
2020-04-07 20:41:11 +00:00
|
|
|
std::make_unique<FileLogStream>(threadpool, logfile, 100ms, true);
|
2020-04-02 17:45:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogInfo("Logger remains stdout");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case LogType::Json:
|
|
|
|
LogInfo("Switching logger to JSON with file: ", file);
|
|
|
|
std::cout << std::flush;
|
|
|
|
|
2020-04-07 20:41:11 +00:00
|
|
|
LogContext::Instance().logStream =
|
|
|
|
std::make_unique<JSONLogStream>(threadpool, logfile, 100ms, logfile != stdout);
|
2020-04-02 17:45:33 +00:00
|
|
|
break;
|
|
|
|
case LogType::Syslog:
|
|
|
|
if (logfile)
|
|
|
|
{
|
|
|
|
// TODO: this logic should be handled in Config
|
|
|
|
// TODO: this won't even work because of default value for 'file' (== "stdout")
|
|
|
|
::fclose(logfile);
|
|
|
|
throw std::invalid_argument("Cannot mix log type=syslog and file=*");
|
|
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
|
|
throw std::runtime_error("syslog not supported on win32");
|
|
|
|
#else
|
|
|
|
LogInfo("Switching logger to syslog");
|
|
|
|
std::cout << std::flush;
|
2020-04-07 20:41:11 +00:00
|
|
|
LogContext::Instance().logStream = std::make_unique<SysLogStream>();
|
2020-04-02 17:45:33 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 13:07:49 +00:00
|
|
|
void
|
|
|
|
SilenceLog(std::function<void(void)> func)
|
|
|
|
{
|
|
|
|
auto& log = LogContext::Instance();
|
|
|
|
ILogStream_ptr oldStream = std::move(log.logStream);
|
|
|
|
log.logStream = nullptr;
|
|
|
|
func();
|
|
|
|
log.logStream = std::move(oldStream);
|
|
|
|
}
|
|
|
|
|
2018-07-09 03:34:29 +00:00
|
|
|
} // namespace llarp
|
2018-06-23 14:52:38 +00:00
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
extern "C"
|
2018-06-23 14:52:38 +00:00
|
|
|
{
|
2018-08-02 00:48:43 +00:00
|
|
|
void
|
|
|
|
cSetLogLevel(LogLevel lvl)
|
|
|
|
{
|
|
|
|
llarp::SetLogLevel((llarp::LogLevel)lvl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cSetLogNodeName(const char* name)
|
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
llarp::LogContext::Instance().nodeName = name;
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
2018-12-12 02:52:51 +00:00
|
|
|
}
|