2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Logger.hpp"
|
2019-06-16 01:51:16 +02:00
|
|
|
#include "Core.hpp"
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
2015-11-08 11:34:46 +01:00
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include <sqrat.h>
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace {
|
2020-03-22 09:31:43 +01:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma ide diagnostic ignored "hicpp-signed-bitwise"
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Common windows colors.
|
2016-02-20 23:25:00 +01:00
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
LC_NORMAL = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
|
|
|
LC_WHITE = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
|
|
|
LC_GRAY = FOREGROUND_INTENSITY,
|
|
|
|
LC_LIGHT_BLUE = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
|
|
|
|
LC_DARK_BLUE = FOREGROUND_BLUE,
|
|
|
|
LC_LIGHT_GREEN = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
|
|
|
|
LC_DARK_GREEN = FOREGROUND_GREEN,
|
|
|
|
LC_LIGHT_CYAN = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
|
|
|
LC_DARK_CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE,
|
|
|
|
LC_LIGHT_YELLOW = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
|
|
|
|
LC_DARK_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN,
|
|
|
|
LC_LIGHT_RED = FOREGROUND_INTENSITY | FOREGROUND_RED,
|
|
|
|
LC_DARK_RED = FOREGROUND_RED,
|
|
|
|
LC_LIGHT_MAGENTA = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
|
|
|
|
LC_DARK_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE
|
|
|
|
};
|
2020-03-22 09:31:43 +01:00
|
|
|
#pragma clang diagnostic pop
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Logging colors.
|
2016-02-20 23:25:00 +01:00
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
LC_DBG = LC_LIGHT_BLUE,
|
2016-05-22 05:20:38 +02:00
|
|
|
LC_USR = LC_GRAY,
|
2016-02-20 23:25:00 +01:00
|
|
|
LC_SCS = LC_LIGHT_GREEN,
|
|
|
|
LC_INF = LC_LIGHT_CYAN,
|
|
|
|
LC_WRN = LC_LIGHT_YELLOW,
|
2016-05-22 05:20:38 +02:00
|
|
|
LC_ERR = LC_LIGHT_RED,
|
|
|
|
LC_FTL = LC_LIGHT_MAGENTA
|
2016-02-20 23:25:00 +01:00
|
|
|
};
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Identify the associated message color.
|
|
|
|
*/
|
|
|
|
inline WORD GetLevelColor(BYTE level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case SqMod::LOGL_DBG: return LC_DBG;
|
|
|
|
case SqMod::LOGL_USR: return LC_USR;
|
|
|
|
case SqMod::LOGL_SCS: return LC_SCS;
|
|
|
|
case SqMod::LOGL_INF: return LC_INF;
|
|
|
|
case SqMod::LOGL_WRN: return LC_WRN;
|
|
|
|
case SqMod::LOGL_ERR: return LC_ERR;
|
|
|
|
case SqMod::LOGL_FTL: return LC_FTL;
|
|
|
|
default: return LC_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
} // Namespace::
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Identify the message prefix.
|
2016-02-20 23:25:00 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static inline CCStr GetLevelTag(Uint8 level)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
switch (level)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-07-10 17:44:54 +02:00
|
|
|
case LOGL_DBG: return "[DBG]";
|
|
|
|
case LOGL_USR: return "[USR]";
|
|
|
|
case LOGL_SCS: return "[SCS]";
|
|
|
|
case LOGL_INF: return "[INF]";
|
|
|
|
case LOGL_WRN: return "[WRN]";
|
|
|
|
case LOGL_ERR: return "[ERR]";
|
|
|
|
case LOGL_FTL: return "[FTL]";
|
|
|
|
default: return "[UNK]";
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:00:24 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2019-05-07 16:49:13 +02:00
|
|
|
* Logging level to callback index.
|
2019-05-01 17:00:24 +02:00
|
|
|
*/
|
|
|
|
static inline Uint8 GetLevelIdx(Uint8 level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case LOGL_DBG: return 0;
|
|
|
|
case LOGL_USR: return 1;
|
|
|
|
case LOGL_SCS: return 2;
|
|
|
|
case LOGL_INF: return 3;
|
|
|
|
case LOGL_WRN: return 4;
|
|
|
|
case LOGL_ERR: return 5;
|
|
|
|
case LOGL_FTL: return 6;
|
|
|
|
default: return 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 17:01:26 +02:00
|
|
|
#ifndef SQMOD_OS_WINDOWS
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Identify the message prefix and color.
|
|
|
|
*/
|
|
|
|
static inline CCStr GetColoredLevelTag(Uint8 level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
2016-07-10 17:44:54 +02:00
|
|
|
case LOGL_DBG: return "\033[21;94m[DBG]\033[0m";
|
|
|
|
case LOGL_USR: return "\033[21;37m[USR]\033[0m";
|
|
|
|
case LOGL_SCS: return "\033[21;92m[SCS]\033[0m";
|
|
|
|
case LOGL_INF: return "\033[21;96m[INF]\033[0m";
|
|
|
|
case LOGL_WRN: return "\033[21;93m[WRN]\033[0m";
|
|
|
|
case LOGL_ERR: return "\033[21;91m[ERR]\033[0m";
|
|
|
|
case LOGL_FTL: return "\033[21;95m[FTL]\033[0m";
|
|
|
|
default: return "\033[21;0m[UNK]\033[0m";
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-06-11 17:01:26 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Identify the message prefix and color.
|
|
|
|
*/
|
|
|
|
static inline CCStr GetColoredLevelTagDim(Uint8 level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
2016-07-10 17:44:54 +02:00
|
|
|
case LOGL_DBG: return "\033[21;94m[DBG]\033[2m";
|
|
|
|
case LOGL_USR: return "\033[21;37m[USR]\033[2m";
|
|
|
|
case LOGL_SCS: return "\033[21;92m[SCS]\033[2m";
|
|
|
|
case LOGL_INF: return "\033[21;96m[INF]\033[2m";
|
|
|
|
case LOGL_WRN: return "\033[21;93m[WRN]\033[2m";
|
|
|
|
case LOGL_ERR: return "\033[21;91m[ERR]\033[2m";
|
|
|
|
case LOGL_FTL: return "\033[21;95m[FTL]\033[2m";
|
|
|
|
default: return "\033[21;0m[UNK]\033[0m";
|
2016-06-11 17:01:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Output a logging message to the console window.
|
2016-02-20 23:25:00 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static inline void OutputConsoleMessage(Uint8 level, bool sub, CCStr tms, CCStr msg)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csb_state;
|
|
|
|
GetConsoleScreenBufferInfo(hstdout, &csb_state);
|
|
|
|
SetConsoleTextAttribute(hstdout, GetLevelColor(level));
|
|
|
|
if (tms)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
std::printf("%s %s ", GetLevelTag(level), tms);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::printf("%s ", GetLevelTag(level));
|
|
|
|
}
|
|
|
|
SetConsoleTextAttribute(hstdout, sub ? LC_NORMAL : LC_WHITE);
|
2016-06-11 17:01:26 +02:00
|
|
|
std::printf("%s\n", msg);
|
2016-05-22 05:20:38 +02:00
|
|
|
SetConsoleTextAttribute(hstdout, csb_state.wAttributes);
|
|
|
|
#else
|
|
|
|
if (tms)
|
|
|
|
{
|
2016-07-10 17:44:54 +02:00
|
|
|
std::printf("%s %s %s\033[0m\n",
|
2016-06-11 17:01:26 +02:00
|
|
|
sub ? GetColoredLevelTagDim(level) : GetColoredLevelTag(level), tms, msg);
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-10 17:44:54 +02:00
|
|
|
std::printf("%s %s\033[0m\n",
|
2016-06-11 17:01:26 +02:00
|
|
|
sub ? GetColoredLevelTagDim(level) : GetColoredLevelTag(level), msg);
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Identify the associated message color.
|
2016-02-20 23:25:00 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static inline CCStr GetTimeStampStr()
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
static CharT tmbuff[80];
|
|
|
|
std::time_t t = std::time(nullptr);
|
|
|
|
std::strftime(tmbuff, sizeof(tmbuff), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
|
|
|
|
// Return the resulted buffer
|
|
|
|
return tmbuff;
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Logger Logger::s_Inst;
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-22 09:31:43 +01:00
|
|
|
Logger::Logger() noexcept
|
2016-05-22 05:20:38 +02:00
|
|
|
: m_Buffer()
|
|
|
|
, m_ConsoleLevels(LOGL_ANY)
|
|
|
|
, m_LogFileLevels(~LOGL_DBG)
|
|
|
|
, m_ConsoleTime(false)
|
|
|
|
, m_LogFileTime(true)
|
2019-05-01 17:00:24 +02:00
|
|
|
, m_CyclicLock(false)
|
2019-05-17 21:54:08 +02:00
|
|
|
, m_StringTruncate(32)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_File(nullptr)
|
|
|
|
, m_Filename()
|
2019-05-01 17:00:24 +02:00
|
|
|
, m_LogCb{}
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ... */
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Logger::~Logger()
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
Close();
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Logger::Close()
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Is there a file handle to close?
|
|
|
|
if (m_File)
|
|
|
|
{
|
|
|
|
// Flush buffered data
|
|
|
|
std::fflush(m_File);
|
|
|
|
// Close the file handle
|
|
|
|
std::fclose(m_File);
|
|
|
|
// Prevent further use of this file handle
|
|
|
|
m_File = nullptr;
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Logger::SetLogFilename(CCStr filename)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Close the current logging file, if any
|
|
|
|
Close();
|
|
|
|
// Clear the current name
|
|
|
|
m_Filename.clear();
|
|
|
|
// Was there a name specified?
|
|
|
|
if (!filename || *filename == '\0')
|
|
|
|
{
|
|
|
|
return; // We're done here!
|
|
|
|
}
|
|
|
|
// Make sure the internal buffer has some memory
|
|
|
|
m_Buffer.Adjust(1024);
|
2016-06-19 14:24:25 +02:00
|
|
|
// Obtain the current time for generating the filename
|
|
|
|
const std::time_t t = std::time(nullptr);
|
|
|
|
// Generate the filename using the current time-stamp
|
|
|
|
if (std::strftime(m_Buffer.Data(), m_Buffer.Size(), filename, std::localtime(&t)) > 0)
|
2016-05-22 05:20:38 +02:00
|
|
|
{
|
|
|
|
m_Filename.assign(m_Buffer.Data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return; // We're done here!
|
|
|
|
}
|
|
|
|
// Attempt to open the file for writing
|
|
|
|
m_File = std::fopen(m_Filename.c_str(), "w");
|
|
|
|
// See if the file could be opened
|
|
|
|
if (!m_File)
|
|
|
|
{
|
|
|
|
OutputError("Unable to open the log file (%s) : %s", m_Filename.c_str(), std::strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:00:24 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-27 12:53:16 +02:00
|
|
|
void Logger::BindCb(Uint8 level, Function & func)
|
2019-05-01 17:00:24 +02:00
|
|
|
{
|
|
|
|
// Get the index of this log level
|
|
|
|
const Uint8 idx = GetLevelIdx(level);
|
|
|
|
// Is the log level valid?
|
|
|
|
if (idx > 6)
|
|
|
|
{
|
|
|
|
STHROWF("Out of range log level index: %d > 4", int(idx));
|
|
|
|
}
|
|
|
|
// Obtain the function instance called for this log level
|
|
|
|
Function & cb = m_LogCb[idx];
|
|
|
|
// Assign the specified environment and function
|
2020-04-27 12:53:16 +02:00
|
|
|
cb = std::move(func);
|
2019-05-01 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Initialize(CCStr filename)
|
|
|
|
{
|
|
|
|
// Close the logging file
|
|
|
|
Close();
|
|
|
|
// Allocate some memory in the buffer
|
|
|
|
m_Buffer.Adjust(1024);
|
|
|
|
// Set the log file name and open the file if necessary
|
|
|
|
SetLogFilename(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Terminate()
|
|
|
|
{
|
|
|
|
// Release all the buffer resources and references
|
|
|
|
m_Buffer.ResetAll();
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:00:24 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Release()
|
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
for (auto & f : m_LogCb)
|
2019-05-01 17:00:24 +02:00
|
|
|
{
|
2020-04-27 12:10:54 +02:00
|
|
|
f.Release();
|
2019-05-01 17:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQBool Logger::ProcessCb(Uint8 level, bool sub)
|
|
|
|
{
|
|
|
|
// Get the index of this log level
|
|
|
|
const Uint8 idx = GetLevelIdx(level);
|
|
|
|
// Is the log level valid and is there a callback associated?
|
|
|
|
if (idx > 6 || m_LogCb[idx].IsNull())
|
|
|
|
{
|
|
|
|
return SQFalse;
|
|
|
|
}
|
|
|
|
// Grab the associated function
|
|
|
|
Function & fn = m_LogCb[idx];
|
|
|
|
// Grab the default VM
|
2020-04-27 12:10:54 +02:00
|
|
|
HSQUIRRELVM vm = SqVM();
|
2019-05-01 17:00:24 +02:00
|
|
|
// Gram the top of the stack
|
|
|
|
SQInteger top = sq_gettop(vm);
|
|
|
|
// Push the function followed by the environment
|
|
|
|
sq_pushobject(vm, fn.GetFunc());
|
|
|
|
sq_pushobject(vm, fn.GetEnv());
|
|
|
|
// Push the log message
|
|
|
|
sq_pushstring(vm, m_Buffer.Get< SQChar >(), -1);
|
|
|
|
// Specify whether this is a sub message
|
|
|
|
sq_pushbool(vm, static_cast< SQBool >(sub));
|
|
|
|
// Make the function call and store the result
|
2020-03-22 09:31:43 +01:00
|
|
|
SQRESULT res = sq_call(vm, 3, static_cast< SQBool >(true),
|
|
|
|
static_cast< SQBool >(ErrorHandling::IsEnabled()));
|
2019-05-01 17:00:24 +02:00
|
|
|
// Default to non greedy callback
|
|
|
|
SQBool ret = SQFalse;
|
|
|
|
// Did the function succeeded and is the returned value not null?
|
|
|
|
if (SQ_SUCCEEDED(res) && sq_gettype(vm, -1) != OT_NULL) {
|
|
|
|
// Obtain the returned value
|
|
|
|
sq_tobool(vm, -1, &ret);
|
|
|
|
}
|
|
|
|
// Pop the callback object and return value from the stack
|
|
|
|
sq_settop(vm, top);
|
|
|
|
// Return the outcome of this callback
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Proccess(Uint8 level, bool sub)
|
|
|
|
{
|
2019-05-01 17:00:24 +02:00
|
|
|
// Is there a cyclic lock on the logger?
|
|
|
|
if (!m_CyclicLock)
|
|
|
|
{
|
|
|
|
// Lock the logger to prevent a cyclic dependency
|
|
|
|
m_CyclicLock = true;
|
|
|
|
// Attempt to process the script callback first
|
2020-03-22 09:31:43 +01:00
|
|
|
const bool greedy = static_cast< bool >(ProcessCb(level, sub));
|
2019-05-01 17:00:24 +02:00
|
|
|
// Unlock the logger after the callback was invoked
|
|
|
|
m_CyclicLock = false;
|
|
|
|
// Is the callback for this level greedy?
|
|
|
|
if (greedy)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// Obtain the time-stamp if necessary
|
|
|
|
CCStr tms = (m_ConsoleTime || m_LogFileTime) ? GetTimeStampStr() : nullptr;
|
|
|
|
// Are we allowed to send this message level to console?
|
|
|
|
if (m_ConsoleLevels & level)
|
|
|
|
{
|
|
|
|
OutputConsoleMessage(level, sub, (m_ConsoleTime ? tms : nullptr), m_Buffer.Get());
|
|
|
|
}
|
|
|
|
// Are we allowed to write it to a file?
|
|
|
|
if (m_File && (m_LogFileLevels & level))
|
|
|
|
{
|
|
|
|
// Write the level tag
|
|
|
|
std::fputs(GetLevelTag(level), m_File);
|
|
|
|
std::fputc(' ', m_File);
|
|
|
|
// Should we include the time-stamp?
|
|
|
|
if (m_LogFileTime && tms)
|
|
|
|
{
|
|
|
|
std::fputs(tms, m_File);
|
|
|
|
std::fputc(' ', m_File);
|
|
|
|
}
|
|
|
|
// Write the message
|
|
|
|
std::fputs(m_Buffer.Get(), m_File);
|
|
|
|
// Append a new line
|
|
|
|
std::fputc('\n', m_File);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 22:54:44 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Send(Uint8 level, bool sub, CCStr msg, size_t len)
|
|
|
|
{
|
|
|
|
// Is this level even allowed?
|
|
|
|
if ((m_ConsoleLevels & level) || (m_LogFileLevels & level))
|
|
|
|
{
|
|
|
|
// Generate the message in the buffer
|
|
|
|
m_Buffer.Write(0, msg, static_cast< Buffer::SzType >(len));
|
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(level, sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Send(Uint8 level, bool sub, CCStr fmt, va_list args)
|
|
|
|
{
|
|
|
|
// Is this level even allowed?
|
|
|
|
if ((m_ConsoleLevels & level) || (m_LogFileLevels & level))
|
|
|
|
{
|
|
|
|
// Generate the message in the buffer
|
|
|
|
m_Buffer.WriteF(0, fmt, args);
|
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(level, sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Write(Uint8 level, bool sub, CCStr fmt, ...)
|
|
|
|
{
|
|
|
|
if ((m_ConsoleLevels & level) || (m_LogFileLevels & level))
|
|
|
|
{
|
|
|
|
// Initialize the variable argument list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
// Generate the message in the buffer
|
|
|
|
m_Buffer.WriteF(0, fmt, args);
|
|
|
|
// Finalize the variable argument list
|
|
|
|
va_end(args);
|
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(level, sub);
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void Logger::Debug(CCStr fmt, ...)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Initialize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Forward the call to the actual debug function
|
2016-02-20 23:25:00 +01:00
|
|
|
Debug(fmt, args);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Finalize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_end(args);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
void Logger::Debug(CCStr fmt, va_list args)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
using namespace Sqrat;
|
|
|
|
// Retrieve the default Squirrel VM
|
2020-04-27 12:10:54 +02:00
|
|
|
HSQUIRRELVM vm = SqVM();
|
2016-05-22 05:20:38 +02:00
|
|
|
// Used to acquire
|
2016-02-20 23:25:00 +01:00
|
|
|
SQStackInfos si;
|
2016-05-22 05:20:38 +02:00
|
|
|
// Write the message to the buffer
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 ret = m_Buffer.WriteF(0, fmt, args);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Obtain information about the current stack level
|
2016-02-20 23:25:00 +01:00
|
|
|
if (SQ_SUCCEEDED(sq_stackinfos(vm, 1, &si)))
|
2016-05-22 05:20:38 +02:00
|
|
|
{
|
2019-06-16 01:51:16 +02:00
|
|
|
// Whether we should fall back to normal message
|
|
|
|
bool fall_back = true;
|
|
|
|
// Should (can) we include a snippet of code in the traceback?
|
|
|
|
if (Core::Get().IsDebugging() && si.source && (si.line > 0)) {
|
|
|
|
// Grab the associated line of code
|
|
|
|
String code = Core::Get().FetchCodeLine(si.source, si.line-1, true);
|
|
|
|
// Valid line of code?
|
|
|
|
if (!code.empty())
|
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n=>Code: %s\n]"
|
2019-06-16 01:51:16 +02:00
|
|
|
, si.source, si.line, si.funcname ? si.funcname : _SC("unknown"), code.c_str());
|
|
|
|
fall_back = false; // No need to fall back to normal message!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Should the regular message be shown instead?
|
|
|
|
if (fall_back)
|
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n]"
|
2019-06-16 01:51:16 +02:00
|
|
|
, si.source ? si.source : _SC("unknown")
|
|
|
|
, si.line
|
|
|
|
, si.funcname ? si.funcname : _SC("unknown"));
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
2016-05-22 05:20:38 +02:00
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "\n[\n=>Location: unknown\n=>Line: unknown\n=>Function: unknown\n]");
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(LOGL_ERR, true);
|
|
|
|
// Begin the traceback process
|
2016-02-20 23:25:00 +01:00
|
|
|
ret = m_Buffer.WriteF(0, "Traceback:\n[\n");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Traceback the function call
|
2016-02-20 23:25:00 +01:00
|
|
|
for (Int32 level = 1; SQ_SUCCEEDED(sq_stackinfos(vm, level, &si)); ++level)
|
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] %s (%d) [%s]\n", level
|
2016-02-20 23:25:00 +01:00
|
|
|
, si.source ? si.source : _SC("unknown")
|
|
|
|
, si.line
|
|
|
|
, si.funcname ? si.funcname : _SC("unknown"));
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// End the function call traceback
|
2020-03-22 09:31:43 +01:00
|
|
|
m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "]");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(LOGL_INF, true);
|
|
|
|
// Temporary variables to retrieve stack information
|
2020-03-22 09:31:43 +01:00
|
|
|
CSStr s_ = nullptr, name = nullptr;
|
2016-02-20 23:25:00 +01:00
|
|
|
SQInteger i_, seq = 0;
|
|
|
|
SQFloat f_;
|
|
|
|
SQUserPointer p_;
|
2019-05-17 21:54:08 +02:00
|
|
|
StackStrF ssf_;
|
2016-05-22 05:20:38 +02:00
|
|
|
// Begin the local variables information
|
2016-02-20 23:25:00 +01:00
|
|
|
ret = m_Buffer.WriteF(0, "Locals:\n[\n");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Process each stack level
|
2016-02-20 23:25:00 +01:00
|
|
|
for (Int32 level = 0; level < 10; level++)
|
|
|
|
{
|
|
|
|
seq = 0;
|
2016-05-22 05:20:38 +02:00
|
|
|
// Display all locals in the current stack level
|
2020-03-22 09:31:43 +01:00
|
|
|
while((name = sq_getlocal(vm, static_cast< SQUnsignedInteger >(level), static_cast< SQUnsignedInteger >(seq))))
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
++seq;
|
|
|
|
switch(sq_gettype(vm, -1))
|
|
|
|
{
|
|
|
|
case OT_NULL:
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] NULL [%s]\n", level, name);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_INTEGER:
|
|
|
|
sq_getinteger(vm, -1, &i_);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] INTEGER [%s] with value: %" PRINT_INT_FMT "\n", level, name, i_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_FLOAT:
|
|
|
|
sq_getfloat(vm, -1, &f_);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] FLOAT [%s] with value: %f\n", level, name, f_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_USERPOINTER:
|
|
|
|
sq_getuserpointer(vm, -1, &p_);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] USERPOINTER [%s] pointing at: %p\n", level, name, p_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_STRING:
|
2019-05-17 21:54:08 +02:00
|
|
|
sq_getstringandsize(vm, -1, &s_, &i_);
|
|
|
|
if (i_ > 0) {
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] STRING [%s] of %" PRINT_INT_FMT " characters: %.*s\n", level, name, i_, m_StringTruncate, s_);
|
2019-05-17 21:54:08 +02:00
|
|
|
} else {
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] STRING [%s] empty\n", level, name);
|
2019-05-17 21:54:08 +02:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_TABLE:
|
2019-05-17 21:54:08 +02:00
|
|
|
i_ = sq_getsize(vm, -1);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] TABLE [%s] with %" PRINT_INT_FMT " elements\n", level, name, i_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_ARRAY:
|
2019-05-17 21:54:08 +02:00
|
|
|
i_ = sq_getsize(vm, -1);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] ARRAY [%s] with %" PRINT_INT_FMT " elements\n", level, name, i_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_CLOSURE:
|
2019-05-17 21:54:08 +02:00
|
|
|
s_ = _SC("@anonymous");
|
|
|
|
if (SQ_SUCCEEDED(sq_getclosurename(vm, -1))) {
|
|
|
|
if (sq_gettype(vm, -1) != OT_NULL && SQ_SUCCEEDED(ssf_.Release(vm).Proc())) {
|
|
|
|
s_ = ssf_.mPtr;
|
|
|
|
}
|
|
|
|
sq_poptop(vm);
|
|
|
|
}
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] CLOSURE [%s] with name: %s\n", level, name, s_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_NATIVECLOSURE:
|
2019-05-17 21:54:08 +02:00
|
|
|
s_ = _SC("@unknown");
|
|
|
|
if (SQ_SUCCEEDED(sq_getclosurename(vm, -1))) {
|
|
|
|
if (sq_gettype(vm, -1) != OT_NULL && SQ_SUCCEEDED(ssf_.Release(vm).Proc())) {
|
|
|
|
s_ = ssf_.mPtr;
|
|
|
|
}
|
|
|
|
sq_poptop(vm);
|
|
|
|
}
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] NATIVECLOSURE [%s] with name: %s\n", level, name, s_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_GENERATOR:
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] GENERATOR [%s]\n", level, name);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_USERDATA:
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] USERDATA [%s]\n", level, name);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_THREAD:
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] THREAD [%s]\n", level, name);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_CLASS:
|
2019-05-17 21:54:08 +02:00
|
|
|
// Brute force our way into getting the name of this class without blowing up
|
|
|
|
s_ = _SC("@unknown");
|
|
|
|
// Create a dummy, non-constructed instance and hope `_typeof` doesn't rely on member variables
|
|
|
|
if (SQ_SUCCEEDED(sq_createinstance(vm, -1))) {
|
|
|
|
// Attempt a `_typeof` on that instance
|
|
|
|
if (SQ_SUCCEEDED(sq_typeof(vm, -1))) {
|
|
|
|
if (SQ_SUCCEEDED(ssf_.Release(vm).Proc())) {
|
|
|
|
s_ = ssf_.mPtr;
|
|
|
|
}
|
|
|
|
// Pop the name object
|
|
|
|
sq_poptop(vm);
|
2019-06-02 16:12:17 +02:00
|
|
|
}
|
2019-05-17 21:54:08 +02:00
|
|
|
// Pop the dummy instance
|
|
|
|
sq_poptop(vm);
|
|
|
|
}
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] CLASS [%s] of type: %s\n", level, name, s_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_INSTANCE:
|
2019-05-17 21:54:08 +02:00
|
|
|
s_ = _SC("@unknown");
|
|
|
|
if (SQ_SUCCEEDED(sq_typeof(vm, -1))) {
|
|
|
|
if (SQ_SUCCEEDED(ssf_.Release(vm).Proc())) {
|
|
|
|
s_ = ssf_.mPtr;
|
|
|
|
}
|
|
|
|
sq_poptop(vm);
|
|
|
|
}
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(
|
|
|
|
static_cast< Buffer::SzType >(ret), "=> [%d] INSTANCE [%s] of type: %s\n", level, name, s_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_WEAKREF:
|
2019-05-17 21:54:08 +02:00
|
|
|
s_ = _SC("@unknown");
|
|
|
|
// Attempt to grab the value pointed by the weak reference
|
|
|
|
if (SQ_SUCCEEDED(sq_getweakrefval(vm, -1))) {
|
|
|
|
// Attempt a `_typeof` on that instance
|
|
|
|
if (SQ_SUCCEEDED(sq_typeof(vm, -1))) {
|
|
|
|
if (SQ_SUCCEEDED(ssf_.Release(vm).Proc())) {
|
|
|
|
s_ = ssf_.mPtr;
|
|
|
|
}
|
|
|
|
// Pop the name object
|
|
|
|
sq_poptop(vm);
|
2019-06-02 16:12:17 +02:00
|
|
|
}
|
2019-05-17 21:54:08 +02:00
|
|
|
// Pop the referenced value
|
|
|
|
sq_poptop(vm);
|
|
|
|
}
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] WEAKREF [%s] of type: %s\n", level, name, s_);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
case OT_BOOL:
|
|
|
|
sq_getinteger(vm, -1, &i_);
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] BOOL [%s] with value: %s\n", level, name, i_ ? _SC("true") : _SC("false"));
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2020-03-22 09:31:43 +01:00
|
|
|
ret += m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "=> [%d] UNKNOWN [%s]\n", level, name);
|
2016-02-20 23:25:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// End the variables information
|
2020-03-22 09:31:43 +01:00
|
|
|
m_Buffer.WriteF(static_cast< Buffer::SzType >(ret), "]");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Process the message in the buffer
|
|
|
|
Proccess(LOGL_INF, true);
|
2015-11-08 11:34:46 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#define SQMOD_LOG(N_, L_, S_) /*
|
2016-02-20 23:25:00 +01:00
|
|
|
*/ void N_(CCStr fmt, ...) /*
|
2015-09-30 02:56:11 +02:00
|
|
|
*/ { /*
|
|
|
|
*/ va_list args; /*
|
|
|
|
*/ va_start(args, fmt); /*
|
2016-05-22 05:20:38 +02:00
|
|
|
*/ Logger::Get().Send(L_, S_, fmt, args); /*
|
2015-09-30 02:56:11 +02:00
|
|
|
*/ va_end(args); /*
|
|
|
|
*/ } /*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
SQMOD_LOG(LogDbg, LOGL_DBG, false)
|
|
|
|
SQMOD_LOG(LogUsr, LOGL_USR, false)
|
|
|
|
SQMOD_LOG(LogScs, LOGL_SCS, false)
|
|
|
|
SQMOD_LOG(LogInf, LOGL_INF, false)
|
|
|
|
SQMOD_LOG(LogWrn, LOGL_WRN, false)
|
|
|
|
SQMOD_LOG(LogErr, LOGL_ERR, false)
|
|
|
|
SQMOD_LOG(LogFtl, LOGL_FTL, false)
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
SQMOD_LOG(LogSDbg, LOGL_DBG, true)
|
|
|
|
SQMOD_LOG(LogSUsr, LOGL_USR, true)
|
|
|
|
SQMOD_LOG(LogSScs, LOGL_SCS, true)
|
|
|
|
SQMOD_LOG(LogSInf, LOGL_INF, true)
|
|
|
|
SQMOD_LOG(LogSWrn, LOGL_WRN, true)
|
|
|
|
SQMOD_LOG(LogSErr, LOGL_ERR, true)
|
|
|
|
SQMOD_LOG(LogSFtl, LOGL_FTL, true)
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2020-05-28 19:59:29 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#define SQMOD_VLOG(N_, L_, S_) /*
|
|
|
|
*/ void N_(CCStr fmt, va_list vlist) /*
|
|
|
|
*/ { /*
|
|
|
|
*/ Logger::Get().Send(L_, S_, fmt, vlist); /*
|
|
|
|
*/ } /*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQMOD_VLOG(LogDbgV, LOGL_DBG, false)
|
|
|
|
SQMOD_VLOG(LogUsrV, LOGL_USR, false)
|
|
|
|
SQMOD_VLOG(LogScsV, LOGL_SCS, false)
|
|
|
|
SQMOD_VLOG(LogInfV, LOGL_INF, false)
|
|
|
|
SQMOD_VLOG(LogWrnV, LOGL_WRN, false)
|
|
|
|
SQMOD_VLOG(LogErrV, LOGL_ERR, false)
|
|
|
|
SQMOD_VLOG(LogFtlV, LOGL_FTL, false)
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQMOD_VLOG(LogSDbgV, LOGL_DBG, true)
|
|
|
|
SQMOD_VLOG(LogSUsrV, LOGL_USR, true)
|
|
|
|
SQMOD_VLOG(LogSScsV, LOGL_SCS, true)
|
|
|
|
SQMOD_VLOG(LogSInfV, LOGL_INF, true)
|
|
|
|
SQMOD_VLOG(LogSWrnV, LOGL_WRN, true)
|
|
|
|
SQMOD_VLOG(LogSErrV, LOGL_ERR, true)
|
|
|
|
SQMOD_VLOG(LogSFtlV, LOGL_FTL, true)
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#define SQMOD_CLOG(N_, L_, S_) /*
|
2016-02-20 23:25:00 +01:00
|
|
|
*/bool N_(bool c, CCStr fmt, ...) /*
|
2015-09-30 02:56:11 +02:00
|
|
|
*/ { /*
|
2016-02-20 23:25:00 +01:00
|
|
|
*/ if (!c) /*
|
2016-05-22 05:20:38 +02:00
|
|
|
*/ { /*
|
|
|
|
*/ return c; /*
|
|
|
|
*/ } /*
|
2016-02-20 23:25:00 +01:00
|
|
|
*/ va_list args; /*
|
|
|
|
*/ va_start(args, fmt); /*
|
2016-05-22 05:20:38 +02:00
|
|
|
*/ Logger::Get().Send(L_, S_, fmt, args); /*
|
2016-02-20 23:25:00 +01:00
|
|
|
*/ va_end(args); /*
|
2015-09-30 02:56:11 +02:00
|
|
|
*/ return c; /*
|
|
|
|
*/ } /*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
SQMOD_CLOG(cLogDbg, LOGL_DBG, false)
|
|
|
|
SQMOD_CLOG(cLogUsr, LOGL_USR, false)
|
|
|
|
SQMOD_CLOG(cLogScs, LOGL_SCS, false)
|
|
|
|
SQMOD_CLOG(cLogInf, LOGL_INF, false)
|
|
|
|
SQMOD_CLOG(cLogWrn, LOGL_WRN, false)
|
|
|
|
SQMOD_CLOG(cLogErr, LOGL_ERR, false)
|
|
|
|
SQMOD_CLOG(cLogFtl, LOGL_FTL, false)
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
SQMOD_CLOG(cLogSDbg, LOGL_DBG, true)
|
|
|
|
SQMOD_CLOG(cLogSUsr, LOGL_USR, true)
|
|
|
|
SQMOD_CLOG(cLogSScs, LOGL_SCS, true)
|
|
|
|
SQMOD_CLOG(cLogSInf, LOGL_INF, true)
|
|
|
|
SQMOD_CLOG(cLogSWrn, LOGL_WRN, true)
|
|
|
|
SQMOD_CLOG(cLogSErr, LOGL_ERR, true)
|
|
|
|
SQMOD_CLOG(cLogSFtl, LOGL_FTL, true)
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-05-24 17:45:50 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template < Uint8 L, bool S > static SQInteger LogBasicMessage(HSQUIRRELVM vm)
|
|
|
|
{
|
2020-03-22 09:31:43 +01:00
|
|
|
const auto top = sq_gettop(vm);
|
2016-05-24 17:45:50 +02:00
|
|
|
// Was the message value specified?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing message value");
|
|
|
|
}
|
|
|
|
// Attempt to generate the string value
|
2018-07-29 23:58:27 +02:00
|
|
|
StackStrF val(vm, 2);
|
2016-05-24 17:45:50 +02:00
|
|
|
// Have we failed to retrieve the string?
|
2018-07-29 23:58:27 +02:00
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
2016-05-24 17:45:50 +02:00
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// Forward the resulted string value to the logger
|
|
|
|
Logger::Get().Write(L, S, "%s", val.mPtr);
|
|
|
|
// This function does not return a value
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:00:24 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-27 12:53:16 +02:00
|
|
|
template < Uint8 L > static void BindLogCallback(Function & func)
|
2019-05-01 17:00:24 +02:00
|
|
|
{
|
2020-04-27 12:53:16 +02:00
|
|
|
Logger::Get().BindCb(L, func);
|
2019-05-01 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 14:24:25 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogClose()
|
|
|
|
{
|
|
|
|
Logger::Get().Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogInitialize(CSStr filename)
|
|
|
|
{
|
|
|
|
Logger::Get().Initialize(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogToggleConsoleTime(bool toggle)
|
|
|
|
{
|
|
|
|
Logger::Get().ToggleConsoleTime(toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static bool SqLogConsoleHasTime()
|
|
|
|
{
|
|
|
|
return Logger::Get().ConsoleHasTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogToggleLogFileTime(bool toggle)
|
|
|
|
{
|
|
|
|
Logger::Get().ToggleLogFileTime(toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static bool SqLogLogFileHasTime()
|
|
|
|
{
|
|
|
|
return Logger::Get().LogFileHasTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogSetConsoleLevels(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().SetConsoleLevels(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static bool SqLogGetConsoleLevels()
|
|
|
|
{
|
|
|
|
return Logger::Get().GetConsoleLevels();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogSetLogFileLevels(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().SetLogFileLevels(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static bool SqLogGetLogFileLevels()
|
|
|
|
{
|
|
|
|
return Logger::Get().GetLogFileLevels();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogEnableConsoleLevel(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().EnableConsoleLevel(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogDisableConsoleLevel(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().DisableConsoleLevel(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogToggleConsoleLevel(SQInteger level, bool toggle)
|
|
|
|
{
|
|
|
|
Logger::Get().ToggleConsoleLevel(ConvTo< Uint8 >::From(level), toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogEnableLogFileLevel(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().EnableLogFileLevel(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogDisableLogFileLevel(SQInteger level)
|
|
|
|
{
|
|
|
|
Logger::Get().DisableLogFileLevel(ConvTo< Uint8 >::From(level));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogToggleLogFileLevel(SQInteger level, bool toggle)
|
|
|
|
{
|
|
|
|
Logger::Get().ToggleLogFileLevel(ConvTo< Uint8 >::From(level), toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static const String & SqLogGetLogFilename()
|
|
|
|
{
|
|
|
|
return Logger::Get().GetLogFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogSetLogFilename(CSStr filename)
|
|
|
|
{
|
|
|
|
Logger::Get().SetLogFilename(filename);
|
|
|
|
}
|
|
|
|
|
2019-05-17 21:54:08 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQInteger SqLogGetStringTruncate()
|
|
|
|
{
|
|
|
|
return static_cast< SQInteger >(Logger::Get().GetStringTruncate());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static void SqLogSetStringTruncate(SQInteger nc)
|
|
|
|
{
|
|
|
|
Logger::Get().SetStringTruncate(ConvTo< Uint32 >::From(nc));
|
|
|
|
}
|
|
|
|
|
2016-05-24 17:45:50 +02:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_Log(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
RootTable(vm)
|
|
|
|
.Bind(_SC("SqLog"), Table(vm)
|
|
|
|
.SquirrelFunc(_SC("Dbg"), &LogBasicMessage< LOGL_DBG, false >)
|
|
|
|
.SquirrelFunc(_SC("Usr"), &LogBasicMessage< LOGL_USR, false >)
|
|
|
|
.SquirrelFunc(_SC("Scs"), &LogBasicMessage< LOGL_SCS, false >)
|
|
|
|
.SquirrelFunc(_SC("Inf"), &LogBasicMessage< LOGL_INF, false >)
|
|
|
|
.SquirrelFunc(_SC("Wrn"), &LogBasicMessage< LOGL_WRN, false >)
|
|
|
|
.SquirrelFunc(_SC("Err"), &LogBasicMessage< LOGL_ERR, false >)
|
|
|
|
.SquirrelFunc(_SC("Ftl"), &LogBasicMessage< LOGL_FTL, false >)
|
|
|
|
.SquirrelFunc(_SC("SDbg"), &LogBasicMessage< LOGL_DBG, true >)
|
|
|
|
.SquirrelFunc(_SC("SUsr"), &LogBasicMessage< LOGL_USR, true >)
|
|
|
|
.SquirrelFunc(_SC("SScs"), &LogBasicMessage< LOGL_SCS, true >)
|
|
|
|
.SquirrelFunc(_SC("SInf"), &LogBasicMessage< LOGL_INF, true >)
|
|
|
|
.SquirrelFunc(_SC("SWrn"), &LogBasicMessage< LOGL_WRN, true >)
|
|
|
|
.SquirrelFunc(_SC("SErr"), &LogBasicMessage< LOGL_ERR, true >)
|
|
|
|
.SquirrelFunc(_SC("SFtl"), &LogBasicMessage< LOGL_FTL, true >)
|
2019-05-01 17:00:24 +02:00
|
|
|
.Func(_SC("BindDbg"), &BindLogCallback< LOGL_DBG >)
|
|
|
|
.Func(_SC("BindUsr"), &BindLogCallback< LOGL_USR >)
|
|
|
|
.Func(_SC("BindScs"), &BindLogCallback< LOGL_SCS >)
|
|
|
|
.Func(_SC("BindInf"), &BindLogCallback< LOGL_INF >)
|
|
|
|
.Func(_SC("BindWrn"), &BindLogCallback< LOGL_WRN >)
|
|
|
|
.Func(_SC("BindErr"), &BindLogCallback< LOGL_ERR >)
|
|
|
|
.Func(_SC("BindFtl"), &BindLogCallback< LOGL_FTL >)
|
2016-06-19 14:24:25 +02:00
|
|
|
.Func(_SC("Close"), &SqLogClose)
|
|
|
|
.Func(_SC("Initialize"), &SqLogInitialize)
|
|
|
|
.Func(_SC("ToggleConsoleTime"), &SqLogToggleConsoleTime)
|
|
|
|
.Func(_SC("ConsoleHasTime"), &SqLogConsoleHasTime)
|
|
|
|
.Func(_SC("ToggleLogFileTime"), &SqLogToggleLogFileTime)
|
|
|
|
.Func(_SC("LogFileHasTime"), &SqLogLogFileHasTime)
|
|
|
|
.Func(_SC("SetConsoleLevels"), &SqLogSetConsoleLevels)
|
|
|
|
.Func(_SC("GetConsoleLevels"), &SqLogGetConsoleLevels)
|
|
|
|
.Func(_SC("SetLogFileLevels"), &SqLogSetLogFileLevels)
|
|
|
|
.Func(_SC("GetLogFileLevels"), &SqLogGetLogFileLevels)
|
|
|
|
.Func(_SC("EnableConsoleLevel"), &SqLogEnableConsoleLevel)
|
|
|
|
.Func(_SC("DisableConsoleLevel"), &SqLogDisableConsoleLevel)
|
|
|
|
.Func(_SC("ToggleConsoleLevel"), &SqLogToggleConsoleLevel)
|
|
|
|
.Func(_SC("EnableLogFileLevel"), &SqLogEnableLogFileLevel)
|
|
|
|
.Func(_SC("DisableLogFileLevel"), &SqLogDisableLogFileLevel)
|
|
|
|
.Func(_SC("ToggleLogFileLevel"), &SqLogToggleLogFileLevel)
|
|
|
|
.Func(_SC("GetLogFilename"), &SqLogGetLogFilename)
|
|
|
|
.Func(_SC("SetLogFilename"), &SqLogSetLogFilename)
|
2019-05-17 21:54:08 +02:00
|
|
|
.Func(_SC("GetStringTruncate"), &SqLogGetStringTruncate)
|
|
|
|
.Func(_SC("SetStringTruncate"), &SqLogSetStringTruncate)
|
2016-05-24 17:45:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
} // Namespace:: SqMod
|