From e7a47b2be87bd87761cb9b6ed484dc9e70206e34 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sat, 18 May 2024 22:41:16 +0800 Subject: [PATCH 1/2] Adding log `max_size` --- README.md | 3 +++ lua/fittencode/config.lua | 9 ++++++++- lua/fittencode/fs/init.lua | 18 ++++++++++++++++++ lua/fittencode/log.lua | 11 +++++++++-- lua/fittencode/rest/manager.lua | 4 ++-- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 10aed7ef..13802177 100644 --- a/README.md +++ b/README.md @@ -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, }, } ``` diff --git a/lua/fittencode/config.lua b/lua/fittencode/config.lua index 8e7355d2..d4720039 100644 --- a/lua/fittencode/config.lua +++ b/lua/fittencode/config.lua @@ -78,7 +78,7 @@ M.options = { -- Rest backend to use. Available options: -- * 'curl' -- * 'libcurl' - -- * 'node' + -- * 'libuv' backend = 'curl', }, syntax_highlighting = { @@ -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, }, } diff --git a/lua/fittencode/fs/init.lua b/lua/fittencode/fs/init.lua index a7d50cb0..ea041dab 100644 --- a/lua/fittencode/fs/init.lua +++ b/lua/fittencode/fs/init.lua @@ -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 diff --git a/lua/fittencode/log.lua b/lua/fittencode/log.lua index 35e6af09..ec2900d3 100644 --- a/lua/fittencode/log.lua +++ b/lua/fittencode/log.lua @@ -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 diff --git a/lua/fittencode/rest/manager.lua b/lua/fittencode/rest/manager.lua index 1da2eecc..04f779a4 100644 --- a/lua/fittencode/rest/manager.lua +++ b/lua/fittencode/rest/manager.lua @@ -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, } From 0ad90380e85097327c3f8b6372af44dc10e33ec0 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sat, 18 May 2024 22:45:10 +0800 Subject: [PATCH 2/2] Code Clean --- lua/fittencode/config.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/fittencode/config.lua b/lua/fittencode/config.lua index d4720039..34e9fadb 100644 --- a/lua/fittencode/config.lua +++ b/lua/fittencode/config.lua @@ -1,7 +1,7 @@ local M = {} ---@class FittenCodeOptions -M.options = { +local defaults = { -- Same options as `fittentech.fitten-code` in vscode action = { document_code = { @@ -107,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