Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Logger Class Reference

Logger class for all text output. More...

#include <Logger.h>

+ Inheritance diagram for Leviathan::Logger:

Public Member Functions

DLLEXPORT Logger (const std::string &file)
 
virtual DLLEXPORT ~Logger ()
 
DLLEXPORT void Write (const std::string &data) override
 
DLLEXPORT void Info (const std::string &data) override
 
DLLEXPORT void Error (const std::string &data) override
 
DLLEXPORT void Warning (const std::string &data) override
 
DLLEXPORT void WriteLine (const std::string &Text) override
 
DLLEXPORT void Fatal (const std::string &Text) override
 Quits the current game with an error message. More...
 
DLLEXPORT void WriteRaw (const std::string &data)
 Prints output without extra newlines (make sure to add them manually) More...
 
DLLEXPORT void Save ()
 
DLLEXPORT void DirectWriteBuffer (const std::string &data)
 Adds raw data to the queue unmodified. More...
 
DLLEXPORT std::string GetLogFile () const
 Gets the file the log is being written to. More...
 

Static Public Member Functions

static DLLEXPORT void SendDebugMessage (const std::string &str)
 
static DLLEXPORT void Print (const std::string &message)
 Script wrapper. More...
 
static DLLEXPORT LoggerGet ()
 

Detailed Description

Logger class for all text output.

Todo:
Allow logs that don't save to a file

Definition at line 10 of file Logger.h.

Constructor & Destructor Documentation

◆ Logger()

DLLEXPORT Logger::Logger ( const std::string &  file)

Definition at line 33 of file Logger.cpp.

33  :
34  Path(file)
35 {
36 
37  // Get time for putting to the beginning of the log file //
38  auto t = std::time(nullptr);
39 
40  std::stringstream formatedtime;
41 
42 #ifdef _WIN32
43  struct tm curtime;
44  localtime_s(&curtime, &t);
45  formatedtime << std::put_time(&curtime, "%H:%M:%S %A %d.%m.%Y (%Z)");
46 #else
47  struct tm* curtime = localtime(&t);
48  formatedtime << std::put_time(curtime, "%H:%M:%S %A %d.%m.%Y (%Z)");
49 #endif //_WIN32
50 
51  string write = "Start of Leviathan log for leviathan version: " + VERSIONS;
52 
53  write += "\nWriting to file \"" + file + "\"";
54  write += "\n------------------------TIME: " + formatedtime.str() + "------------------------\n";
55 
56  // Create the target folder if it doesn't exist
57  const auto path = boost::filesystem::path(file).parent_path();
58 
59  if(!path.empty()){
60  try{
61  boost::filesystem::create_directories(path);
62  } catch(const std::exception &e){
63 
64  SendDebugMessage("Failed to create folder for log file, exception: " +
65  std::string(e.what()));
66  }
67  }
68 
69  std::ofstream writer(Path);
70 
71  if (!writer.is_open()) {
72 
73  #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
74  throw Exception("Cannot open log file");
75  #else
76  SendDebugMessage("Cannot open log file, quitting");
77  abort(6);
78  #endif //ALTERNATIVE_EXCEPTIONS_FATAL
79  }
80 
81  writer << write;
82 
83  writer.close();
84 
85 
86  PendingLog = "";
87  LatestLogger = this;
88 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
Base class for all exceptions thrown by Leviathan.
Definition: Exceptions.h:10

◆ ~Logger()

Logger::~Logger ( )
virtual

Definition at line 93 of file Logger.cpp.

93  {
94  // Save if unsaved //
95  Lock lock(LoggerWriteMutex);
96 
97  _Save();
98 
99  // Reset latest logger (this allows to create new logger,
100  // which is quite bad, but won't crash
101  // There is also probably a race condition here
102  if(LatestLogger == this)
103  LatestLogger = nullptr;
104 }
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

Member Function Documentation

◆ DirectWriteBuffer()

DLLEXPORT void Logger::DirectWriteBuffer ( const std::string &  data)

Adds raw data to the queue unmodified.

Note
You will need to add new lines '
' manually

Definition at line 237 of file Logger.cpp.

237  {
238 
239  Lock guard(LoggerWriteMutex);
240 
241  PendingLog += data;
242 }
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ Error()

DLLEXPORT void Logger::Error ( const std::string &  data)
overridevirtual

Implements LErrorReporter.

Definition at line 177 of file Logger.cpp.

177  {
178 
179  const auto message = "[ERROR] " + data + "\n";
180 
181  Lock lock(LoggerWriteMutex);
182 
183  SendDebugMessage(message);
184 
185  PendingLog += message;
186 
187  _LogUpdateEndPart();
188 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ Fatal()

void Logger::Fatal ( const std::string &  Text)
overridevirtual

Quits the current game with an error message.

Implements LErrorReporter.

Definition at line 142 of file Logger.cpp.

142  {
143 
144  const auto message = "[FATAL] " + data + "\n";
145 
146  {
147  Lock lock(LoggerWriteMutex);
148 
149  SendDebugMessage(message);
150 
151  PendingLog += message;
152 
153  _LogUpdateEndPart();
154 
155  // This might call Save for the second time, but better call
156  // it twice than miss saving the log before aborting
157  _Save();
158  }
159 
160  // Exit process //
161  abort();
162 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ Get()

DLLEXPORT Logger * Logger::Get ( )
static

Definition at line 106 of file Logger.cpp.

106  {
107 
108  return LatestLogger;
109 }

◆ GetLogFile()

DLLEXPORT std::string Logger::GetLogFile ( ) const

Gets the file the log is being written to.

Definition at line 244 of file Logger.cpp.

244  {
245 
246  return Path;
247 }

◆ Info()

DLLEXPORT void Logger::Info ( const std::string &  data)
overridevirtual

Implements LErrorReporter.

Definition at line 164 of file Logger.cpp.

164  {
165 
166  const auto message = "[INFO] " + data + "\n";
167 
168  Lock lock(LoggerWriteMutex);
169 
170  SendDebugMessage(message);
171 
172  PendingLog += message;
173 
174  _LogUpdateEndPart();
175 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ Print()

void Logger::Print ( const std::string &  message)
static

Script wrapper.

Definition at line 223 of file Logger.cpp.

223  {
224  Get()->Write(message);
225 }
DLLEXPORT void Write(const std::string &data) override
Definition: Logger.cpp:113
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ Save()

void Logger::Save ( )

Definition at line 203 of file Logger.cpp.

203  {
204 
205  Lock lock(LoggerWriteMutex);
206 
207  _Save();
208 }
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ SendDebugMessage()

DLLEXPORT void Logger::SendDebugMessage ( const std::string &  str)
static

Definition at line 227 of file Logger.cpp.

227  {
228 #ifdef _WIN32
229  const wstring converted = Convert::Utf8ToUtf16(str);
230  OutputDebugString(&*converted.begin());
231 #endif // _WIN32
232  // We also want standard output messages //
233  // Using cout should be fine for most other platforms //
234  std::cout << str;
235 }
static DLLEXPORT std::wstring Utf8ToUtf16(const std::string &utf8str)
Decodes an UTF8 string to an UTF16 string (wide string/wstring)
Definition: Convert.cpp:76

◆ Warning()

DLLEXPORT void Logger::Warning ( const std::string &  data)
overridevirtual

Implements LErrorReporter.

Definition at line 190 of file Logger.cpp.

190  {
191 
192  const auto message = "[WARNING] " + data + "\n";
193 
194  Lock lock(LoggerWriteMutex);
195 
196  SendDebugMessage(message);
197 
198  PendingLog += message;
199 
200  _LogUpdateEndPart();
201 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ Write()

DLLEXPORT void Logger::Write ( const std::string &  data)
overridevirtual

Implements LErrorReporter.

Definition at line 113 of file Logger.cpp.

113  {
114 
115  const auto message = data+"\n";
116 
117  Lock lock(LoggerWriteMutex);
118 
119  SendDebugMessage(message);
120 
121  PendingLog += message;
122 
123  _LogUpdateEndPart();
124 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

◆ WriteLine()

void Logger::WriteLine ( const std::string &  Text)
overridevirtual

Implements LErrorReporter.

Definition at line 137 of file Logger.cpp.

137  {
138 
139  Write(Text);
140 }
DLLEXPORT void Write(const std::string &data) override
Definition: Logger.cpp:113

◆ WriteRaw()

DLLEXPORT void Logger::WriteRaw ( const std::string &  data)

Prints output without extra newlines (make sure to add them manually)

This doesn't play nice with tests so use sparingly

Definition at line 126 of file Logger.cpp.

126  {
127 
128  Lock lock(LoggerWriteMutex);
129 
130  SendDebugMessage(data);
131 
132  PendingLog += data;
133 
134  _LogUpdateEndPart();
135 }
static DLLEXPORT void SendDebugMessage(const std::string &str)
Definition: Logger.cpp:227
static Mutex LoggerWriteMutex
Lock when using the logger singleton.
Definition: Logger.cpp:91
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16

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