From 24d2ca30c2e7b4bc68937707752dd3f97ba05e4f Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 8 Nov 2015 12:34:46 +0200 Subject: [PATCH] Exposed a couple logging functions to the script and prepared the logging class for documentation. --- source/Logger.cpp | 99 +++++++++++++- source/Logger.hpp | 312 +++++++++++++++++++++++++++++++++++++++----- source/Register.cpp | 1 + 3 files changed, 381 insertions(+), 31 deletions(-) diff --git a/source/Logger.cpp b/source/Logger.cpp index 1a458263..90e46fde 100644 --- a/source/Logger.cpp +++ b/source/Logger.cpp @@ -1,10 +1,15 @@ #include "Logger.hpp" +#include "Register.hpp" +#include "Core.hpp" // ------------------------------------------------------------------------------------------------ #include #include #include +// ------------------------------------------------------------------------------------------------ +#include + // ------------------------------------------------------------------------------------------------ namespace { @@ -241,7 +246,7 @@ void Logger::SetVerbosity(SQInt32 level) } // ------------------------------------------------------------------------------------------------ -void Logger::Send(Uint8 type, bool sub, const char * fmt, va_list args) +void Logger::Send(Uint8 type, bool sub, const char * fmt, va_list args) { // Verify that this level is allowed to be streamed if (!(m_ConsoleLevels & type) && !(m_FileLevels & type)) return; @@ -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 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 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 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 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 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 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 diff --git a/source/Logger.hpp b/source/Logger.hpp index dc8164ea..18dca805 100644 --- a/source/Logger.hpp +++ b/source/Logger.hpp @@ -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; +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 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; }; // -------------------------------------------------------------------------------------------- diff --git a/source/Register.cpp b/source/Register.cpp index c1b98523..6e3ff3ce 100644 --- a/source/Register.cpp +++ b/source/Register.cpp @@ -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;