Logging: moved to a separate file
This commit is contained in:
parent
e1f515e4ef
commit
315e45165d
4 changed files with 138 additions and 149 deletions
|
@ -1,106 +1,9 @@
|
||||||
--@TODO: return right after output function call if it's not used in chat and in log
|
|
||||||
--@TODO: move logging options to developer mode in options menu
|
|
||||||
local vmf = get_mod("VMF")
|
local vmf = get_mod("VMF")
|
||||||
|
|
||||||
local _UNSENT_CHAT_MESSAGES = {}
|
|
||||||
local _LOGGING_SETTINGS
|
|
||||||
|
|
||||||
-- ####################################################################################################################
|
|
||||||
-- ##### Local functions ##############################################################################################
|
|
||||||
-- ####################################################################################################################
|
|
||||||
|
|
||||||
local function safe_format(mod, str, ...)
|
|
||||||
|
|
||||||
-- the game still crash with unknown error if there is non-standard character after '%' @TODO: any solutions?
|
|
||||||
local success, message = pcall(string.format, str, ...)
|
|
||||||
|
|
||||||
if success then
|
|
||||||
return message
|
|
||||||
else
|
|
||||||
mod:error("string.format: " .. tostring(message)) --@TODO: good description
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function send_to_chat(self, msg_type, message)
|
|
||||||
|
|
||||||
if msg_type ~= "echo" then
|
|
||||||
message = string.format("[%s][%s] %s", self:get_name(), string.upper(msg_type), message)
|
|
||||||
end
|
|
||||||
|
|
||||||
if Managers.chat and Managers.chat:has_channel(1) then
|
|
||||||
Managers.chat:add_local_system_message(1, message, true)
|
|
||||||
else
|
|
||||||
table.insert(_UNSENT_CHAT_MESSAGES, message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function send_to_log(self, msg_type, message)
|
|
||||||
|
|
||||||
printf("[MOD][%s][%s] %s", self:get_name(), string.upper(msg_type), message)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function log_message(self, msg_type, message, ...)
|
|
||||||
|
|
||||||
message = safe_format(self, tostring(message), ...)
|
|
||||||
|
|
||||||
if message then
|
|
||||||
|
|
||||||
if _LOGGING_SETTINGS[msg_type].send_to_chat then
|
|
||||||
send_to_chat(self, msg_type, message)
|
|
||||||
end
|
|
||||||
|
|
||||||
if _LOGGING_SETTINGS[msg_type].send_to_log then
|
|
||||||
send_to_log(self, msg_type, message)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- ####################################################################################################################
|
|
||||||
-- ##### VMFMod #######################################################################################################
|
|
||||||
-- ####################################################################################################################
|
|
||||||
--_LOGGING_SETTINGS.echo
|
|
||||||
function VMFMod:echo(message, ...)
|
|
||||||
if _LOGGING_SETTINGS.echo.enabled then
|
|
||||||
log_message(self, "echo", message, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function VMFMod:error(message, ...)
|
|
||||||
if _LOGGING_SETTINGS.error.enabled then
|
|
||||||
log_message(self, "error", message, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function VMFMod:warning(message, ...)
|
|
||||||
if _LOGGING_SETTINGS.warning.enabled then
|
|
||||||
log_message(self, "warning", message, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function VMFMod:info(message, ...)
|
|
||||||
if _LOGGING_SETTINGS.info.enabled then
|
|
||||||
log_message(self, "info", message, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function VMFMod:debug(message, ...)
|
|
||||||
if _LOGGING_SETTINGS.debug.enabled then
|
|
||||||
log_message(self, "debug", message, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- ####################################################################################################################
|
-- ####################################################################################################################
|
||||||
-- ##### VMF internal functions and variables #########################################################################
|
-- ##### VMF internal functions and variables #########################################################################
|
||||||
-- ####################################################################################################################
|
-- ####################################################################################################################
|
||||||
-- @TODO: rename it (get rid of "wrong")
|
|
||||||
vmf.check_wrong_argument_type = function(mod, vmf_function_name, argument_name, argument, ...)
|
vmf.check_wrong_argument_type = function(mod, vmf_function_name, argument_name, argument, ...)
|
||||||
|
|
||||||
local allowed_types = {...}
|
local allowed_types = {...}
|
||||||
|
@ -117,30 +20,3 @@ vmf.check_wrong_argument_type = function(mod, vmf_function_name, argument_name,
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
vmf.unsent_chat_messages = _UNSENT_CHAT_MESSAGES
|
|
||||||
|
|
||||||
function vmf.load_logging_settings()
|
|
||||||
|
|
||||||
_LOGGING_SETTINGS = {
|
|
||||||
echo = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_echo") or 3, -- @TODO: clean up?
|
|
||||||
error = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_error") or 3,
|
|
||||||
warning = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_warning") or 3,
|
|
||||||
info = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_info") or 1,
|
|
||||||
debug = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_debug") or 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
for method_name, logging_mode in pairs(_LOGGING_SETTINGS) do
|
|
||||||
_LOGGING_SETTINGS[method_name] = {
|
|
||||||
send_to_chat = logging_mode and logging_mode >= 2,
|
|
||||||
send_to_log = logging_mode and logging_mode % 2 == 1,
|
|
||||||
enabled = logging_mode and logging_mode > 0
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- ####################################################################################################################
|
|
||||||
-- ##### Script #######################################################################################################
|
|
||||||
-- ####################################################################################################################
|
|
||||||
|
|
||||||
vmf.load_logging_settings()
|
|
|
@ -1,20 +0,0 @@
|
||||||
local vmf = get_mod("VMF")
|
|
||||||
|
|
||||||
-- ####################################################################################################################
|
|
||||||
-- ##### Hooks ########################################################################################################
|
|
||||||
-- ####################################################################################################################
|
|
||||||
|
|
||||||
vmf:hook("ChatManager.register_channel", function (func, self, channel_id, members_func)
|
|
||||||
|
|
||||||
func(self, channel_id, members_func)
|
|
||||||
|
|
||||||
if (channel_id == 1) and (#vmf.unsent_chat_messages > 0) then
|
|
||||||
for _, message in ipairs(vmf.unsent_chat_messages) do
|
|
||||||
self:add_local_system_message(1, message, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
for i, _ in ipairs(vmf.unsent_chat_messages) do
|
|
||||||
vmf.unsent_chat_messages[i] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
134
vmf/scripts/mods/vmf/modules/core/logging.lua
Normal file
134
vmf/scripts/mods/vmf/modules/core/logging.lua
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
local vmf = get_mod("VMF")
|
||||||
|
|
||||||
|
local _unsent_chat_messages = {}
|
||||||
|
local _logging_settings
|
||||||
|
|
||||||
|
-- #####################################################################################################################
|
||||||
|
-- ##### Local functions ###############################################################################################
|
||||||
|
-- #####################################################################################################################
|
||||||
|
|
||||||
|
local function safe_format(mod, str, ...)
|
||||||
|
-- the game still crash with unknown error if there is non-standard character after '%'
|
||||||
|
local success, message = pcall(string.format, str, ...)
|
||||||
|
if success then
|
||||||
|
return message
|
||||||
|
else
|
||||||
|
mod:error("(logging) string.format: " .. tostring(message))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function send_to_chat(self, msg_type, message)
|
||||||
|
|
||||||
|
if msg_type ~= "echo" then
|
||||||
|
message = string.format("[%s][%s] %s", self:get_name(), string.upper(msg_type), message)
|
||||||
|
end
|
||||||
|
|
||||||
|
if Managers.chat and Managers.chat:has_channel(1) then
|
||||||
|
Managers.chat:add_local_system_message(1, message, true)
|
||||||
|
else
|
||||||
|
table.insert(_unsent_chat_messages, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function send_to_log(self, msg_type, message)
|
||||||
|
printf("[MOD][%s][%s] %s", self:get_name(), string.upper(msg_type), message)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function log_message(self, msg_type, message, ...)
|
||||||
|
|
||||||
|
message = safe_format(self, tostring(message), ...)
|
||||||
|
if message then
|
||||||
|
if _logging_settings[msg_type].send_to_chat then
|
||||||
|
send_to_chat(self, msg_type, message)
|
||||||
|
end
|
||||||
|
if _logging_settings[msg_type].send_to_log then
|
||||||
|
send_to_log(self, msg_type, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- #####################################################################################################################
|
||||||
|
-- ##### VMFMod ########################################################################################################
|
||||||
|
-- #####################################################################################################################
|
||||||
|
|
||||||
|
function VMFMod:echo(message, ...)
|
||||||
|
if _logging_settings.echo.enabled then
|
||||||
|
log_message(self, "echo", message, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function VMFMod:error(message, ...)
|
||||||
|
if _logging_settings.error.enabled then
|
||||||
|
log_message(self, "error", message, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function VMFMod:warning(message, ...)
|
||||||
|
if _logging_settings.warning.enabled then
|
||||||
|
log_message(self, "warning", message, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function VMFMod:info(message, ...)
|
||||||
|
if _logging_settings.info.enabled then
|
||||||
|
log_message(self, "info", message, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function VMFMod:debug(message, ...)
|
||||||
|
if _logging_settings.debug.enabled then
|
||||||
|
log_message(self, "debug", message, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- #####################################################################################################################
|
||||||
|
-- ##### VMF internal functions and variables ##########################################################################
|
||||||
|
-- #####################################################################################################################
|
||||||
|
|
||||||
|
-- Can't be hooked right away, since hooking module is not initialized yet
|
||||||
|
-- Sends unsent messages to chat when chat channel is finally created
|
||||||
|
function vmf.delayed_chat_messages_hook()
|
||||||
|
vmf:hook_safe("ChatManager", "register_channel", function (self, channel_id)
|
||||||
|
if (channel_id == 1) and (#_unsent_chat_messages > 0) then
|
||||||
|
for _, message in ipairs(_unsent_chat_messages) do
|
||||||
|
self:add_local_system_message(1, message, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i, _ in ipairs(_unsent_chat_messages) do
|
||||||
|
_unsent_chat_messages[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function vmf.load_logging_settings()
|
||||||
|
|
||||||
|
_logging_settings = {
|
||||||
|
echo = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_echo") or 3,
|
||||||
|
error = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_error") or 3,
|
||||||
|
warning = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_warning") or 3,
|
||||||
|
info = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_info") or 1,
|
||||||
|
debug = vmf:get("logging_mode") == "custom" and vmf:get("output_mode_debug") or 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
for method_name, logging_mode in pairs(_logging_settings) do
|
||||||
|
_logging_settings[method_name] = {
|
||||||
|
send_to_chat = logging_mode and logging_mode >= 2,
|
||||||
|
send_to_log = logging_mode and logging_mode % 2 == 1,
|
||||||
|
enabled = logging_mode and logging_mode > 0
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- #####################################################################################################################
|
||||||
|
-- ##### Script ########################################################################################################
|
||||||
|
-- #####################################################################################################################
|
||||||
|
|
||||||
|
vmf.load_logging_settings()
|
|
@ -14,6 +14,7 @@ function vmf_mod_object:init()
|
||||||
dofile("scripts/mods/vmf/modules/vmf_mod_manager")
|
dofile("scripts/mods/vmf/modules/vmf_mod_manager")
|
||||||
dofile("scripts/mods/vmf/modules/core/events")
|
dofile("scripts/mods/vmf/modules/core/events")
|
||||||
dofile("scripts/mods/vmf/modules/core/settings")
|
dofile("scripts/mods/vmf/modules/core/settings")
|
||||||
|
dofile("scripts/mods/vmf/modules/core/logging")
|
||||||
dofile("scripts/mods/vmf/modules/core/core_functions")
|
dofile("scripts/mods/vmf/modules/core/core_functions")
|
||||||
dofile("scripts/mods/vmf/modules/core/persistent_tables")
|
dofile("scripts/mods/vmf/modules/core/persistent_tables")
|
||||||
dofile("scripts/mods/vmf/modules/debug/dev_console")
|
dofile("scripts/mods/vmf/modules/debug/dev_console")
|
||||||
|
@ -21,7 +22,6 @@ function vmf_mod_object:init()
|
||||||
dofile("scripts/mods/vmf/modules/core/hooks")
|
dofile("scripts/mods/vmf/modules/core/hooks")
|
||||||
dofile("scripts/mods/vmf/modules/core/toggling")
|
dofile("scripts/mods/vmf/modules/core/toggling")
|
||||||
dofile("scripts/mods/vmf/modules/core/keybindings")
|
dofile("scripts/mods/vmf/modules/core/keybindings")
|
||||||
dofile("scripts/mods/vmf/modules/core/delayed_chat_messages")
|
|
||||||
dofile("scripts/mods/vmf/modules/core/chat")
|
dofile("scripts/mods/vmf/modules/core/chat")
|
||||||
dofile("scripts/mods/vmf/modules/core/localization")
|
dofile("scripts/mods/vmf/modules/core/localization")
|
||||||
dofile("scripts/mods/vmf/modules/core/network")
|
dofile("scripts/mods/vmf/modules/core/network")
|
||||||
|
@ -39,9 +39,8 @@ function vmf_mod_object:init()
|
||||||
end
|
end
|
||||||
|
|
||||||
vmf = get_mod("VMF")
|
vmf = get_mod("VMF")
|
||||||
|
vmf.delayed_chat_messages_hook()
|
||||||
vmf:hook("ModManager.destroy", function(func, ...)
|
vmf:hook("ModManager.destroy", function(func, ...)
|
||||||
|
|
||||||
vmf.mods_unload_event(true)
|
vmf.mods_unload_event(true)
|
||||||
func(...)
|
func(...)
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Add table
Reference in a new issue