Created
October 13, 2018 17:02
-
-
Save vakho10/0ac3ac6d579f3f1540d531569f47afaf to your computer and use it in GitHub Desktop.
Basic Logger class in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Cat.h" | |
Cat::Cat(std::string name) | |
{ | |
m_name = name; | |
m_sound = "Meow!"; | |
} | |
std::string Cat::sound() | |
{ | |
std::stringstream messageSstream; | |
messageSstream << "Called sound method of Cat with sound: " << m_sound; | |
std::string message = messageSstream.str(); | |
logger->info(message); | |
return m_sound; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Logger.h" | |
#include <string> | |
#include <sstream> | |
class Cat { | |
private: | |
Logger* logger = new Logger(LoggingLevel::TRACE, "resources\\cat_log.txt"); | |
std::string m_name; | |
std::string m_sound; | |
public: | |
Cat(std::string name); | |
std::string sound(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Logger.h" | |
Logger::Logger() { | |
} | |
Logger::Logger(LoggingLevel level) { | |
this->setFilter(level); | |
} | |
Logger::Logger(LoggingLevel level, std::string filePath) { | |
this->setFilter(level); | |
this->addFileLog(filePath); | |
} | |
Logger::Logger(std::string filePath) { | |
this->addFileLog(filePath); | |
} | |
Logger::~Logger() | |
{ | |
m_ofs->close(); | |
} | |
std::string Logger::levelToString(LoggingLevel level) { | |
std::string retValue; | |
switch (level) | |
{ | |
case TRACE: retValue = "trace"; | |
break; | |
case DEBUG: retValue = "debug"; | |
break; | |
case INFO: retValue = "info"; | |
break; | |
case WARN: retValue = "warn"; | |
break; | |
case ERROR: retValue = "error"; | |
break; | |
case FATAL: retValue = "fatal"; | |
break; | |
default: retValue = std::to_string(level); | |
break; | |
} | |
return retValue; | |
} | |
void Logger::log(LoggingLevel level, std::string message) { | |
if (m_level <= level) { | |
std::cout << "[" << levelToString(level) << "]: " << message << std::endl; | |
if (m_ofs) { | |
(*m_ofs) << "[" << levelToString(level) << "]: " << message << std::endl; | |
} | |
} | |
} | |
void Logger::trace(std::string message) | |
{ | |
this->log(LoggingLevel::TRACE, message); | |
} | |
void Logger::debug(std::string message) | |
{ | |
this->log(LoggingLevel::DEBUG, message); | |
} | |
void Logger::info(std::string message) | |
{ | |
this->log(LoggingLevel::INFO, message); | |
} | |
void Logger::warn(std::string message) | |
{ | |
this->log(LoggingLevel::WARN, message); | |
} | |
void Logger::error(std::string message) | |
{ | |
this->log(LoggingLevel::ERROR, message); | |
} | |
void Logger::fatal(std::string message) | |
{ | |
this->log(LoggingLevel::FATAL, message); | |
} | |
void Logger::setFilter(LoggingLevel level) | |
{ | |
m_level = level; | |
} | |
void Logger::addFileLog(std::string filePath) | |
{ | |
this->m_ofs = new std::ofstream(filePath); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include "LoggingLevel.h" | |
class Logger { | |
private: | |
LoggingLevel m_level = LoggingLevel::INFO; | |
std::ofstream* m_ofs = NULL; | |
std::string levelToString(LoggingLevel level); | |
void log(LoggingLevel level, std::string message); | |
public: | |
Logger(); | |
Logger(LoggingLevel level); | |
Logger(LoggingLevel level, std::string filePath); | |
Logger(std::string filePath); | |
~Logger(); | |
void trace(std::string message); | |
void debug(std::string message); | |
void info(std::string message); | |
void warn(std::string message); | |
void error(std::string message); | |
void fatal(std::string message); | |
void setFilter(LoggingLevel level); | |
void addFileLog(std::string filePath); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
enum LoggingLevel { | |
TRACE, DEBUG, INFO, WARN, ERROR, FATAL | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Logger.h" | |
#include "Cat.h" | |
int main() { | |
Logger* logger = new Logger(); | |
logger->setFilter(LoggingLevel::TRACE); | |
logger->addFileLog("resources\\log.txt"); | |
// Cat logic | |
Cat* cat = new Cat("Piso"); | |
std::cout << cat->sound() << std::endl; | |
delete logger; | |
logger = NULL; | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment