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

Format validation.

Include format validation in log messages by using GCC's format attribute.
This commit is contained in:
Sandu Liviu Catalin 2019-06-02 17:12:17 +03:00
parent faf0aea78e
commit b3ab60a6c7
4 changed files with 58 additions and 42 deletions

View File

@ -47,49 +47,49 @@ typedef std::pair< Signal *, LightObj > SignalPair;
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary logging functions.
*/
extern void LogDbg(CCStr fmt, ...);
extern void LogUsr(CCStr fmt, ...);
extern void LogScs(CCStr fmt, ...);
extern void LogInf(CCStr fmt, ...);
extern void LogWrn(CCStr fmt, ...);
extern void LogErr(CCStr fmt, ...);
extern void LogFtl(CCStr fmt, ...);
extern void LogDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary logging functions.
*/
extern void LogSDbg(CCStr fmt, ...);
extern void LogSUsr(CCStr fmt, ...);
extern void LogSScs(CCStr fmt, ...);
extern void LogSInf(CCStr fmt, ...);
extern void LogSWrn(CCStr fmt, ...);
extern void LogSErr(CCStr fmt, ...);
extern void LogSFtl(CCStr fmt, ...);
extern void LogSDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary conditional logging functions.
*/
extern bool cLogDbg(bool exp, CCStr fmt, ...);
extern bool cLogUsr(bool exp, CCStr fmt, ...);
extern bool cLogScs(bool exp, CCStr fmt, ...);
extern bool cLogInf(bool exp, CCStr fmt, ...);
extern bool cLogWrn(bool exp, CCStr fmt, ...);
extern bool cLogErr(bool exp, CCStr fmt, ...);
extern bool cLogFtl(bool exp, CCStr fmt, ...);
extern bool cLogDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary conditional logging functions.
*/
extern bool cLogSDbg(bool exp, CCStr fmt, ...);
extern bool cLogSUsr(bool exp, CCStr fmt, ...);
extern bool cLogSScs(bool exp, CCStr fmt, ...);
extern bool cLogSInf(bool exp, CCStr fmt, ...);
extern bool cLogSWrn(bool exp, CCStr fmt, ...);
extern bool cLogSErr(bool exp, CCStr fmt, ...);
extern bool cLogSFtl(bool exp, CCStr fmt, ...);
extern bool cLogSDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Get a persistent AABB instance with the given values.

View File

@ -350,7 +350,7 @@ bool Core::Initialize()
// Are there any options to read?
if (conf.GetAllKeys("Options", options) || options.size() > 0)
{
cLogDbg(m_Verbosity >= 1, "Found (%u) options in the configuration file", options.size());
cLogDbg(m_Verbosity >= 1, "Found (%" PRINT_SZ_FMT ") options in the configuration file", options.size());
// Process all the specified keys under the [Options] section
for (const auto & option : options)
{
@ -423,7 +423,7 @@ bool Core::Execute()
return false; // One of the scripts failed to execute
}
cLogDbg(m_Verbosity >= 2, "Completed execution of stage (%u) scripts. Pending scripts %u", levels, m_PendingScripts.size());
cLogDbg(m_Verbosity >= 2, "Completed execution of stage (%u) scripts. Pending scripts %" PRINT_SZ_FMT, levels, m_PendingScripts.size());
}
// Create the null entity instances
@ -852,7 +852,7 @@ SQInteger Core::RuntimeErrorHandler(HSQUIRRELVM vm)
// ------------------------------------------------------------------------------------------------
void Core::CompilerErrorHandler(HSQUIRRELVM /*vm*/, CSStr desc, CSStr src, SQInteger line, SQInteger column)
{
LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %d\n=>Column: %d\n]", desc, src, line, column);
LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_INT_FMT "\n=>Column: " PRINT_INT_FMT "\n]", desc, src, line, column);
}
} // Namespace:: SqMod

View File

@ -521,7 +521,7 @@ void Logger::Debug(CCStr fmt, va_list args)
break;
case OT_INTEGER:
sq_getinteger(vm, -1, &i_);
ret += m_Buffer.WriteF(ret, "=> [%d] INTEGER [%s] with value: " _PRINT_INT_FMT "\n", level, name, i_);
ret += m_Buffer.WriteF(ret, "=> [%d] INTEGER [%s] with value: %" PRINT_INT_FMT "\n", level, name, i_);
break;
case OT_FLOAT:
sq_getfloat(vm, -1, &f_);
@ -534,18 +534,18 @@ void Logger::Debug(CCStr fmt, va_list args)
case OT_STRING:
sq_getstringandsize(vm, -1, &s_, &i_);
if (i_ > 0) {
ret += m_Buffer.WriteF(ret, "=> [%d] STRING [%s] of " _PRINT_INT_FMT " characters: %.*s\n", level, name, i_, m_StringTruncate, s_);
ret += m_Buffer.WriteF(ret, "=> [%d] STRING [%s] of %" PRINT_INT_FMT " characters: %.*s\n", level, name, i_, m_StringTruncate, s_);
} else {
ret += m_Buffer.WriteF(ret, "=> [%d] STRING [%s] empty\n", level, name);
}
break;
case OT_TABLE:
i_ = sq_getsize(vm, -1);
ret += m_Buffer.WriteF(ret, "=> [%d] TABLE [%s] with " _PRINT_INT_FMT " elements\n", level, name, i_);
ret += m_Buffer.WriteF(ret, "=> [%d] TABLE [%s] with %" PRINT_INT_FMT " elements\n", level, name, i_);
break;
case OT_ARRAY:
i_ = sq_getsize(vm, -1);
ret += m_Buffer.WriteF(ret, "=> [%d] ARRAY [%s] with " _PRINT_INT_FMT " elements\n", level, name, i_);
ret += m_Buffer.WriteF(ret, "=> [%d] ARRAY [%s] with %" PRINT_INT_FMT " elements\n", level, name, i_);
break;
case OT_CLOSURE:
s_ = _SC("@anonymous");

View File

@ -6,6 +6,7 @@
// ------------------------------------------------------------------------------------------------
#include <cstddef>
#include <cstdint>
#include <cassert>
#include <string>
@ -57,7 +58,7 @@
#else
#define SQMOD_OS_32
#define SQMOD_OS_LINUX32
#define SQMOD_ARCHITECTURE 1
#define SQMOD_ARCHITECTURE 1
#define SQMOD_PLATFORM 2
#endif
#endif
@ -68,12 +69,12 @@
#if __x86_64__ || __ppc64__
#define SQMOD_OS_64
#define SQMOD_OS_MACOS64
#define SQMOD_ARCHITECTURE 2
#define SQMOD_ARCHITECTURE 2
#define SQMOD_PLATFORM 3
#else
#define SQMOD_OS_32
#define SQMOD_OS_MACOS32
#define SQMOD_ARCHITECTURE 1
#define SQMOD_ARCHITECTURE 1
#define SQMOD_PLATFORM 3
#endif
#endif
@ -84,12 +85,12 @@
#if __x86_64__ || __ppc64__
#define SQMOD_OS_64
#define SQMOD_OS_UNIX64
#define SQMOD_ARCHITECTURE 2
#define SQMOD_ARCHITECTURE 2
#define SQMOD_PLATFORM 4
#else
#define SQMOD_OS_32
#define SQMOD_OS_UNIX32
#define SQMOD_ARCHITECTURE 1
#define SQMOD_ARCHITECTURE 1
#define SQMOD_PLATFORM 4
#endif
#endif
@ -451,6 +452,21 @@ enum EntityType
} // Namespace:: SqMod
/* ------------------------------------------------------------------------------------------------
* FORMAT ATTRIBUTE
*/
#define SQMOD_FORMAT_ATTR(mode, index, first) __attribute__ ((format(mode, index, first)))
#ifdef _SQ64
#define PRINT_SZ_FMT "llu"
#define PRINT_INT_FMT "lld"
#define PRINT_UINT_FMT "llu"
#else
#define PRINT_SZ_FMT "u"
#define PRINT_INT_FMT "d"
#define PRINT_UINT_FMT "u"
#endif
/* ------------------------------------------------------------------------------------------------
* HELPERS
*/