51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LOG_HEADER
-#define LOG_HEADER
+#pragma once
#include <map>
#include <queue>
#include <string>
#include <fstream>
-#include "threads.h"
+#include <thread>
+#include <mutex>
+#if !defined(_WIN32) // POSIX
+ #include <unistd.h>
+#endif
+#include "irrlichttypes.h"
class ILogOutput;
LL_MAX,
};
+enum LogColor {
+ LOG_COLOR_NEVER,
+ LOG_COLOR_ALWAYS,
+ LOG_COLOR_AUTO,
+};
+
+typedef u8 LogLevelMask;
+#define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
+
class Logger {
public:
void addOutput(ILogOutput *out);
void addOutput(ILogOutput *out, LogLevel lev);
+ void addOutputMasked(ILogOutput *out, LogLevelMask mask);
void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
- void removeOutput(ILogOutput *out);
+ LogLevelMask removeOutput(ILogOutput *out);
void setLevelSilenced(LogLevel lev, bool silenced);
void registerThread(const std::string &name);
bool getTraceEnabled() { return m_trace_enabled; }
static LogLevel stringToLevel(const std::string &name);
+ static const std::string getLevelLabel(LogLevel lev);
+
+ static LogColor color_mode;
private:
- void logToSystem(LogLevel, const std::string &text);
- void logToOutputs(LogLevel, const std::string &text);
+ void logToOutputsRaw(LogLevel, const std::string &line);
+ void logToOutputs(LogLevel, const std::string &combined,
+ const std::string &time, const std::string &thread_name,
+ const std::string &payload_text);
- const std::string getLevelLabel(LogLevel lev);
const std::string getThreadName();
std::vector<ILogOutput *> m_outputs[LL_MAX];
// written to when one thread has access currently).
// Works on all known architectures (x86, ARM, MIPS).
volatile bool m_silenced_levels[LL_MAX];
- std::map<threadid_t, std::string> m_thread_names;
- mutable Mutex m_mutex;
+ std::map<std::thread::id, std::string> m_thread_names;
+ mutable std::mutex m_mutex;
bool m_trace_enabled;
};
class ILogOutput {
public:
- virtual void log(const std::string &line) = 0;
+ virtual void logRaw(LogLevel, const std::string &line) = 0;
+ virtual void log(LogLevel, const std::string &combined,
+ const std::string &time, const std::string &thread_name,
+ const std::string &payload_text) = 0;
};
-class StreamLogOutput : public ILogOutput {
+class ICombinedLogOutput : public ILogOutput {
public:
- StreamLogOutput(std::ostream &stream) :
- stream(stream)
+ void log(LogLevel lev, const std::string &combined,
+ const std::string &time, const std::string &thread_name,
+ const std::string &payload_text)
{
+ logRaw(lev, combined);
}
+};
- void log(const std::string &line)
+class StreamLogOutput : public ICombinedLogOutput {
+public:
+ StreamLogOutput(std::ostream &stream) :
+ m_stream(stream)
{
- stream << line << std::endl;
+#if !defined(_WIN32)
+ is_tty = isatty(fileno(stdout));
+#else
+ is_tty = false;
+#endif
}
+ void logRaw(LogLevel lev, const std::string &line);
+
private:
- std::ostream &stream;
+ std::ostream &m_stream;
+ bool is_tty;
};
-class FileLogOutput : public ILogOutput {
+class FileLogOutput : public ICombinedLogOutput {
public:
- void open(const std::string &filename);
+ void setFile(const std::string &filename, s64 file_size_max);
- void log(const std::string &line)
+ void logRaw(LogLevel lev, const std::string &line)
{
- stream << line << std::endl;
+ m_stream << line << std::endl;
}
private:
- std::ofstream stream;
+ std::ofstream m_stream;
};
-class LogOutputBuffer : public ILogOutput {
+class LogOutputBuffer : public ICombinedLogOutput {
public:
- LogOutputBuffer(Logger &logger, LogLevel lev) :
- logger(logger)
+ LogOutputBuffer(Logger &logger) :
+ m_logger(logger)
{
- logger.addOutput(this, lev);
- }
+ updateLogLevel();
+ };
- ~LogOutputBuffer()
+ virtual ~LogOutputBuffer()
{
- logger.removeOutput(this);
+ m_logger.removeOutput(this);
}
- virtual void log(const std::string &line)
+ void updateLogLevel();
+
+ void logRaw(LogLevel lev, const std::string &line);
+
+ void clear()
{
- buffer.push(line);
+ m_buffer = std::queue<std::string>();
}
- bool empty()
+ bool empty() const
{
- return buffer.empty();
+ return m_buffer.empty();
}
std::string get()
{
if (empty())
return "";
- std::string s = buffer.front();
- buffer.pop();
+ std::string s = m_buffer.front();
+ m_buffer.pop();
return s;
}
private:
- std::queue<std::string> buffer;
- Logger &logger;
+ std::queue<std::string> m_buffer;
+ Logger &m_logger;
};
#define dout_con (*dout_con_ptr)
#define derr_con (*derr_con_ptr)
#define dout_server (*dout_server_ptr)
-#define derr_server (*derr_server_ptr)
#ifndef SERVER
#define dout_client (*dout_client_ptr)
- #define derr_client (*derr_client_ptr)
-#endif
-
-
#endif