rime-ice/lua/cold_word_drop/debugtool.lua

93 lines
2.1 KiB
Lua
Raw Normal View History

#! /usr/bin/env lua
--
-- debugtool.lua
-- Copyright (C) 2021 Shewer Lu <shewer@gmail.com>
--
-- Distributed under terms of the MIT license.
--
-- puts(tag,...)
-- DEBUG --> log.error
-- WARN --> log.warning
-- INFO --> log.info
-- CONSOLE --> print
--
-- ex:
-- test.lua
--
-- local puts = require 'tools/debugtool'
-- --set tag D103 C102
-- local D103= DEBUG .. "103"
-- local C102= CONSOLE .. "102"
-- local C103= nil
--
--
-- puts(ERROR,__FILE__(),__LINE__(),__FUNC__(), 1, 2 , 3 )
-- --> log.error( "error" .. tran_msg(...))
--
-- puts(DEBUG,__FILE__(),__LINE__(),__FUNC__(), 1, 2 , 3 )
-- --> log.error( DEBUG .. tran_msg(...))
--
-- puts(D103,__FILE__(),__LINE__(),__FUNC__(), 1 2 3)
-- --> log.error("trace103" .. tran_msg(...)
--
-- puts(C102,__FILE__(),__LINE__(),__FUNC__(), 1 2 3)
-- --> print("console103" .. tran_msg(...)
--
-- puts(C103,__FILE__(),__LINE__(),__FUNC__(), 1 2 3)
-- --> pass
--
--
--
-- puts(DEBUG,__FILE__(),__LINE__(),__FUNC__() , ...)
-- puts(INFO,__FILE__(),__LINE__(),__FUNC__() , ...)
--
-- global variable
2024-02-08 11:39:49 +01:00
function __FILE__(n)
n = n or 2
return debug.getinfo(n, 'S').source
end
function __LINE__(n)
n = n or 2
return debug.getinfo(n, 'l').currentline
end
function __FUNC__(n)
n = n or 2
return debug.getinfo(n, 'n').name
end
INFO = "log"
WARN = "warn"
ERROR = "error"
DEBUG = "trace"
CONSOLE = "console"
local function tran_msg(...)
2024-02-08 11:39:49 +01:00
local msg = "\t"
for i, k in next, { ... } do msg = msg .. ": " .. tostring(k) end
return msg
end
2024-02-08 11:39:49 +01:00
local function puts(tag, ...)
if type(tag) ~= "string" then return end
2024-02-08 11:39:49 +01:00
if INFO and tag:match("^" .. INFO) then
(log and log.info or print)(tag .. tran_msg(...))
elseif WARN and tag:match("^" .. WARN) then
(log and log.warning or print)(tag .. tran_msg(...))
elseif ERROR and tag:match("^" .. ERROR) then
(log and log.error or print)(tag .. tran_msg(...))
elseif DEBUG and tag:match("^" .. DEBUG) then
(log and log.error or print)(tag .. tran_msg(...))
elseif CONSOLE and tag:match("^" .. CONSOLE) then
(print)(tag .. tran_msg(...))
else
return
end
end
return puts