From 942a030afd348ab2e02eac8054b43e3c3a72ea48 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 15 Nov 2021 13:53:59 +0800 Subject: *rename --- .../Libraries/lua-addons/addons/libs/logger.lua | 281 +++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 Data/BuiltIn/Libraries/lua-addons/addons/libs/logger.lua (limited to 'Data/BuiltIn/Libraries/lua-addons/addons/libs/logger.lua') diff --git a/Data/BuiltIn/Libraries/lua-addons/addons/libs/logger.lua b/Data/BuiltIn/Libraries/lua-addons/addons/libs/logger.lua new file mode 100644 index 0000000..b1fb360 --- /dev/null +++ b/Data/BuiltIn/Libraries/lua-addons/addons/libs/logger.lua @@ -0,0 +1,281 @@ +--[[ +This library provides a set of functions to aid in debugging. +]] + +_libs = _libs or {} + +require('strings') +require('chat') + +local string, chat = _libs.strings, _libs.chat +local table = require('table') + +local logger = {} + +_libs.logger = logger + +_raw = _raw or {} + +-- Set up, based on addon. +logger.defaults = {} +logger.defaults.logtofile = false +logger.defaults.defaultfile = 'lua.log' +logger.defaults.logcolor = 207 +logger.defaults.errorcolor = 167 +logger.defaults.warningcolor = 200 +logger.defaults.noticecolor = 160 + +--[[ + Local functions +]] + +local arrstring +local captionlog + +-- Returns a concatenated string list, separated by whitespaces, for the chat output function. +-- Converts any kind of object type to a string, so it's type-safe. +-- Concatenates all provided arguments with whitespaces. +function arrstring(...) + local str = '' + local args = {...} + + for i = 1, select('#', ...) do + if i > 1 then + str = str..' ' + end + str = str .. tostring(args[i]) + end + + return str +end + +-- Prints the arguments provided to the FFXI chatlog, in the same color used for Campaign/Bastion alerts and Kupower messages. Can be changed below. +function captionlog(msg, msgcolor, ...) + local caption = table.concat({_addon and _addon.name, msg}, ' ') + + if #caption > 0 then + if logger.settings.logtofile then + flog(nil, caption .. ':', ...) + return + end + caption = (caption .. ':'):color(msgcolor) .. ' ' + end + + local str = '' + if select('#', ...) == 0 or ... == '' then + str = ' ' + else + str = arrstring(...):gsub('\t', (' '):rep(4)) + end + + for _, line in ipairs(str:split('\n')) do + windower.add_to_chat(logger.settings.logcolor, caption .. windower.to_shift_jis(line) .. _libs.chat.controls.reset) + end +end + +function log(...) + captionlog(nil, logger.settings.logcolor, ...) +end + +_raw.error = error +function error(...) + captionlog('Error', logger.settings.errorcolor, ...) +end + +function warning(...) + captionlog('Warning', logger.settings.warningcolor, ...) +end + +function notice(...) + captionlog('Notice', logger.settings.noticecolor, ...) +end + +-- Prints the arguments provided to a file, analogous to log(...) in functionality. +-- If the first argument ends with '.log', it will print to that output file, otherwise to 'lua.log' in the addon directory. +function flog(filename, ...) + filename = filename or logger.settings.defaultfile + + local fh, err = io.open(windower.addon_path..filename, 'a') + if fh == nil then + if err ~= nil then + error('File error:', err) + else + error('File error:', 'Unknown error.') + end + else + fh:write(os.date('%Y-%m-%d %H:%M:%S') .. '| ' .. arrstring(...) .. '\n') + fh:close() + end +end + +-- Returns a string representation of a table in explicit Lua syntax: {...} +function table.tostring(t) + if next(t) == nil then + return '{}' + end + + keys = keys or false + + -- Iterate over table. + local tstr = '' + local kt = {} + k = 0 + for key in pairs(t) do + k = k + 1 + kt[k] = key + end + table.sort(kt, function(x, y) + if type(x) == 'number' and type(y) == 'string' then + return true + elseif type(x) == 'string' and type(y) == 'number' then + return false + end + + return x