3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "threading/mutex_auto_lock.h"
27 #include "exceptions.h"
28 #include "util/numeric.h"
37 class StringBuffer : public std::streambuf {
42 virtual void flush(const std::string &buf) = 0;
43 std::streamsize xsputn(const char *s, std::streamsize n);
44 void push_back(char c);
51 class LogBuffer : public StringBuffer {
53 LogBuffer(Logger &logger, LogLevel lev) :
58 void flush(const std::string &buffer);
66 class RawLogBuffer : public StringBuffer {
68 void flush(const std::string &buffer);
77 StreamLogOutput stdout_output(std::cout);
78 StreamLogOutput stderr_output(std::cerr);
79 std::ostream null_stream(NULL);
83 LogBuffer none_buf(g_logger, LL_NONE);
84 LogBuffer error_buf(g_logger, LL_ERROR);
85 LogBuffer warning_buf(g_logger, LL_WARNING);
86 LogBuffer action_buf(g_logger, LL_ACTION);
87 LogBuffer info_buf(g_logger, LL_INFO);
88 LogBuffer verbose_buf(g_logger, LL_VERBOSE);
91 std::ostream *dout_con_ptr = &null_stream;
92 std::ostream *derr_con_ptr = &verbosestream;
95 std::ostream *dout_server_ptr = &infostream;
96 std::ostream *derr_server_ptr = &errorstream;
100 std::ostream *dout_client_ptr = &infostream;
101 std::ostream *derr_client_ptr = &errorstream;
104 std::ostream rawstream(&raw_buf);
105 std::ostream dstream(&none_buf);
106 std::ostream errorstream(&error_buf);
107 std::ostream warningstream(&warning_buf);
108 std::ostream actionstream(&action_buf);
109 std::ostream infostream(&info_buf);
110 std::ostream verbosestream(&verbose_buf);
115 static unsigned int g_level_to_android[] = {
116 ANDROID_LOG_INFO, // LL_NONE
118 ANDROID_LOG_ERROR, // LL_ERROR
119 ANDROID_LOG_WARN, // LL_WARNING
120 ANDROID_LOG_WARN, // LL_ACTION
122 ANDROID_LOG_DEBUG, // LL_INFO
123 ANDROID_LOG_VERBOSE, // LL_VERBOSE
126 class AndroidSystemLogOutput : public ICombinedLogOutput {
128 AndroidSystemLogOutput()
130 g_logger.addOutput(this);
132 ~AndroidSystemLogOutput()
134 g_logger.removeOutput(this);
136 void logRaw(LogLevel lev, const std::string &line)
138 STATIC_ASSERT(ARRLEN(g_level_to_android) == LL_MAX,
139 mismatch_between_android_and_internal_loglevels);
140 __android_log_print(g_level_to_android[lev],
141 PROJECT_NAME_C, "%s", line.c_str());
145 AndroidSystemLogOutput g_android_log_output;
149 ///////////////////////////////////////////////////////////////////////////////
156 LogLevel Logger::stringToLevel(const std::string &name)
160 else if (name == "error")
162 else if (name == "warning")
164 else if (name == "action")
166 else if (name == "info")
168 else if (name == "verbose")
174 void Logger::addOutput(ILogOutput *out)
176 addOutputMaxLevel(out, (LogLevel)(LL_MAX - 1));
179 void Logger::addOutput(ILogOutput *out, LogLevel lev)
181 m_outputs[lev].push_back(out);
184 void Logger::addOutputMaxLevel(ILogOutput *out, LogLevel lev)
186 assert(lev < LL_MAX);
187 for (size_t i = 0; i <= lev; i++)
188 m_outputs[i].push_back(out);
191 void Logger::removeOutput(ILogOutput *out)
193 for (size_t i = 0; i < LL_MAX; i++) {
194 std::vector<ILogOutput *>::iterator it;
196 it = std::find(m_outputs[i].begin(), m_outputs[i].end(), out);
197 if (it != m_outputs[i].end())
198 m_outputs[i].erase(it);
202 void Logger::setLevelSilenced(LogLevel lev, bool silenced)
204 m_silenced_levels[lev] = silenced;
207 void Logger::registerThread(const std::string &name)
209 threadid_t id = thr_get_current_thread_id();
210 MutexAutoLock lock(m_mutex);
211 m_thread_names[id] = name;
214 void Logger::deregisterThread()
216 threadid_t id = thr_get_current_thread_id();
217 MutexAutoLock lock(m_mutex);
218 m_thread_names.erase(id);
221 const std::string Logger::getLevelLabel(LogLevel lev)
223 static const std::string names[] = {
231 assert(lev < LL_MAX && lev >= 0);
232 STATIC_ASSERT(ARRLEN(names) == LL_MAX,
233 mismatch_between_loglevel_names_and_enum);
237 const std::string Logger::getThreadName()
239 std::map<threadid_t, std::string>::const_iterator it;
241 threadid_t id = thr_get_current_thread_id();
242 it = m_thread_names.find(id);
243 if (it != m_thread_names.end())
246 std::ostringstream os;
247 os << "#0x" << std::hex << id;
251 void Logger::log(LogLevel lev, const std::string &text)
253 if (m_silenced_levels[lev])
256 const std::string thread_name = getThreadName();
257 const std::string label = getLevelLabel(lev);
258 const std::string timestamp = getTimestamp();
259 std::ostringstream os(std::ios_base::binary);
260 os << timestamp << ": " << label << "[" << thread_name << "]: " << text;
262 logToOutputs(lev, os.str(), timestamp, thread_name, text);
265 void Logger::logRaw(LogLevel lev, const std::string &text)
267 if (m_silenced_levels[lev])
270 logToOutputsRaw(lev, text);
273 void Logger::logToOutputsRaw(LogLevel lev, const std::string &line)
275 MutexAutoLock lock(m_mutex);
276 for (size_t i = 0; i != m_outputs[lev].size(); i++)
277 m_outputs[lev][i]->logRaw(lev, line);
280 void Logger::logToOutputs(LogLevel lev, const std::string &combined,
281 const std::string &time, const std::string &thread_name,
282 const std::string &payload_text)
284 MutexAutoLock lock(m_mutex);
285 for (size_t i = 0; i != m_outputs[lev].size(); i++)
286 m_outputs[lev][i]->log(lev, combined, time, thread_name, payload_text);
291 //// *LogOutput methods
294 void FileLogOutput::open(const std::string &filename)
296 m_stream.open(filename.c_str(), std::ios::app | std::ios::ate);
297 if (!m_stream.good())
298 throw FileNotGoodException("Failed to open log file " +
299 filename + ": " + strerror(errno));
301 "-------------" << std::endl
302 << " Separator" << std::endl
303 << "-------------\n" << std::endl;
312 int StringBuffer::overflow(int c)
319 std::streamsize StringBuffer::xsputn(const char *s, std::streamsize n)
321 for (int i = 0; i < n; ++i)
326 void StringBuffer::push_back(char c)
328 if (c == '\n' || c == '\r') {
338 void LogBuffer::flush(const std::string &buffer)
340 logger.log(level, buffer);
343 void RawLogBuffer::flush(const std::string &buffer)
345 g_logger.logRaw(LL_NONE, buffer);