Botcraft 1.21.4
Loading...
Searching...
No Matches
Botcraft::Logger Class Reference

#include <Logger.hpp>

Public Member Functions

 Logger (const Logger &)=delete
 
Loggeroperator= (const Logger &)=delete
 
 Logger (Logger &&)=delete
 
Loggeroperator= (Logger &&)=delete
 
 ~Logger ()
 
void Log (const std::string &s)
 
void SetFilename (const std::string &s)
 
void SetLogLevel (const LogLevel l)
 
LogLevel GetLogLevel () const
 
void SetLogFunc (const std::function< void(const std::string &)> &f)
 
std::stringstream GetDate () const
 
void RegisterThread (const std::string &name)
 Register the current thread in the map.
 
void RegisterThread (const std::thread::id id, const std::string &name)
 Register a thread in the map.
 
std::string GetThreadName (const std::thread::id id)
 Get the name of a given thread.
 
void UnregisterThread (const std::thread::id id)
 Remove a thread from the map.
 

Static Public Member Functions

static LoggerGetInstance ()
 

Static Public Attributes

static constexpr std::array< std::string_view, static_cast< size_t >(LogLevel::NUM_LOG_LEVEL)> level_strings
 

Private Member Functions

 Logger ()
 

Private Attributes

std::mutex mutex
 
std::string filename
 
std::atomic< LogLevellog_level
 
std::function< void(const std::string &)> log_func
 
std::chrono::steady_clock::time_point last_time_logged
 
std::string file_buffer
 
std::mutex thread_mutex
 
std::unordered_map< std::thread::id, std::string > thread_names
 

Detailed Description

Definition at line 63 of file Logger.hpp.

Constructor & Destructor Documentation

◆ Logger() [1/3]

Botcraft::Logger::Logger ( )
private

Definition at line 12 of file Logger.cpp.

References file_buffer, filename, Botcraft::Info, last_time_logged, log_func, and log_level.

◆ Logger() [2/3]

Botcraft::Logger::Logger ( const Logger )
delete

◆ Logger() [3/3]

Botcraft::Logger::Logger ( Logger &&  )
delete

◆ ~Logger()

Botcraft::Logger::~Logger ( )

Definition at line 25 of file Logger.cpp.

References file_buffer, filename, and mutex.

Member Function Documentation

◆ GetDate()

std::stringstream Botcraft::Logger::GetDate ( ) const

Definition at line 84 of file Logger.cpp.

◆ GetInstance()

◆ GetLogLevel()

LogLevel Botcraft::Logger::GetLogLevel ( ) const

Definition at line 73 of file Logger.cpp.

References log_level.

◆ GetThreadName()

std::string Botcraft::Logger::GetThreadName ( const std::thread::id  id)

Get the name of a given thread.

Parameters
idThread id
Returns
The name of the thread, "" if not in map

Definition at line 124 of file Logger.cpp.

References thread_mutex, and thread_names.

◆ Log()

void Botcraft::Logger::Log ( const std::string &  s)

Definition at line 42 of file Logger.cpp.

References file_buffer, filename, last_time_logged, log_func, and mutex.

◆ operator=() [1/2]

Logger & Botcraft::Logger::operator= ( const Logger )
delete

◆ operator=() [2/2]

Logger & Botcraft::Logger::operator= ( Logger &&  )
delete

◆ RegisterThread() [1/2]

void Botcraft::Logger::RegisterThread ( const std::string &  name)

◆ RegisterThread() [2/2]

void Botcraft::Logger::RegisterThread ( const std::thread::id  id,
const std::string &  name 
)

Register a thread in the map.

It will NOT be automatically removed on thread exit.

Parameters
idThread id
nameThread name

Definition at line 118 of file Logger.cpp.

References thread_mutex, and thread_names.

◆ SetFilename()

void Botcraft::Logger::SetFilename ( const std::string &  s)

Definition at line 62 of file Logger.cpp.

References filename, and mutex.

◆ SetLogFunc()

void Botcraft::Logger::SetLogFunc ( const std::function< void(const std::string &)> &  f)

Definition at line 78 of file Logger.cpp.

References log_func, and mutex.

◆ SetLogLevel()

void Botcraft::Logger::SetLogLevel ( const LogLevel  l)

Definition at line 68 of file Logger.cpp.

References log_level.

◆ UnregisterThread()

void Botcraft::Logger::UnregisterThread ( const std::thread::id  id)

Remove a thread from the map.

Parameters
idThread id

Definition at line 130 of file Logger.cpp.

References thread_mutex, and thread_names.

Referenced by Botcraft::TCP_Com::~TCP_Com().

Member Data Documentation

◆ file_buffer

std::string Botcraft::Logger::file_buffer
private

Definition at line 117 of file Logger.hpp.

Referenced by Log(), Logger(), and ~Logger().

◆ filename

std::string Botcraft::Logger::filename
private

Definition at line 112 of file Logger.hpp.

Referenced by Log(), Logger(), SetFilename(), and ~Logger().

◆ last_time_logged

std::chrono::steady_clock::time_point Botcraft::Logger::last_time_logged
private

Definition at line 116 of file Logger.hpp.

Referenced by Log(), and Logger().

◆ level_strings

constexpr std::array<std::string_view, static_cast<size_t>(LogLevel::NUM_LOG_LEVEL)> Botcraft::Logger::level_strings
staticconstexpr
Initial value:
=
{
"[ TRACE ]",
"[ DEBUG ]",
"[ INFO ]",
"[WARNING]",
"[ ERROR ]",
"[ FATAL ]",
"[ ]"
}

Definition at line 68 of file Logger.hpp.

◆ log_func

std::function<void(const std::string&)> Botcraft::Logger::log_func
private

Definition at line 114 of file Logger.hpp.

Referenced by Log(), Logger(), and SetLogFunc().

◆ log_level

std::atomic<LogLevel> Botcraft::Logger::log_level
private

Definition at line 113 of file Logger.hpp.

Referenced by GetLogLevel(), Logger(), and SetLogLevel().

◆ mutex

std::mutex Botcraft::Logger::mutex
private

Definition at line 111 of file Logger.hpp.

Referenced by Log(), SetFilename(), SetLogFunc(), and ~Logger().

◆ thread_mutex

std::mutex Botcraft::Logger::thread_mutex
private

Definition at line 119 of file Logger.hpp.

Referenced by GetThreadName(), RegisterThread(), RegisterThread(), and UnregisterThread().

◆ thread_names

std::unordered_map<std::thread::id, std::string> Botcraft::Logger::thread_names
private

Definition at line 120 of file Logger.hpp.

Referenced by GetThreadName(), RegisterThread(), RegisterThread(), and UnregisterThread().


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