Skip to content

Commit 2f19abd

Browse files
committedOct 24, 2015
Small logging refactor and additional options
-> Get rid of Logger::logToSystem and use normal downstream output system for android instead -> Give the downstream output system more information: enrich the log function of ILogOutput with information and add ICombinedLogOutput for easier use. -> Make Logger::getLevelLabel() static and public so that it can be used by downstream log output. -> Add g_ and m_ prefixes where required
1 parent 6f2d9de commit 2f19abd

File tree

2 files changed

+89
-57
lines changed

2 files changed

+89
-57
lines changed
 

‎src/log.cpp

+50-33
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,6 @@ class RawLogBuffer : public StringBuffer {
6868
void flush(const std::string &buffer);
6969
};
7070

71-
72-
#ifdef __ANDROID__
73-
static unsigned int level_to_android[] = {
74-
ANDROID_LOG_INFO, // LL_NONE
75-
//ANDROID_LOG_FATAL,
76-
ANDROID_LOG_ERROR, // LL_ERROR
77-
ANDROID_LOG_WARN, // LL_WARNING
78-
ANDROID_LOG_WARN, // LL_ACTION
79-
//ANDROID_LOG_INFO,
80-
ANDROID_LOG_DEBUG, // LL_INFO
81-
ANDROID_LOG_VERBOSE, // LL_VERBOSE
82-
83-
};
84-
#endif
85-
8671
////
8772
//// Globals
8873
////
@@ -124,6 +109,41 @@ std::ostream actionstream(&action_buf);
124109
std::ostream infostream(&info_buf);
125110
std::ostream verbosestream(&verbose_buf);
126111

112+
// Android
113+
#ifdef __ANDROID__
114+
115+
static unsigned int g_level_to_android[] = {
116+
ANDROID_LOG_INFO, // LL_NONE
117+
//ANDROID_LOG_FATAL,
118+
ANDROID_LOG_ERROR, // LL_ERROR
119+
ANDROID_LOG_WARN, // LL_WARNING
120+
ANDROID_LOG_WARN, // LL_ACTION
121+
//ANDROID_LOG_INFO,
122+
ANDROID_LOG_DEBUG, // LL_INFO
123+
ANDROID_LOG_VERBOSE, // LL_VERBOSE
124+
};
125+
126+
class AndroidSystemLogOutput : public ICombinedLogOutput {
127+
public:
128+
AndroidSystemLogOutput()
129+
{
130+
g_logger.addOutput(this);
131+
}
132+
~AndroidSystemLogOutput()
133+
{
134+
g_logger.removeOutput(this);
135+
}
136+
void logRaw(LogLevel lev, const std::string &line)
137+
{
138+
assert(ARRLEN(g_level_to_android) == LL_MAX);
139+
__android_log_print(g_level_to_android[lev],
140+
PROJECT_NAME_C, "%s", line.c_str());
141+
}
142+
};
143+
144+
AndroidSystemLogOutput g_android_log_output;
145+
146+
#endif
127147

128148
///////////////////////////////////////////////////////////////////////////////
129149

@@ -232,36 +252,35 @@ void Logger::log(LogLevel lev, const std::string &text)
232252

233253
const std::string thread_name = getThreadName();
234254
const std::string label = getLevelLabel(lev);
255+
const std::string timestamp = getTimestamp();
235256
std::ostringstream os(std::ios_base::binary);
236-
os << getTimestamp() << ": " << label << "[" << thread_name << "]: " << text;
257+
os << timestamp << ": " << label << "[" << thread_name << "]: " << text;
237258

238-
logToSystem(lev, text);
239-
logToOutputs(lev, os.str());
259+
logToOutputs(lev, os.str(), timestamp, thread_name, text);
240260
}
241261

242262
void Logger::logRaw(LogLevel lev, const std::string &text)
243263
{
244264
if (m_silenced_levels[lev])
245265
return;
246266

247-
logToSystem(lev, text);
248-
logToOutputs(lev, text);
267+
logToOutputsRaw(lev, text);
249268
}
250269

251-
void Logger::logToSystem(LogLevel lev, const std::string &text)
270+
void Logger::logToOutputsRaw(LogLevel lev, const std::string &line)
252271
{
253-
#ifdef __ANDROID__
254-
assert(ARRLEN(level_to_android) == LL_MAX);
255-
__android_log_print(level_to_android[lev],
256-
PROJECT_NAME_C, "%s", text.c_str());
257-
#endif
272+
MutexAutoLock lock(m_mutex);
273+
for (size_t i = 0; i != m_outputs[lev].size(); i++)
274+
m_outputs[lev][i]->logRaw(lev, line);
258275
}
259276

260-
void Logger::logToOutputs(LogLevel lev, const std::string &text)
277+
void Logger::logToOutputs(LogLevel lev, const std::string &combined,
278+
const std::string &time, const std::string &thread_name,
279+
const std::string &payload_text)
261280
{
262281
MutexAutoLock lock(m_mutex);
263282
for (size_t i = 0; i != m_outputs[lev].size(); i++)
264-
m_outputs[lev][i]->log(text);
283+
m_outputs[lev][i]->log(lev, combined, time, thread_name, payload_text);
265284
}
266285

267286

@@ -271,11 +290,11 @@ void Logger::logToOutputs(LogLevel lev, const std::string &text)
271290

272291
void FileLogOutput::open(const std::string &filename)
273292
{
274-
stream.open(filename.c_str(), std::ios::app | std::ios::ate);
275-
if (!stream.good())
293+
m_stream.open(filename.c_str(), std::ios::app | std::ios::ate);
294+
if (!m_stream.good())
276295
throw FileNotGoodException("Failed to open log file " +
277296
filename + ": " + strerror(errno));
278-
stream << "\n\n"
297+
m_stream << "\n\n"
279298
"-------------" << std::endl
280299
<< " Separator" << std::endl
281300
<< "-------------\n" << std::endl;
@@ -313,8 +332,6 @@ void StringBuffer::push_back(char c)
313332
}
314333

315334

316-
317-
318335
void LogBuffer::flush(const std::string &buffer)
319336
{
320337
logger.log(level, buffer);

‎src/log.h

+39-24
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ class Logger {
5757
bool getTraceEnabled() { return m_trace_enabled; }
5858

5959
static LogLevel stringToLevel(const std::string &name);
60+
static const std::string getLevelLabel(LogLevel lev);
6061

6162
private:
62-
void logToSystem(LogLevel, const std::string &text);
63-
void logToOutputs(LogLevel, const std::string &text);
63+
void logToOutputsRaw(LogLevel, const std::string &line);
64+
void logToOutputs(LogLevel, const std::string &combined,
65+
const std::string &time, const std::string &thread_name,
66+
const std::string &payload_text);
6467

65-
const std::string getLevelLabel(LogLevel lev);
6668
const std::string getThreadName();
6769

6870
std::vector<ILogOutput *> m_outputs[LL_MAX];
@@ -78,73 +80,86 @@ class Logger {
7880

7981
class ILogOutput {
8082
public:
81-
virtual void log(const std::string &line) = 0;
83+
virtual void logRaw(LogLevel, const std::string &line) = 0;
84+
virtual void log(LogLevel, const std::string &combined,
85+
const std::string &time, const std::string &thread_name,
86+
const std::string &payload_text) = 0;
8287
};
8388

84-
class StreamLogOutput : public ILogOutput {
89+
class ICombinedLogOutput : public ILogOutput {
90+
public:
91+
void log(LogLevel lev, const std::string &combined,
92+
const std::string &time, const std::string &thread_name,
93+
const std::string &payload_text)
94+
{
95+
logRaw(lev, combined);
96+
}
97+
};
98+
99+
class StreamLogOutput : public ICombinedLogOutput {
85100
public:
86101
StreamLogOutput(std::ostream &stream) :
87-
stream(stream)
102+
m_stream(stream)
88103
{
89104
}
90105

91-
void log(const std::string &line)
106+
void logRaw(LogLevel lev, const std::string &line)
92107
{
93-
stream << line << std::endl;
108+
m_stream << line << std::endl;
94109
}
95110

96111
private:
97-
std::ostream &stream;
112+
std::ostream &m_stream;
98113
};
99114

100-
class FileLogOutput : public ILogOutput {
115+
class FileLogOutput : public ICombinedLogOutput {
101116
public:
102117
void open(const std::string &filename);
103118

104-
void log(const std::string &line)
119+
void logRaw(LogLevel lev, const std::string &line)
105120
{
106-
stream << line << std::endl;
121+
m_stream << line << std::endl;
107122
}
108123

109124
private:
110-
std::ofstream stream;
125+
std::ofstream m_stream;
111126
};
112127

113-
class LogOutputBuffer : public ILogOutput {
128+
class LogOutputBuffer : public ICombinedLogOutput {
114129
public:
115130
LogOutputBuffer(Logger &logger, LogLevel lev) :
116-
logger(logger)
131+
m_logger(logger)
117132
{
118-
logger.addOutput(this, lev);
133+
m_logger.addOutput(this, lev);
119134
}
120135

121136
~LogOutputBuffer()
122137
{
123-
logger.removeOutput(this);
138+
m_logger.removeOutput(this);
124139
}
125140

126-
virtual void log(const std::string &line)
141+
void logRaw(LogLevel lev, const std::string &line)
127142
{
128-
buffer.push(line);
143+
m_buffer.push(line);
129144
}
130145

131146
bool empty()
132147
{
133-
return buffer.empty();
148+
return m_buffer.empty();
134149
}
135150

136151
std::string get()
137152
{
138153
if (empty())
139154
return "";
140-
std::string s = buffer.front();
141-
buffer.pop();
155+
std::string s = m_buffer.front();
156+
m_buffer.pop();
142157
return s;
143158
}
144159

145160
private:
146-
std::queue<std::string> buffer;
147-
Logger &logger;
161+
std::queue<std::string> m_buffer;
162+
Logger &m_logger;
148163
};
149164

150165

0 commit comments

Comments
 (0)
Please sign in to comment.