Add TimeConditional for logging

This commit is contained in:
badaix 2021-02-03 22:55:02 +01:00
parent 1c0a857856
commit 147e4e9b7e
2 changed files with 91 additions and 12 deletions

View file

@ -277,17 +277,14 @@ struct Conditional
{
}
void set(bool value)
{
is_true_ = value;
}
virtual ~Conditional() = default;
bool is_true() const
virtual bool is_true() const
{
return is_true_;
}
private:
protected:
bool is_true_;
};
@ -601,7 +598,7 @@ public:
}
protected:
Log() noexcept : last_buffer_(nullptr)
Log() noexcept : last_buffer_(nullptr), do_log_(true)
{
std::clog.rdbuf(this);
std::clog << Severity() << Tag() << Function() << Conditional() << AixLog::Color::NONE << std::flush;
@ -617,7 +614,7 @@ protected:
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!get_stream().str().empty())
{
if (conditional_.is_true())
if (do_log_)
{
for (const auto& sink : log_sinks_)
{
@ -639,7 +636,7 @@ protected:
{
if (c == '\n')
sync();
else
else if (do_log_)
get_stream() << static_cast<char>(c);
}
else
@ -667,11 +664,14 @@ private:
return *last_buffer_;
}
/// one buffer per thread to avoid mixed log lines
std::map<std::thread::id, std::stringstream> buffer_;
/// the last thread id
std::thread::id last_id_;
/// the last buffer
std::stringstream* last_buffer_ = nullptr;
Metadata metadata_;
Conditional conditional_;
bool do_log_;
std::vector<log_sink_ptr> log_sinks_;
std::recursive_mutex mutex_;
};
@ -1131,7 +1131,7 @@ static std::ostream& operator<<(std::ostream& os, const Severity& log_severity)
log->metadata_.timestamp = nullptr;
log->metadata_.tag = nullptr;
log->metadata_.function = nullptr;
log->conditional_.set(true);
log->do_log_ = true;
}
}
else
@ -1192,7 +1192,7 @@ static std::ostream& operator<<(std::ostream& os, const Conditional& conditional
if (log != nullptr)
{
std::lock_guard<std::recursive_mutex> lock(log->mutex_);
log->conditional_.set(conditional.is_true());
log->do_log_ = conditional.is_true();
}
return os;
}

79
common/utils/logging.hpp Normal file
View file

@ -0,0 +1,79 @@
/***
This file is part of snapcast
Copyright (C) 2014-2021 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef LOGGING_UTILS_HPP
#define LOGGING_UTILS_HPP
#include "common/aixlog.hpp"
#include <chrono>
namespace utils
{
namespace logging
{
using namespace std::chrono_literals;
/// Log Conditional to limit log frequency
struct TimeConditional : public AixLog::Conditional
{
/// c'tor
/// @param interval duration that must be passed since the last log line
TimeConditional(const std::chrono::milliseconds& interval) : interval_(interval)
{
reset();
}
/// return true for the next check
void reset()
{
last_time_ = std::chrono::steady_clock::now() - interval_ - 1s;
}
/// Change log interval
/// @param interval duration that must be passed since the last log line
void setInterval(const std::chrono::milliseconds& interval)
{
interval_ = interval;
}
/// check if the interval is passed
/// @return true if interval passed since the last log
bool is_true() const override
{
auto now = std::chrono::steady_clock::now();
if (now > last_time_ + interval_)
{
last_time_ = now;
return true;
}
return false;
}
private:
/// log interval
std::chrono::milliseconds interval_;
/// last log time
mutable std::chrono::steady_clock::time_point last_time_;
};
} // namespace logging
} // namespace utils
#endif