BARE2D
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.hpp"
2 
3 #include <chrono>
4 
5 #include "BAREErrors.hpp"
6 
7 namespace BARE2D {
8 
9  Logger* Logger::m_instance = nullptr;
10 
12  {
13  // Open the log file!
14  m_logFile.open("GameLog.txt", std::ios::trunc);
15 
16  // Make sure we can load it.
17  if(m_logFile.fail()) {
18  throwFatalError(BAREError::LOGGER_FAILURE, "Logger failed to open log file.");
19  return;
20  }
21 
22  // Just to timestamp the log
23  auto timeA = std::chrono::system_clock::now();
24  auto timeB = std::chrono::system_clock::to_time_t(timeA);
25 
26  std::string date = std::ctime(&timeB);
27  date.pop_back();
28 
29  m_logFile << "\n\n\n### LOG: " << date << " ###\n\n";
30  m_logFile << "Thanks for using BARE2D! Original engine by Davis Garrad\n\n";
31  m_logFile.flush();
32  }
33 
35  {
36  if (m_instance == nullptr) {
37  m_instance = new Logger();
38  }
39  return m_instance;
40  }
41 
42  void Logger::log(std::string message, bool important/** = false*/) {
43  if(important) {
44  message = "\n### " + message + " ###\n";
45  }
46  message = message + "\n";
47  m_logFile << message;
48  std::printf("%s", message.c_str());
49  m_logFile.flush();
50  }
51 
52 }
BARE2D::Logger::Logger
Logger()
Definition: Logger.cpp:11
BARE2D
Definition: App.cpp:13
BARE2D::Logger::getInstance
static Logger * getInstance()
Definition: Logger.cpp:34
BARE2D::Logger::log
void log(std::string message, bool important=false)
Logs a message to a file and the terminal.
Definition: Logger.cpp:42
BAREErrors.hpp
BARE2D::Logger::m_logFile
std::ofstream m_logFile
Definition: Logger.hpp:29
BARE2D::Logger
For general purpose logging, this is the logger! It is a singleton class.
Definition: Logger.hpp:12
BARE2D::Logger::m_instance
static Logger * m_instance
Definition: Logger.hpp:31
BARE2D::BAREError::LOGGER_FAILURE
@ LOGGER_FAILURE
BARE2D::throwFatalError
void throwFatalError(BAREError err, std::string message)
Throws an error (fatal). Also calls displayErrors and exits the program.
Definition: BAREErrors.cpp:178
Logger.hpp