mirror of https://github.com/oxen-io/lokinet
Merge pull request #1638 from majestrate/log-refactor-source-location-2021-05-11
refactor logging to use std::source_locationpull/1760/head
commit
12bca7031b
@ -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
|
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cpp_lib_source_location
|
||||
#include <source_location>
|
||||
namespace slns = std;
|
||||
#else
|
||||
namespace slns
|
||||
{
|
||||
struct source_location
|
||||
{
|
||||
public:
|
||||
static constexpr source_location
|
||||
current(
|
||||
const char* fileName = __builtin_FILE(),
|
||||
const char* functionName = __builtin_FUNCTION(),
|
||||
const uint_least32_t lineNumber = __builtin_LINE()) noexcept
|
||||
{
|
||||
return source_location{fileName, functionName, lineNumber};
|
||||
}
|
||||
|
||||
source_location(const source_location&) = default;
|
||||
source_location(source_location&&) = default;
|
||||
|
||||
constexpr const char*
|
||||
file_name() const noexcept
|
||||
{
|
||||
return fileName;
|
||||
}
|
||||
|
||||
constexpr const char*
|
||||
function_name() const noexcept
|
||||
{
|
||||
return functionName;
|
||||
}
|
||||
|
||||
constexpr uint_least32_t
|
||||
line() const noexcept
|
||||
{
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
private:
|
||||
constexpr explicit source_location(
|
||||
const char* fileName, const char* functionName, const uint_least32_t lineNumber) noexcept
|
||||
: fileName(fileName), functionName(functionName), lineNumber(lineNumber)
|
||||
{}
|
||||
|
||||
const char* const fileName;
|
||||
const char* const functionName;
|
||||
const uint_least32_t lineNumber;
|
||||
};
|
||||
} // namespace slns
|
||||
#endif
|
Loading…
Reference in New Issue