1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Exposed a couple logging functions to the script and prepared the logging class for documentation.

This commit is contained in:
Sandu Liviu Catalin 2015-11-08 12:34:46 +02:00
parent 673119db3d
commit 24d2ca30c2
3 changed files with 381 additions and 31 deletions

View File

@ -1,10 +1,15 @@
#include "Logger.hpp"
#include "Register.hpp"
#include "Core.hpp"
// ------------------------------------------------------------------------------------------------
#include <ctime>
#include <cstdio>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
namespace {
@ -316,6 +321,15 @@ void Logger::Send(Uint8 type, bool sub, const char * fmt, va_list args)
}
}
// ------------------------------------------------------------------------------------------------
void Logger::Message(Uint8 type, bool sub, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
Send(type, sub, fmt, args);
va_end(args);
}
// ------------------------------------------------------------------------------------------------
#define SQMOD_LOG(N_, L_, S_) /*
*/ void Logger::N_(const char * fmt, ...) /*
@ -378,4 +392,87 @@ SQMOD_CLOG(cSWrn, LEVEL_WRN, true)
SQMOD_CLOG(cSErr, LEVEL_ERR, true)
SQMOD_CLOG(cSFtl, LEVEL_FTL, true)
// ------------------------------------------------------------------------------------------------
template < Uint8 L, bool S > static SQInteger LogBasicMessage(HSQUIRRELVM vm)
{
const SQInteger top = sq_gettop(vm);
// Are there any arguments on the stack?
if (top <= 1)
{
_Log->Err("Attempting to <log message> without specifying a value");
}
// Is there a single string or at least something that can convert to a string on the stack?
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
// Variable where the resulted string will be retrieved
const SQChar * msg = 0;
// Attempt to retrieve the specified message from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
{
_Log->Err("Unable to <retrieve the log message> from the stack");
// Failed to log the value
return 0;
}
// Log the specified string
_Log->Message(L, S, "%s", msg);
}
else if (top > 2)
{
// Variables containing the resulted string
SQChar * msg = NULL;
SQInteger len = 0;
// Attempt to call the format function with the passed arguments
if (SQ_FAILED(sqstd_format(vm, 2, &len, &msg)))
{
_Log->Err("Unable to <generate the log message> because : %s", Error::Message(vm).c_str());
// Failed to log the value
return 0;
}
// Log the resulted string
_Log->Message(L, S, "%s", msg);
}
else
{
_Log->Err("Unable to <extract the log message> from the specified value");
// Failed to log the value
return 0;
}
// At this point everything went correctly
return 0;
}
// ================================================================================================
bool Register_Log(HSQUIRRELVM vm)
{
// // Attempt to create the Log namespace
Sqrat::Table logns(vm);
// Output debugging information
LogDbg("Beginning registration of <Log functions> type");
// Attempt to register the free functions
logns.SquirrelFunc(_SC("Dbg"), &LogBasicMessage< Logger::LEVEL_DBG, false >);
logns.SquirrelFunc(_SC("Msg"), &LogBasicMessage< Logger::LEVEL_MSG, false >);
logns.SquirrelFunc(_SC("Scs"), &LogBasicMessage< Logger::LEVEL_SCS, false >);
logns.SquirrelFunc(_SC("Inf"), &LogBasicMessage< Logger::LEVEL_INF, false >);
logns.SquirrelFunc(_SC("Wrn"), &LogBasicMessage< Logger::LEVEL_WRN, false >);
logns.SquirrelFunc(_SC("Err"), &LogBasicMessage< Logger::LEVEL_ERR, false >);
logns.SquirrelFunc(_SC("Ftl"), &LogBasicMessage< Logger::LEVEL_FTL, false >);
logns.SquirrelFunc(_SC("SDbg"), &LogBasicMessage< Logger::LEVEL_DBG, true >);
logns.SquirrelFunc(_SC("SMsg"), &LogBasicMessage< Logger::LEVEL_MSG, true >);
logns.SquirrelFunc(_SC("SScs"), &LogBasicMessage< Logger::LEVEL_SCS, true >);
logns.SquirrelFunc(_SC("SInf"), &LogBasicMessage< Logger::LEVEL_INF, true >);
logns.SquirrelFunc(_SC("SWrn"), &LogBasicMessage< Logger::LEVEL_WRN, true >);
logns.SquirrelFunc(_SC("SErr"), &LogBasicMessage< Logger::LEVEL_ERR, true >);
logns.SquirrelFunc(_SC("SFtl"), &LogBasicMessage< Logger::LEVEL_FTL, true >);
// Output debugging information
LogDbg("Registration of <Log functions> type was successful");
// Attempt to bind the namespace to the root table
Sqrat::RootTable(vm).Bind(_SC("Log"), logns);
// Registration succeeded
return true;
}
} // Namespace:: SqMod

View File

@ -7,11 +7,21 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* ...
*/
class Logger
{
/* --------------------------------------------------------------------------------------------
* Allow only the smart pointer to delete this class instance as soon as it's not needed.
*/
friend class std::unique_ptr< Logger, void(*)(Logger *) >;
public:
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
static constexpr Uint8 LEVEL_NIL = (1 << 0);
static constexpr Uint8 LEVEL_DBG = (1 << 1);
static constexpr Uint8 LEVEL_MSG = (1 << 2);
@ -22,121 +32,363 @@ public:
static constexpr Uint8 LEVEL_FTL = (1 << 7);
static constexpr Uint8 LEVEL_ANY = 0xFF;
private:
// --------------------------------------------------------------------------------------------
friend class std::unique_ptr<Logger, void(*)(Logger *)>;
protected:
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
Logger();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
Logger(Logger const &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
Logger(Logger &&) = delete;
/* --------------------------------------------------------------------------------------------
* ...
*/
~Logger();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
Logger & operator=(Logger const &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator (disabled).
*/
Logger & operator=(Logger &&) = delete;
/* --------------------------------------------------------------------------------------------
* ...
*/
static void _Finalizer(Logger * ptr);
// --------------------------------------------------------------------------------------------
public:
// --------------------------------------------------------------------------------------------
typedef std::unique_ptr<Logger, void(*)(Logger *)> Pointer;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
typedef std::unique_ptr< Logger, void(*)(Logger *) > Pointer;
/* --------------------------------------------------------------------------------------------
* ...
*/
static Pointer Inst();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
bool Init();
/* --------------------------------------------------------------------------------------------
* ...
*/
bool Load();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void Deinit();
/* --------------------------------------------------------------------------------------------
* ...
*/
void Unload();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void Terminate();
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Called by the core class before the VM is closed to release all resources.
*/
void VMClose();
/* --------------------------------------------------------------------------------------------
* ...
*/
void ToggleConsoleTime(bool enabled);
/* --------------------------------------------------------------------------------------------
* ...
*/
void ToggleFileTime(bool enabled);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool HasConsoleTime() const;
/* --------------------------------------------------------------------------------------------
* ...
*/
bool HasFileTime() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetConsoleLevels(Uint8 levels);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetFileLevels(Uint8 levels);
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 GetConsoleLevels() const;
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 GetFileLevels() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void EnableConsoleLevel(Uint8 level);
/* --------------------------------------------------------------------------------------------
* ...
*/
void EnableFileLevel(Uint8 level);
/* --------------------------------------------------------------------------------------------
* ...
*/
void DisableConsoleLevel(Uint8 level);
/* --------------------------------------------------------------------------------------------
* ...
*/
void DisableFileLevel(Uint8 level);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 GetDebugLevel() const;
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetDebugLevel(Uint8 level);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
SQInt32 GetVerbosity() const;
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetVerbosity(SQInt32 level);
public:
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void Send(Uint8 type, bool sub, const char * fmt, va_list args);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void Message(Uint8 type, bool sub, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Dbg(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Msg(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Scs(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Inf(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Wrn(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Err(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Ftl(const char * fmt, ...);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void SDbg(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SMsg(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SScs(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SInf(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SWrn(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SErr(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
void SFtl(const char * fmt, ...);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cDbg(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cMsg(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cScs(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cInf(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cWrn(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cErr(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cFtl(bool cond, const char * fmt, ...);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSDbg(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSMsg(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSScs(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSInf(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSWrn(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSErr(bool cond, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool cSFtl(bool cond, const char * fmt, ...);
private:
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
bool m_ConsoleTime;
/* --------------------------------------------------------------------------------------------
* ...
*/
bool m_FileTime;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 m_ConsoleLevels;
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 m_FileLevels;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
String m_LogPath;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
Uint8 m_DebugLevel;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
SQInt32 m_Verbosity;
/* --------------------------------------------------------------------------------------------
* ...
*/
Function m_OnMessage;
/* --------------------------------------------------------------------------------------------
* ...
*/
Function m_Format;
};
// --------------------------------------------------------------------------------------------

View File

@ -78,6 +78,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
_Log->cFtl(!Register_LocalEvent(vm), "Unable to register: LocalEvent") || \
_Log->cFtl(!Register_Event(vm), "Unable to register: Event") || \
_Log->cFtl(!Register_Log(vm), "Unable to register: Logger") || \
_Log->cFtl(!Register_Cmd(vm), "Unable to register: Command")
) return false;