Skip to content

Commit

Permalink
Update AixLog to v1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Jan 14, 2025
1 parent a638da0 commit 23019b5
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions common/aixlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/ _\ ( )( \/ )( ) / \ / __)
/ \ )( ) ( / (_/\( O )( (_ \
\_/\_/(__)(_/\_)\____/ \__/ \___/
version 1.4.0
version 1.5.1
https://github.com/badaix/aixlog
This file is part of aixlog
Copyright (C) 2017-2020 Johannes Pohl
Copyright (C) 2017-2025 Johannes Pohl
This software may be modified and distributed under the terms
of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -269,23 +269,29 @@ struct TextColor
*/
struct Conditional
{
Conditional() : Conditional(true)
using EvalFunc = std::function<bool()>;

Conditional() : func_([](void) { return true; })
{
}

Conditional(EvalFunc func) : func_(std::move(func))
{
}

Conditional(bool value) : is_true_(value)
Conditional(bool value) : func_([value](void) { return value; })
{
}

virtual ~Conditional() = default;

virtual bool is_true() const
{
return is_true_;
return func_();
}

protected:
bool is_true_;
EvalFunc func_;
};

/**
Expand All @@ -310,7 +316,7 @@ struct Timestamp
{
}

Timestamp(time_point_sys_clock&& time_point) : time_point(std::move(time_point)), is_null_(false)
Timestamp(time_point_sys_clock&& time_point) : time_point(time_point), is_null_(false)
{
}

Expand Down Expand Up @@ -367,7 +373,7 @@ struct Timestamp
*/
struct Tag
{
Tag(std::nullptr_t) : text(""), is_null_(true)
Tag(std::nullptr_t) : is_null_(true)
{
}

Expand Down Expand Up @@ -419,7 +425,7 @@ struct Function
{
}

Function(std::nullptr_t) : name(""), file(""), line(0), is_null_(true)
Function(std::nullptr_t) : line(0), is_null_(true)
{
}

Expand Down Expand Up @@ -499,7 +505,7 @@ class Filter

void add_filter(const std::string& filter)
{
auto pos = filter.find(":");
auto pos = filter.find(':');
if (pos != std::string::npos)
add_filter(filter.substr(0, pos), to_severity(filter.substr(pos + 1)));
else
Expand All @@ -519,7 +525,7 @@ class Filter
*/
struct Sink
{
Sink(const Filter& filter) : filter(filter)
Sink(Filter filter) : filter(std::move(filter))
{
}

Expand Down Expand Up @@ -606,15 +612,10 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>

virtual ~Log()
{
do_sync();
sync();
}

int sync() override
{
return do_sync();
}

int do_sync()
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!get_stream().str().empty())
Expand Down Expand Up @@ -761,14 +762,14 @@ struct SinkFormat : public Sink
if (pos != std::string::npos)
{
result.replace(pos, 8, message);
stream << result << std::endl;
stream << result << "\n";
}
else
{
if (result.empty() || (result.back() == ' '))
stream << result << message << std::endl;
stream << result << message << "\n";
else
stream << result << " " << message << std::endl;
stream << result << " " << message << "\n";
}
}

Expand Down Expand Up @@ -846,10 +847,14 @@ struct SinkOutputDebugString : public Sink
{
}

void log(const Metadata& /*metadata*/, const std::string& message) override
void log(const Metadata& metadata, const std::string& message) override
{
#ifdef UNICODE
std::wstring wide = std::wstring(message.begin(), message.end());
OutputDebugString(wide.c_str());
#else
OutputDebugString(message.c_str());
#endif
}
};
#endif
Expand Down Expand Up @@ -936,7 +941,7 @@ struct SinkSyslog : public Sink

void log(const Metadata& metadata, const std::string& message) override
{
syslog(get_syslog_priority(metadata.severity), "%s", message.c_str());
syslog(get_syslog_priority(metadata.severity), "(%s) %s", metadata.tag.text.c_str(), message.c_str());
}
};
#endif
Expand Down Expand Up @@ -1009,8 +1014,12 @@ struct SinkEventLog : public Sink
{
SinkEventLog(const std::string& ident, const Filter& filter) : Sink(filter)
{
#ifdef UNICODE
std::wstring wide = std::wstring(ident.begin(), ident.end()); // stijnvdb: RegisterEventSource expands to RegisterEventSourceW which takes wchar_t
event_log = RegisterEventSource(NULL, wide.c_str());
#else
event_log = RegisterEventSource(NULL, ident.c_str());
#endif
}

WORD get_type(Severity severity) const
Expand All @@ -1036,11 +1045,15 @@ struct SinkEventLog : public Sink

void log(const Metadata& metadata, const std::string& message) override
{
#ifdef UNICODE
std::wstring wide = std::wstring(message.begin(), message.end());
// We need this temp variable because we cannot take address of rValue
const wchar_t* c_str = wide.c_str();

const auto* c_str = wide.c_str();
ReportEvent(event_log, get_type(metadata.severity), 0, 0, NULL, 1, 0, &c_str, NULL);
#else
const auto* c_str = message.c_str();
ReportEvent(event_log, get_type(metadata.severity), 0, 0, NULL, 1, 0, &c_str, NULL);
#endif
}

protected:
Expand Down Expand Up @@ -1103,7 +1116,7 @@ struct SinkCallback : public Sink
{
using callback_fun = std::function<void(const Metadata& metadata, const std::string& message)>;

SinkCallback(const Filter& filter, callback_fun callback) : Sink(filter), callback_(callback)
SinkCallback(const Filter& filter, callback_fun callback) : Sink(filter), callback_(std::move(callback))
{
}

Expand Down

0 comments on commit 23019b5

Please sign in to comment.