aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform/log.lua
blob: 79ca15a0999b10146a9dd265dd8586c39e231491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
local uv = vim.uv or vim.loop
local levels_reverse = {}
for k, v in pairs(vim.log.levels) do
  levels_reverse[v] = k
end

local Log = {}

---@type integer
Log.level = vim.log.levels.WARN

---@return string
Log.get_logfile = function()
  local fs = require("conform.fs")

  local ok, stdpath = pcall(vim.fn.stdpath, "log")
  if not ok then
    stdpath = vim.fn.stdpath("cache")
  end
  assert(type(stdpath) == "string")
  return fs.join(stdpath, "conform.log")
end

---@param level integer
---@param msg string
---@param ... any[]
---@return string
local function format(level, msg, ...)
  local args = vim.F.pack_len(...)
  for i = 1, args.n do
    local v = args[i]
    if type(v) == "table" then
      args[i] = vim.inspect(v)
    elseif v == nil then
      args[i] = "nil"
    end
  end
  local ok, text = pcall(string.format, msg, vim.F.unpack_len(args))
  local timestr = vim.fn.strftime("%H:%M:%S")
  if ok then
    local str_level = levels_reverse[level]
    return string.format("%s[%s] %s", timestr, str_level, text)
  else
    return string.format(
      "%s[ERROR] error formatting log line: '%s' args %s",
      timestr,
      vim.inspect(msg),
      vim.inspect(args)
    )
  end
end

---@param line string
local function write(line)
  -- This will be replaced during initialization
end

local initialized = false
local function initialize()
  if initialized then
    return
  end
  initialized = true
  local filepath = Log.get_logfile()

  local stat = uv.fs_stat(filepath)
  if stat and stat.size > 10 * 1024 * 1024 then
    local backup = filepath .. ".1"
    uv.fs_unlink(backup)
    uv.fs_rename(filepath, backup)
  end

  local parent = vim.fs.dirname(filepath)
  vim.fn.mkdir(parent, "p")

  local logfile, openerr = io.open(filepath, "a+")
  if not logfile then
    local err_msg = string.format("Failed to open conform.nvim log file: %s", openerr)
    vim.notify(err_msg, vim.log.levels.ERROR)
  else
    write = function(line)
      logfile:write(line)
      logfile:write("\n")
      logfile:flush()
    end
  end
end

---Override the file handler e.g. for tests
---@param handler fun(line: string)
function Log.set_handler(handler)
  write = handler
  initialized = true
end

function Log.log(level, msg, ...)
  if Log.level <= level then
    initialize()
    local text = format(level, msg, ...)
    write(text)
  end
end

function Log.trace(...)
  Log.log(vim.log.levels.TRACE, ...)
end

function Log.debug(...)
  Log.log(vim.log.levels.DEBUG, ...)
end

function Log.info(...)
  Log.log(vim.log.levels.INFO, ...)
end

function Log.warn(...)
  Log.log(vim.log.levels.WARN, ...)
end

function Log.error(...)
  Log.log(vim.log.levels.ERROR, ...)
end

return Log