-
Notifications
You must be signed in to change notification settings - Fork 3
/
Log.h
149 lines (124 loc) · 3.53 KB
/
Log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* File: Log.h
* Author: mdesana
*
* Created on May 15, 2014, 5:27 PM
*/
//this is REALLY a simple log
//we put this ifdef because we want to prevent usage of this class from the code when USE_DEBUG_LOG is off
//NOTE: the log must always be used between #ifdef USE_DEBUG_LOG ... #endif directives
#ifdef USE_DEBUG_LOG
#pragma once
#include <fstream>
#include <sstream>
#include "Params.h"
// a minimal log represented as a singleton
class Log
{
std::ofstream m_file;
int m_indentationLevel;
static Log* m_log;
static Log* GetInstance();
void WriteAux(const std::stringstream& ss, bool endline, int level)
{
if (level < ENABLED_LOG_LEVEL)
return;
std::string indent;
for (int i = 0; i < m_indentationLevel; i++)
indent += ". ";
indentedOutput(m_file, ss.str().c_str(),indent.c_str());
// m_file << ss.str();
if (endline)
{
m_file << std::endl;
}
m_file.flush(); //todo remove if too slow
}
static void indentedOutput(std::ostream &outStream, const char *message, const char *indentation)
{
bool newline;
while (char cur = *message)
{
if (newline)
{
outStream << indentation;
newline = false;
}
outStream << cur;
if (cur == '\n')
{
newline = true;
}
++message;
}
}
public:
~Log()
{
m_file.close();
}
static void Indent()
{
GetInstance()->m_indentationLevel++;
}
static void Dedent()
{
if (GetInstance()->m_indentationLevel > 0)
GetInstance()->m_indentationLevel--;
else
std::cout << "\nWARNING trying to dedent log with no indentation\n";
}
static void Write(const std::stringstream& s, int level = LOG_LEVEL_LOW)
{
Log::GetInstance()->WriteAux(s, true, level);
}
static void Write(const std::string& s, int level = LOG_LEVEL_LOW)
{
std::stringstream sstr;
sstr << s;
Log::GetInstance()->WriteAux(sstr, true, level);
}
static void Endl(int level = LOG_LEVEL_LOW)
{
std::stringstream sstr;
Log::GetInstance()->WriteAux(sstr, true, level);
}
static void Write(Real s, int level = LOG_LEVEL_LOW)
{
std::stringstream sstr;
sstr << s;
Log::GetInstance()->WriteAux(sstr, true, level);
}
static void Write(int s, int level = LOG_LEVEL_LOW)
{
std::stringstream sstr;
sstr << s;
Log::GetInstance()->WriteAux(sstr, true, level);
}
// static void Write(const std::stringstream& s, int level = LOG_LEVEL_LOW)
// {
// Log::GetInstance()->WriteAux(s, false, level);
// }
//
// static void Write(const std::string& s, int level = LOG_LEVEL_LOW)
// {
// std::stringstream sstr;
// sstr << s;
// Log::GetInstance()->WriteAux(sstr, false, level);
// }
//
// static void Write(Real s, int level = LOG_LEVEL_LOW)
// {
// std::stringstream sstr;
// sstr << s;
// Log::GetInstance()->WriteAux(sstr, false, level);
// }
//
// static void Write(int s, int level = LOG_LEVEL_LOW)
// {
// std::stringstream sstr;
// sstr << s;
// Log::GetInstance()->WriteAux(sstr, false, level);
// }
};
#endif /* USE_DEBUG_LOG */