logging changes
This commit is contained in:
parent
de8c3ad603
commit
ca1e03f3ea
@ -29,24 +29,27 @@ class logging_consumer {
|
|||||||
E_CONSUMER();
|
E_CONSUMER();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
logging_consumer(const std::string &log_directory, const event_level &level);
|
logging_consumer(event_level level, std::string log_directory);
|
||||||
|
|
||||||
~logging_consumer();
|
~logging_consumer();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::uint8_t MAX_LOG_FILES = 5;
|
static constexpr const std::uint8_t MAX_LOG_FILES{5U};
|
||||||
const std::uint64_t MAX_LOG_FILE_SIZE = (1024 * 1024 * 5);
|
static constexpr const std::uint64_t MAX_LOG_FILE_SIZE{1024ULL * 1024ULL *
|
||||||
|
5ULL};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
event_level event_level_ = event_level::normal;
|
event_level event_level_;
|
||||||
const std::string log_directory_;
|
std::string log_directory_;
|
||||||
const std::string log_path_;
|
std::string log_path_;
|
||||||
bool logging_active_ = true;
|
|
||||||
|
private:
|
||||||
|
std::deque<std::shared_ptr<event>> event_queue_;
|
||||||
|
FILE *log_file_{nullptr};
|
||||||
std::mutex log_mutex_;
|
std::mutex log_mutex_;
|
||||||
std::condition_variable log_notify_;
|
std::condition_variable log_notify_;
|
||||||
std::deque<std::shared_ptr<event>> event_queue_;
|
bool logging_active_{true};
|
||||||
std::unique_ptr<std::thread> logging_thread_;
|
std::unique_ptr<std::thread> logging_thread_;
|
||||||
FILE *log_file_ = nullptr;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void check_log_roll(std::size_t count);
|
void check_log_roll(std::size_t count);
|
||||||
|
@ -30,8 +30,7 @@
|
|||||||
#include "utils/utils.hpp"
|
#include "utils/utils.hpp"
|
||||||
|
|
||||||
namespace repertory {
|
namespace repertory {
|
||||||
logging_consumer::logging_consumer(const std::string &log_directory,
|
logging_consumer::logging_consumer(event_level level, std::string log_directory)
|
||||||
const event_level &level)
|
|
||||||
: event_level_(level),
|
: event_level_(level),
|
||||||
log_directory_(utils::path::absolute(log_directory)),
|
log_directory_(utils::path::absolute(log_directory)),
|
||||||
log_path_(utils::path::combine(log_directory, {"repertory.log"})) {
|
log_path_(utils::path::combine(log_directory, {"repertory.log"})) {
|
||||||
@ -56,10 +55,10 @@ logging_consumer::logging_consumer(const std::string &log_directory,
|
|||||||
logging_consumer::~logging_consumer() {
|
logging_consumer::~logging_consumer() {
|
||||||
E_CONSUMER_RELEASE();
|
E_CONSUMER_RELEASE();
|
||||||
|
|
||||||
unique_mutex_lock l(log_mutex_);
|
unique_mutex_lock lock(log_mutex_);
|
||||||
logging_active_ = false;
|
logging_active_ = false;
|
||||||
log_notify_.notify_all();
|
log_notify_.notify_all();
|
||||||
l.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
logging_thread_->join();
|
logging_thread_->join();
|
||||||
logging_thread_.reset();
|
logging_thread_.reset();
|
||||||
@ -72,18 +71,19 @@ void logging_consumer::check_log_roll(std::size_t count) {
|
|||||||
constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__);
|
constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__);
|
||||||
|
|
||||||
std::uint64_t file_size{};
|
std::uint64_t file_size{};
|
||||||
const auto success = utils::file::get_file_size(log_path_, file_size);
|
auto success = utils::file::get_file_size(log_path_, file_size);
|
||||||
if (success && (file_size + count) >= MAX_LOG_FILE_SIZE) {
|
if (success && (file_size + count) >= MAX_LOG_FILE_SIZE) {
|
||||||
close_log_file();
|
close_log_file();
|
||||||
for (std::uint8_t i = MAX_LOG_FILES; i > 0u; i--) {
|
for (std::uint8_t i = MAX_LOG_FILES; i > 0u; i--) {
|
||||||
const auto temp_log_path = utils::path::combine(
|
auto temp_log_path = utils::path::combine(
|
||||||
log_directory_, {"repertory." + std::to_string(i) + ".log"});
|
log_directory_, {"repertory." + std::to_string(i) + ".log"});
|
||||||
if (utils::file::is_file(temp_log_path)) {
|
if (utils::file::is_file(temp_log_path)) {
|
||||||
if (i == MAX_LOG_FILES) {
|
if (i == MAX_LOG_FILES) {
|
||||||
if (not utils::file::retry_delete_file(temp_log_path)) {
|
if (not utils::file::retry_delete_file(temp_log_path)) {
|
||||||
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const auto next_file_path = utils::path::combine(
|
auto next_file_path = utils::path::combine(
|
||||||
log_directory_,
|
log_directory_,
|
||||||
{"repertory." + std::to_string(i + std::uint8_t(1)) + ".log"});
|
{"repertory." + std::to_string(i + std::uint8_t(1)) + ".log"});
|
||||||
if (not utils::file::move_file(temp_log_path, next_file_path)) {
|
if (not utils::file::move_file(temp_log_path, next_file_path)) {
|
||||||
@ -132,7 +132,7 @@ void logging_consumer::logging_thread(bool drain) {
|
|||||||
events.pop_front();
|
events.pop_front();
|
||||||
|
|
||||||
if (event->get_event_level() <= event_level_) {
|
if (event->get_event_level() <= event_level_) {
|
||||||
const std::string msg = ([&]() -> std::string {
|
auto msg = ([&]() -> std::string {
|
||||||
struct tm local_time {};
|
struct tm local_time {};
|
||||||
utils::get_local_time_now(local_time);
|
utils::get_local_time_now(local_time);
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ void logging_consumer::logging_thread(bool drain) {
|
|||||||
check_log_roll(msg.length());
|
check_log_roll(msg.length());
|
||||||
auto retry = true;
|
auto retry = true;
|
||||||
for (int i = 0; retry && (i < 2); i++) {
|
for (int i = 0; retry && (i < 2); i++) {
|
||||||
retry = (not log_file_ || (fwrite(&msg[0], 1, msg.length(),
|
retry = (not log_file_ || (fwrite(msg.data(), 1, msg.length(),
|
||||||
log_file_) != msg.length()));
|
log_file_) != msg.length()));
|
||||||
if (retry) {
|
if (retry) {
|
||||||
reopen_log_file();
|
reopen_log_file();
|
||||||
@ -162,19 +162,17 @@ void logging_consumer::logging_thread(bool drain) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void logging_consumer::process_event(const event &event) {
|
void logging_consumer::process_event(const event &event) {
|
||||||
{
|
mutex_lock lock(log_mutex_);
|
||||||
mutex_lock l(log_mutex_);
|
|
||||||
event_queue_.push_back(event.clone());
|
event_queue_.push_back(event.clone());
|
||||||
log_notify_.notify_all();
|
log_notify_.notify_all();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void logging_consumer::reopen_log_file() {
|
void logging_consumer::reopen_log_file() {
|
||||||
close_log_file();
|
close_log_file();
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
log_file_ = _fsopen(&log_path_[0], "a+", _SH_DENYWR);
|
log_file_ = _fsopen(log_path_.c_str(), "a+", _SH_DENYWR);
|
||||||
#else
|
#else
|
||||||
log_file_ = fopen(&log_path_[0], "a+");
|
log_file_ = fopen(log_path_.c_str(), "a+");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} // namespace repertory
|
} // namespace repertory
|
||||||
|
Loading…
x
Reference in New Issue
Block a user