YggdrasilWM  0.1.1
A tiny window manager coded in C++
Logger Class Reference

Logger class This class is responsible for logging. It can be created with a file name or an ostream. The log level can be set to filter the messages. More...

#include <Logger.hpp>

Collaboration diagram for Logger:
Collaboration graph

Public Member Functions

 Logger (const Logger &)=delete
 
Loggeroperator= (const Logger &)=delete
 
virtual ~Logger ()
 Destroy the Logger:: Logger object closes the log file if it was opened. The destructor is virtual to allow for subclassing.for gmock. More...
 
virtual void Log (const std::string &message, LogLevel level) const
 Log a message This method logs a message to the log file or stream. The message is only logged if the log level is high enough. the time and log level are prepended to the message. The method is virtual to allow for subclassing.for gmock. More...
 

Static Public Member Functions

static void Create (const std::string &logFile, LogLevel logLevel)
 
static void Create (std::ostream &output, LogLevel logLevel)
 
static LoggerGetInstance ()
 Get the Logger object. More...
 
static void Destroy ()
 Destroy the Logger:: Logger object closes the log file if it was opened. More...
 

Private Member Functions

 Logger (const std::string &logFile, LogLevel logLevel)
 Construct a new Logger:: Logger object This constructor is used when the user wants to log to a file. More...
 
 Logger (std::ostream &output, LogLevel logLevel)
 Construct a new Logger:: Logger object This constructor is used when the user wants to log to a stream. More...
 

Static Private Member Functions

static std::string GetLogLevel (LogLevel level)
 Get the log level as a string. More...
 
static std::string GetTime ()
 Get the current time. More...
 

Private Attributes

bool streamIsFile_
 
std::ostream * logStream_
 
LogLevel logLevel_
 

Static Private Attributes

static Loggerinstance_ = nullptr
 

Detailed Description

Logger class This class is responsible for logging. It can be created with a file name or an ostream. The log level can be set to filter the messages.

Constructor & Destructor Documentation

◆ Logger() [1/3]

Logger::Logger ( const Logger )
delete

Referenced by Create().

Here is the caller graph for this function:

◆ ~Logger()

Logger::~Logger ( )
virtual

Destroy the Logger:: Logger object closes the log file if it was opened. The destructor is virtual to allow for subclassing.for gmock.

62  {
63  *logStream_ << GetTime() << "Closing Session \n"
64  << " =================================================================================== "
65  << std::endl;
66  if (streamIsFile_) {
67  if (dynamic_cast<std::ofstream*>(logStream_) != nullptr) {
68  dynamic_cast<std::ofstream*>(logStream_)->close();
69  }
70  delete logStream_;
71  }
72 }
static std::string GetTime()
Get the current time.
Definition: Logger.cpp:79
bool streamIsFile_
Definition: Logger.hpp:146
std::ostream * logStream_
Definition: Logger.hpp:148

References GetTime(), logStream_, and streamIsFile_.

Here is the call graph for this function:

◆ Logger() [2/3]

Logger::Logger ( const std::string &  logFile,
LogLevel  logLevel 
)
private

Construct a new Logger:: Logger object This constructor is used when the user wants to log to a file.

Parameters
logFilethe file to log to
logLevellevel of logging 0: info, 1: warning, 2: error
48  : logLevel_(logLevel) {
49  auto* fileStream = new std::ofstream(logFile, std::ios::out | std::ios::app);
50  if (!fileStream->good()) {
51  std::cerr << "Failed to open log file: " << logFile << std::endl;
52  delete fileStream;
53  throw std::runtime_error("Failed to open log file.");
54  }
55  logStream_ = fileStream;
56  streamIsFile_ = true;
57 }
LogLevel logLevel_
Definition: Logger.hpp:149

References logStream_, and streamIsFile_.

◆ Logger() [3/3]

Logger::Logger ( std::ostream &  output,
LogLevel  logLevel 
)
private

Construct a new Logger:: Logger object This constructor is used when the user wants to log to a stream.

Parameters
outputthe stream to log to
logLevellevel of logging 0: info, 1: warning, 2: error
59  : logStream_(&output), logLevel_(logLevel) {
60  streamIsFile_ = false;
61 }

References streamIsFile_.

Member Function Documentation

◆ Create() [1/2]

void Logger::Create ( const std::string &  logFile,
LogLevel  logLevel 
)
static
31  {
32  if (instance_ == nullptr) {
33  instance_ = new Logger(logFile, logLevel);
34  }
35 }
static Logger * instance_
Definition: Logger.hpp:147
Logger(const Logger &)=delete

References instance_, and Logger().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create() [2/2]

void Logger::Create ( std::ostream &  output,
LogLevel  logLevel 
)
static
36  {
37  if (instance_ == nullptr) {
38  instance_ = new Logger(output, logLevel);
39  }
40 }

References instance_, and Logger().

Here is the call graph for this function:

◆ Destroy()

static void Logger::Destroy ( )
static

Destroy the Logger:: Logger object closes the log file if it was opened.

99  {
100  if (instance_ != nullptr) {
101  delete instance_;
102  instance_ = nullptr;
103  }
104 }

References instance_.

Referenced by main().

Here is the caller graph for this function:

◆ GetInstance()

static Logger * Logger::GetInstance ( )
static

Get the Logger object.

Returns
41  {
42  if (instance_ == nullptr) {
43  throw std::runtime_error("Logger instance not created");
44  }
45  return instance_;
46 }

References instance_.

Referenced by WindowManager::addGroupsFromConfig(), Bar::addWidget(), Client::Client(), ConfigFileHandler::ConfigFileHandler(), ConfigHandler::configInit(), ConfigDataBar::configInit(), ConfigDataGroup::configInit(), ConfigDataGroups::configInit(), ConfigDataWidget::configInit(), ConfigDataBars::configInit(), ConfigDataBindings::configInit(), WindowManager::create(), EventHandler::dispatchEvent(), FocusGroup::execute(), Spawn::execute(), Client::frame(), WindowManager::getTopLevelWindows(), Bars::getWidgetTypeHandle(), Group::Group(), EventHandler::handleButtonPress(), EventHandler::handleDestroyNotify(), EventHandler::handleFocusIn(), EventHandler::handleFocusOut(), ConfigDataBindings::handleKeypressEvent(), EventHandler::handleMapNotify(), EventHandler::handleMapRequest(), ewmh::handleMessage(), EventHandler::handlePropertyNotify(), EventHandler::handleUnmapNotify(), Bars::init(), Binding::init_keycode(), ewmh::initEwmh(), WindowManager::insertClient(), main(), Client::move(), Group::moveClientToGroup(), WindowManager::OnXError(), ConfigFileHandler::readConfig(), Group::removeClient(), Client::resize(), Client::restack(), Bars::run(), WindowManager::Run(), Group::switchFrom(), Group::switchTo(), Client::unframe(), ewmh::updateDesktopGeometry(), ConfigFileHandler::writeConfig(), Bars::~Bars(), Client::~Client(), and WindowManager::~WindowManager().

◆ GetLogLevel()

std::string Logger::GetLogLevel ( LogLevel  level)
staticprivate

Get the log level as a string.

Parameters
level
Returns
a string representation of the log level
86  {
87  switch (level) {
88  case L_INFO:
89  return "[INFO]\t";
90  case L_WARNING:
91  return "[WARNING]\t";
92  case L_ERROR:
93  return "[ERROR]\t";
94  default:
95  return "[UNKNOWN]\t";
96  }
97 }
@ L_ERROR
Definition: Logger.hpp:51
@ L_WARNING
Definition: Logger.hpp:50
@ L_INFO
Definition: Logger.hpp:49

References L_ERROR, L_INFO, and L_WARNING.

Referenced by Log().

Here is the caller graph for this function:

◆ GetTime()

std::string Logger::GetTime ( )
staticprivate

Get the current time.

  • This method returns the current time in the format: [YYYY-MM-DD-HH:MM:SS]
    Returns
79  {
80  time_t now = time(nullptr);
81  tm* ltm = localtime(&now);
82  std::stringstream ss;
83  ss << std::put_time(ltm, "[%Y-%m-%d-%H:%M:%S]\t");
84  return ss.str();
85 }

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

Here is the caller graph for this function:

◆ Log()

void Logger::Log ( const std::string &  message,
LogLevel  level 
) const
virtual

Log a message This method logs a message to the log file or stream. The message is only logged if the log level is high enough. the time and log level are prepended to the message. The method is virtual to allow for subclassing.for gmock.

Parameters
messagethe message to log
levelthe level of the message
73  {
74  if (level < logLevel_) {
75  return;
76  }
77  *logStream_ << GetTime() << GetLogLevel(level) << message << std::endl;
78 }
static std::string GetLogLevel(LogLevel level)
Get the log level as a string.
Definition: Logger.cpp:86

References GetLogLevel(), GetTime(), logLevel_, and logStream_.

Referenced by WindowManager::addGroupsFromConfig(), Bar::addWidget(), Client::Client(), ConfigFileHandler::ConfigFileHandler(), ConfigHandler::configInit(), ConfigDataBar::configInit(), ConfigDataGroup::configInit(), ConfigDataGroups::configInit(), ConfigDataWidget::configInit(), ConfigDataBars::configInit(), ConfigDataBindings::configInit(), WindowManager::create(), EventHandler::dispatchEvent(), FocusGroup::execute(), Spawn::execute(), Client::frame(), WindowManager::getTopLevelWindows(), Bars::getWidgetTypeHandle(), Group::Group(), EventHandler::handleButtonPress(), EventHandler::handleDestroyNotify(), EventHandler::handleFocusIn(), EventHandler::handleFocusOut(), ConfigDataBindings::handleKeypressEvent(), EventHandler::handleMapNotify(), EventHandler::handleMapRequest(), ewmh::handleMessage(), EventHandler::handlePropertyNotify(), EventHandler::handleUnmapNotify(), Bars::init(), Binding::init_keycode(), ewmh::initEwmh(), WindowManager::insertClient(), main(), Client::move(), Group::moveClientToGroup(), WindowManager::OnXError(), ConfigFileHandler::readConfig(), Group::removeClient(), Client::resize(), Client::restack(), Bars::run(), WindowManager::Run(), Group::switchFrom(), Group::switchTo(), Client::unframe(), ewmh::updateDesktopGeometry(), ConfigFileHandler::writeConfig(), Bars::~Bars(), Client::~Client(), and WindowManager::~WindowManager().

Here is the call graph for this function:

◆ operator=()

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

Member Data Documentation

◆ instance_

Logger * Logger::instance_ = nullptr
staticprivate

Referenced by Create(), Destroy(), and GetInstance().

◆ logLevel_

LogLevel Logger::logLevel_
private

Referenced by Log().

◆ logStream_

std::ostream* Logger::logStream_
private

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

◆ streamIsFile_

bool Logger::streamIsFile_
private

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


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