2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Logger.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
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 {
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
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-05-22 05:20:38 +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]";
|
2015-09-30 02:56:11 +02:00
|
|
|
default: return "[UNK]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Identify the message prefix and color.
|
|
|
|
*/
|
|
|
|
static inline CCStr GetColoredLevelTag(Uint8 level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
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);
|
|
|
|
std::puts(msg);
|
|
|
|
SetConsoleTextAttribute(hstdout, csb_state.wAttributes);
|
|
|
|
#else
|
|
|
|
if (tms)
|
|
|
|
{
|
|
|
|
std::printf("%s %s ", GetColoredLevelTag(level), tms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::printf("%s ", GetColoredLevelTag(level));
|
|
|
|
}
|
|
|
|
std::puts(msg);
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Raw console message output.
|
|
|
|
*/
|
|
|
|
static inline void OutputMessageImpl(CCStr msg, va_list args)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csb_before;
|
|
|
|
GetConsoleScreenBufferInfo( hstdout, &csb_before);
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
|
|
|
|
std::printf("[SQMOD] ");
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
|
2015-09-30 02:56:11 +02:00
|
|
|
#else
|
2016-05-22 05:20:38 +02:00
|
|
|
std::printf("\033[21;32m[SQMOD]\033[0m");
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Raw console error output.
|
|
|
|
*/
|
|
|
|
static inline void OutputErrorImpl(CCStr msg, va_list args)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csb_before;
|
|
|
|
GetConsoleScreenBufferInfo( hstdout, &csb_before);
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY);
|
|
|
|
std::printf("[SQMOD] ");
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
|
|
|
|
#else
|
|
|
|
std::printf("\033[21;91m[SQMOD]\033[0m");
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
|
|
|
#endif
|
|
|
|
}
|
2015-09-30 02:56:11 +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
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-11-01 04:48:01 +01:00
|
|
|
Logger::Logger()
|
2016-05-22 05:20:38 +02:00
|
|
|
: m_Buffer()
|
|
|
|
, m_ConsoleLevels(LOGL_ANY)
|
|
|
|
, m_LogFileLevels(~LOGL_DBG)
|
|
|
|
, m_ConsoleTime(false)
|
|
|
|
, m_LogFileTime(true)
|
|
|
|
, m_File(nullptr)
|
|
|
|
, m_Filename()
|
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);
|
|
|
|
// Generate the filename using the current timestamp
|
|
|
|
std::time_t t = std::time(nullptr);
|
|
|
|
std::strftime(m_Buffer.Data(), m_Buffer.Size(), filename, std::localtime(&t));
|
|
|
|
// Is the resulted filename valid?
|
|
|
|
if (m_Buffer.At(0) != '\0')
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Logger::Proccess(Uint8 level, bool sub)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
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
|
2016-02-20 23:25:00 +01:00
|
|
|
HSQUIRRELVM vm = DefaultVM::Get();
|
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
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Buffer.WriteF(ret, "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n]"
|
|
|
|
, 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
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Buffer.WriteF(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)
|
|
|
|
{
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] %s (%d) [%s]\n", level
|
|
|
|
, 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
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Buffer.WriteF(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
|
|
|
|
CSStr s_ = 0, name = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
SQInteger i_, seq = 0;
|
|
|
|
SQFloat f_;
|
|
|
|
SQUserPointer p_;
|
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
|
2016-02-20 23:25:00 +01:00
|
|
|
while((name = sq_getlocal(vm, level, seq)))
|
|
|
|
{
|
|
|
|
++seq;
|
|
|
|
switch(sq_gettype(vm, -1))
|
|
|
|
{
|
|
|
|
case OT_NULL:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] NULL [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_INTEGER:
|
|
|
|
sq_getinteger(vm, -1, &i_);
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] INTEGER [%s] : %d\n", level, name, i_);
|
|
|
|
break;
|
|
|
|
case OT_FLOAT:
|
|
|
|
sq_getfloat(vm, -1, &f_);
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] FLOAT [%s] : %f\n", level, name, f_);
|
|
|
|
break;
|
|
|
|
case OT_USERPOINTER:
|
|
|
|
sq_getuserpointer(vm, -1, &p_);
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] USERPOINTER [%s] : %p\n", level, name, p_);
|
|
|
|
break;
|
|
|
|
case OT_STRING:
|
|
|
|
sq_getstring(vm, -1, &s_);
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] STRING [%s] : %s\n", level, name, s_);
|
|
|
|
break;
|
|
|
|
case OT_TABLE:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] TABLE [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_ARRAY:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] ARRAY [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_CLOSURE:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] CLOSURE [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_NATIVECLOSURE:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] NATIVECLOSURE [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_GENERATOR:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] GENERATOR [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_USERDATA:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] USERDATA [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_THREAD:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] THREAD [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_CLASS:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] CLASS [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_INSTANCE:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] INSTANCE [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_WEAKREF:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] WEAKREF [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
case OT_BOOL:
|
|
|
|
sq_getinteger(vm, -1, &i_);
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] BOOL [%s] : %s\n", level, name, i_ ? _SC("true") : _SC("false"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret += m_Buffer.WriteF(ret, "=> [%d] UNKNOWN [%s] : ...\n", level, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// End the variables information
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Buffer.WriteF(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
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#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-22 05:20:38 +02:00
|
|
|
void OutputDebug(CCStr msg, ...)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputMessageImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
#else
|
|
|
|
SQMOD_UNUSED_VAR(msg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void OutputMessage(CCStr msg, ...)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputMessageImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void OutputError(CCStr msg, ...)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputErrorImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
} // Namespace:: SqMod
|