Skip to content

Commit

Permalink
Added Log.Dump for dumping json data into debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
justas-d committed Feb 6, 2017
1 parent 1b80b04 commit 049be8f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 22 deletions.
3 changes: 2 additions & 1 deletion base/Api/ChatConsole.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ function ChatConsole:RegisterCommand(command)
assert_e(Api.IsTable(command))
assert_e(Api.IsString(command:GetTrigger()))

Log.Debug("Registering command:", Api.json.encode(command))
Log.Debug("Registering command:")
Log.Dump(Api.json.encode(command))
if self._commands[command:GetTrigger()] ~= nil then
Log.Debug("Failed registering command.")
return false
Expand Down
27 changes: 18 additions & 9 deletions base/Api/FunctionHookHandle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ FunctionHookHandle.Create = function(targetSignature, hookFunction, isPre, allow
-- do some tests
local entry = FunctionHookHandle.Hooks[targetSignature]
assert_e(entry)
Log.Debug(targetSignature, "Pre hooks:", Api.json.encode(entry.PreHooks))
Log.Debug(targetSignature, "Post hooks:", Api.json.encode(entry.PostHooks))
Log.Debug(targetSignature, "Pre hooks:")
Log.Dump(Api.json.encode(entry.PreHooks))
Log.Debug(targetSignature, "Post hooks:")
Log.Dump(Api.json.encode(entry.PostHooks))

return handle
end
Expand All @@ -93,7 +95,8 @@ function FunctionHookHandle:GetTargetFunction(shouldUseCache)

if self._targetFunction == nil then
Log.Warn("Tried to force cache GetTargetFunction but cache was null.\r\n"..debug.traceback())
Log.Debug("Function hook handle dump:", Api.json.encode(self))
Log.Debug("Function hook handle dump:")
Log.Dump(Api.json.encode(self))
end

return self._targetFunction
Expand All @@ -103,7 +106,8 @@ function FunctionHookHandle:GetTargetFunction(shouldUseCache)

if not Api.IsFunction(func) then
Log.Warn("GetTargetFunction eval returns returns a type other then function:", type(func))
Log.Debug("Hook dump:", Api.json.encode(self))
Log.Debug("Hook dump:")
Log.Dump(Api.json.encode(self))
return nil
end

Expand Down Expand Up @@ -210,7 +214,8 @@ function FunctionHookHandle:Enable(allowDuplicates)
-- make sure signature is all good
if self._targetFunction == nil then
Log.Warn("Failed evaluating target function for FunctionHookHandle.")
Log.Debug("Hook dump:", Api.json.encode(self))
Log.Debug("Hook dump:")
Log.Dump(Api.json.encode(self))
return
end

Expand All @@ -237,8 +242,10 @@ local f = function(...)
local status, ret = Api.Std.pcall(v:GetHookFunction(), ...)
if not status then
Log.Warn("Function hook Api.Std.pcall failed, error:", tostring(ret))
Log.Debug("Hook data:", Api.json.encode(v))
Log.Debug("In hook table:", Api.json.encode(tabl))
Log.Debug("Hook data:")
Log.Dump(Api.json.encode(v))
Log.Debug("In hook table:")
Log.Dump(Api.json.encode(tabl))
end
end
end
Expand Down Expand Up @@ -315,8 +322,10 @@ function FunctionHookHandle:_append_hooks_entry(allowDuplicates)
if hook:GetHookFunction() == self:GetHookFunction() then
Log.Warn("Duplicate hooks found in table at", tostring(tabl))
Log.Debug("Tables: PreHook:", tostring(entry.PreHooks), "PostHook:", tostring(entry.PostHook))
Log.Debug("Our hook dump:", Api.json.encode(self))
Log.Debug("Other hook dump:", Api.json.encode(hook))
Log.Debug("Our hook dump:")
Log.Dump(Api.json.encode(self))
Log.Debug("Other hook dump:")
Log.Dump(Api.json.encode(hook))
return 1
end
end
Expand Down
10 changes: 6 additions & 4 deletions base/Api/ModManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function ModManager:initialize(modDir, configDir, ...)

if self._moad_load_result then
Log.Warn("Error in ModManager LoadModsInDir. Dumping error table:")
Log.Warn(Api.json.encode(self._moad_load_result))
Log.Dump(Api.json.encode(self._moad_load_result))
end

Log.Debug("Loaded", tostring(self:GetModCount()), "mods.")
Expand Down Expand Up @@ -139,8 +139,10 @@ function ModManager:Enable(modHandle)

if index == nil then
Log.Warn("Tried to enable mod while it is not in the disabled list.")
Log.Debug("Mod handle dump:", Api.json.encode(modHandle))
Log.Debug("Disabled mod dump:", Api.json.encode(disabledMods))
Log.Debug("Mod handle dump:")
Log.Dump(Api.json.encode(modHandle))
Log.Debug("Disabled mod dump:")
Log.Dump(Api.json.encode(disabledMods))
return
end

Expand Down Expand Up @@ -191,7 +193,7 @@ function ModManager:SaveConfig()
end

local data = Api.json.encode(self:GetConfig())
Log.Debug("Config data dump:", data)
Log.Dump("Config data dump:", data)

fHandle:write(data)
fHandle:close()
Expand Down
2 changes: 1 addition & 1 deletion base/Internal/LuaModLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function LuaModLoader:LoadMod(directory, modFolder, disabledMods)
end

Log.Write("Loaded mod:", modFolder)
Log.Debug("Dump:", Api.json.encode(mod))
Log.Dump(Api.json.encode(mod))
return mod
end

Expand Down
8 changes: 5 additions & 3 deletions base/Internal/hooks/AbstractHook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function AbstractHook:HandleHook(hookTable, key)
if hookTable[key] == nil or #hookTable[key] == 0 then return end

for _, hookData in ipairs(hookTable[key]) do
Log.Debug("Handling hook:", Api.json.encode(hookData))
Log.Debug("Handling hook:")
Log.Dump(Api.json.encode(hookData))

if hookData:GetFileExecuteDir() ~= nil then
Log.Debug("Handling file")
Expand Down Expand Up @@ -76,7 +77,8 @@ function AbstractHook:AddHook(key, file, chunk, modHandle, isPreHook)
-- insert hook data into the indexed table hookTable[requireString]
table.insert(hookTable[key], hookData)

Log.Debug("Added hook data:", Api.json.encode(hookData))
Log.Debug("Added hook data:")
Log.Dump(Api.json.encode(hookData))

return hookData
end
Expand Down Expand Up @@ -124,7 +126,7 @@ function AbstractHook:ReadConfig(modHandle, config, modFolder)
return 0, nil
end

Log.Debug(Api.json.encode(obj))
Log.Dump(Api.json.encode(obj))

local code, err = self:_json_append_hooks(true, obj[self.JSON_PRE], modHandle)
if code ~= 0 then return code, err end
Expand Down
2 changes: 2 additions & 0 deletions base/bootstrap.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Log.Write("Hello world")

if false then
Log.Create()
end
Expand Down
11 changes: 8 additions & 3 deletions src/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace VermHook

void Logger::Write(string msg, const char* line, const char* filename)
{
_write(" ... ", msg, line, filename);
_write(" ... ", msg, line, filename);
}

void Logger::RawWrite(std::string msg)
Expand All @@ -25,6 +25,12 @@ namespace VermHook
_out << msg;
}

void Logger::NewLine()
{
std::cout << std::endl;
_out << std::endl;
}

void Logger::_write(std::string prefix, std::string msg, const char* line, const char* filename)
{
RawWrite(prefix);
Expand All @@ -40,7 +46,6 @@ namespace VermHook
}

RawWrite(msg);
std::cout << std::endl;
_out << std::endl;
NewLine();
}
}
3 changes: 2 additions & 1 deletion src/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace VermHook

int LuaApi::Log::Dump(LuaState* state)
{
Logger::Write(" "s + ConcatVaargs(state));
Logger::RawWrite(" "s + ConcatVaargs(state));
Logger::NewLine();
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/include/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ namespace VermHook
static void Debug(std::string msg, const char* line = nullptr, const char* filename = nullptr);
static void Write(std::string msg, const char* line = nullptr, const char* filename = nullptr);
static void RawWrite(std::string msg);
static void NewLine();
};
}

0 comments on commit 049be8f

Please sign in to comment.