2015-09-30 02:56:11 +02:00
|
|
|
#ifndef _LOGGER_HPP_
|
|
|
|
#define _LOGGER_HPP_
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include "SqBase.hpp"
|
2016-02-20 23:25:00 +01:00
|
|
|
#include "Base/Buffer.hpp"
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Supported levels of logging.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
enum LogLvl
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
LOGL_NIL = (1 << 0),
|
|
|
|
LOGL_DBG = (1 << 1),
|
|
|
|
LOGL_USR = (1 << 2),
|
|
|
|
LOGL_SCS = (1 << 3),
|
|
|
|
LOGL_INF = (1 << 4),
|
|
|
|
LOGL_WRN = (1 << 5),
|
|
|
|
LOGL_ERR = (1 << 6),
|
|
|
|
LOGL_FTL = (1 << 7),
|
|
|
|
LOGL_ANY = 0xFF
|
2016-02-20 23:25:00 +01:00
|
|
|
};
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Class responsible for logging output.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
class Logger
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
private:
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
static Logger s_Inst; // Logger instance.
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
2015-11-01 04:48:01 +01:00
|
|
|
Logger();
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
2016-04-20 08:24:37 +02:00
|
|
|
Logger(const Logger & o) = delete;
|
2016-05-22 05:20:38 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor. (disabled)
|
|
|
|
*/
|
2016-04-20 08:24:37 +02:00
|
|
|
Logger(Logger && o) = delete;
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~Logger();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Logger & operator = (const Logger & o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Logger & operator = (Logger && o) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Buffer m_Buffer; // Common buffer where the message is written.
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
Uint8 m_ConsoleLevels; // The levels allowed to be outputted to console.
|
|
|
|
Uint8 m_LogFileLevels; // The levels allowed to be outputted to log file.
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
bool m_ConsoleTime; // Whether console messages should be timestamped.
|
|
|
|
bool m_LogFileTime; // Whether log file messages should be timestamped.
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
std::FILE* m_File; // Handle to the file where the logs should be saved.
|
|
|
|
std::string m_Filename; // The name of the file where the logs are saved.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Process the message in the internal buffer.
|
|
|
|
*/
|
|
|
|
void Proccess(Uint8 level, bool sub);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the logger instance.
|
|
|
|
*/
|
|
|
|
static Logger & Get()
|
|
|
|
{
|
|
|
|
return s_Inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Flush buffered data and close the logging file.
|
|
|
|
*/
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Initialize the logging utility.
|
|
|
|
*/
|
|
|
|
void Initialize(CCStr filename);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Terminate the logging utility.
|
|
|
|
*/
|
|
|
|
void Terminate();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Enable or disable console message time stamping.
|
|
|
|
*/
|
|
|
|
void ToggleConsoleTime(bool enabled)
|
|
|
|
{
|
|
|
|
m_ConsoleTime = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether console message time stamping is enabled.
|
|
|
|
*/
|
|
|
|
bool ConsoleHasTime() const
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return m_ConsoleTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Enable or disable log file message time stamping.
|
|
|
|
*/
|
|
|
|
void ToggleLogFileTime(bool enabled)
|
|
|
|
{
|
|
|
|
m_LogFileTime = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether log file message time stamping is enabled.
|
|
|
|
*/
|
|
|
|
bool LogFileHasTime() const
|
|
|
|
{
|
|
|
|
return m_LogFileTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Set the console level flags.
|
|
|
|
*/
|
|
|
|
void SetConsoleLevels(Uint8 levels)
|
|
|
|
{
|
|
|
|
m_ConsoleLevels = levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the console level flags.
|
|
|
|
*/
|
|
|
|
Uint8 GetConsoleLevels() const
|
|
|
|
{
|
|
|
|
return m_ConsoleLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Set the log file level flags.
|
|
|
|
*/
|
|
|
|
void SetLogFileLevels(Uint8 levels)
|
|
|
|
{
|
|
|
|
m_LogFileLevels = levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the log file level flags.
|
|
|
|
*/
|
|
|
|
Uint8 GetLogFileLevels() const
|
|
|
|
{
|
|
|
|
return m_LogFileLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Enable a certain console logging level.
|
|
|
|
*/
|
|
|
|
void EnableConsoleLevel(Uint8 level)
|
|
|
|
{
|
|
|
|
m_ConsoleLevels |= level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Disable a certain console logging level.
|
|
|
|
*/
|
|
|
|
void DisableConsoleLevel(Uint8 level)
|
|
|
|
{
|
|
|
|
if (m_ConsoleLevels & level)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_ConsoleLevels ^= level;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Toggle a certain console logging level.
|
|
|
|
*/
|
|
|
|
void ToggleConsoleLevel(Uint8 level, bool toggle)
|
|
|
|
{
|
|
|
|
if (toggle)
|
|
|
|
{
|
|
|
|
EnableConsoleLevel(level);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DisableConsoleLevel(level);
|
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Enable a certain log file logging level.
|
|
|
|
*/
|
|
|
|
void EnableLogFileLevel(Uint8 level)
|
|
|
|
{
|
|
|
|
m_LogFileLevels |= level;
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Disable a certain log file logging level.
|
|
|
|
*/
|
|
|
|
void DisableLogFileLevel(Uint8 level)
|
|
|
|
{
|
|
|
|
m_LogFileLevels |= level;
|
|
|
|
m_LogFileLevels ^= level;
|
|
|
|
}
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Toggle a certain log file logging level.
|
|
|
|
*/
|
|
|
|
void ToggleLogFileLevel(Uint8 level, bool toggle)
|
|
|
|
{
|
|
|
|
if (toggle)
|
|
|
|
{
|
|
|
|
EnableLogFileLevel(level);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DisableLogFileLevel(level);
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the log file name.
|
|
|
|
*/
|
|
|
|
const std::string & GetLogFilename() const
|
|
|
|
{
|
|
|
|
return m_Filename;
|
|
|
|
}
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the log file name.
|
|
|
|
*/
|
|
|
|
void SetLogFilename(CCStr filename);
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Send a log message.
|
|
|
|
*/
|
|
|
|
void Send(Uint8 level, bool sub, CCStr fmt, va_list args);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Write a log message.
|
|
|
|
*/
|
|
|
|
void Write(Uint8 level, bool sub, CCStr fmt, ...);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Generate a debug message.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void Debug(CCStr fmt, ...);
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Generate a debug message.
|
|
|
|
*/
|
|
|
|
void Debug(CCStr fmt, va_list args);
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
};
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Raw console message output.
|
|
|
|
*/
|
|
|
|
void OutputDebug(CCStr msg, ...);
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Raw console message output.
|
|
|
|
*/
|
|
|
|
void OutputMessage(CCStr msg, ...);
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Raw console message output.
|
|
|
|
*/
|
|
|
|
void OutputError(CCStr msg, ...);
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
|
|
|
#endif // _LOGGER_HPP_
|