1
0
Fork 0
cuberite-2a/src/Logger.h

76 lines
1.6 KiB
C
Raw Normal View History

2014-08-10 18:44:49 +00:00
#pragma once
2014-08-12 15:05:04 +00:00
class cLogger
2014-08-10 18:44:49 +00:00
{
2014-08-12 15:05:04 +00:00
public:
2014-08-10 18:44:49 +00:00
2014-08-12 15:05:04 +00:00
class cListener
2014-08-10 18:44:49 +00:00
{
public:
virtual void Log(std::string_view a_Message, eLogLevel a_LogLevel) = 0;
2014-08-12 15:05:04 +00:00
virtual ~cListener(){}
2014-08-10 18:44:49 +00:00
};
class cAttachment
{
public:
cAttachment() : m_listener(nullptr) {}
cAttachment(cAttachment && a_other)
: m_listener(a_other.m_listener)
{
a_other.m_listener = nullptr;
}
~cAttachment()
{
if (m_listener != nullptr)
{
cLogger::GetInstance().DetachListener(m_listener);
}
}
cAttachment & operator=(cAttachment && a_other)
{
m_listener = a_other.m_listener;
a_other.m_listener = nullptr;
return *this;
}
private:
cListener * m_listener;
friend class cLogger;
cAttachment(cListener * a_listener) : m_listener(a_listener) {}
};
/** Log a message formatted with a printf style formatting string. */
2020-05-04 18:21:48 +00:00
void LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList);
/** Log a message formatted with a python style formatting string. */
2020-05-04 18:21:48 +00:00
void LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList);
2014-08-10 18:44:49 +00:00
2014-08-12 15:05:04 +00:00
/** Logs the simple text message at the specified log level. */
void LogSimple(std::string_view a_Message, eLogLevel a_LogLevel = eLogLevel::Regular);
2014-08-10 18:44:49 +00:00
cAttachment AttachListener(std::unique_ptr<cListener> a_Listener);
2016-02-05 21:45:45 +00:00
2014-08-12 15:05:04 +00:00
static cLogger & GetInstance(void);
2020-05-04 18:21:48 +00:00
2014-08-12 15:05:04 +00:00
// Must be called before calling GetInstance in a multithreaded context
static void InitiateMultithreading();
2020-05-04 18:21:48 +00:00
2014-08-12 15:05:04 +00:00
private:
2014-08-10 18:44:49 +00:00
2014-08-12 15:05:04 +00:00
cCriticalSection m_CriticalSection;
std::vector<std::unique_ptr<cListener>> m_LogListeners;
2014-08-10 18:44:49 +00:00
void DetachListener(cListener * a_Listener);
void LogLine(std::string_view a_Line, eLogLevel a_LogLevel);
};