Add macros for logging.

This commit is contained in:
2026-02-22 10:09:53 -05:00
parent e5f77ef51c
commit 779872b2f6
21 changed files with 246 additions and 79 deletions

View File

@@ -5,7 +5,7 @@
use crate::tui::component::{Action, Component, Focus, FocusState};
use crate::tui::editor::Editor;
use anyhow::{Context, Result, anyhow};
use log::{error, info, trace, warn};
use libclide_macros::log_id;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;
@@ -16,6 +16,7 @@ use std::collections::HashMap;
// Render the tabs with keys as titles
// Tab keys can be file names.
// Render the editor using the key as a reference for lookup
#[log_id]
pub struct EditorTab {
pub(crate) editors: HashMap<String, Editor>,
tab_order: Vec<String>,
@@ -23,10 +24,8 @@ pub struct EditorTab {
}
impl EditorTab {
pub const ID: &str = "EditorTab";
pub fn new() -> Self {
trace!(target:Self::ID, "Building {}", Self::ID);
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
Self {
editors: HashMap::new(),
tab_order: Vec::new(),
@@ -36,7 +35,7 @@ impl EditorTab {
pub fn next_editor(&mut self) {
let next = (self.current_editor + 1) % self.tab_order.len();
trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
libclide::trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
self.set_tab_focus(Focus::Active, next);
self.current_editor = next;
}
@@ -46,7 +45,7 @@ impl EditorTab {
.current_editor
.checked_sub(1)
.unwrap_or(self.tab_order.len() - 1);
trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
libclide::trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
self.set_tab_focus(Focus::Active, prev);
self.current_editor = prev;
}
@@ -55,7 +54,7 @@ impl EditorTab {
match self.tab_order.get(index) {
None => {
if !self.tab_order.is_empty() {
error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
libclide::error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
}
None
}
@@ -73,15 +72,15 @@ impl EditorTab {
}
pub fn set_current_tab_focus(&mut self, focus: Focus) {
trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
libclide::trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
self.set_tab_focus(focus, self.current_editor)
}
pub fn set_tab_focus(&mut self, focus: Focus, index: usize) {
trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
libclide::trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
if focus == Focus::Active && index != self.current_editor {
// If we are setting another tab to active, disable the current one.
trace!(
libclide::trace!(
target:Self::ID,
"New tab {} focus set to Active; Setting current tab {} to Inactive",
index,
@@ -91,11 +90,11 @@ impl EditorTab {
}
match self.get_editor_key(index) {
None => {
error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
libclide::error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
}
Some(key) => match self.editors.get_mut(&key) {
None => {
error!(
libclide::error!(
target:Self::ID,
"Failed to update tab focus at index {} with invalid key: {}",
self.current_editor,
@@ -108,12 +107,12 @@ impl EditorTab {
}
pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> {
trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
libclide::trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
if self
.editors
.contains_key(&path.to_string_lossy().to_string())
{
warn!(target:Self::ID, "EditorTab already opened with this file");
libclide::warn!(target:Self::ID, "EditorTab already opened with this file");
return Ok(());
}
@@ -138,12 +137,12 @@ impl EditorTab {
.to_owned();
match self.editors.remove(&key) {
None => {
error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
libclide::error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
}
Some(_) => {
self.prev_editor();
self.tab_order.remove(index);
info!(target:Self::ID, "Closed editor tab {key} at index {index}")
libclide::info!(target:Self::ID, "Closed editor tab {key} at index {index}")
}
}
Ok(())