Skip to content
API Documentation
BastionGuard™
bsc::logging::Logger Class Reference

#include <Logger.hpp>

Public Member Functions

void set_level (Level level)
void set_utc (bool enabled)
void set_microseconds (bool enabled)
void set_log_file (const std::string &path)
void debug (const std::string &msg)
void info (const std::string &msg)
void warning (const std::string &msg)
void error (const std::string &msg)

Static Public Member Functions

static Logger & instance ()

Private Member Functions

void log (Level level, const std::string &msg)
std::string timestamp () const
std::string level_name (Level level) const

Private Attributes

Level level_ {Level::Info}
bool utc_ {true}
bool micro_ {false}
std::string file_path_
std::mutex mutex_

Detailed Description

Definition at line 9 of file Logger.hpp.

Member Function Documentation

◆ debug()

void bsc::logging::Logger::debug ( const std::string & msg)

Definition at line 20 of file Logger.cpp.

20{ log(Level::Debug, msg); }
void log(Level level, const std::string &msg)
Definition Logger.cpp:25

References bsc::logging::Debug, and log().

◆ error()

void bsc::logging::Logger::error ( const std::string & msg)

Definition at line 23 of file Logger.cpp.

References bsc::logging::Error, and log().

◆ info()

◆ instance()

◆ level_name()

std::string bsc::logging::Logger::level_name ( Level level) const
private

Definition at line 52 of file Logger.cpp.

52 {
53 switch (level) {
54 case Level::Debug: return "DEBUG";
55 case Level::Info: return "INFO";
56 case Level::Warning: return "WARN";
57 case Level::Error: return "ERROR";
58 }
59 return "INFO";
60}

References bsc::logging::Debug, bsc::logging::Error, bsc::logging::Info, and bsc::logging::Warning.

Referenced by log().

◆ log()

void bsc::logging::Logger::log ( Level level,
const std::string & msg )
private

Definition at line 25 of file Logger.cpp.

25 {
26 if (static_cast<int>(level) < static_cast<int>(level_)) return;
27 std::lock_guard lock(mutex_);
28 std::ostringstream line;
29 line << timestamp() << " [" << level_name(level) << "] " << msg << '\n';
30 std::cerr << line.str();
31 if (!file_path_.empty()) {
32 std::ofstream out(file_path_, std::ios::app);
33 if (out) out << line.str();
34 }
35}
std::mutex mutex_
Definition Logger.hpp:30
std::string file_path_
Definition Logger.hpp:29
std::string timestamp() const
Definition Logger.cpp:37
std::string level_name(Level level) const
Definition Logger.cpp:52

References file_path_, level_, level_name(), mutex_, and timestamp().

Referenced by debug(), error(), info(), and warning().

◆ set_level()

void bsc::logging::Logger::set_level ( Level level)

Definition at line 16 of file Logger.cpp.

16{ level_ = level; }

References level_.

Referenced by bsc::service::DaemonService::handle_notification().

◆ set_log_file()

void bsc::logging::Logger::set_log_file ( const std::string & path)

Definition at line 19 of file Logger.cpp.

19{ file_path_ = path; }

References file_path_.

◆ set_microseconds()

void bsc::logging::Logger::set_microseconds ( bool enabled)

Definition at line 18 of file Logger.cpp.

18{ micro_ = enabled; }

References micro_.

Referenced by bsc::service::DaemonService::handle_notification().

◆ set_utc()

void bsc::logging::Logger::set_utc ( bool enabled)

Definition at line 17 of file Logger.cpp.

17{ utc_ = enabled; }

References utc_.

Referenced by bsc::service::DaemonService::handle_notification().

◆ timestamp()

std::string bsc::logging::Logger::timestamp ( ) const
private

Definition at line 37 of file Logger.cpp.

37 {
38 using namespace std::chrono;
39 auto now = system_clock::now();
40 auto tt = system_clock::to_time_t(now);
41 std::tm tm{};
42 if (utc_) gmtime_r(&tt, &tm); else localtime_r(&tt, &tm);
43 std::ostringstream oss;
44 oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
45 if (micro_) {
46 auto us = duration_cast<microseconds>(now.time_since_epoch()) % seconds(1);
47 oss << '.' << std::setw(6) << std::setfill('0') << us.count();
48 }
49 return oss.str();
50}

References micro_, and utc_.

Referenced by log().

◆ warning()

void bsc::logging::Logger::warning ( const std::string & msg)

Member Data Documentation

◆ file_path_

std::string bsc::logging::Logger::file_path_
private

Definition at line 29 of file Logger.hpp.

Referenced by log(), and set_log_file().

◆ level_

Level bsc::logging::Logger::level_ {Level::Info}
private

Definition at line 26 of file Logger.hpp.

Referenced by log(), and set_level().

◆ micro_

bool bsc::logging::Logger::micro_ {false}
private

Definition at line 28 of file Logger.hpp.

Referenced by set_microseconds(), and timestamp().

◆ mutex_

std::mutex bsc::logging::Logger::mutex_
mutableprivate

Definition at line 30 of file Logger.hpp.

Referenced by log().

◆ utc_

bool bsc::logging::Logger::utc_ {true}
private

Definition at line 27 of file Logger.hpp.

Referenced by set_utc(), and timestamp().


The documentation for this class was generated from the following files:
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/include/bsc/logging/Logger.hpp
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/src/logging/Logger.cpp