Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LOG Class Reference

#include <LOG.hxx>

+ Collaboration diagram for LOG:

Public Types

using MAX_LOG_SIZE_BYTES = Constant< 1000000 >
 

Public Member Functions

 LOG (LogType type)
 Create a Logger. More...
 
 ~LOG ()
 Destroy a Logger. More...
 
template<class Object >
LOGoperator<< (const Object &msg)
 Log a message. More...
 

Private Member Functions

const std::string getTimeStamp ()
 Gets formatted TimeStamp. More...
 
void writeErrorLog (const std::string &errorMessage) const
 Write log message to error.log file. More...
 

Private Attributes

LogType m_LogType
 
std::ostringstream m_Logger
 

Static Private Attributes

static Mutex StreamMutex
 
static constexpr const char * LOG_PREFIX_COLORED []
 
static constexpr const char * LOG_PREFIX []
 

Detailed Description

Examples
tests/util/MessageQueue.cxx.

Definition at line 32 of file LOG.hxx.

Member Typedef Documentation

◆ MAX_LOG_SIZE_BYTES

Definition at line 50 of file LOG.hxx.

Constructor & Destructor Documentation

◆ LOG()

LOG::LOG ( LogType  type)

Create a Logger.

Definition at line 12 of file LOG.cxx.

12 : m_LogType(type) {}

◆ ~LOG()

LOG::~LOG ( )

Destroy a Logger.

Definition at line 14 of file LOG.cxx.

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 }
+ Here is the call graph for this function:

Member Function Documentation

◆ getTimeStamp()

const std::string LOG::getTimeStamp ( )
private

Gets formatted TimeStamp.

Returns
std::string containing current TimeStamp

Definition at line 41 of file LOG.cxx.

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 }
+ Here is the caller graph for this function:

◆ operator<<()

template<class Object >
LOG & LOG::operator<< ( const Object &  msg)

Log a message.

Template Parameters
msgthe streamable message type

Definition at line 2 of file LOG.inl.hxx.

3 {
4  m_Logger << msg;
5  return *this;
6 }

◆ writeErrorLog()

void LOG::writeErrorLog ( const std::string errorMessage) const
private

Write log message to error.log file.

If the log file exceeds MAX_LOG_SIZE_BYTES, logs will be cut in half

Parameters
errorMessageto write to error logfile

Definition at line 49 of file LOG.cxx.

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 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ LOG_PREFIX

constexpr const char* LOG::LOG_PREFIX[]
staticconstexprprivate
Initial value:
= {
" - [INFO] - ", " - [DEBUG] - ", " - [WARNING] - ", " - [ERROR] - ", " - [FATAL] - ",
}

Definition at line 44 of file LOG.hxx.

◆ LOG_PREFIX_COLORED

constexpr const char* LOG::LOG_PREFIX_COLORED[]
staticconstexprprivate
Initial value:
= {
" - \x1B[38;5;39m\x1B[1m[INFO]\x1B[0m - ", " - \x1B[38;5;57m\x1B[1m[DEBUG]\x1B[0m - ",
" - \x1B[38;5;214m\x1B[1m[WARNING]\x1B[0m - ", " - \x1B[38;5;196m\x1B[1m[ERROR]\x1B[0m - ",
" - \x1B[38;5;124m\x1B[1m[FATAL]\x1B[0m - ",
}

Definition at line 38 of file LOG.hxx.

◆ m_Logger

std::ostringstream LOG::m_Logger
private

Definition at line 70 of file LOG.hxx.

◆ m_LogType

LogType LOG::m_LogType
private

Definition at line 69 of file LOG.hxx.

◆ StreamMutex

Mutex LOG::StreamMutex
staticprivate

Definition at line 36 of file LOG.hxx.


The documentation for this class was generated from the following files:
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::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_DEBUG
@ LOG_DEBUG
Definition: LOG.hxx:26
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
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
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::m_Logger
std::ostringstream m_Logger
Definition: LOG.hxx:70
CytopiaError
A generic error in Cytopia.
Definition: Exception.hxx:28