Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LOG.cxx
Go to the documentation of this file.
1 #include "LOG.hxx"
2 #include "Constants.hxx"
3 #include "Filesystem.hxx"
4 #include "Settings.hxx"
5 
6 #ifdef __ANDROID__
7 #include <android/log.h>
8 #endif
9 
11 
12 LOG::LOG(LogType type) : m_LogType(type) {}
13 
15 {
16  LockGuard lock(StreamMutex);
17  string message = getTimeStamp() + LOG_PREFIX[m_LogType] + m_Logger.str();
18  if (!getenv("TERM"))
19  {
20 #ifndef DEBUG
21  if (m_LogType != LOG_DEBUG)
22 #endif
23 #ifdef __ANDROID__
24  __android_log_print(ANDROID_LOG_INFO, "Cytopia", "%s", message.c_str());
25 #else
26  std::cout << message << std::endl;
27 #endif
28  }
29  else
30  {
31 #ifndef DEBUG
32  if (m_LogType != LOG_DEBUG)
33 #endif
34  std::cout << getTimeStamp() << LOG_PREFIX_COLORED[m_LogType] << m_Logger.str() << std::endl;
35  }
36 #ifndef __ANDROID__
37  writeErrorLog(message);
38 #endif
39 }
40 
42 {
43  std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
44  char buf[100] = {0};
45  std::strftime(buf, sizeof(buf), "%b %d %H:%M:%S", std::localtime(&now)); // lgtm [cpp/potentially-dangerous-function]
46  return buf;
47 }
48 
49 void LOG::writeErrorLog(const std::string &errorMessage) const
50 {
52  string errfname = CYTOPIA_DATA_DIR + string{"error.log"};
53 
54  /* First we create the file if it doesn't exist */
55  std::fstream fs(errfname, std::fstream::out | std::fstream::app);
56  if (!fs)
57  throw CytopiaError(TRACE_INFO "Could not open file " + errfname);
58 
59  /* We send the message */
60  fs << errorMessage << std::endl;
61 
62  /* We compute the size of the file */
63  fs.seekp(0, std::fstream::end);
64  std::streampos Size = fs.tellp();
65  if (Size > MAX_LOG_SIZE_BYTES::value)
66  {
67  /* We need to rotate the logs */
68  std::fstream fsToRotate(errfname, std::fstream::in | std::fstream::out);
69  fsToRotate.seekg(0);
70  string line;
71  std::streampos Cut = 0;
72  while (Size - Cut > MAX_LOG_SIZE_BYTES::value / 2 && std::getline(fsToRotate, line))
73  Cut += line.size() + 1;
74  stringstream truncatedstream;
75  truncatedstream << fsToRotate.rdbuf();
76  fsToRotate.close();
77  fsToRotate.open(fs::getBasePath() + string{"error.log"}, std::fstream::trunc | std::fstream::out);
78  fsToRotate << truncatedstream.str();
79  }
80 }
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
LOG::getTimeStamp
const std::string getTimeStamp()
Gets formatted TimeStamp.
Definition: LOG.cxx:41
LockGuard
std::lock_guard< Mutex > LockGuard
Definition: LOG.hxx:21
LOG.hxx
LOG::StreamMutex
static Mutex StreamMutex
Definition: LOG.hxx:36
LOG::LOG_PREFIX
static constexpr const char * LOG_PREFIX[]
Definition: LOG.hxx:44
LOG::writeErrorLog
void writeErrorLog(const std::string &errorMessage) const
Write log message to error.log file.
Definition: LOG.cxx:49
LOG::LOG
LOG(LogType type)
Create a Logger.
Definition: LOG.cxx:12
Mutex
std::mutex Mutex
Definition: MessageQueue.hxx:12
LOG_DEBUG
@ LOG_DEBUG
Definition: LOG.hxx:26
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
LogType
LogType
Definition: LOG.hxx:23
Filesystem.hxx
LOG::LOG_PREFIX_COLORED
static constexpr const char * LOG_PREFIX_COLORED[]
Definition: LOG.hxx:38
LOG::m_LogType
LogType m_LogType
Definition: LOG.hxx:69
Settings.hxx
createDirectory
void createDirectory(const std::string &dir)
Definition: Filesystem.cxx:69
CYTOPIA_DATA_DIR
const std::string CYTOPIA_DATA_DIR
Definition: Constants.hxx:17
LOG::~LOG
~LOG()
Destroy a Logger.
Definition: LOG.cxx:14
LOG::m_Logger
std::ostringstream m_Logger
Definition: LOG.hxx:70
string
std::string string
Definition: AudioConfig.hxx:14
Constants.hxx
CytopiaError
A generic error in Cytopia.
Definition: Exception.hxx:28