mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-21 13:06:15 +02:00
update aixlog
This commit is contained in:
parent
72cb5a0e7e
commit
fe1b227319
3 changed files with 201 additions and 158 deletions
|
@ -157,8 +157,8 @@ int main (int argc, char **argv)
|
||||||
|
|
||||||
Log::init(
|
Log::init(
|
||||||
{
|
{
|
||||||
make_shared<LogSinkCout>(debugSwitch.isSet()?(LogPriority::debug):(LogPriority::info), LogSink::Type::all, debugSwitch.isSet()?"%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)":"%Y-%m-%d %H-%M-%S [#prio]"),
|
make_shared<LogSinkCout>(debugSwitch.isSet()?(LogSeverity::trace):(LogSeverity::info), LogSink::Type::all, debugSwitch.isSet()?"%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)":"%Y-%m-%d %H-%M-%S [#prio]"),
|
||||||
make_shared<LogSinkNative>("snapclient", LogPriority::debug, LogSink::Type::special)
|
make_shared<LogSinkNative>("snapclient", LogSeverity::trace, LogSink::Type::special)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
325
common/log.h
325
common/log.h
|
@ -3,7 +3,7 @@
|
||||||
/ _\ ( )( \/ )( ) / \ / __)
|
/ _\ ( )( \/ )( ) / \ / __)
|
||||||
/ \ )( ) ( / (_/\( O )( (_ \
|
/ \ )( ) ( / (_/\( O )( (_ \
|
||||||
\_/\_/(__)(_/\_)\____/ \__/ \___/
|
\_/\_/(__)(_/\_)\____/ \__/ \___/
|
||||||
version 0.6.0
|
version 0.7.0
|
||||||
https://github.com/badaix/aixlog
|
https://github.com/badaix/aixlog
|
||||||
|
|
||||||
This file is part of aixlog
|
This file is part of aixlog
|
||||||
|
@ -32,6 +32,10 @@
|
||||||
#ifndef AIX_LOG_HPP
|
#ifndef AIX_LOG_HPP
|
||||||
#define AIX_LOG_HPP
|
#define AIX_LOG_HPP
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#define _HAS_SYSLOG_
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
@ -48,17 +52,18 @@
|
||||||
#endif
|
#endif
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#else
|
#endif
|
||||||
|
#ifdef _HAS_SYSLOG_
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/// Internal helper defines
|
/// Internal helper defines
|
||||||
#define LOG_WO_TAG(P) std::clog << (LogPriority)P << Tag(__func__) << LogType::normal
|
#define LOG_WO_TAG(P) std::clog << (LogSeverity)P << Tag(__func__) << LogType::normal
|
||||||
#define SLOG_WO_TAG(P) std::clog << (LogPriority)P << Tag(__func__) << LogType::special
|
#define SLOG_WO_TAG(P) std::clog << (LogSeverity)P << Tag(__func__) << LogType::special
|
||||||
|
|
||||||
#define LOG_TAG(P, T) std::clog << (LogPriority)P << Tag(T) << LogType::normal
|
#define LOG_TAG(P, T) std::clog << (LogSeverity)P << Tag(T) << LogType::normal
|
||||||
#define SLOG_TAG(P, T) std::clog << (LogPriority)P << Tag(T) << LogType::special
|
#define SLOG_TAG(P, T) std::clog << (LogSeverity)P << Tag(T) << LogType::special
|
||||||
|
|
||||||
#define LOG_X(x,P,T,FUNC, ...) FUNC
|
#define LOG_X(x,P,T,FUNC, ...) FUNC
|
||||||
#define SLOG_X(x,P,T,FUNC, ...) FUNC
|
#define SLOG_X(x,P,T,FUNC, ...) FUNC
|
||||||
|
@ -80,29 +85,43 @@ enum class LogType
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
enum Priority
|
enum Severity
|
||||||
{
|
{
|
||||||
EMERG = 0, // 0 system is unusable
|
// https://chromium.googlesource.com/chromium/mini_chromium/+/master/base/logging.cc
|
||||||
ALERT = 1, // 1 action must be taken immediately
|
|
||||||
CRIT = 2, // 2 critical conditions
|
// Aixlog Boost Syslog Android macOS Syslog Desc
|
||||||
ERROR = 3, // 3 error conditions
|
//
|
||||||
WARNING = 4, // 4 warning conditions
|
// UNKNOWN
|
||||||
NOTICE = 5, // 5 normal, but significant, condition
|
// DEFAULT
|
||||||
INFO = 6, // 6 informational message
|
// trace trace VERBOSE
|
||||||
DEBUG = 7 // 7 debug-level message
|
// debug debug DEBUG DEBUG DEBUG debug-level message
|
||||||
|
// info info INFO INFO INFO informational message
|
||||||
|
// notice NOTICE normal, but significant, condition
|
||||||
|
// warning warning WARNING WARN DEFAULT warning conditions
|
||||||
|
// error error ERROR ERROR ERROR error conditions
|
||||||
|
// fatal fatal CRIT FATAL FAULT critical conditions
|
||||||
|
// ALERT action must be taken immediately
|
||||||
|
// EMERG system is unusable
|
||||||
|
|
||||||
|
TRACE = 0,
|
||||||
|
DEBUG = 1,
|
||||||
|
INFO = 2,
|
||||||
|
NOTICE = 3,
|
||||||
|
WARNING = 4,
|
||||||
|
ERROR = 5,
|
||||||
|
FATAL = 6
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
enum class LogPriority : std::int8_t
|
enum class LogSeverity : std::int8_t
|
||||||
{
|
{
|
||||||
emerg = EMERG,
|
trace = TRACE,
|
||||||
alert = ALERT,
|
debug = DEBUG,
|
||||||
critical= CRIT,
|
|
||||||
error = ERROR,
|
|
||||||
warning = WARNING,
|
|
||||||
notice = NOTICE,
|
|
||||||
info = INFO,
|
info = INFO,
|
||||||
debug = DEBUG
|
notice = NOTICE,
|
||||||
|
warning = WARNING,
|
||||||
|
error = ERROR,
|
||||||
|
fatal = FATAL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -199,7 +218,7 @@ struct LogSink
|
||||||
all
|
all
|
||||||
};
|
};
|
||||||
|
|
||||||
LogSink(LogPriority priority, Type type) : priority(priority), sink_type_(type)
|
LogSink(LogSeverity severity, Type type) : severity(severity), sink_type_(type)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +226,7 @@ struct LogSink
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const = 0;
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const = 0;
|
||||||
virtual Type get_type() const
|
virtual Type get_type() const
|
||||||
{
|
{
|
||||||
return sink_type_;
|
return sink_type_;
|
||||||
|
@ -219,7 +238,7 @@ struct LogSink
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogPriority priority;
|
LogSeverity severity;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Type sink_type_;
|
Type sink_type_;
|
||||||
|
@ -227,7 +246,7 @@ protected:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
|
static std::ostream& operator<< (std::ostream& os, const LogSeverity& log_severity);
|
||||||
static std::ostream& operator<< (std::ostream& os, const LogType& log_type);
|
static std::ostream& operator<< (std::ostream& os, const LogType& log_type);
|
||||||
static std::ostream& operator<< (std::ostream& os, const Tag& tag);
|
static std::ostream& operator<< (std::ostream& os, const Tag& tag);
|
||||||
static std::ostream& operator<< (std::ostream& os, const Conditional& conditional);
|
static std::ostream& operator<< (std::ostream& os, const Conditional& conditional);
|
||||||
|
@ -263,29 +282,27 @@ public:
|
||||||
logSinks.erase(std::remove(logSinks.begin(), logSinks.end(), sink), logSinks.end());
|
logSinks.erase(std::remove(logSinks.begin(), logSinks.end(), sink), logSinks.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string toString(LogPriority logPriority)
|
static std::string toString(LogSeverity logSeverity)
|
||||||
{
|
{
|
||||||
switch (logPriority)
|
switch (logSeverity)
|
||||||
{
|
{
|
||||||
case LogPriority::emerg:
|
case LogSeverity::trace:
|
||||||
return "Emerg";
|
return "Trace";
|
||||||
case LogPriority::alert:
|
case LogSeverity::debug:
|
||||||
return "Alert";
|
|
||||||
case LogPriority::critical:
|
|
||||||
return "Crit";
|
|
||||||
case LogPriority::error:
|
|
||||||
return "Err";
|
|
||||||
case LogPriority::warning:
|
|
||||||
return "Warn";
|
|
||||||
case LogPriority::notice:
|
|
||||||
return "Notice";
|
|
||||||
case LogPriority::info:
|
|
||||||
return "Info";
|
|
||||||
case LogPriority::debug:
|
|
||||||
return "Debug";
|
return "Debug";
|
||||||
|
case LogSeverity::info:
|
||||||
|
return "Info";
|
||||||
|
case LogSeverity::notice:
|
||||||
|
return "Notice";
|
||||||
|
case LogSeverity::warning:
|
||||||
|
return "Warn";
|
||||||
|
case LogSeverity::error:
|
||||||
|
return "Err";
|
||||||
|
case LogSeverity::fatal:
|
||||||
|
return "Fatal";
|
||||||
default:
|
default:
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << logPriority;
|
ss << logSeverity;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,13 +327,13 @@ protected:
|
||||||
((type_ == LogType::special) && (sink->get_type() == LogSink::Type::special)) ||
|
((type_ == LogType::special) && (sink->get_type() == LogSink::Type::special)) ||
|
||||||
((type_ == LogType::normal) && (sink->get_type() == LogSink::Type::normal))
|
((type_ == LogType::normal) && (sink->get_type() == LogSink::Type::normal))
|
||||||
)
|
)
|
||||||
if (priority_ <= sink->priority)
|
if (severity_ >= sink->severity)
|
||||||
sink->log(now, priority_, type_, tag_, buffer_.str());
|
sink->log(now, severity_, type_, tag_, buffer_.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer_.str("");
|
buffer_.str("");
|
||||||
buffer_.clear();
|
buffer_.clear();
|
||||||
//priority_ = debug; // default to debug for each message
|
//severity_ = debug; // default to debug for each message
|
||||||
//type_ = kNormal;
|
//type_ = kNormal;
|
||||||
tag_ = nullptr;
|
tag_ = nullptr;
|
||||||
conditional_.set(true);
|
conditional_.set(true);
|
||||||
|
@ -327,8 +344,8 @@ protected:
|
||||||
int overflow(int c)
|
int overflow(int c)
|
||||||
{
|
{
|
||||||
/* if (
|
/* if (
|
||||||
(priority_ > loglevel_) &&
|
(severity_ > loglevel_) &&
|
||||||
((type_ == kNormal) || !syslog_enabled_) // || (syslogpriority_ > loglevel_))
|
((type_ == kNormal) || !syslog_enabled_) // || (syslogseverity_ > loglevel_))
|
||||||
)
|
)
|
||||||
return c;
|
return c;
|
||||||
*/ if (c != EOF)
|
*/ if (c != EOF)
|
||||||
|
@ -348,13 +365,13 @@ protected:
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
|
friend std::ostream& operator<< (std::ostream& os, const LogSeverity& log_severity);
|
||||||
friend std::ostream& operator<< (std::ostream& os, const LogType& log_type);
|
friend std::ostream& operator<< (std::ostream& os, const LogType& log_type);
|
||||||
friend std::ostream& operator<< (std::ostream& os, const Tag& tag);
|
friend std::ostream& operator<< (std::ostream& os, const Tag& tag);
|
||||||
friend std::ostream& operator<< (std::ostream& os, const Conditional& conditional);
|
friend std::ostream& operator<< (std::ostream& os, const Conditional& conditional);
|
||||||
|
|
||||||
std::stringstream buffer_;
|
std::stringstream buffer_;
|
||||||
LogPriority priority_;
|
LogSeverity severity_;
|
||||||
LogType type_;
|
LogType type_;
|
||||||
Tag tag_;
|
Tag tag_;
|
||||||
Conditional conditional_;
|
Conditional conditional_;
|
||||||
|
@ -365,8 +382,8 @@ private:
|
||||||
|
|
||||||
struct LogSinkFormat : public LogSink
|
struct LogSinkFormat : public LogSink
|
||||||
{
|
{
|
||||||
LogSinkFormat(LogPriority priority, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S [#prio] (#tag)") : // #logline") :
|
LogSinkFormat(LogSeverity severity, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S [#prio] (#tag)") : // #logline") :
|
||||||
LogSink(priority, type),
|
LogSink(severity, type),
|
||||||
format_(format)
|
format_(format)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -376,12 +393,12 @@ struct LogSinkFormat : public LogSink
|
||||||
format_ = format;
|
format_ = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const = 0;
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const = 0;
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// strftime format + proprietary "#ms" for milliseconds
|
/// strftime format + proprietary "#ms" for milliseconds
|
||||||
virtual void do_log(std::ostream& stream, const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void do_log(std::ostream& stream, const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
std::time_t now_c = std::chrono::system_clock::to_time_t(timestamp);
|
std::time_t now_c = std::chrono::system_clock::to_time_t(timestamp);
|
||||||
struct::tm now_tm = *std::localtime(&now_c);
|
struct::tm now_tm = *std::localtime(&now_c);
|
||||||
|
@ -400,7 +417,7 @@ protected:
|
||||||
|
|
||||||
pos = result.find("#prio");
|
pos = result.find("#prio");
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
result.replace(pos, 5, Log::toString(priority));
|
result.replace(pos, 5, Log::toString(severity));
|
||||||
|
|
||||||
|
|
||||||
pos = result.find("#tag");
|
pos = result.find("#tag");
|
||||||
|
@ -429,15 +446,15 @@ protected:
|
||||||
|
|
||||||
struct LogSinkCout : public LogSinkFormat
|
struct LogSinkCout : public LogSinkFormat
|
||||||
{
|
{
|
||||||
LogSinkCout(LogPriority priority, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)") : // #logline") :
|
LogSinkCout(LogSeverity severity, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)") : // #logline") :
|
||||||
LogSinkFormat(priority, type, format)
|
LogSinkFormat(severity, type, format)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
if (priority <= this->priority)
|
if (severity >= this->severity)
|
||||||
do_log(std::cout, timestamp, priority, type, tag, message);
|
do_log(std::cout, timestamp, severity, type, tag, message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -445,15 +462,15 @@ struct LogSinkCout : public LogSinkFormat
|
||||||
|
|
||||||
struct LogSinkCerr : public LogSinkFormat
|
struct LogSinkCerr : public LogSinkFormat
|
||||||
{
|
{
|
||||||
LogSinkCerr(LogPriority priority, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)") : // #logline") :
|
LogSinkCerr(LogSeverity severity, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)") : // #logline") :
|
||||||
LogSinkFormat(priority, type, format)
|
LogSinkFormat(severity, type, format)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
if (priority <= this->priority)
|
if (severity >= this->severity)
|
||||||
do_log(std::cerr, timestamp, priority, type, tag, message);
|
do_log(std::cerr, timestamp, severity, type, tag, message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -462,11 +479,11 @@ struct LogSinkCerr : public LogSinkFormat
|
||||||
/// Not tested due to unavailability of Windows
|
/// Not tested due to unavailability of Windows
|
||||||
struct LogSinkOutputDebugString : LogSink
|
struct LogSinkOutputDebugString : LogSink
|
||||||
{
|
{
|
||||||
LogSinkOutputDebugString(LogPriority priority, Type type = Type::all, const std::string& default_tag = "") : LogSink(priority, type)
|
LogSinkOutputDebugString(LogSeverity severity, Type type = Type::all, const std::string& default_tag = "") : LogSink(severity, type)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
OutputDebugString(message.c_str());
|
OutputDebugString(message.c_str());
|
||||||
|
@ -478,38 +495,38 @@ struct LogSinkOutputDebugString : LogSink
|
||||||
|
|
||||||
struct LogSinkUnifiedLogging : LogSink
|
struct LogSinkUnifiedLogging : LogSink
|
||||||
{
|
{
|
||||||
LogSinkUnifiedLogging(LogPriority priority, Type type = Type::all) : LogSink(priority, type)
|
LogSinkUnifiedLogging(LogSeverity severity, Type type = Type::all) : LogSink(severity, type)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
os_log_type_t get_os_log_type(LogPriority priority) const
|
os_log_type_t get_os_log_type(LogSeverity severity) const
|
||||||
{
|
{
|
||||||
switch (priority)
|
// https://developer.apple.com/documentation/os/os_log_type_t?language=objc
|
||||||
|
switch (severity)
|
||||||
{
|
{
|
||||||
case LogPriority::emerg:
|
case LogSeverity::trace:
|
||||||
case LogPriority::alert:
|
case LogSeverity::debug:
|
||||||
case LogPriority::critical:
|
|
||||||
return OS_LOG_TYPE_FAULT;
|
|
||||||
case LogPriority::error:
|
|
||||||
return OS_LOG_TYPE_ERROR;
|
|
||||||
case LogPriority::warning:
|
|
||||||
case LogPriority::notice:
|
|
||||||
return OS_LOG_TYPE_DEFAULT;
|
|
||||||
case LogPriority::info:
|
|
||||||
return OS_LOG_TYPE_INFO;
|
|
||||||
case LogPriority::debug:
|
|
||||||
return OS_LOG_TYPE_DEBUG;
|
return OS_LOG_TYPE_DEBUG;
|
||||||
|
case LogSeverity::info:
|
||||||
|
case LogSeverity::notice:
|
||||||
|
return OS_LOG_TYPE_INFO;
|
||||||
|
case LogSeverity::warning:
|
||||||
|
return OS_LOG_TYPE_DEFAULT;
|
||||||
|
case LogSeverity::error:
|
||||||
|
return OS_LOG_TYPE_ERROR;
|
||||||
|
case LogSeverity::fatal:
|
||||||
|
return OS_LOG_TYPE_FAULT;
|
||||||
default:
|
default:
|
||||||
return OS_LOG_TYPE_DEFAULT;
|
return OS_LOG_TYPE_DEFAULT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
os_log_with_type(OS_LOG_DEFAULT, get_os_log_type(priority), "%{public}s", message.c_str());
|
os_log_with_type(OS_LOG_DEFAULT, get_os_log_type(severity), "%{public}s", message.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -518,9 +535,9 @@ struct LogSinkUnifiedLogging : LogSink
|
||||||
|
|
||||||
struct LogSinkSyslog : public LogSink
|
struct LogSinkSyslog : public LogSink
|
||||||
{
|
{
|
||||||
LogSinkSyslog(const char* ident, LogPriority priority, Type type) : LogSink(priority, type)
|
LogSinkSyslog(const char* ident, LogSeverity severity, Type type) : LogSink(severity, type)
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
#ifdef _HAS_SYSLOG_
|
||||||
openlog(ident, LOG_PID, LOG_USER);
|
openlog(ident, LOG_PID, LOG_USER);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -530,10 +547,36 @@ struct LogSinkSyslog : public LogSink
|
||||||
closelog();
|
closelog();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
#ifdef _HAS_SYSLOG_
|
||||||
|
int get_syslog_priority(LogSeverity severity) const
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/syslog.h.html
|
||||||
syslog((int)priority, "%s", message.c_str());
|
switch (severity)
|
||||||
|
{
|
||||||
|
case LogSeverity::trace:
|
||||||
|
case LogSeverity::debug:
|
||||||
|
return LOG_DEBUG;
|
||||||
|
case LogSeverity::info:
|
||||||
|
return LOG_INFO;
|
||||||
|
case LogSeverity::notice:
|
||||||
|
return LOG_NOTICE;
|
||||||
|
case LogSeverity::warning:
|
||||||
|
return LOG_WARNING;
|
||||||
|
case LogSeverity::error:
|
||||||
|
return LOG_ERR;
|
||||||
|
case LogSeverity::fatal:
|
||||||
|
return LOG_CRIT;
|
||||||
|
default:
|
||||||
|
return LOG_INFO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
|
{
|
||||||
|
#ifdef _HAS_SYSLOG_
|
||||||
|
syslog(get_syslog_priority(severity), "%s", message.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -542,36 +585,36 @@ struct LogSinkSyslog : public LogSink
|
||||||
|
|
||||||
struct LogSinkAndroid : public LogSink
|
struct LogSinkAndroid : public LogSink
|
||||||
{
|
{
|
||||||
LogSinkAndroid(const std::string& ident, LogPriority priority, Type type = Type::all) : LogSink(priority, type), ident_(ident)
|
LogSinkAndroid(const std::string& ident, LogSeverity severity, Type type = Type::all) : LogSink(severity, type), ident_(ident)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
android_LogPriority get_android_prio(LogPriority priority) const
|
android_LogSeverity get_android_prio(LogSeverity severity) const
|
||||||
{
|
{
|
||||||
switch (priority)
|
// https://developer.android.com/ndk/reference/log_8h.html
|
||||||
|
switch (severity)
|
||||||
{
|
{
|
||||||
case LogPriority::emerg:
|
case LogSeverity::trace:
|
||||||
case LogPriority::alert:
|
return ANDROID_LOG_VERBOSE;
|
||||||
case LogPriority::critical:
|
case LogSeverity::debug:
|
||||||
return ANDROID_LOG_FATAL;
|
|
||||||
case LogPriority::error:
|
|
||||||
return ANDROID_LOG_ERROR;
|
|
||||||
case LogPriority::warning:
|
|
||||||
return ANDROID_LOG_WARN;
|
|
||||||
case LogPriority::notice:
|
|
||||||
return ANDROID_LOG_DEFAULT;
|
|
||||||
case LogPriority::info:
|
|
||||||
return ANDROID_LOG_INFO;
|
|
||||||
case LogPriority::debug:
|
|
||||||
return ANDROID_LOG_DEBUG;
|
return ANDROID_LOG_DEBUG;
|
||||||
|
case LogSeverity::info:
|
||||||
|
case LogSeverity::notice:
|
||||||
|
return ANDROID_LOG_INFO;
|
||||||
|
case LogSeverity::warning:
|
||||||
|
return ANDROID_LOG_WARN;
|
||||||
|
case LogSeverity::error:
|
||||||
|
return ANDROID_LOG_ERROR;
|
||||||
|
case LogSeverity::fatal:
|
||||||
|
return ANDROID_LOG_FATAL;
|
||||||
default:
|
default:
|
||||||
return ANDROID_LOG_UNKNOWN;
|
return ANDROID_LOG_UNKNOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
std::string log_tag;// = default_tag_;
|
std::string log_tag;// = default_tag_;
|
||||||
|
@ -585,7 +628,7 @@ struct LogSinkAndroid : public LogSink
|
||||||
else
|
else
|
||||||
log_tag = ident_;
|
log_tag = ident_;
|
||||||
|
|
||||||
__android_log_write(get_android_prio(priority), log_tag.c_str(), message.c_str());
|
__android_log_write(get_android_prio(severity), log_tag.c_str(), message.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -598,7 +641,7 @@ protected:
|
||||||
/// Not tested due to unavailability of Windows
|
/// Not tested due to unavailability of Windows
|
||||||
struct LogSinkEventLog : public LogSink
|
struct LogSinkEventLog : public LogSink
|
||||||
{
|
{
|
||||||
LogSinkEventLog(const std::string& ident, LogPriority priority, Type type = Type::all) : LogSink(priority, type)
|
LogSinkEventLog(const std::string& ident, LogSeverity severity, Type type = Type::all) : LogSink(severity, type)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
event_log = RegisterEventSource(NULL, ident.c_str());
|
event_log = RegisterEventSource(NULL, ident.c_str());
|
||||||
|
@ -606,32 +649,32 @@ struct LogSinkEventLog : public LogSink
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WORD get_type(LogPriority priority) const
|
WORD get_type(LogSeverity severity) const
|
||||||
{
|
{
|
||||||
switch (priority)
|
// https://msdn.microsoft.com/de-de/library/windows/desktop/aa363679(v=vs.85).aspx
|
||||||
|
switch (severity)
|
||||||
{
|
{
|
||||||
case LogPriority::emerg:
|
case LogSeverity::trace:
|
||||||
case LogPriority::alert:
|
case LogSeverity::debug:
|
||||||
case LogPriority::critical:
|
|
||||||
case LogPriority::error:
|
|
||||||
return EVENTLOG_ERROR_TYPE;
|
|
||||||
case LogPriority::warning:
|
|
||||||
return EVENTLOG_WARNING_TYPE;
|
|
||||||
case LogPriority::notice:
|
|
||||||
return EVENTLOG_SUCCESS;
|
|
||||||
case LogPriority::info:
|
|
||||||
case LogPriority::debug:
|
|
||||||
return EVENTLOG_INFORMATION_TYPE;
|
return EVENTLOG_INFORMATION_TYPE;
|
||||||
|
case LogSeverity::info:
|
||||||
|
case LogSeverity::notice:
|
||||||
|
return EVENTLOG_SUCCESS;
|
||||||
|
case LogSeverity::warning:
|
||||||
|
return EVENTLOG_WARNING_TYPE;
|
||||||
|
case LogSeverity::error:
|
||||||
|
case LogSeverity::fatal:
|
||||||
|
return EVENTLOG_ERROR_TYPE;
|
||||||
default:
|
default:
|
||||||
return EVENTLOG_INFORMATION_TYPE;
|
return EVENTLOG_INFORMATION_TYPE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ReportEvent(event_log, get_type(priority), 0, 0, NULL, 1, 0, &message.c_str(), NULL);
|
ReportEvent(event_log, get_type(severity), 0, 0, NULL, 1, 0, &message.c_str(), NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -645,26 +688,26 @@ protected:
|
||||||
|
|
||||||
struct LogSinkNative : public LogSink
|
struct LogSinkNative : public LogSink
|
||||||
{
|
{
|
||||||
LogSinkNative(const std::string& ident, LogPriority priority, Type type = Type::all) :
|
LogSinkNative(const std::string& ident, LogSeverity severity, Type type = Type::all) :
|
||||||
LogSink(priority, type),
|
LogSink(severity, type),
|
||||||
log_sink_(nullptr),
|
log_sink_(nullptr),
|
||||||
ident_(ident)
|
ident_(ident)
|
||||||
{
|
{
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
log_sink_ = std::make_shared<LogSinkAndroid>(ident_, priority, type);
|
log_sink_ = std::make_shared<LogSinkAndroid>(ident_, severity, type);
|
||||||
#elif __APPLE__
|
#elif __APPLE__
|
||||||
log_sink_ = std::make_shared<LogSinkUnifiedLogging>(priority, type);
|
log_sink_ = std::make_shared<LogSinkUnifiedLogging>(severity, type);
|
||||||
#elif _WIN32
|
#elif _WIN32
|
||||||
log_sink_ = std::make_shared<LogSinkEventLog>(priority, type);
|
log_sink_ = std::make_shared<LogSinkEventLog>(severity, type);
|
||||||
#else
|
#else
|
||||||
log_sink_ = std::make_shared<LogSinkSyslog>(ident_.c_str(), priority, type);
|
log_sink_ = std::make_shared<LogSinkSyslog>(ident_.c_str(), severity, type);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
if (log_sink_)
|
if (log_sink_)
|
||||||
log_sink_->log(timestamp, priority, type, tag, message);
|
log_sink_->log(timestamp, severity, type, tag, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -676,16 +719,16 @@ protected:
|
||||||
|
|
||||||
struct LogSinkCallback : public LogSink
|
struct LogSinkCallback : public LogSink
|
||||||
{
|
{
|
||||||
typedef std::function<void(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message)> callback_fun;
|
typedef std::function<void(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message)> callback_fun;
|
||||||
|
|
||||||
LogSinkCallback(LogPriority priority, Type type, callback_fun callback) : LogSink(priority, type), callback(callback)
|
LogSinkCallback(LogSeverity severity, Type type, callback_fun callback) : LogSink(severity, type), callback(callback)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void log(const time_point_sys_clock& timestamp, LogPriority priority, LogType type, const Tag& tag, const std::string& message) const
|
virtual void log(const time_point_sys_clock& timestamp, LogSeverity severity, LogType type, const Tag& tag, const std::string& message) const
|
||||||
{
|
{
|
||||||
if (callback && (priority <= this->priority))
|
if (callback && (severity >= this->severity))
|
||||||
callback(timestamp, priority, type, tag, message);
|
callback(timestamp, severity, type, tag, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -694,13 +737,13 @@ private:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority)
|
static std::ostream& operator<< (std::ostream& os, const LogSeverity& log_severity)
|
||||||
{
|
{
|
||||||
Log* log = dynamic_cast<Log*>(os.rdbuf());
|
Log* log = dynamic_cast<Log*>(os.rdbuf());
|
||||||
if (log && (log->priority_ != log_priority))
|
if (log && (log->severity_ != log_severity))
|
||||||
{
|
{
|
||||||
log->sync();
|
log->sync();
|
||||||
log->priority_ = log_priority;
|
log->severity_ = log_severity;
|
||||||
}
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,8 +145,8 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
Log::init(
|
Log::init(
|
||||||
{
|
{
|
||||||
make_shared<LogSinkCout>(debugSwitch.isSet()?(LogPriority::debug):(LogPriority::info), LogSink::Type::all, debugSwitch.isSet()?"%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)":"%Y-%m-%d %H-%M-%S [#prio]"),
|
make_shared<LogSinkCout>(debugSwitch.isSet()?(LogSeverity::trace):(LogSeverity::info), LogSink::Type::all, debugSwitch.isSet()?"%Y-%m-%d %H-%M-%S.#ms [#prio] (#tag)":"%Y-%m-%d %H-%M-%S [#prio]"),
|
||||||
make_shared<LogSinkNative>("snapserver", LogPriority::debug, LogSink::Type::special)
|
make_shared<LogSinkNative>("snapserver", LogSeverity::trace, LogSink::Type::special)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue