Skip to content

Commit b62d9d0

Browse files
committed
Add first implementation
1 parent 59fdf27 commit b62d9d0

File tree

3 files changed

+436
-1
lines changed

3 files changed

+436
-1
lines changed

LICENSE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
MIT License
22

3-
Copyright (c) 2018 Jussi Lind
3+
Copyright (c) 2018 Jussi Lind <jussi.lind@iki.fi>
4+
5+
https://github.com/juzzlin/SimpleLogger
46

57
Permission is hereby granted, free of charge, to any person obtaining a copy
68
of this software and associated documentation files (the "Software"), to deal

src/logger.cpp

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2018 Jussi Lind <jussi.lind@iki.fi>
4+
//
5+
// https://github.com/juzzlin/SimpleLogger
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
25+
#define _CRT_SECURE_NO_WARNINGS
26+
27+
#include "logger.hpp"
28+
29+
#include <ctime>
30+
#include <fstream>
31+
#include <iostream>
32+
#include <map>
33+
#include <stdexcept>
34+
35+
#ifdef Q_OS_ANDROID
36+
#include <QDebug>
37+
#else
38+
#include <cstdio>
39+
#endif
40+
41+
namespace juzzlin {
42+
43+
class Logger::Impl
44+
{
45+
public:
46+
47+
Impl();
48+
49+
~Impl();
50+
51+
std::ostringstream & trace();
52+
53+
std::ostringstream & debug();
54+
55+
std::ostringstream & info();
56+
57+
std::ostringstream & warning();
58+
59+
std::ostringstream & error();
60+
61+
std::ostringstream & fatal();
62+
63+
static void enableEchoMode(bool enable);
64+
65+
static void enableDateTime(bool enable);
66+
67+
static void setLevelSymbol(Logger::Level level, std::string symbol);
68+
69+
static void setLoggingLevel(Logger::Level level);
70+
71+
static void init(std::string filename, bool append);
72+
73+
void flush();
74+
75+
std::ostringstream & getStream(Logger::Level level);
76+
77+
void prefixDateTime();
78+
79+
private:
80+
81+
static bool m_echoMode;
82+
83+
static bool m_dateTime;
84+
85+
static Logger::Level m_level;
86+
87+
static std::ofstream m_fout;
88+
89+
using SymbolMap = std::map<Logger::Level, std::string>;
90+
static SymbolMap m_symbols;
91+
92+
using StreamMap = std::map<Logger::Level, std::ostream *>;
93+
static StreamMap m_streams;
94+
95+
Logger::Level m_activeLevel = Logger::Level::Info;
96+
97+
std::ostringstream m_oss;
98+
};
99+
100+
bool Logger::Impl::m_echoMode = true;
101+
102+
bool Logger::Impl::m_dateTime = true;
103+
104+
Logger::Level Logger::Impl::m_level = Logger::Level::Info;
105+
106+
std::ofstream Logger::Impl::m_fout;
107+
108+
// Default level symbols
109+
Logger::Impl::SymbolMap Logger::Impl::m_symbols = {
110+
{Logger::Level::Trace, "T:"},
111+
{Logger::Level::Debug, "D:"},
112+
{Logger::Level::Info, "I:"},
113+
{Logger::Level::Warning, "W:"},
114+
{Logger::Level::Error, "E:"},
115+
{Logger::Level::Fatal, "F:"}
116+
};
117+
118+
// Default streams
119+
Logger::Impl::StreamMap Logger::Impl::m_streams = {
120+
{Logger::Level::Trace, &std::cout},
121+
{Logger::Level::Debug, &std::cout},
122+
{Logger::Level::Info, &std::cout},
123+
{Logger::Level::Warning, &std::cerr},
124+
{Logger::Level::Error, &std::cerr},
125+
{Logger::Level::Fatal, &std::cerr}
126+
};
127+
128+
Logger::Impl::Impl()
129+
{
130+
}
131+
132+
Logger::Impl::~Impl()
133+
{
134+
flush();
135+
}
136+
137+
void Logger::Impl::enableEchoMode(bool enable)
138+
{
139+
Impl::m_echoMode = enable;
140+
}
141+
142+
void Logger::Impl::enableDateTime(bool enable)
143+
{
144+
Impl::m_dateTime = enable;
145+
}
146+
147+
std::ostringstream & Logger::Impl::getStream(Logger::Level level)
148+
{
149+
m_activeLevel = level;
150+
Impl::prefixDateTime();
151+
m_oss << Impl::m_symbols[level] << " ";
152+
return m_oss;
153+
}
154+
155+
void Logger::Impl::setLevelSymbol(Level level, std::string symbol)
156+
{
157+
Impl::m_symbols[level] = symbol;
158+
}
159+
160+
void Logger::Impl::setLoggingLevel(Logger::Level level)
161+
{
162+
Impl::m_level = level;
163+
}
164+
165+
void Logger::Impl::prefixDateTime()
166+
{
167+
if (Impl::m_dateTime)
168+
{
169+
time_t rawTime;
170+
time(&rawTime);
171+
std::string timeStr(ctime(&rawTime));
172+
timeStr.erase(timeStr.length() - 1);
173+
m_oss << "[" << timeStr << "] ";
174+
}
175+
}
176+
177+
void Logger::Impl::flush()
178+
{
179+
if (m_activeLevel < m_level)
180+
{
181+
return;
182+
}
183+
184+
if (!m_oss.str().size())
185+
{
186+
return;
187+
}
188+
189+
if (Impl::m_fout.is_open())
190+
{
191+
Impl::m_fout << m_oss.str() << std::endl;
192+
Impl::m_fout.flush();
193+
}
194+
195+
if (Impl::m_echoMode)
196+
{
197+
#ifdef Q_OS_ANDROID
198+
qDebug() << m_oss.str().c_str();
199+
#else
200+
auto stream = Impl::m_streams[m_activeLevel];
201+
if (stream) {
202+
*stream << m_oss.str() << std::endl;
203+
stream->flush();
204+
}
205+
#endif
206+
}
207+
}
208+
209+
void Logger::Impl::init(std::string filename, bool append)
210+
{
211+
if (!filename.empty())
212+
{
213+
Impl::m_fout.open(filename, append ? std::ofstream::out | std::ofstream::app : std::ofstream::out);
214+
if (!Impl::m_fout.is_open())
215+
{
216+
throw std::runtime_error("ERROR!!: Couldn't open '" + filename + "' for write.\n");
217+
}
218+
}
219+
}
220+
221+
std::ostringstream & Logger::Impl::trace()
222+
{
223+
return getStream(Logger::Level::Trace);
224+
}
225+
226+
std::ostringstream & Logger::Impl::debug()
227+
{
228+
return getStream(Logger::Level::Debug);
229+
}
230+
231+
std::ostringstream & Logger::Impl::info()
232+
{
233+
return getStream(Logger::Level::Info);
234+
}
235+
236+
std::ostringstream & Logger::Impl::warning()
237+
{
238+
return getStream(Logger::Level::Warning);
239+
}
240+
241+
std::ostringstream & Logger::Impl::error()
242+
{
243+
return getStream(Logger::Level::Error);
244+
}
245+
246+
std::ostringstream & Logger::Impl::fatal()
247+
{
248+
return getStream(Logger::Level::Fatal);
249+
}
250+
251+
Logger::Logger()
252+
: m_impl(new Logger::Impl)
253+
{
254+
}
255+
256+
void Logger::init(std::string filename, bool append)
257+
{
258+
Impl::init(filename, append);
259+
}
260+
261+
void Logger::enableEchoMode(bool enable)
262+
{
263+
Impl::enableEchoMode(enable);
264+
}
265+
266+
void Logger::enableDateTime(bool enable)
267+
{
268+
Impl::enableDateTime(enable);
269+
}
270+
271+
void Logger::setLoggingLevel(Level level)
272+
{
273+
Impl::setLoggingLevel(level);
274+
}
275+
276+
void Logger::setLevelSymbol(Level level, std::string symbol)
277+
{
278+
Impl::setLevelSymbol(level, symbol);
279+
}
280+
281+
std::ostringstream & Logger::trace()
282+
{
283+
return m_impl->trace();
284+
}
285+
286+
std::ostringstream & Logger::debug()
287+
{
288+
return m_impl->debug();
289+
}
290+
291+
std::ostringstream & Logger::info()
292+
{
293+
return m_impl->info();
294+
}
295+
296+
std::ostringstream & Logger::warning()
297+
{
298+
return m_impl->warning();
299+
}
300+
301+
std::ostringstream & Logger::error()
302+
{
303+
return m_impl->error();
304+
}
305+
306+
std::ostringstream & Logger::fatal()
307+
{
308+
return m_impl->fatal();
309+
}
310+
311+
Logger::~Logger() = default;
312+
313+
} // juzzlin

0 commit comments

Comments
 (0)