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);
73 static unsigned int level_to_android[] = {
74 ANDROID_LOG_INFO, // LL_NONE
76 ANDROID_LOG_ERROR, // LL_ERROR
77 ANDROID_LOG_WARN, // LL_WARNING
78 ANDROID_LOG_WARN, // LL_ACTION
80 ANDROID_LOG_DEBUG, // LL_INFO
81 ANDROID_LOG_VERBOSE, // LL_VERBOSE
92 StreamLogOutput stdout_output(std::cout);
93 StreamLogOutput stderr_output(std::cerr);
94 std::ostream null_stream(NULL);
98 LogBuffer none_buf(g_logger, LL_NONE);
99 LogBuffer error_buf(g_logger, LL_ERROR);
100 LogBuffer warning_buf(g_logger, LL_WARNING);
101 LogBuffer action_buf(g_logger, LL_ACTION);
102 LogBuffer info_buf(g_logger, LL_INFO);
103 LogBuffer verbose_buf(g_logger, LL_VERBOSE);
106 std::ostream *dout_con_ptr = &null_stream;
107 std::ostream *derr_con_ptr = &verbosestream;
110 std::ostream *dout_server_ptr = &infostream;
111 std::ostream *derr_server_ptr = &errorstream;
115 std::ostream *dout_client_ptr = &infostream;
116 std::ostream *derr_client_ptr = &errorstream;
119 std::ostream rawstream(&raw_buf);
120 std::ostream dstream(&none_buf);
121 std::ostream errorstream(&error_buf);
122 std::ostream warningstream(&warning_buf);
123 std::ostream actionstream(&action_buf);
124 std::ostream infostream(&info_buf);
125 std::ostream verbosestream(&verbose_buf);
128 ///////////////////////////////////////////////////////////////////////////////
135 LogLevel Logger::stringToLevel(const std::string &name)
139 else if (name == "error")
141 else if (name == "warning")
143 else if (name == "action")
145 else if (name == "info")
147 else if (name == "verbose")
153 void Logger::addOutput(ILogOutput *out)
155 addOutputMaxLevel(out, LL_MAX);
158 void Logger::addOutput(ILogOutput *out, LogLevel lev)
160 m_outputs[lev].push_back(out);
163 void Logger::addOutputMaxLevel(ILogOutput *out, LogLevel lev)
165 for (size_t i = 0; i <= lev; i++)
166 m_outputs[i].push_back(out);
169 void Logger::removeOutput(ILogOutput *out)
171 for (size_t i = 0; i < LL_MAX; i++) {
172 std::vector<ILogOutput *>::iterator it;
174 it = std::find(m_outputs[i].begin(), m_outputs[i].end(), out);
175 if (it != m_outputs[i].end())
176 m_outputs[i].erase(it);
180 void Logger::setLevelSilenced(LogLevel lev, bool silenced)
182 m_silenced_levels[lev] = silenced;
185 void Logger::registerThread(const std::string &name)
187 threadid_t id = thr_get_current_thread_id();
188 MutexAutoLock lock(m_mutex);
189 m_thread_names[id] = name;
192 void Logger::deregisterThread()
194 threadid_t id = thr_get_current_thread_id();
195 MutexAutoLock lock(m_mutex);
196 m_thread_names.erase(id);
199 const std::string Logger::getLevelLabel(LogLevel lev)
201 static const std::string names[] = {
209 assert(lev < LL_MAX && lev >= 0);
210 assert(ARRLEN(names) == LL_MAX);
214 const std::string Logger::getThreadName()
216 std::map<threadid_t, std::string>::const_iterator it;
218 threadid_t id = thr_get_current_thread_id();
219 it = m_thread_names.find(id);
220 if (it != m_thread_names.end())
223 std::ostringstream os;
224 os << "#0x" << std::hex << id;
228 void Logger::log(LogLevel lev, const std::string &text)
230 if (m_silenced_levels[lev])
233 const std::string thread_name = getThreadName();
234 const std::string label = getLevelLabel(lev);
235 std::ostringstream os(std::ios_base::binary);
236 os << getTimestamp() << ": " << label << "[" << thread_name << "]: " << text;
238 logToSystem(lev, text);
239 logToOutputs(lev, os.str());
242 void Logger::logRaw(LogLevel lev, const std::string &text)
244 if (m_silenced_levels[lev])
247 logToSystem(lev, text);
248 logToOutputs(lev, text);
251 void Logger::logToSystem(LogLevel lev, const std::string &text)
254 assert(ARRLEN(level_to_android) == LL_MAX);
255 __android_log_print(level_to_android[lev],
256 PROJECT_NAME_C, "%s", text.c_str());
260 void Logger::logToOutputs(LogLevel lev, const std::string &text)
262 MutexAutoLock lock(m_mutex);
263 for (size_t i = 0; i != m_outputs[lev].size(); i++)
264 m_outputs[lev][i]->log(text);
269 //// *LogOutput methods
272 void FileLogOutput::open(const std::string &filename)
274 stream.open(filename.c_str(), std::ios::app | std::ios::ate);
276 throw FileNotGoodException("Failed to open log file " +
277 filename + ": " + strerror(errno));
279 "-------------" << std::endl
280 << " Separator" << std::endl
281 << "-------------\n" << std::endl;
290 int StringBuffer::overflow(int c)
297 std::streamsize StringBuffer::xsputn(const char *s, std::streamsize n)
299 for (int i = 0; i < n; ++i)
304 void StringBuffer::push_back(char c)
306 if (c == '\n' || c == '\r') {
318 void LogBuffer::flush(const std::string &buffer)
320 logger.log(level, buffer);
323 void RawLogBuffer::flush(const std::string &buffer)
325 g_logger.logRaw(LL_NONE, buffer);