Skip to content

Commit

Permalink
add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
fedemengo committed Mar 31, 2023
1 parent 0ad1afd commit a74751d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fnl/mods/tools/telescope.fnl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(module mods.tools.telescope
{ autoload {
;;log log
telescope telescope
utils telescope.utils
builtin telescope.builtin
actions telescope.actions
themes telescope.themes
generate telescope.actions.generate }})

;;(log.caller false)
;;(log.trace "Loading telescope")

(telescope.setup {
:defaults {
:vimgrep_arguments [
Expand Down
8 changes: 8 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ ensure("fedemengo", "github-nvim-theme")

require("impatient").enable_profile()

--local log = require("log").setup({
-- level = "trace",
-- outfile = "/tmp/nvim.log",
-- caller = false
--})
--
--log.trace("Hello world", { "foo", "bar" }, 1, 2, 3)

vim.g["aniseed#env"] = {
module = "init",
compile = true
Expand Down
133 changes: 133 additions & 0 deletions log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
--
-- log.lua
--
-- Copyright (c) 2016 rxi
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
-- https://github.com/rxi/log.lua

local log = { _version = "0.1.0" }

log.caller = false
log.usecolor = true
log.outfile = nil
log.level = "trace"

local modes = {
{ name = "trace", color = "\27[34m" },
{ name = "debug", color = "\27[36m" },
{ name = "info", color = "\27[32m" },
{ name = "warn", color = "\27[33m" },
{ name = "error", color = "\27[31m" },
{ name = "fatal", color = "\27[35m" },
}

local levels = {}
for i, v in ipairs(modes) do
levels[v.name] = i
end

local round = function(x, increment)
increment = increment or 1
x = x / increment
return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment
end

local _tostring = tostring

local tostring = function(...)
local t = {}
for i = 1, select("#", ...) do
local x = select(i, ...)
if type(x) == "number" then
x = round(x, 0.01)
elseif type(x) == "table" then
x = tabletostring(x)
end
t[#t + 1] = _tostring(x)
end
return table.concat(t, " ")
end

function tabletostring(table)
local str = ""
local len = #table
local done = 0
local sep = ", "
for k, v in pairs(table) do
done = done + 1
if done == len then
sep = ""
end
if type(v) == "table" then
str = str .. '"' .. tostring(k) .. '": ' .. tabletostring(v) .. sep
else
str = str .. '"' .. tostring(k) .. '":"' .. tostring(v) .. '"' .. sep
end
end
return "{" .. str .. "}"
end

for i, x in ipairs(modes) do
local nameupper = x.name:upper()
log[x.name] = function(...)
-- Return early if we're below the log level
if i < levels[log.level] then
return
end

local msg = tostring(...)
local info = debug.getinfo(2, "Sl")
local short = info.short_src
local home = os.getenv("HOME")
if not home then
home = "~"
end

short = short:gsub(home .. "/.dotfiles/.config/nvim/", "nvim/")
short = short:gsub(home .. "/.config/nvim/", "nvim/")
short = short:gsub(home, "~")

local lineinfo = short .. ":" .. info.currentline

-- Output to log file
if log.outfile then
local fp = io.open(log.outfile, "a")
local str = string.format(
"%s[%-6s%s]%s %s %s\n",
log.usecolor and x.color or "",
nameupper,
os.date("%H:%M:%S"),
log.usecolor and "\27[0m" or "",
log.caller and lineinfo or "",
msg
)

if fp then
fp:write(str)
fp:close()
end
end
end
end

log["caller"] = function(b)
log.caller = b
end

log["setfile"] = function(filename)
log.outfile = filename
end

log["setup"] = function(opts)
opts = opts or {}
log.caller = opts.caller or log.caller
log.usecolor = opts.usecolor or log.usecolor
log.outfile = opts.outfile or log.outfile
log.level = opts.level or log.level
return log
end

return log

0 comments on commit a74751d

Please sign in to comment.