Skip to content

Commit

Permalink
Merge pull request #61 from luozhiya/refactor_log
Browse files Browse the repository at this point in the history
Adding log `max_size`
  • Loading branch information
luozhiya authored May 18, 2024
2 parents ab23c6a + 0ad9038 commit 75bd0f5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ use {
completion_mode = 'inline',
---@class LogOptions
log = {
-- Log level.
level = vim.log.levels.WARN,
-- Max log file size in MB, default is 10MB
max_size = 10,
},
}
```
Expand Down
13 changes: 10 additions & 3 deletions lua/fittencode/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

---@class FittenCodeOptions
M.options = {
local defaults = {
-- Same options as `fittentech.fitten-code` in vscode
action = {
document_code = {
Expand Down Expand Up @@ -78,7 +78,7 @@ M.options = {
-- Rest backend to use. Available options:
-- * 'curl'
-- * 'libcurl'
-- * 'node'
-- * 'libuv'
backend = 'curl',
},
syntax_highlighting = {
Expand All @@ -87,7 +87,14 @@ M.options = {
},
---@class LogOptions
log = {
-- Log level.
level = vim.log.levels.WARN,
-- Max log file size in MB, default is 10MB
max_size = 10,
-- Create new log file on startup, for debugging purposes.
new_file_on_startup = false,
-- TODO: Aynchronous logging.
async = true,
},
}

Expand All @@ -100,7 +107,7 @@ M.internal = {

---@param opts? FittenCodeOptions
function M.setup(opts)
M.options = vim.tbl_deep_extend('force', M.options, opts or {})
M.options = vim.tbl_deep_extend('force', defaults, opts or {})
end

return M
18 changes: 18 additions & 0 deletions lua/fittencode/fs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@ function M.delete(path, on_success, on_error)
end)
end

function M.stat(path, on_success, on_error)
Promise:new(function(resolve, reject)
uv.fs_stat(
path,
function(err, stat)
if err then
reject(err)
else
resolve(stat)
end
end)
end):forward(function(stat)
schedule(on_success, stat)
end, function(err)
schedule(on_error, uv_err(err))
end)
end

return M
11 changes: 9 additions & 2 deletions lua/fittencode/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ local function do_log(level, msg)
log_file(msg)
end

local function size_over_limit()
local size = fn.getfsize(LOG_PATH)
return size > Config.options.log.max_size * 1024 * 1024
end

function M.setup()
local LOG_HOME = fn.fnamemodify(LOG_PATH, ':h')
fn.mkdir(LOG_HOME, 'p')
fn.mkdir(fn.fnamemodify(LOG_PATH, ':h'), 'p')
if Config.options.log.new_file_on_startup or size_over_limit() then
fn.delete(LOG_PATH)
end
end

---@param level integer @one of the `vim.log.levels` values
Expand Down
4 changes: 2 additions & 2 deletions lua/fittencode/rest/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ local builtin_backends = {
['libcurl'] = function()
return require('fittencode.rest.backend.libcurl'):new()
end,
['node'] = function()
return require('fittencode.rest.backend.node'):new()
['libuv'] = function()
return require('fittencode.rest.backend.libuv'):new()
end,
}

Expand Down

0 comments on commit 75bd0f5

Please sign in to comment.