-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |