Last active
December 5, 2015 20:51
-
-
Save thelastpenguin/606859fd43df15a629d3 to your computer and use it in GitHub Desktop.
A simple tool for logging and eventually debugging errors in hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'hook' -- make sure it's loaded | |
local function stringifyArgs(...) | |
local limit = select('#', ...) | |
local function stringifyHelper(n, a, ...) | |
if n > limit then return end | |
local t = type(a) | |
if t == 'player' then | |
return '('..n..') ' .. a:Name() .. "(" .. a:SteamID() .. ")", stringifyHelper(n + 1, ...) | |
elseif t == 'number' or t == 'string' then | |
return '('..n..') ' .. tostring(a), stringifyHelper(n + 1, ...) | |
else | |
return '('..n..') ' .. t, stringifyHelper(n + 1, ...) | |
end | |
end | |
return table.concat({stringifyHelper(1, ...)}, ', ') | |
end | |
local hook_Call = hook.Call | |
hook.Call = function(hook, ...) | |
local succ, a, b, c, d, e, f = pcall(hook_Call, hook, ...) | |
if succ then | |
-- it was successful. return. | |
return a, b, c, d, e, f | |
else | |
-- pass along the error | |
local TimeStr = os.date( "%X - %d/%m/%Y" , os.time() ) | |
local message = TimeStr .. ' - hook: ' .. tostring(hook) .. ' error: ' .. a .. '\n\tARGUMENTS: ' .. stringifyArgs(...) .. '\n\n' | |
file.Append('hookErrors.txt', message) | |
error(a) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment