2011-10-16 11:45:00 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-10-16 11:45:00 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-10-16 11:45:00 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2011-10-16 11:45:00 +02:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#include "threading/mutex_auto_lock.h"
|
2011-10-16 11:45:00 +02:00
|
|
|
#include "debug.h"
|
|
|
|
#include "gettime.h"
|
2014-04-21 14:10:59 +02:00
|
|
|
#include "porting.h"
|
|
|
|
#include "config.h"
|
2015-10-13 09:57:44 +02:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "util/numeric.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
class StringBuffer : public std::streambuf {
|
|
|
|
public:
|
|
|
|
StringBuffer() {}
|
|
|
|
|
|
|
|
int overflow(int c);
|
|
|
|
virtual void flush(const std::string &buf) = 0;
|
|
|
|
std::streamsize xsputn(const char *s, std::streamsize n);
|
|
|
|
void push_back(char c);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LogBuffer : public StringBuffer {
|
|
|
|
public:
|
|
|
|
LogBuffer(Logger &logger, LogLevel lev) :
|
|
|
|
logger(logger),
|
|
|
|
level(lev)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void flush(const std::string &buffer);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Logger &logger;
|
|
|
|
LogLevel level;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class RawLogBuffer : public StringBuffer {
|
|
|
|
public:
|
|
|
|
void flush(const std::string &buffer);
|
|
|
|
};
|
|
|
|
|
|
|
|
////
|
|
|
|
//// Globals
|
|
|
|
////
|
|
|
|
|
|
|
|
Logger g_logger;
|
|
|
|
|
|
|
|
StreamLogOutput stdout_output(std::cout);
|
|
|
|
StreamLogOutput stderr_output(std::cerr);
|
|
|
|
std::ostream null_stream(NULL);
|
|
|
|
|
|
|
|
RawLogBuffer raw_buf;
|
|
|
|
|
|
|
|
LogBuffer none_buf(g_logger, LL_NONE);
|
|
|
|
LogBuffer error_buf(g_logger, LL_ERROR);
|
|
|
|
LogBuffer warning_buf(g_logger, LL_WARNING);
|
|
|
|
LogBuffer action_buf(g_logger, LL_ACTION);
|
|
|
|
LogBuffer info_buf(g_logger, LL_INFO);
|
|
|
|
LogBuffer verbose_buf(g_logger, LL_VERBOSE);
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-04-01 15:01:28 +02:00
|
|
|
// Connection
|
2015-10-13 09:57:44 +02:00
|
|
|
std::ostream *dout_con_ptr = &null_stream;
|
2015-04-01 15:01:28 +02:00
|
|
|
std::ostream *derr_con_ptr = &verbosestream;
|
|
|
|
|
|
|
|
// Server
|
|
|
|
std::ostream *dout_server_ptr = &infostream;
|
|
|
|
std::ostream *derr_server_ptr = &errorstream;
|
|
|
|
|
|
|
|
#ifndef SERVER
|
|
|
|
// Client
|
|
|
|
std::ostream *dout_client_ptr = &infostream;
|
|
|
|
std::ostream *derr_client_ptr = &errorstream;
|
|
|
|
#endif
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
std::ostream rawstream(&raw_buf);
|
|
|
|
std::ostream dstream(&none_buf);
|
|
|
|
std::ostream errorstream(&error_buf);
|
|
|
|
std::ostream warningstream(&warning_buf);
|
|
|
|
std::ostream actionstream(&action_buf);
|
|
|
|
std::ostream infostream(&info_buf);
|
|
|
|
std::ostream verbosestream(&verbose_buf);
|
|
|
|
|
2015-10-24 12:52:14 +02:00
|
|
|
// Android
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
|
|
|
static unsigned int g_level_to_android[] = {
|
|
|
|
ANDROID_LOG_INFO, // LL_NONE
|
|
|
|
//ANDROID_LOG_FATAL,
|
|
|
|
ANDROID_LOG_ERROR, // LL_ERROR
|
|
|
|
ANDROID_LOG_WARN, // LL_WARNING
|
|
|
|
ANDROID_LOG_WARN, // LL_ACTION
|
|
|
|
//ANDROID_LOG_INFO,
|
|
|
|
ANDROID_LOG_DEBUG, // LL_INFO
|
|
|
|
ANDROID_LOG_VERBOSE, // LL_VERBOSE
|
|
|
|
};
|
|
|
|
|
|
|
|
class AndroidSystemLogOutput : public ICombinedLogOutput {
|
|
|
|
public:
|
|
|
|
AndroidSystemLogOutput()
|
|
|
|
{
|
|
|
|
g_logger.addOutput(this);
|
|
|
|
}
|
|
|
|
~AndroidSystemLogOutput()
|
|
|
|
{
|
|
|
|
g_logger.removeOutput(this);
|
|
|
|
}
|
|
|
|
void logRaw(LogLevel lev, const std::string &line)
|
|
|
|
{
|
2015-10-28 03:27:32 +01:00
|
|
|
STATIC_ASSERT(ARRLEN(g_level_to_android) == LL_MAX,
|
|
|
|
mismatch_between_android_and_internal_loglevels);
|
2015-10-24 12:52:14 +02:00
|
|
|
__android_log_print(g_level_to_android[lev],
|
|
|
|
PROJECT_NAME_C, "%s", line.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AndroidSystemLogOutput g_android_log_output;
|
|
|
|
|
|
|
|
#endif
|
2015-01-06 16:01:49 +01:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
|
|
|
|
////
|
|
|
|
//// Logger
|
|
|
|
////
|
|
|
|
|
|
|
|
LogLevel Logger::stringToLevel(const std::string &name)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
if (name == "none")
|
|
|
|
return LL_NONE;
|
|
|
|
else if (name == "error")
|
|
|
|
return LL_ERROR;
|
|
|
|
else if (name == "warning")
|
|
|
|
return LL_WARNING;
|
|
|
|
else if (name == "action")
|
|
|
|
return LL_ACTION;
|
|
|
|
else if (name == "info")
|
|
|
|
return LL_INFO;
|
|
|
|
else if (name == "verbose")
|
|
|
|
return LL_VERBOSE;
|
|
|
|
else
|
|
|
|
return LL_MAX;
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::addOutput(ILogOutput *out)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-25 00:01:57 +02:00
|
|
|
addOutputMaxLevel(out, (LogLevel)(LL_MAX - 1));
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::addOutput(ILogOutput *out, LogLevel lev)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
m_outputs[lev].push_back(out);
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 07:43:32 +02:00
|
|
|
void Logger::addOutputMasked(ILogOutput *out, LogLevelMask mask)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < LL_MAX; i++) {
|
|
|
|
if (mask & LOGLEVEL_TO_MASKLEVEL(i))
|
|
|
|
m_outputs[i].push_back(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::addOutputMaxLevel(ILogOutput *out, LogLevel lev)
|
2011-11-27 12:52:10 +01:00
|
|
|
{
|
2015-10-25 00:01:57 +02:00
|
|
|
assert(lev < LL_MAX);
|
2015-10-13 09:57:44 +02:00
|
|
|
for (size_t i = 0; i <= lev; i++)
|
|
|
|
m_outputs[i].push_back(out);
|
2011-11-27 12:52:10 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 07:43:32 +02:00
|
|
|
LogLevelMask Logger::removeOutput(ILogOutput *out)
|
2014-12-12 21:12:31 +01:00
|
|
|
{
|
2015-07-25 07:43:32 +02:00
|
|
|
LogLevelMask ret_mask = 0;
|
2015-10-13 09:57:44 +02:00
|
|
|
for (size_t i = 0; i < LL_MAX; i++) {
|
|
|
|
std::vector<ILogOutput *>::iterator it;
|
2014-12-12 21:12:31 +01:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
it = std::find(m_outputs[i].begin(), m_outputs[i].end(), out);
|
2015-07-25 07:43:32 +02:00
|
|
|
if (it != m_outputs[i].end()) {
|
|
|
|
ret_mask |= LOGLEVEL_TO_MASKLEVEL(i);
|
2015-10-13 09:57:44 +02:00
|
|
|
m_outputs[i].erase(it);
|
2015-07-25 07:43:32 +02:00
|
|
|
}
|
2014-12-12 21:12:31 +01:00
|
|
|
}
|
2015-07-25 07:43:32 +02:00
|
|
|
return ret_mask;
|
2014-12-12 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::setLevelSilenced(LogLevel lev, bool silenced)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
m_silenced_levels[lev] = silenced;
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::registerThread(const std::string &name)
|
2011-11-27 12:52:10 +01:00
|
|
|
{
|
2015-10-17 03:12:30 +02:00
|
|
|
threadid_t id = thr_get_current_thread_id();
|
2015-10-13 09:57:44 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
m_thread_names[id] = name;
|
|
|
|
}
|
2015-03-05 16:20:56 +01:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::deregisterThread()
|
|
|
|
{
|
2015-10-17 03:12:30 +02:00
|
|
|
threadid_t id = thr_get_current_thread_id();
|
2015-10-13 09:57:44 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
m_thread_names.erase(id);
|
2011-11-27 12:52:10 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
const std::string Logger::getLevelLabel(LogLevel lev)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
static const std::string names[] = {
|
|
|
|
"",
|
|
|
|
"ERROR",
|
|
|
|
"WARNING",
|
|
|
|
"ACTION",
|
|
|
|
"INFO",
|
|
|
|
"VERBOSE",
|
|
|
|
};
|
|
|
|
assert(lev < LL_MAX && lev >= 0);
|
2015-10-28 03:27:32 +01:00
|
|
|
STATIC_ASSERT(ARRLEN(names) == LL_MAX,
|
|
|
|
mismatch_between_loglevel_names_and_enum);
|
2015-10-13 09:57:44 +02:00
|
|
|
return names[lev];
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
const std::string Logger::getThreadName()
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
std::map<threadid_t, std::string>::const_iterator it;
|
2015-03-05 16:20:56 +01:00
|
|
|
|
2015-10-17 03:12:30 +02:00
|
|
|
threadid_t id = thr_get_current_thread_id();
|
2015-10-13 09:57:44 +02:00
|
|
|
it = m_thread_names.find(id);
|
|
|
|
if (it != m_thread_names.end())
|
|
|
|
return it->second;
|
2014-12-12 21:12:31 +01:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
std::ostringstream os;
|
|
|
|
os << "#0x" << std::hex << id;
|
|
|
|
return os.str();
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::log(LogLevel lev, const std::string &text)
|
2011-10-16 11:45:00 +02:00
|
|
|
{
|
2015-10-13 09:57:44 +02:00
|
|
|
if (m_silenced_levels[lev])
|
|
|
|
return;
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
const std::string thread_name = getThreadName();
|
|
|
|
const std::string label = getLevelLabel(lev);
|
2015-10-24 12:52:14 +02:00
|
|
|
const std::string timestamp = getTimestamp();
|
2015-10-13 09:57:44 +02:00
|
|
|
std::ostringstream os(std::ios_base::binary);
|
2015-10-24 12:52:14 +02:00
|
|
|
os << timestamp << ": " << label << "[" << thread_name << "]: " << text;
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-24 12:52:14 +02:00
|
|
|
logToOutputs(lev, os.str(), timestamp, thread_name, text);
|
2015-10-13 09:57:44 +02:00
|
|
|
}
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void Logger::logRaw(LogLevel lev, const std::string &text)
|
|
|
|
{
|
|
|
|
if (m_silenced_levels[lev])
|
|
|
|
return;
|
|
|
|
|
2015-10-24 12:52:14 +02:00
|
|
|
logToOutputsRaw(lev, text);
|
2015-10-13 09:57:44 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 12:52:14 +02:00
|
|
|
void Logger::logToOutputsRaw(LogLevel lev, const std::string &line)
|
2015-10-13 09:57:44 +02:00
|
|
|
{
|
2015-10-24 12:52:14 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
for (size_t i = 0; i != m_outputs[lev].size(); i++)
|
|
|
|
m_outputs[lev][i]->logRaw(lev, line);
|
2015-10-13 09:57:44 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 12:52:14 +02:00
|
|
|
void Logger::logToOutputs(LogLevel lev, const std::string &combined,
|
|
|
|
const std::string &time, const std::string &thread_name,
|
|
|
|
const std::string &payload_text)
|
2015-10-13 09:57:44 +02:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
for (size_t i = 0; i != m_outputs[lev].size(); i++)
|
2015-10-24 12:52:14 +02:00
|
|
|
m_outputs[lev][i]->log(lev, combined, time, thread_name, payload_text);
|
2015-10-13 09:57:44 +02:00
|
|
|
}
|
|
|
|
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
////
|
|
|
|
//// *LogOutput methods
|
|
|
|
////
|
|
|
|
|
|
|
|
void FileLogOutput::open(const std::string &filename)
|
|
|
|
{
|
2015-10-24 12:52:14 +02:00
|
|
|
m_stream.open(filename.c_str(), std::ios::app | std::ios::ate);
|
|
|
|
if (!m_stream.good())
|
2015-10-13 09:57:44 +02:00
|
|
|
throw FileNotGoodException("Failed to open log file " +
|
|
|
|
filename + ": " + strerror(errno));
|
2015-10-24 12:52:14 +02:00
|
|
|
m_stream << "\n\n"
|
2015-10-13 09:57:44 +02:00
|
|
|
"-------------" << std::endl
|
|
|
|
<< " Separator" << std::endl
|
|
|
|
<< "-------------\n" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// *Buffer methods
|
|
|
|
////
|
|
|
|
|
|
|
|
int StringBuffer::overflow(int c)
|
|
|
|
{
|
|
|
|
push_back(c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::streamsize StringBuffer::xsputn(const char *s, std::streamsize n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
push_back(s[i]);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuffer::push_back(char c)
|
|
|
|
{
|
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
if (!buffer.empty())
|
|
|
|
flush(buffer);
|
|
|
|
buffer.clear();
|
|
|
|
} else {
|
|
|
|
buffer.push_back(c);
|
2011-10-16 11:45:00 +02:00
|
|
|
}
|
2015-10-13 09:57:44 +02:00
|
|
|
}
|
2013-11-30 21:22:15 +01:00
|
|
|
|
2011-10-16 11:45:00 +02:00
|
|
|
|
2015-10-13 09:57:44 +02:00
|
|
|
void LogBuffer::flush(const std::string &buffer)
|
|
|
|
{
|
|
|
|
logger.log(level, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RawLogBuffer::flush(const std::string &buffer)
|
|
|
|
{
|
|
|
|
g_logger.logRaw(LL_NONE, buffer);
|
|
|
|
}
|