multiple improvements to logging

- added style.log table
- removed contextmenu
- use ctrl+click to copy individual log entries
- use icon instead of + or - for log items in logview
This commit is contained in:
takase1121 2021-12-18 10:51:44 +08:00
parent 69de42b078
commit ab4ecd515b
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
7 changed files with 56 additions and 37 deletions

View File

@ -64,7 +64,7 @@ end
function command.add_defaults() function command.add_defaults()
local reg = { local reg = {
"core", "root", "command", "doc", "findreplace", "core", "root", "command", "doc", "findreplace",
"files", "drawwhitespace", "dialog" "files", "drawwhitespace", "dialog", "log"
} }
for _, name in ipairs(reg) do for _, name in ipairs(reg) do
require("core.commands." .. name) require("core.commands." .. name)

View File

@ -0,0 +1,26 @@
local core = require "core"
local command = require "core.command"
command.add("core.logview", {
["log:expand-item"] = function()
if core.active_view.hovered_item then
core.active_view:expand_item(core.active_view.hovered_item)
end
end,
["log:copy-entry"] = function()
if core.active_view.hovered_item then
system.set_clipboard(core.get_log(core.active_view.hovered_item))
end
end
})
command.add(nil, {
["log:open-as-doc"] = function()
local doc = core.open_doc("logs.txt")
core.root_view:open_doc(doc)
doc:insert(1, 1, core.get_log())
end,
["log:copy-to-clipboard"] = function()
system.set_clipboard(core.get_log())
end
})

View File

@ -1034,15 +1034,21 @@ function core.get_views_referencing_doc(doc)
end end
local function log(icon, icon_color, fmt, ...) local function log(level, show, fmt, ...)
local text = string.format(fmt, ...) local text = string.format(fmt, ...)
if icon then if show then
core.status_view:show_message(icon, icon_color, text) local s = style.log[level]
core.status_view:show_message(s.icon, s.color, text)
end end
local info = debug.getinfo(2, "Sl") local info = debug.getinfo(2, "Sl")
local at = string.format("%s:%d", info.short_src, info.currentline) local at = string.format("%s:%d", info.short_src, info.currentline)
local item = { text = text, time = os.time(), at = at } local item = {
level = level,
text = text,
time = os.time(),
at = at
}
table.insert(core.log_items, item) table.insert(core.log_items, item)
if #core.log_items > config.max_log_items then if #core.log_items > config.max_log_items then
table.remove(core.log_items, 1) table.remove(core.log_items, 1)
@ -1052,17 +1058,17 @@ end
function core.log(...) function core.log(...)
return log("i", style.text, ...) return log("INFO", true, ...)
end end
function core.log_quiet(...) function core.log_quiet(...)
return log(nil, nil, ...) return log("INFO", false, ...)
end end
function core.error(...) function core.error(...)
return log("!", style.accent, ...) return log("ERROR", true, ...)
end end
@ -1075,7 +1081,7 @@ function core.get_log(i)
return table.concat(r, "\n") return table.concat(r, "\n")
end end
local item = type(i) == "number" and core.log_items[i] or i local item = type(i) == "number" and core.log_items[i] or i
local text = string.format("[%s] %s at %s", os.date(nil, item.time), item.text, item.at) local text = string.format("%s [%s] %s at %s", os.date(nil, item.time), item.level, item.text, item.at)
if item.info then if item.info then
text = string.format("%s\n%s\n", text, item.info) text = string.format("%s\n%s\n", text, item.info)
end end

View File

@ -235,7 +235,8 @@ keymap.add_direct {
["pagedown"] = "doc:move-to-next-page", ["pagedown"] = "doc:move-to-next-page",
["shift+1lclick"] = "doc:select-to-cursor", ["shift+1lclick"] = "doc:select-to-cursor",
["ctrl+1lclick"] = "doc:split-cursor", ["ctrl+1lclick"] = { "doc:split-cursor", "log:copy-entry" },
["lclick"] = "log:expand-item",
["1lclick"] = "doc:set-cursor", ["1lclick"] = "doc:set-cursor",
["2lclick"] = "doc:set-cursor-word", ["2lclick"] = "doc:set-cursor-word",
["3lclick"] = "doc:set-cursor-line", ["3lclick"] = "doc:set-cursor-line",

View File

@ -36,6 +36,7 @@ local LogView = View:extend()
LogView.context = "session" LogView.context = "session"
function LogView:new() function LogView:new()
LogView.super.new(self) LogView.super.new(self)
self.last_item = core.log_items[#core.log_items] self.last_item = core.log_items[#core.log_items]
@ -91,14 +92,6 @@ function LogView:on_mouse_moved(px, py, ...)
end end
function LogView:on_mouse_pressed(button, mx, my, clicks)
if LogView.super.on_mouse_pressed(self, button, mx, my, clicks) then return end
if self.hovered_item then
self:expand_item(self.hovered_item)
end
end
function LogView:update() function LogView:update()
local item = core.log_items[#core.log_items] local item = core.log_items[#core.log_items]
if self.last_item ~= item then if self.last_item ~= item then
@ -144,7 +137,13 @@ function LogView:draw()
x = common.draw_text(style.font, style.dim, time, "left", x, y, w, lh) x = common.draw_text(style.font, style.dim, time, "left", x, y, w, lh)
x = x + style.padding.x x = x + style.padding.x
x = common.draw_text(style.code_font, style.dim, is_expanded(item) and "-" or "+", "left", x, y, w, lh) x = common.draw_text(
style.icon_font,
style.log[item.level].color,
style.log[item.level].icon,
"left",
x, y, w, lh
)
x = x + style.padding.x x = x + style.padding.x
w = w - (x - self:get_content_offset()) w = w - (x - self:get_content_offset())

View File

@ -66,4 +66,9 @@ style.syntax["function"] = { common.color "#93DDFA" }
style.syntax_fonts = {} style.syntax_fonts = {}
-- style.syntax_fonts["comment"] = renderer.font.load(path_to_font, size_of_font, rendering_options) -- style.syntax_fonts["comment"] = renderer.font.load(path_to_font, size_of_font, rendering_options)
style.log = {
INFO = { icon = "i", color = style.text },
ERROR = { icon = "!", color = style.accent }
}
return style return style

View File

@ -42,24 +42,6 @@ keymap.add {
["menu"] = "context:show" ["menu"] = "context:show"
} }
local function copy_log()
local item = core.active_view.hovered_item
if item then
system.set_clipboard(core.get_log(item))
end
end
local function open_as_doc()
local doc = core.open_doc("logs.txt")
core.root_view:open_doc(doc)
doc:insert(1, 1, core.get_log())
end
menu:register("core.logview", {
{ text = "Copy entry", command = copy_log },
{ text = "Open as file", command = open_as_doc }
})
if require("plugins.scale") then if require("plugins.scale") then
menu:register("core.docview", { menu:register("core.docview", {
{ text = "Cut", command = "doc:cut" }, { text = "Cut", command = "doc:cut" },