jubilant-funicular
Logger.cpp
1 #include <iomanip>
2 
3 #include <SDL2/SDL.h>
4 
5 #include "nta/Logger.h"
6 
7 namespace nta {
8  std::ofstream Logger::m_logFile;
9  std::ostream* Logger::m_secondLog = nullptr;
10  size_t Logger::m_tabs = 0;
11  const size_t Logger::TAB_SIZE = 4;
13  m_logFile.open("Log.log");
14  m_logFile<<"";
15  writeToLog("Log created");
16  }
17  void Logger::writeToLog(crstring entry) {
18  std::string tabs(m_tabs, ' ');
19 
20  std::stringstream logEntry;
21  logEntry<<std::fixed<<std::setprecision(3);
22  logEntry<<SDL_GetTicks()/1000.<<" seconds: "<<tabs<<entry;
23 
24  m_logFile<<lock_stream<<logEntry.str()<<std::endl<<std::endl<<unlock_stream;
25  if (m_secondLog) {
26  (*m_secondLog)<<lock_stream<<logEntry.str()<<std::endl<<std::endl<<unlock_stream;
27  }
28  }
31  Error Logger::writeErrorToLog(crstring error, ErrorType type) {
32  const std::string err_indicator =
33  "********** ERROR (" + get_errortype_string(type) + ") **********";
34 
35  writeToLog(err_indicator);
36  writeToLog(error);
37  writeToLog(err_indicator);
38 
39  Error err = Error(error, type);
41  return err;
42  }
43  void Logger::indent(size_t tab_size) {
44  m_tabs += tab_size;
45  }
46  void Logger::unindent(size_t tab_size) {
47  if (m_tabs >= tab_size) m_tabs -= tab_size;
48  }
49 
50  ScopeLog::ScopeLog(crstring entry, crstring exit) : on_exit(exit) {
51  Logger::writeToLog(entry);
53  }
54  ScopeLog::~ScopeLog() {
56  Logger::writeToLog(on_exit);
57  }
58 }
nta::ErrorManager::push_error
static void push_error(const Error &err)
Adds and error to the front of the list.
Definition: Errors.cpp:28
nta::get_errortype_string
std::string get_errortype_string(ErrorType t)
converts ErrorType enum to string
Definition: Errors.cpp:48
nta::Logger::writeToLog
static void writeToLog(crstring entry)
writes an entry in the log
Definition: Logger.cpp:17
nta::Logger::unindent
static void unindent(size_t tab_size=TAB_SIZE)
unindents entries
Definition: Logger.cpp:46
nta::Error
Something went wrong.
Definition: Errors.h:24
nta::unlock_stream
std::ostream & unlock_stream(std::ostream &stream)
Unlock stream.
Definition: MyEngine.cpp:111
nta
Definition: Animation2D.h:6
nta::lock_stream
std::ostream & lock_stream(std::ostream &stream)
Lock stream for thread-safe manipulation.
Definition: MyEngine.cpp:103
nta::ErrorType
ErrorType
Types of Error.
Definition: Errors.h:13
nta::Logger::m_logFile
static std::ofstream m_logFile
the file that keeps the external log
Definition: Logger.h:14
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31
nta::Logger::createLog
static void createLog()
creates the log
Definition: Logger.cpp:12
nta::Logger::m_secondLog
static std::ostream * m_secondLog
Optional secondary stream to log data to as well.
Definition: Logger.h:16
nta::Logger::indent
static void indent(size_t tab_size=TAB_SIZE)
indents entries
Definition: Logger.cpp:43