From 315e45165d7cfa81745a455a7586a0ced6b3e286 Mon Sep 17 00:00:00 2001 From: bi Date: Mon, 11 Jun 2018 18:58:40 +0300 Subject: [PATCH] Logging: moved to a separate file --- .../mods/vmf/modules/core/core_functions.lua | 128 +---------------- .../modules/core/delayed_chat_messages.lua | 20 --- vmf/scripts/mods/vmf/modules/core/logging.lua | 134 ++++++++++++++++++ vmf/scripts/mods/vmf/vmf_loader.lua | 5 +- 4 files changed, 138 insertions(+), 149 deletions(-) delete mode 100644 vmf/scripts/mods/vmf/modules/core/delayed_chat_messages.lua create mode 100644 vmf/scripts/mods/vmf/modules/core/logging.lua diff --git a/vmf/scripts/mods/vmf/modules/core/core_functions.lua b/vmf/scripts/mods/vmf/modules/core/core_functions.lua index 01906fe..e33fd3f 100644 --- a/vmf/scripts/mods/vmf/modules/core/core_functions.lua +++ b/vmf/scripts/mods/vmf/modules/core/core_functions.lua @@ -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 _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 ######################################################################### -- #################################################################################################################### --- @TODO: rename it (get rid of "wrong") + vmf.check_wrong_argument_type = function(mod, vmf_function_name, argument_name, argument, ...) local allowed_types = {...} @@ -116,31 +19,4 @@ vmf.check_wrong_argument_type = function(mod, vmf_function_name, argument_name, vmf_function_name, argument_name, table.concat(allowed_types, "/"), argument_type) return true -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() \ No newline at end of file +end \ No newline at end of file diff --git a/vmf/scripts/mods/vmf/modules/core/delayed_chat_messages.lua b/vmf/scripts/mods/vmf/modules/core/delayed_chat_messages.lua deleted file mode 100644 index 0e4f8ff..0000000 --- a/vmf/scripts/mods/vmf/modules/core/delayed_chat_messages.lua +++ /dev/null @@ -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) \ No newline at end of file diff --git a/vmf/scripts/mods/vmf/modules/core/logging.lua b/vmf/scripts/mods/vmf/modules/core/logging.lua new file mode 100644 index 0000000..5eb8809 --- /dev/null +++ b/vmf/scripts/mods/vmf/modules/core/logging.lua @@ -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() \ No newline at end of file diff --git a/vmf/scripts/mods/vmf/vmf_loader.lua b/vmf/scripts/mods/vmf/vmf_loader.lua index ebf50b3..076c7d7 100644 --- a/vmf/scripts/mods/vmf/vmf_loader.lua +++ b/vmf/scripts/mods/vmf/vmf_loader.lua @@ -14,6 +14,7 @@ function vmf_mod_object:init() dofile("scripts/mods/vmf/modules/vmf_mod_manager") dofile("scripts/mods/vmf/modules/core/events") 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/persistent_tables") 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/toggling") 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/localization") dofile("scripts/mods/vmf/modules/core/network") @@ -39,9 +39,8 @@ function vmf_mod_object:init() end vmf = get_mod("VMF") - + vmf.delayed_chat_messages_hook() vmf:hook("ModManager.destroy", function(func, ...) - vmf.mods_unload_event(true) func(...) end)