Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lua/keyevent/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ local default_config = {
delta = 20,
tap = 500,
},
log = {
level = vim.log.levels.ERROR,
output = "file", -- "buffer", "file", "print", "notify"
buffer_name = "keyevent://log",
file_name = "/tmp/keyevent.log",
use_timestamp = false,
single_line = true,
probe = true,
monitor = false,
},
}

function M.setup(opts)
Expand Down
3 changes: 2 additions & 1 deletion lua/keyevent/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require("keyevent.config")
local log = require("keyevent.log")
local os = require("keyevent.os")
local KeyEvent = require("keyevent.keyevent")
local diagnosis = require("keyevent.diagnosis")
Expand All @@ -25,7 +26,7 @@ local function initialize()
})
end

---@param opts? table
---@param opts table<string, any>|nil
function M.setup(opts)
config.setup(opts or {})
end
Expand Down
2 changes: 1 addition & 1 deletion lua/keyevent/keyevent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ local function emit(event)
end
callback(event)
end
-- log.watch(event.source, "emit:" .. M.to_string(event))
log.watch(event.source, "emit:" .. M.to_string(event))
end

local function stop_repeat_timer()
Expand Down
84 changes: 25 additions & 59 deletions lua/keyevent/log.lua
Original file line number Diff line number Diff line change
@@ -1,77 +1,43 @@
local ok, logger = pcall(require, "kibi2.logger.logger")
local ok, logger = pcall(require, "kibi2.logger")

local names = {
"setup",
"debug",
"info",
"warn",
"error",
"probe",
"watch",
"assert",
}

if not ok then
logger = {
debug = function() end,
info = function() end,
warn = function() end,
error = function(message)
vim.schedule(function()
vim.notify(message, vim.log.levels.ERROR)
end)
end,
probe = function() end,
watch = function() end,
is_debug = function()
return false
end,
assert = function(condition, message, ...)
if not condition then
error(string.format(message, ...), 2)
end
return condition
end,
}
local noop = function() end
logger = {}
for _, name in ipairs(names) do
logger[name] = noop
end
logger.new = noop
logger.is_debug = noop
end

-- =============================================================================

local M = {}
local log = logger.new() or {}

-- =============================================================================
-- Public API

---@param ... unknown
function M.debug(...)
logger.debug(...)
end

---@param ... unknown
function M.info(...)
logger.info(...)
end

---@param ... unknown
function M.warn(...)
logger.warn(...)
end

---@param ... unknown
function M.error(...)
logger.error(...)
end

---@param ... unknown
function M.probe(...)
logger.probe(...)
end

---@param category string
---@param ... unknown
function M.watch(category, ...)
logger.watch(category, ...)
for _, name in ipairs(names) do
M[name] = function(...)
logger[name](log, ...)
end
end

---@return boolean
function M.is_debug()
return logger.is_debug()
end

---@param condition any
---@param message string
---@param ... unknown
function M.assert(condition, message, ...)
logger.assert(condition, message, ...)
return logger.is_debug(log) == true
end

return M
2 changes: 1 addition & 1 deletion tests/backend/nvim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ backend_run() {
[ -f run.env ] && . run.env
env \
NVIM_APPNAME="${NVIM_APPNAME:-}" \
NVIM_TIRENVI_DEV=1 \
NVIM_KIBI2_DEV=1 \
$TEST_BIN --headless -u NONE -n \
-c "lua require('luacov')" \
-c "source run.vim" \
Expand Down
12 changes: 6 additions & 6 deletions tests/cases/logger/logger_error/out-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
=== MESSAGE ===

--- CASE 6: logger ---
[KB2][DEBUG][log.lua] log debug 1 two three
[KB2][INFO][log.lua] log info 1 two three
[KB2][WARN][log.lua] log warn 1 two three
[KB2][🟧PRB][log.lua 56] log probe 1 two three
[KB2][LOG][log.lua] watch
[KB2][DEBUG][[string ":lua"]] log debug 1 two three
[KB2][INFO][[string ":lua"]] log info 1 two three
[KB2][WARN][[string ":lua"]] log warn 1 two three
[KB2][🟧PRB][[string ":lua"] 15] log probe 1 two three
[KB2][LOG][[string ":lua"]] watch
true
[KB2][ERROR][log.lua] log error
[KB2][ERROR][[string ":lua"]] log error

=== DISPLAY ===

9 changes: 5 additions & 4 deletions tests/cases/logger/logger_error/run.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ lua << EOF
local root = assert(os.getenv("KIBI2_REPO_ROOT"))
vim.opt.rtp:prepend(root)
vim.opt.rtp:prepend(root .. "/../logger.nvim")
require("kibi2.logger").setup({
level = vim.log.levels.DEBUG,
output = "print",
})

package.loaded["keyevent.log"] = nil
log = require("keyevent.log")
log.setup({
key = "KB2",
level = vim.log.levels.DEBUG,
output = "print",
})
log.debug("log debug %d %s %s", 1, "two", "three")
log.info("log info %d %s %s", 1, "two", "three")
log.warn("log warn %d %s %s", 1, "two", "three")
Expand Down
5 changes: 1 addition & 4 deletions tests/cases/logger/no_logger_error/out-expected.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

--- CASE 6: logger ---
false
Error detected while processing command line..script /tests/cases/logger/no_logger_error/run.vim[21]..function Snapshot:
line 1:
log error
false
1 change: 1 addition & 0 deletions tests/cases/logger/no_logger_error/run.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lua << EOF
log.watch("LOG", "watch")
print(log.is_debug())
log.assert(true, "OK")
log.assert(false, "NG")
log.error("log error")
EOF

Expand Down
Loading