From 7dcfa9c8f0a7e2204c958de891179a3ea188f767 Mon Sep 17 00:00:00 2001 From: DanyLE Date: Fri, 7 Jul 2023 19:51:33 +0200 Subject: [PATCH] fix JSON encode bug + improve logging function --- silkmvc/core/hook.lua | 25 +++++++++++++++++++++---- silkmvc/core/utils.lua | 11 +++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/silkmvc/core/hook.lua b/silkmvc/core/hook.lua index 6cd7820..601d7d3 100644 --- a/silkmvc/core/hook.lua +++ b/silkmvc/core/hook.lua @@ -126,17 +126,34 @@ function loadscript(file, args) end end --- logging helpers +local __MSG = function(fmt,...) + local va = {...} + if va then + local out = {} + for i,v in ipairs(va) do + local esc = v:gsub("[%%]", {["%"] = "%%"}) + table.insert(out, esc) + end + va = out + end + local log_msg = fmt + local ret,msg = pcall(function() return string.format(fmt, table.unpack(va)) end) + if msg then + log_msg = msg + end + return log_msg +end + function LOG_INFO(fmt, ...) - fcgio:log_info(string.format(fmt or "LOG", ...)) + fcgio:log_info(__MSG(fmt or "INFO",table.unpack({...}))) end function LOG_ERROR(fmt, ...) - fcgio:log_error(string.format(fmt or "ERROR", ...)) + fcgio:log_error(__MSG(fmt or "ERROR",table.unpack({...}))) end function LOG_DEBUG(fmt, ...) - fcgio:log_debug(string.format(fmt or "ERROR", ...)) + fcgio:log_debug(__MSG(fmt or "DEBUG",table.unpack({...}))) end function LOG_WARN(fmt, ...) diff --git a/silkmvc/core/utils.lua b/silkmvc/core/utils.lua index 85453a3..b59d10b 100644 --- a/silkmvc/core/utils.lua +++ b/silkmvc/core/utils.lua @@ -20,7 +20,7 @@ function utils.is_array(table) return true end -function utils.escape(s) +function utils.escape(s, ignore_percent) local replacements = { ["\\"] = "\\\\", ['"'] = '\\"', @@ -28,9 +28,11 @@ function utils.escape(s) ["\t"] = "\\t", ["\b"] = "\\b", ["\f"] = "\\f", - ["\r"] = "\\r", - ["%"] = "%%" + ["\r"] = "\\r" } + if not ignore_percent then + replacements["%"] = "%%" + end return (s:gsub("[\\'\"\n\t\b\f\r%%]", replacements)) end @@ -194,7 +196,8 @@ function JSON.encode(obj) end elseif t == 'string' then -- print('"'..utils.escape(obj)..'"') - return '"' .. utils.escape(obj) .. '"' + -- ignore % escape as this is for a LUA using + return '"' .. utils.escape(obj, true) .. '"' elseif t == 'boolean' or t == 'number' then return tostring(obj) elseif obj == nil then