From b20aefc8421a12cba07c8f86a27249f634ce18d9 Mon Sep 17 00:00:00 2001 From: Isaac Clayton Date: Fri, 22 Jul 2022 09:43:15 +0200 Subject: [PATCH 1/3] First draft for opening Zed.log --- crates/zed/src/zed.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 867913fc7ba914ed18461cc212b4fed91e72d44c..709dc31f64a3a498ecfc8280d852826b85010504 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -52,6 +52,7 @@ actions!( Quit, DebugElements, OpenSettings, + OpenLog, OpenKeymap, OpenDefaultSettings, OpenDefaultKeymap, @@ -65,9 +66,10 @@ actions!( const MIN_FONT_SIZE: f32 = 6.0; lazy_static! { - pub static ref ROOT_PATH: PathBuf = dirs::home_dir() - .expect("failed to determine home directory") - .join(".zed"); + pub static ref HOME_PATH: PathBuf = + dirs::home_dir().expect("failed to determine home directory"); + pub static ref LOG_PATH: PathBuf = HOME_PATH.join("Library/Logs/Zed/Zed.log"); + pub static ref ROOT_PATH: PathBuf = HOME_PATH.join(".zed"); pub static ref SETTINGS_PATH: PathBuf = ROOT_PATH.join("settings.json"); pub static ref KEYMAP_PATH: PathBuf = ROOT_PATH.join("keymap.json"); } @@ -120,6 +122,12 @@ pub fn init(app_state: &Arc, cx: &mut gpui::MutableAppContext) { }); } }); + cx.add_action({ + let app_state = app_state.clone(); + move |_: &mut Workspace, _: &OpenLog, cx: &mut ViewContext| { + open_config_file(&LOG_PATH, app_state.clone(), cx, || Default::default()); + } + }); cx.add_action({ let app_state = app_state.clone(); move |_: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext| { From de35c3f99d709c5f763af174abc12667d43a4f59 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 28 Jul 2022 10:02:38 +0200 Subject: [PATCH 2/3] Prevent log file from becoming larger than 1MB --- crates/zed/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 337e3afc2dc03d04d4753377c31f584067fc7871..2ca1403b6c868c8683f16c13f1e0970467525b4c 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -57,6 +57,7 @@ fn main() { fs::create_dir_all(&logs_dir_path).expect("could not create logs path"); init_logger(&logs_dir_path); + log::info!("========== starting zed =========="); let mut app = gpui::App::new(Assets).unwrap(); let app_version = ZED_APP_VERSION .or_else(|| app.platform().app_version().ok()) @@ -210,6 +211,13 @@ fn init_logger(logs_dir_path: &Path) { } else { let level = LevelFilter::Info; let log_file_path = logs_dir_path.join("Zed.log"); + + // Prevent log file from becoming too large. + const MAX_LOG_BYTES: u64 = 1 * 1024 * 1024; + if fs::metadata(&log_file_path).map_or(false, |metadata| metadata.len() > MAX_LOG_BYTES) { + let _ = fs::rename(&log_file_path, logs_dir_path.join("Zed.log.old")); + } + let log_file = OpenOptions::new() .create(true) .append(true) From df6e733e687bf6e7bb26adf7252e82735ca2801e Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 28 Jul 2022 10:25:40 +0200 Subject: [PATCH 3/3] Open both old and new log files, but just retain the last 1000 lines --- crates/workspace/src/workspace.rs | 4 +-- crates/zed/src/zed.rs | 60 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index e8ff1932d4e2d9f9956a45e0072f08423b679c85..ac3d1c36960d8914d40506a2e30cf7bdbec8f302 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -949,11 +949,11 @@ impl Workspace { &mut self, cx: &mut ViewContext, app_state: Arc, - mut callback: F, + callback: F, ) -> T where T: 'static, - F: FnMut(&mut Workspace, &mut ViewContext) -> T, + F: FnOnce(&mut Workspace, &mut ViewContext) -> T, { if self.project.read(cx).is_local() { callback(self, cx) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 709dc31f64a3a498ecfc8280d852826b85010504..f7ccefec2abd79298968a056272816c1771d63ea 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -9,6 +9,7 @@ use anyhow::{anyhow, Context, Result}; use assets::Assets; use breadcrumbs::Breadcrumbs; pub use client; +use collections::VecDeque; pub use contacts_panel; use contacts_panel::ContactsPanel; pub use editor; @@ -69,6 +70,7 @@ lazy_static! { pub static ref HOME_PATH: PathBuf = dirs::home_dir().expect("failed to determine home directory"); pub static ref LOG_PATH: PathBuf = HOME_PATH.join("Library/Logs/Zed/Zed.log"); + pub static ref OLD_LOG_PATH: PathBuf = HOME_PATH.join("Library/Logs/Zed/Zed.log.old"); pub static ref ROOT_PATH: PathBuf = HOME_PATH.join(".zed"); pub static ref SETTINGS_PATH: PathBuf = ROOT_PATH.join("settings.json"); pub static ref KEYMAP_PATH: PathBuf = ROOT_PATH.join("keymap.json"); @@ -124,8 +126,8 @@ pub fn init(app_state: &Arc, cx: &mut gpui::MutableAppContext) { }); cx.add_action({ let app_state = app_state.clone(); - move |_: &mut Workspace, _: &OpenLog, cx: &mut ViewContext| { - open_config_file(&LOG_PATH, app_state.clone(), cx, || Default::default()); + move |workspace: &mut Workspace, _: &OpenLog, cx: &mut ViewContext| { + open_log_file(workspace, app_state.clone(), cx); } }); cx.add_action({ @@ -415,6 +417,60 @@ fn open_config_file( .detach_and_log_err(cx) } +fn open_log_file( + workspace: &mut Workspace, + app_state: Arc, + cx: &mut ViewContext, +) { + const MAX_LINES: usize = 1000; + + workspace.with_local_workspace(cx, app_state.clone(), |_, cx| { + cx.spawn_weak(|workspace, mut cx| async move { + let (old_log, new_log) = futures::join!( + app_state.fs.load(&OLD_LOG_PATH), + app_state.fs.load(&LOG_PATH) + ); + + if let Some(workspace) = workspace.upgrade(&cx) { + let mut lines = VecDeque::with_capacity(MAX_LINES); + for line in old_log + .iter() + .flat_map(|log| log.lines()) + .chain(new_log.iter().flat_map(|log| log.lines())) + { + if lines.len() == MAX_LINES { + lines.pop_front(); + } + lines.push_back(line); + } + let log = lines + .into_iter() + .flat_map(|line| [line, "\n"]) + .collect::(); + + workspace.update(&mut cx, |workspace, cx| { + let project = workspace.project().clone(); + let buffer = project + .update(cx, |project, cx| project.create_buffer("", None, cx)) + .expect("creating buffers on a local workspace always succeeds"); + buffer.update(cx, |buffer, cx| buffer.edit([(0..0, log)], cx)); + + let buffer = cx.add_model(|cx| { + MultiBuffer::singleton(buffer, cx).with_title("Log".into()) + }); + workspace.add_item( + Box::new( + cx.add_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx)), + ), + cx, + ); + }); + } + }) + .detach(); + }); +} + fn open_bundled_config_file( workspace: &mut Workspace, app_state: Arc,