Skip to content
API Documentation
BastionGuard™
AsyncLogger Class Reference

#include <async_logger.hpp>

Public Member Functions

void log_block (const std::string &msg)
void flush ()
void stop ()

Static Public Member Functions

static AsyncLogger & instance ()

Private Member Functions

 AsyncLogger ()
 ~AsyncLogger ()

Static Private Member Functions

static std::string timestamp ()

Private Attributes

std::thread worker_
std::queue< std::string > queue_
std::mutex mtx_
std::condition_variable cv_
std::string log_path_
std::atomic< bool > stop_ {false}

Detailed Description

Definition at line 33 of file async_logger.hpp.

Constructor & Destructor Documentation

◆ AsyncLogger()

AsyncLogger::AsyncLogger ( )
inlineprivate

Definition at line 67 of file async_logger.hpp.

67 {
68 try {
69 std::filesystem::create_directories("/var/log/BastionGuard");
70 } catch (...) {
71 }
72
73 log_path_ = "/var/log/BastionGuard/phishing_scanner.log";
74 stop_.store(false, std::memory_order_release);
75
76 worker_ = std::thread([this]() {
77 std::unique_lock<std::mutex> lock(mtx_);
78 while (true) {
79 cv_.wait(lock, [this]() {
80 return !queue_.empty() || stop_.load(std::memory_order_acquire);
81 });
82
83 if (stop_.load(std::memory_order_acquire) && queue_.empty())
84 break;
85
86 std::queue<std::string> local_queue;
87 std::swap(local_queue, queue_);
88 lock.unlock();
89
90 try {
91 std::ofstream fout(log_path_, std::ios::app | std::ios::binary);
92 if (fout.is_open()) {
93 while (!local_queue.empty()) {
94 fout << local_queue.front() << '\n';
95 local_queue.pop();
96 }
97 fout.flush();
98 }
99 } catch (...) {
100 }
101
102 lock.lock();
103 }
104 });
105 }
std::atomic< bool > stop_
std::thread worker_
std::queue< std::string > queue_
std::condition_variable cv_
std::mutex mtx_
std::string log_path_

References cv_, log_path_, mtx_, queue_, stop_, and worker_.

Referenced by instance().

◆ ~AsyncLogger()

AsyncLogger::~AsyncLogger ( )
inlineprivate

Definition at line 107 of file async_logger.hpp.

107 {
108 stop();
109 }

References stop().

Member Function Documentation

◆ flush()

void AsyncLogger::flush ( )
inline

Definition at line 51 of file async_logger.hpp.

51 {
52 std::unique_lock<std::mutex> lock(mtx_);
53 cv_.notify_one();
54 }

References cv_, and mtx_.

◆ instance()

AsyncLogger & AsyncLogger::instance ( )
inlinestatic

Definition at line 35 of file async_logger.hpp.

35 {
36 static AsyncLogger inst;
37 return inst;
38 }

References AsyncLogger().

◆ log_block()

void AsyncLogger::log_block ( const std::string & msg)
inline

Definition at line 40 of file async_logger.hpp.

40 {
41 if (stop_.load(std::memory_order_acquire))
42 return;
43
44 {
45 std::lock_guard<std::mutex> lock(mtx_);
46 queue_.push(timestamp() + " " + msg);
47 }
48 cv_.notify_one();
49 }
static std::string timestamp()

References cv_, mtx_, queue_, stop_, and timestamp().

◆ stop()

void AsyncLogger::stop ( )
inline

Definition at line 56 of file async_logger.hpp.

56 {
57 bool expected = false;
58 if (!stop_.compare_exchange_strong(expected, true))
59 return; // già fermato
60
61 cv_.notify_one();
62 if (worker_.joinable())
63 worker_.join();
64 }

References cv_, stop_, and worker_.

Referenced by ~AsyncLogger().

◆ timestamp()

std::string AsyncLogger::timestamp ( )
inlinestaticprivate

Definition at line 111 of file async_logger.hpp.

111 {
112 using namespace std::chrono;
113 auto now = system_clock::now();
114 std::time_t t = system_clock::to_time_t(now);
115 std::tm tm_buf{};
116 localtime_r(&t, &tm_buf);
117 char buf[64];
118 std::strftime(buf, sizeof(buf), "[%Y-%m-%d %H:%M:%S]", &tm_buf);
119 return buf;
120 }

Referenced by log_block().

Member Data Documentation

◆ cv_

std::condition_variable AsyncLogger::cv_
private

Definition at line 125 of file async_logger.hpp.

Referenced by AsyncLogger(), flush(), log_block(), and stop().

◆ log_path_

std::string AsyncLogger::log_path_
private

Definition at line 126 of file async_logger.hpp.

Referenced by AsyncLogger().

◆ mtx_

std::mutex AsyncLogger::mtx_
private

Definition at line 124 of file async_logger.hpp.

Referenced by AsyncLogger(), flush(), and log_block().

◆ queue_

std::queue<std::string> AsyncLogger::queue_
private

Definition at line 123 of file async_logger.hpp.

Referenced by AsyncLogger(), and log_block().

◆ stop_

std::atomic<bool> AsyncLogger::stop_ {false}
private

Definition at line 127 of file async_logger.hpp.

Referenced by AsyncLogger(), log_block(), and stop().

◆ worker_

std::thread AsyncLogger::worker_
private

Definition at line 122 of file async_logger.hpp.

Referenced by AsyncLogger(), and stop().


The documentation for this class was generated from the following file: