SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
FileLog.cpp
Go to the documentation of this file.
1 /**
2  * \file FileLog.cpp
3  * \authors Michael Goettlicher
4  * \date March 2018
5  * \remarks Please use clangformat to format the code. See more code style on
6  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
7  * \copyright http://opensource.org/licenses/GPL-3.0
8  */
9 
10 #include "FileLog.h"
11 
12 #include <iostream>
13 #include <sstream>
14 #include <time.h>
15 #include <Utils.h>
16 
17 namespace Utils
18 {
19 //-----------------------------------------------------------------------------
20 FileLog::FileLog(std::string logDir, bool forceFlush)
21  : _forceFlush(forceFlush)
22 {
23  if (!Utils::dirExists(logDir))
24  Utils::makeDir(logDir);
25 
26  time_t now = time(nullptr);
27  std::string fileName = logDir + "/" + std::to_string(now) + "_log.txt";
28  _logFile.open(fileName, std::ofstream::out);
29 
30  if (!_logFile.is_open())
31  {
32  std::string msg = "Could not create log file in dir: " + logDir;
33  Utils::errorMsg("Utils", msg.c_str(), __LINE__, __FILE__);
34  }
35 }
36 //-----------------------------------------------------------------------------
38 {
39  _logFile.flush();
40  _logFile.close();
41 }
42 //-----------------------------------------------------------------------------
44 {
45  _logFile.flush();
46 }
47 //-----------------------------------------------------------------------------
48 void FileLog::post(const std::string& message)
49 {
50  _logFile << message;
51  if (_forceFlush)
52  _logFile.flush();
53 }
54 //-----------------------------------------------------------------------------
55 };
bool _forceFlush
Definition: FileLog.h:30
std::ofstream _logFile
Definition: FileLog.h:29
void flush()
Definition: FileLog.cpp:43
virtual ~FileLog()
Definition: FileLog.cpp:37
void post(const std::string &message)
Definition: FileLog.cpp:48
FileLog(std::string logDir, bool forceFlush)
Definition: FileLog.cpp:20
Utils provides utilities for string & file handling, logging and math functions.
Definition: Averaged.h:22
bool dirExists(const string &path)
Returns true if a directory exists.
Definition: Utils.cpp:790
bool makeDir(const string &path)
Creates a directory with given path.
Definition: Utils.cpp:810
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
Definition: Utils.cpp:1168