mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-31 09:20:21 +00:00
refactor logging to use std::source_location
* use std::source_location instead of godawful macros in logging * remove unused/absolutely haram af json logstream * fix bug in android logger where it doesn't respect eLogNone
This commit is contained in:
parent
e32f7faf55
commit
5c457ff486
@ -2,7 +2,6 @@ function(add_log_tag target)
|
|||||||
get_target_property(TARGET_SRCS ${target} SOURCES)
|
get_target_property(TARGET_SRCS ${target} SOURCES)
|
||||||
foreach(F ${TARGET_SRCS})
|
foreach(F ${TARGET_SRCS})
|
||||||
get_filename_component(fpath "${F}" ABSOLUTE)
|
get_filename_component(fpath "${F}" ABSOLUTE)
|
||||||
string(REPLACE "${PROJECT_SOURCE_DIR}/" "" logtag "${fpath}")
|
set_property(SOURCE ${F} APPEND PROPERTY COMPILE_DEFINITIONS SOURCE_ROOT=\"${PROJECT_SOURCE_DIR}\")
|
||||||
set_property(SOURCE ${F} APPEND PROPERTY COMPILE_DEFINITIONS LOG_TAG=\"${logtag}\")
|
|
||||||
endforeach()
|
endforeach()
|
||||||
endfunction()
|
endfunction()
|
||||||
|
@ -496,7 +496,7 @@ lokinet_main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
{
|
{
|
||||||
LogError("cannot generate config at ", *configFile, ": ", ex.what());
|
llarp::LogError("cannot generate config at ", *configFile, ": ", ex.what());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -512,7 +512,7 @@ lokinet_main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
{
|
{
|
||||||
LogError("cannot check if ", *configFile, " exists: ", ex.what());
|
llarp::LogError("cannot check if ", *configFile, " exists: ", ex.what());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -574,7 +574,7 @@ lokinet_main(int argc, char* argv[])
|
|||||||
"file a bug report now or be cursed with this "
|
"file a bug report now or be cursed with this "
|
||||||
"annoying image in your syslog for all time."})
|
"annoying image in your syslog for all time."})
|
||||||
{
|
{
|
||||||
LogError(wtf);
|
llarp::LogError{wtf};
|
||||||
llarp::LogContext::Instance().ImmediateFlush();
|
llarp::LogContext::Instance().ImmediateFlush();
|
||||||
}
|
}
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -10,7 +10,6 @@ add_library(lokinet-util
|
|||||||
util/logging/android_logger.cpp
|
util/logging/android_logger.cpp
|
||||||
util/logging/buffer.cpp
|
util/logging/buffer.cpp
|
||||||
util/logging/file_logger.cpp
|
util/logging/file_logger.cpp
|
||||||
util/logging/json_logger.cpp
|
|
||||||
util/logging/logger.cpp
|
util/logging/logger.cpp
|
||||||
util/logging/logger_internal.cpp
|
util/logging/logger_internal.cpp
|
||||||
util/logging/loglevel.cpp
|
util/logging/loglevel.cpp
|
||||||
|
@ -1048,7 +1048,6 @@ namespace llarp
|
|||||||
Comment{
|
Comment{
|
||||||
"Log type (format). Valid options are:",
|
"Log type (format). Valid options are:",
|
||||||
" file - plaintext formatting",
|
" file - plaintext formatting",
|
||||||
" json - json-formatted log statements",
|
|
||||||
" syslog - logs directed to syslog",
|
" syslog - logs directed to syslog",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -117,16 +117,16 @@ namespace llarp
|
|||||||
{
|
{
|
||||||
if (!qd.Decode(buf))
|
if (!qd.Decode(buf))
|
||||||
{
|
{
|
||||||
llarp::LogError("failed to decode question");
|
LogError("failed to decode question");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
llarp::LogDebug(qd);
|
LogDebug("dns question: ", qd);
|
||||||
}
|
}
|
||||||
for (auto& an : answers)
|
for (auto& an : answers)
|
||||||
{
|
{
|
||||||
if (not an.Decode(buf))
|
if (not an.Decode(buf))
|
||||||
{
|
{
|
||||||
llarp::LogDebug("failed to decode answer");
|
LogDebug("failed to decode answer");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <llarp/util/buffer.hpp>
|
#include <llarp/util/buffer.hpp>
|
||||||
#include <llarp/util/logging/file_logger.hpp>
|
#include <llarp/util/logging/file_logger.hpp>
|
||||||
#include <llarp/util/logging/json_logger.hpp>
|
|
||||||
#include <llarp/util/logging/logger_syslog.hpp>
|
#include <llarp/util/logging/logger_syslog.hpp>
|
||||||
#include <llarp/util/logging/logger.hpp>
|
#include <llarp/util/logging/logger.hpp>
|
||||||
#include <llarp/util/meta/memfn.hpp>
|
#include <llarp/util/meta/memfn.hpp>
|
||||||
|
@ -75,7 +75,7 @@ namespace llarp
|
|||||||
{
|
{
|
||||||
if (!item.BDecode(buf))
|
if (!item.BDecode(buf))
|
||||||
{
|
{
|
||||||
llarp::LogWarnTag("llarp/bencode.hpp", "failed to decode key ", k, " for entry in dict");
|
llarp::LogWarn("failed to decode key ", k, " for entry in dict");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ namespace llarp
|
|||||||
uint64_t read_i;
|
uint64_t read_i;
|
||||||
if (!bencode_read_integer(buf, &read_i))
|
if (!bencode_read_integer(buf, &read_i))
|
||||||
{
|
{
|
||||||
llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k, " for integer in dict");
|
llarp::LogWarn("failed to decode key ", k, " for integer in dict");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,8 +210,7 @@ namespace llarp
|
|||||||
return true;
|
return true;
|
||||||
if (sink.DecodeKey(*key, buffer))
|
if (sink.DecodeKey(*key, buffer))
|
||||||
return true;
|
return true;
|
||||||
llarp::LogWarnTag(
|
llarp::LogWarn("undefined key '", *key->cur, "' for entry in dict");
|
||||||
"llarp/bencode.hpp", "undefined key '", *key->cur, "' for entry in dict");
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
@ -8,7 +8,8 @@ namespace llarp
|
|||||||
{
|
{
|
||||||
void
|
void
|
||||||
AndroidLogStream::PreLog(
|
AndroidLogStream::PreLog(
|
||||||
std::stringstream& ss, LogLevel lvl, const char* fname, int lineno, const std::string&) const
|
std::stringstream& ss, LogLevel lvl, std::string_view fname, int lineno, const std::string&)
|
||||||
|
const
|
||||||
{
|
{
|
||||||
switch (lvl)
|
switch (lvl)
|
||||||
{
|
{
|
||||||
@ -43,7 +44,7 @@ namespace llarp
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
void
|
void
|
||||||
AndroidLogStream::Print(LogLevel lvl, const char* tag, const std::string& msg)
|
AndroidLogStream::Print(LogLevel lvl, std::string_view tag, const std::string& msg)
|
||||||
{
|
{
|
||||||
std::string str("lokinet|");
|
std::string str("lokinet|");
|
||||||
str += tag;
|
str += tag;
|
||||||
@ -62,6 +63,8 @@ namespace llarp
|
|||||||
case eLogError:
|
case eLogError:
|
||||||
__android_log_write(ANDROID_LOG_ERROR, str.c_str(), msg.c_str());
|
__android_log_write(ANDROID_LOG_ERROR, str.c_str(), msg.c_str());
|
||||||
return;
|
return;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace llarp
|
} // namespace llarp
|
||||||
|
@ -12,12 +12,12 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& s,
|
std::stringstream& s,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const override;
|
const std::string& nodename) const override;
|
||||||
|
|
||||||
void
|
void
|
||||||
Print(LogLevel lvl, const char* filename, const std::string& msg) override;
|
Print(LogLevel lvl, std::string_view filename, const std::string& msg) override;
|
||||||
|
|
||||||
void
|
void
|
||||||
PostLog(std::stringstream&) const override;
|
PostLog(std::stringstream&) const override;
|
||||||
|
@ -65,18 +65,18 @@ namespace llarp
|
|||||||
FileLogStream::PreLog(
|
FileLogStream::PreLog(
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const
|
const std::string& nodename) const
|
||||||
{
|
{
|
||||||
ss << "[" << LogLevelToString(lvl) << "] ";
|
ss << "[" << LogLevelToString(lvl) << "] ";
|
||||||
ss << "[" << nodename << "]"
|
ss << "[" << nodename << "]"
|
||||||
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
|
||||||
<< "\t";
|
<< "\t";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
FileLogStream::Print(LogLevel, const char*, const std::string& msg)
|
FileLogStream::Print(LogLevel, std::string_view, const std::string& msg)
|
||||||
{
|
{
|
||||||
m_Lines.pushBack(msg);
|
m_Lines.pushBack(msg);
|
||||||
}
|
}
|
||||||
@ -84,12 +84,12 @@ namespace llarp
|
|||||||
void
|
void
|
||||||
FileLogStream::AppendLog(
|
FileLogStream::AppendLog(
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename,
|
const std::string& nodename,
|
||||||
const std::string msg)
|
const std::string msg)
|
||||||
{
|
{
|
||||||
ILogStream::AppendLog(lvl, fname, lineno, nodename, msg);
|
ILogStream::AppendLog(lvl, filename, lineno, nodename, msg);
|
||||||
Tick(llarp::time_now_ms());
|
Tick(llarp::time_now_ms());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +23,12 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& out,
|
std::stringstream& out,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const override;
|
const std::string& nodename) const override;
|
||||||
|
|
||||||
void
|
void
|
||||||
Print(LogLevel, const char*, const std::string& msg) override;
|
Print(LogLevel, std::string_view filename, const std::string& msg) override;
|
||||||
|
|
||||||
void
|
void
|
||||||
Tick(llarp_time_t now) override;
|
Tick(llarp_time_t now) override;
|
||||||
@ -39,7 +39,7 @@ namespace llarp
|
|||||||
void
|
void
|
||||||
AppendLog(
|
AppendLog(
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename,
|
const std::string& nodename,
|
||||||
const std::string msg) override;
|
const std::string msg) override;
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
#include "json_logger.hpp"
|
|
||||||
#include <llarp/util/json.hpp>
|
|
||||||
|
|
||||||
namespace llarp
|
|
||||||
{
|
|
||||||
void
|
|
||||||
JSONLogStream::AppendLog(
|
|
||||||
LogLevel lvl,
|
|
||||||
const char* fname,
|
|
||||||
int lineno,
|
|
||||||
const std::string& nodename,
|
|
||||||
const std::string msg)
|
|
||||||
{
|
|
||||||
json::Object obj;
|
|
||||||
obj["time"] = to_json(llarp::time_now_ms());
|
|
||||||
obj["nickname"] = nodename;
|
|
||||||
obj["file"] = std::string(fname);
|
|
||||||
obj["line"] = lineno;
|
|
||||||
obj["level"] = LogLevelToString(lvl);
|
|
||||||
obj["message"] = msg;
|
|
||||||
m_Lines.pushBack(obj.dump());
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace llarp
|
|
@ -1,28 +0,0 @@
|
|||||||
#ifndef LLARP_UTIL_JSON_LOGGER
|
|
||||||
#define LLARP_UTIL_JSON_LOGGER
|
|
||||||
|
|
||||||
#include "file_logger.hpp"
|
|
||||||
|
|
||||||
namespace llarp
|
|
||||||
{
|
|
||||||
struct JSONLogStream : public FileLogStream
|
|
||||||
{
|
|
||||||
JSONLogStream(
|
|
||||||
std::function<void(FileLogStream::Work_t)> disk,
|
|
||||||
FILE* f,
|
|
||||||
llarp_time_t flushInterval,
|
|
||||||
bool closeFile)
|
|
||||||
: FileLogStream(std::move(disk), f, flushInterval, closeFile)
|
|
||||||
{}
|
|
||||||
|
|
||||||
void
|
|
||||||
AppendLog(
|
|
||||||
LogLevel lvl,
|
|
||||||
const char* fname,
|
|
||||||
int lineno,
|
|
||||||
const std::string& nodename,
|
|
||||||
const std::string msg) override;
|
|
||||||
};
|
|
||||||
} // namespace llarp
|
|
||||||
|
|
||||||
#endif
|
|
@ -2,7 +2,6 @@
|
|||||||
#include "ostream_logger.hpp"
|
#include "ostream_logger.hpp"
|
||||||
#include "logger_syslog.hpp"
|
#include "logger_syslog.hpp"
|
||||||
#include "file_logger.hpp"
|
#include "file_logger.hpp"
|
||||||
#include "json_logger.hpp"
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include "win32_logger.hpp"
|
#include "win32_logger.hpp"
|
||||||
#endif
|
#endif
|
||||||
@ -36,8 +35,6 @@ namespace llarp
|
|||||||
return LogType::Unknown;
|
return LogType::Unknown;
|
||||||
else if (str == "file")
|
else if (str == "file")
|
||||||
return LogType::File;
|
return LogType::File;
|
||||||
else if (str == "json")
|
|
||||||
return LogType::Json;
|
|
||||||
else if (str == "syslog")
|
else if (str == "syslog")
|
||||||
return LogType::Syslog;
|
return LogType::Syslog;
|
||||||
|
|
||||||
@ -111,7 +108,7 @@ namespace llarp
|
|||||||
{
|
{
|
||||||
logfile = stdout;
|
logfile = stdout;
|
||||||
}
|
}
|
||||||
else
|
else if (type != LogType::Syslog)
|
||||||
{
|
{
|
||||||
logfile = ::fopen(file.c_str(), "a");
|
logfile = ::fopen(file.c_str(), "a");
|
||||||
if (not logfile)
|
if (not logfile)
|
||||||
@ -141,26 +138,10 @@ namespace llarp
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case LogType::Json:
|
|
||||||
LogInfo("Switching logger to JSON with file: ", file);
|
|
||||||
std::cout << std::flush;
|
|
||||||
|
|
||||||
LogContext::Instance().logStream =
|
|
||||||
std::make_unique<JSONLogStream>(io, logfile, 100ms, logfile != stdout);
|
|
||||||
break;
|
|
||||||
case LogType::Syslog:
|
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)
|
#if defined(_WIN32)
|
||||||
throw std::runtime_error("syslog not supported on win32");
|
throw std::runtime_error("syslog not supported on win32");
|
||||||
#else
|
#else
|
||||||
LogInfo("Switching logger to syslog");
|
|
||||||
std::cout << std::flush;
|
|
||||||
LogContext::Instance().logStream = std::make_unique<SysLogStream>();
|
LogContext::Instance().logStream = std::make_unique<SysLogStream>();
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <llarp/util/str.hpp>
|
||||||
#include <llarp/util/time.hpp>
|
#include <llarp/util/time.hpp>
|
||||||
#include "logstream.hpp"
|
#include "logstream.hpp"
|
||||||
#include "logger_internal.hpp"
|
#include "logger_internal.hpp"
|
||||||
|
#include "source_location.hpp"
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp
|
||||||
{
|
{
|
||||||
@ -11,7 +13,6 @@ namespace llarp
|
|||||||
{
|
{
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
File,
|
File,
|
||||||
Json,
|
|
||||||
Syslog,
|
Syslog,
|
||||||
};
|
};
|
||||||
LogType
|
LogType
|
||||||
@ -80,7 +81,7 @@ namespace llarp
|
|||||||
/** internal */
|
/** internal */
|
||||||
template <typename... TArgs>
|
template <typename... TArgs>
|
||||||
inline static void
|
inline static void
|
||||||
_log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
|
_log(LogLevel lvl, const slns::source_location& location, TArgs&&... args) noexcept
|
||||||
{
|
{
|
||||||
auto& log = LogContext::Instance();
|
auto& log = LogContext::Instance();
|
||||||
if (log.curLevel > lvl || log.logStream == nullptr)
|
if (log.curLevel > lvl || log.logStream == nullptr)
|
||||||
@ -88,41 +89,80 @@ namespace llarp
|
|||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
if constexpr (sizeof...(args) > 0)
|
if constexpr (sizeof...(args) > 0)
|
||||||
LogAppend(ss, std::forward<TArgs>(args)...);
|
LogAppend(ss, std::forward<TArgs>(args)...);
|
||||||
log.logStream->AppendLog(lvl, fname, lineno, log.nodeName, ss.str());
|
log.logStream->AppendLog(
|
||||||
|
lvl,
|
||||||
|
strip_prefix(location.file_name(), SOURCE_ROOT),
|
||||||
|
location.line(),
|
||||||
|
log.nodeName,
|
||||||
|
ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void
|
template <typename... T>
|
||||||
_log_noop() noexcept
|
struct LogTrace
|
||||||
{}
|
{
|
||||||
|
LogTrace(
|
||||||
|
[[maybe_unused]] T... args,
|
||||||
|
[[maybe_unused]] const slns::source_location& location = slns::source_location::current())
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
_log(eLogTrace, location, std::forward<T>(args)...);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
LogTrace(T&&...) -> LogTrace<T...>;
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
struct LogDebug
|
||||||
|
{
|
||||||
|
LogDebug(
|
||||||
|
[[maybe_unused]] T... args,
|
||||||
|
[[maybe_unused]] const slns::source_location& location = slns::source_location::current())
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
_log(eLogDebug, location, std::forward<T>(args)...);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
LogDebug(T&&...) -> LogDebug<T...>;
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
struct LogInfo
|
||||||
|
{
|
||||||
|
LogInfo(T... args, const slns::source_location& location = slns::source_location::current())
|
||||||
|
{
|
||||||
|
_log(eLogInfo, location, std::forward<T>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
LogInfo(T&&...) -> LogInfo<T...>;
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
struct LogWarn
|
||||||
|
{
|
||||||
|
LogWarn(T... args, const slns::source_location& location = slns::source_location::current())
|
||||||
|
{
|
||||||
|
_log(eLogWarn, location, std::forward<T>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
LogWarn(T&&...) -> LogWarn<T...>;
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
struct LogError
|
||||||
|
{
|
||||||
|
LogError(T... args, const slns::source_location& location = slns::source_location::current())
|
||||||
|
{
|
||||||
|
_log(eLogError, location, std::forward<T>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
LogError(T&&...) -> LogError<T...>;
|
||||||
|
|
||||||
} // namespace llarp
|
} // namespace llarp
|
||||||
|
|
||||||
#define LogDebug(...) _log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogInfo(...) _log(llarp::eLogInfo, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogWarn(...) _log(llarp::eLogWarn, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogError(...) _log(llarp::eLogError, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define LogDebugTag(tag, ...) _log(llarp::eLogDebug, tag, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogInfoTag(tag, ...) _log(llarp::eLogInfo, tag, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogWarnTag(tag, ...) _log(llarp::eLogWarn, tag, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogErrorTag(tag, ...) _log(llarp::eLogError, tag, __LINE__, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define LogDebugExplicit(tag, line, ...) _log(llarp::eLogDebug, tag, line, __VA_ARGS__)
|
|
||||||
#define LogInfoExplicit(tag, line, ...) _log(llarp::eLogInfo, tag, line __VA_ARGS__)
|
|
||||||
#define LogWarnExplicit(tag, line, ...) _log(llarp::eLogWarn, tag, line, __VA_ARGS__)
|
|
||||||
#define LogErrorExplicit(tag, line, ...) _log(llarp::eLogError, tag, line, __VA_ARGS__)
|
|
||||||
|
|
||||||
// null-op Trace logging if this is a release build
|
|
||||||
#ifdef NDEBUG
|
|
||||||
#define LogTrace(...) _log_noop()
|
|
||||||
#define LogTraceTag(tag, ...) _log_noop()
|
|
||||||
#define LogTraceExplicit(tag, line, ...) _log_noop()
|
|
||||||
#else
|
|
||||||
#define LogTrace(...) _log(llarp::eLogTrace, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogTraceTag(tag, ...) _log(llarp::eLogTrace, tag, __LINE__, __VA_ARGS__)
|
|
||||||
#define LogTraceExplicit(tag, line, ...) _log(llarp::eLogTrace, tag, line, __VA_ARGS__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LOG_TAG
|
|
||||||
#define LOG_TAG "default"
|
|
||||||
#endif
|
|
||||||
|
@ -11,12 +11,12 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& s,
|
std::stringstream& s,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const override;
|
const std::string& nodename) const override;
|
||||||
|
|
||||||
void
|
void
|
||||||
Print(LogLevel lvl, const char* tag, const std::string& msg) override;
|
Print(LogLevel lvl, std::string_view tag, const std::string& msg) override;
|
||||||
|
|
||||||
void
|
void
|
||||||
PostLog(std::stringstream& ss) const override;
|
PostLog(std::stringstream& ss) const override;
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp
|
||||||
{
|
{
|
||||||
@ -18,12 +19,12 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& out,
|
std::stringstream& out,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const = 0;
|
const std::string& nodename) const = 0;
|
||||||
|
|
||||||
virtual void
|
virtual void
|
||||||
Print(LogLevel lvl, const char* filename, const std::string& msg) = 0;
|
Print(LogLevel lvl, std::string_view filename, const std::string& msg) = 0;
|
||||||
|
|
||||||
virtual void
|
virtual void
|
||||||
PostLog(std::stringstream& out) const = 0;
|
PostLog(std::stringstream& out) const = 0;
|
||||||
@ -31,16 +32,16 @@ namespace llarp
|
|||||||
virtual void
|
virtual void
|
||||||
AppendLog(
|
AppendLog(
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename,
|
const std::string& nodename,
|
||||||
const std::string msg)
|
const std::string msg)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
PreLog(ss, lvl, fname, lineno, nodename);
|
PreLog(ss, lvl, filename, lineno, nodename);
|
||||||
ss << msg;
|
ss << msg;
|
||||||
PostLog(ss);
|
PostLog(ss);
|
||||||
Print(lvl, fname, ss.str());
|
Print(lvl, filename, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A blocking call to flush to disk. Should only be called in rare circumstances.
|
/// A blocking call to flush to disk. Should only be called in rare circumstances.
|
||||||
|
@ -11,7 +11,7 @@ namespace llarp
|
|||||||
OStreamLogStream::PreLog(
|
OStreamLogStream::PreLog(
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const
|
const std::string& nodename) const
|
||||||
{
|
{
|
||||||
@ -38,7 +38,7 @@ namespace llarp
|
|||||||
}
|
}
|
||||||
ss << "[" << LogLevelToString(lvl) << "] ";
|
ss << "[" << LogLevelToString(lvl) << "] ";
|
||||||
ss << "[" << nodename << "]"
|
ss << "[" << nodename << "]"
|
||||||
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
|
||||||
<< "\t";
|
<< "\t";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ namespace llarp
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
OStreamLogStream::Print(LogLevel, const char*, const std::string& msg)
|
OStreamLogStream::Print(LogLevel, std::string_view, const std::string& msg)
|
||||||
{
|
{
|
||||||
m_Out << msg << std::flush;
|
m_Out << msg << std::flush;
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,12 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& s,
|
std::stringstream& s,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const override;
|
const std::string& nodename) const override;
|
||||||
|
|
||||||
virtual void
|
virtual void
|
||||||
Print(LogLevel lvl, const char* tag, const std::string& msg) override;
|
Print(LogLevel lvl, std::string_view tag, const std::string& msg) override;
|
||||||
|
|
||||||
void
|
void
|
||||||
PostLog(std::stringstream& ss) const override;
|
PostLog(std::stringstream& ss) const override;
|
||||||
|
9
llarp/util/logging/source_location.hpp
Normal file
9
llarp/util/logging/source_location.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cpp_lib_source_location
|
||||||
|
#include <source_location>
|
||||||
|
namespace slns = std;
|
||||||
|
#else
|
||||||
|
#include <experimental/source_location>
|
||||||
|
namespace slns = std::experimental;
|
||||||
|
#endif
|
@ -10,18 +10,18 @@ namespace llarp
|
|||||||
SysLogStream::PreLog(
|
SysLogStream::PreLog(
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const
|
const std::string& nodename) const
|
||||||
{
|
{
|
||||||
ss << "[" << LogLevelToString(lvl) << "] ";
|
ss << "[" << LogLevelToString(lvl) << "] ";
|
||||||
ss << "[" << nodename << "]"
|
ss << "[" << nodename << "]"
|
||||||
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
|
||||||
<< "\t";
|
<< "\t";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SysLogStream::Print(LogLevel lvl, const char*, const std::string& msg)
|
SysLogStream::Print(LogLevel lvl, std::string_view, const std::string& msg)
|
||||||
{
|
{
|
||||||
switch (lvl)
|
switch (lvl)
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,7 @@ namespace llarp
|
|||||||
Win32LogStream::PreLog(
|
Win32LogStream::PreLog(
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const
|
const std::string& nodename) const
|
||||||
{
|
{
|
||||||
@ -54,11 +54,11 @@ namespace llarp
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ss << "[" << nodename << "]"
|
ss << "[" << nodename << "]"
|
||||||
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
|
||||||
<< "\t";
|
<< "\t";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
OStreamLogStream::PreLog(ss, lvl, fname, lineno, nodename);
|
OStreamLogStream::PreLog(ss, lvl, filename, lineno, nodename);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -71,7 +71,7 @@ namespace llarp
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Win32LogStream::Print(LogLevel lvl, const char*, const std::string& msg)
|
Win32LogStream::Print(LogLevel lvl, std::string_view, const std::string& msg)
|
||||||
{
|
{
|
||||||
if (!isConsoleModern)
|
if (!isConsoleModern)
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,7 @@ namespace llarp
|
|||||||
PreLog(
|
PreLog(
|
||||||
std::stringstream& s,
|
std::stringstream& s,
|
||||||
LogLevel lvl,
|
LogLevel lvl,
|
||||||
const char* fname,
|
std::string_view filename,
|
||||||
int lineno,
|
int lineno,
|
||||||
const std::string& nodename) const override;
|
const std::string& nodename) const override;
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ namespace llarp
|
|||||||
void Tick(llarp_time_t) override{};
|
void Tick(llarp_time_t) override{};
|
||||||
|
|
||||||
void
|
void
|
||||||
Print(LogLevel lvl, const char*, const std::string& msg) override;
|
Print(LogLevel lvl, std::string_view, const std::string& msg) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::ostream& m_Out;
|
std::ostream& m_Out;
|
||||||
|
@ -70,6 +70,15 @@ namespace llarp
|
|||||||
return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix;
|
return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// removes a prefix from a string if it exists
|
||||||
|
inline std::string_view
|
||||||
|
strip_prefix(std::string_view str, std::string_view prefix)
|
||||||
|
{
|
||||||
|
if (starts_with(str, prefix))
|
||||||
|
return str.substr(prefix.size());
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
/// Splits a string on some delimiter string and returns a vector of string_view's pointing into
|
/// Splits a string on some delimiter string and returns a vector of string_view's pointing into
|
||||||
/// the pieces of the original string. The pieces are valid only as long as the original string
|
/// the pieces of the original string. The pieces are valid only as long as the original string
|
||||||
/// remains valid. Leading and trailing empty substrings are not removed. If delim is empty you
|
/// remains valid. Leading and trailing empty substrings are not removed. If delim is empty you
|
||||||
|
@ -205,7 +205,7 @@ namespace llarp::vpn
|
|||||||
NetSH(std::string commands)
|
NetSH(std::string commands)
|
||||||
{
|
{
|
||||||
commands = NetSHCommand() + " interface IPv6 " + commands;
|
commands = NetSHCommand() + " interface IPv6 " + commands;
|
||||||
LogInfo(commands);
|
LogInfo("exec: ", commands);
|
||||||
::system(commands.c_str());
|
::system(commands.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,4 +18,4 @@ pybind11_add_module(pyllarp MODULE
|
|||||||
)
|
)
|
||||||
target_link_libraries(pyllarp PUBLIC liblokinet)
|
target_link_libraries(pyllarp PUBLIC liblokinet)
|
||||||
target_include_directories(pyllarp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(pyllarp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
add_log_tag(pyllarp)
|
||||||
|
@ -53,7 +53,7 @@ add_executable(testAll
|
|||||||
|
|
||||||
target_link_libraries(testAll PUBLIC liblokinet Catch2::Catch2)
|
target_link_libraries(testAll PUBLIC liblokinet Catch2::Catch2)
|
||||||
target_include_directories(testAll PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(testAll PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
add_log_tag(testAll)
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_sources(testAll PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/win32/test.rc")
|
target_sources(testAll PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/win32/test.rc")
|
||||||
target_link_libraries(testAll PUBLIC ws2_32 iphlpapi shlwapi)
|
target_link_libraries(testAll PUBLIC ws2_32 iphlpapi shlwapi)
|
||||||
|
@ -57,7 +57,7 @@ TEST_CASE("BEncode", "[RouterVersion]")
|
|||||||
CHECK(v1235.BEncode(&buf));
|
CHECK(v1235.BEncode(&buf));
|
||||||
|
|
||||||
std::string s((const char*)buf.begin(), (buf.end() - buf.begin()));
|
std::string s((const char*)buf.begin(), (buf.end() - buf.begin()));
|
||||||
LogInfo("bencoded: ", buf.begin());
|
llarp::LogInfo("bencoded: ", buf.begin());
|
||||||
|
|
||||||
CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee"));
|
CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user