Sindbad~EG File Manager
--[[
This file contains a module with some function that we can use to debug stuff. Mainly to log stuff in the Openresty
log to debug / see what happen in the code.
--]]
local _M = {}
local config = require("lib/o2switch_config")
local ngx_log = ngx.log
local type = type
local pairs = pairs
local tostring = tostring
local debug = debug
--- Debug function that log stuff to Nginx log
-- @param s The string to add to the log file
-- @return Nothing.
function _M.debug(s)
if config.debugMode == 1 then
ngx_log(ngx.DEBUG, "LUA DEBUG: " .. s)
end
end
function _M.error(s)
ngx_log(ngx.ERR, "ERROR = " .. s)
end
function _M.critical(s)
ngx_log(ngx.CRIT, "CRITICAL = " .. s)
end
-- Debug function that log error to Nginx log
-- Also add the stackstrace in the log to help debug stuff
function _M.debugErr(s)
ngx_log(ngx.ERR, "\nERROR = " .. s .. "\n" .. debug.traceback() .. "\n\n")
end
function _M.debugCritical(s)
ngx_log(ngx.CRIT, "\nCRITI = " .. s .. "\n" .. debug.traceback() .. "\n\n")
end
-- Dump & Die. Lua equivalent of the famous PHP dd()
function _M.dd(s)
ngx.header.content_type = "text/html; charset=UTF-8"
ngx.header.cache_control = "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
ngx.header.expires = "Thu, 01 Jan 1970 00:00:01 GMT"
ngx.header.referer_policy = "same-origin"
if type(s) == 'string' then
ngx.say(s)
elseif type(s) == 'table' then
ngx.say(_M.varDump(s))
else
ngx.say(type(s))
end
ngx.say(debug.traceback())
ngx.exit(404)
end
local function string(o)
return '"' .. tostring(o) .. '"'
end
local function recurse(o, indent)
if indent == nil then indent = '' end
local indent2 = indent .. ' '
if type(o) == 'table' then
local s = indent .. '{' .. '\n'
local first = true
for k,v in pairs(o) do
if first == false then s = s .. ', \n' end
if type(k) ~= 'number' then k = string(k) end
s = s .. indent2 .. '[' .. k .. '] = ' .. recurse(v, indent2)
first = false
end
return s .. '\n' .. indent .. '}'
else
return string(o)
end
end
-- Lua equivalent of the var_dump PHP function
-- Credit to : https://gist.github.com/lunixbochs/5b0bb27861a396ab7a86
-- @return string
function _M.varDump(...)
local args = {...}
if #args > 1 then
_M.varDump(args)
else
return(recurse(args[1]))
end
end
return _M
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists