TrueReality  v0.1.1912
LogWriterFile.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * @author Erik Johnson
22 * @author Maxim Serebrennik
23 */
25 
26 #include <trUtil/PlatformMacros.h>
27 #include <trUtil/Exception.h>
28 #include <trUtil/FileUtils.h>
29 #include <trUtil/PathUtils.h>
30 
31 #include <iostream>
32 
33 namespace trUtil::Logging
34 {
35  const std::string LogWriterFile::DEFAULT_LOG_FOLDER = std::string("/Logs");
36 
39  : mOpenFailed(false)
40  {
41  }
42 
45  {
46  if (mLogFile.is_open())
47  {
48  EndFile();
49  }
50  }
51 
54  {
55  mLogFile << "</body></html>" << std::endl;
56  mLogFile.flush();
57  }
58 
61  {
62  if (mOpenFailed)
63  {
64  return;
65  }
66 
67  if (mLogFile.is_open())
68  {
69  mLogFile << "<p>Change to log file: " << Logging::LogFile::GetFileName() << std::endl;
70  TimeTag("At ");
71  EndFile();
72  mLogFile.close();
73  }
74 
75  std::string fullLogPath = Logging::LogFile::GetFileName();
77  {
78  //Store logs in a system dependent place.
79  std::string userLogPath = trUtil::PathUtils::GetLogPath();
80  if (!userLogPath.empty())
81  {
82  std::string logDir = userLogPath + DEFAULT_LOG_FOLDER;
83 
84  try
85  {
87  }
88  catch (const trUtil::Exception&)
89  {
90  std::cerr << "Unable to create the log directory : \"" << logDir << "\". The log file will be written to the current working directory if possible." << std::endl;
91  logDir.clear();
92  }
93 
94  fullLogPath = logDir + "/" + Logging::LogFile::GetFileName();
95 
96  }
97  //First attempt to create the log file.
98  }
99  mLogFile.open(fullLogPath.c_str());
100 
101  if (!mLogFile.is_open())
102  {
103  std::cerr << "Could not open the Log file \"" << fullLogPath << "\"" << std::endl;
104  mOpenFailed = true;
105  return;
106  }
107 
108  //Write a decent header to the html file.
109  mLogFile << "<html><title>" << Logging::LogFile::GetTitle() << "</title><body>" << std::endl;
110  mLogFile << "<h1 align=\"center\">" << Logging::LogFile::GetTitle() << "</h1><hr>" << std::endl;
111  mLogFile << "<pre><h3 align=\"center\""
112  "<font color=#000000><b> Always </b></font>"
113  "<font color=#A000A0><b> Debug </b></font>"
114  "<font color=#00A000><b> Information </b></font>"
115  "<font color=#CF6F00><b> Warning </b></font>"
116  "<font color=#C00000><b> Error </b></font></h3></pre><hr>"
117  << std::endl;
118 
119  TimeTag("Started at ");
120  mLogFile << "<br><br>" << std::endl;
121 
122  mLogFile.flush();
123  }
124 
126  void LogWriterFile::TimeTag(std::string prefix)
127  {
128  trUtil::DateTime dt;
129  dt.SetToLocalTime();
131  mLogFile.flush();
132  }
133 
135  void LogWriterFile::LogMessage(const LogData& logData)
136  {
137  if (!mLogFile.is_open())
138  {
139  OpenFile();
140 
141  if (!mLogFile.is_open())
142  {
143  return;
144  }
145  }
146 
147  std::string color;
148  switch (logData.logLevel)
149  {
151  color = "<b><font color=#A000A0>";
152  break;
153 
155  color = "<b><font color=#00A000>";
156  break;
157 
159  color = "<b><font color=#C00000>";
160  break;
161 
163  color = "<b><font color=#CF6F00>";
164  break;
165 
166  case Logging::LogLevel::LOG_ALWAYS:
167  color = "<b><font color=#000000>";
168  break;
169  }
170 
171  static const std::string htmlNewline("<br>\n");
172  std::string htmlMsg(logData.msg);
173 
174  for (size_t lineEnd = htmlMsg.find('\n');
175  lineEnd != std::string::npos;
176  lineEnd = htmlMsg.find('\n', lineEnd))
177  {
178  htmlMsg.replace(lineEnd, 1, htmlNewline);
179  lineEnd += htmlNewline.size() + 1;
180  }
181 
183 
184  if (logData.frameNumber > 0)
185  {
186  mLogFile << " Frm&#35; " << logData.frameNumber;
187  }
188 
189  mLogFile << " " << Logging::Log::GetLogLevelString(logData.logLevel) << "] ";
190 
191  mLogFile << htmlMsg << " [";
192 
193  if (!logData.logName.empty())
194  {
195  mLogFile << "'" << logData.logName << "' ";
196  }
197 
198  if (!logData.method.empty())
199  {
200  mLogFile << logData.method << "()";
201  }
202 
203  if (!logData.file.empty())
204  {
205  mLogFile << " " << logData.file;
206 
207  if (logData.line > 0)
208  {
209  mLogFile << "(" << logData.line << ")";
210  }
211  }
212 
213  mLogFile << "]" << "</font></b><br>" << std::endl;
214 
215  mLogFile.flush(); //Make sure everything is written, in case of a crash.
216  }
217 
220  {
221  if (!mLogFile.is_open())
222  {
223  return;
224  }
225 
226  mLogFile << "<hr>" << std::endl;
227  }
228 
231  {
232  if (mOpenFailed)
233  {
234  return true;
235  }
236  else
237  {
238  return false;
239  }
240  }
241 
242 
245  {
246  mOpenFailed = false;
247  }
248 }
std::string logName
The frame number.
Definition: LogWriter.h:75
LOG_DEBUG
Definition: LogLevel.h:64
std::string file
The name of the Log instance (could be empty)
Definition: LogWriter.h:76
LogWriterFile()
Default constructor.
This is the exception class used throughout the engine.
Definition: Exception.h:48
int line
The calling method of the message.
Definition: LogWriter.h:78
void SetToLocalTime()
Changes time to be system local time.
Definition: DateTime.cpp:205
static const std::string GetFileName()
Get the current filename of the log file.
Definition: LogFile.cpp:58
void ResetOpenFail()
A utility function to reset the OpenFailed flag after a file failure.
static const std::string GetLogLevelString(LogLevel logLevel)
Gets log level string.
Definition: Log.cpp:247
void LogHorizRule()
Logs horiz rule.
void OpenFile()
Opens the file.
virtual ~LogWriterFile()
Destructor.
static const TimeFormat LOCAL_DATE_AND_TIME_FORMAT
LOCAL_DATE_AND_TIME_FORMAT: 04-18-08 13:22:50.
Definition: DateTime.h:149
static const std::string & GetTitle()
Get the current HTML title string.
Definition: LogFile.cpp:70
std::string method
The source file of the message.
Definition: LogWriter.h:77
TR_UTIL_EXPORT std::string GetLogPath()
Get the folder where TR will save and store the log files It is the same as GetUserDataPath() by defa...
Definition: PathUtils.cpp:206
static FileUtils & GetInstance()
Character separating the parts of a file path.
Definition: FileUtils.h:137
static const std::string DEFAULT_LOG_FOLDER
The folder path that is appended to the User Data folder (PathUtil::GetUserTCDataFolder()) ...
Definition: LogWriterFile.h:56
LOG_ERROR
Definition: LogLevel.h:64
std::string ToString() const
The no parameter version of ToString uses the internal TimeFormat, see the TimeFormat enumeration abo...
Definition: DateTime.cpp:577
LOG_WARNING
Definition: LogLevel.h:64
void MakeDirectoryEX(std::string strDir)
A more powerful version of the standard mkdir.
Definition: FileUtils.cpp:651
LOG_INFO
Definition: LogLevel.h:64
unsigned frameNumber
Time of message.
Definition: LogWriter.h:74
std::string msg
The line number of the source code of the message.
Definition: LogWriter.h:79
void TimeTag(std::string prefix)
trUtil::DateTime time
Log level.
Definition: LogWriter.h:73
trUtil::Logging::LogLevel logLevel
Definition: LogWriter.h:70
static const char PATH_SEPARATOR
Definition: FileUtils.h:128
virtual void LogMessage(const LogData &logData)
Logs a message.
bool IsOpenFailed()
Returns true if opening a file failed.