Remove log stuff
This commit is contained in:
parent
ae045167bf
commit
7b0626cdf0
2 changed files with 68 additions and 77 deletions
78
src/state.rs
78
src/state.rs
|
|
@ -1,10 +1,6 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fs::{self, OpenOptions},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TimeItem {
|
||||
|
|
@ -57,12 +53,6 @@ impl AppState {
|
|||
Ok(path)
|
||||
}
|
||||
|
||||
fn log_file() -> anyhow::Result<PathBuf> {
|
||||
let mut path = Self::config_dir()?;
|
||||
path.push("watson.log");
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn load() -> anyhow::Result<Self> {
|
||||
let path = Self::state_file()?;
|
||||
if !path.exists() {
|
||||
|
|
@ -86,19 +76,6 @@ impl AppState {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn read_log_lines(limit: usize) -> anyhow::Result<Vec<String>> {
|
||||
let path = Self::log_file()?;
|
||||
if !path.exists() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let contents = fs::read_to_string(path)?;
|
||||
let mut lines: Vec<String> = contents.lines().map(|l| l.to_string()).collect();
|
||||
if lines.len() > limit {
|
||||
lines = lines.split_off(lines.len() - limit);
|
||||
}
|
||||
Ok(lines)
|
||||
}
|
||||
|
||||
pub fn start_timer(&mut self, item: TimeItem) -> anyhow::Result<()> {
|
||||
if let Some((_, _)) = self.active_timer {
|
||||
self.stop_timer()?;
|
||||
|
|
@ -119,10 +96,6 @@ impl AppState {
|
|||
}
|
||||
|
||||
let output = command.output()?;
|
||||
Self::log_command_output(
|
||||
&format!("watson start {} {}", item.name, item.tags.join(" ")),
|
||||
&output,
|
||||
)?;
|
||||
|
||||
if !output.status.success() {
|
||||
anyhow::bail!(
|
||||
|
|
@ -139,8 +112,6 @@ impl AppState {
|
|||
if let Some((ref item, _)) = self.active_timer {
|
||||
let output = std::process::Command::new("watson").arg("stop").output()?;
|
||||
|
||||
Self::log_command_output("watson stop", &output)?;
|
||||
|
||||
if !output.status.success() {
|
||||
anyhow::bail!(
|
||||
"Watson stop failed: {}",
|
||||
|
|
@ -162,51 +133,4 @@ impl AppState {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn log_command_output(description: &str, output: &std::process::Output) -> anyhow::Result<()> {
|
||||
let mut lines = Vec::new();
|
||||
lines.push(format!("[{}] {description}", Utc::now().to_rfc3339()));
|
||||
lines.push(format!("status: {}", output.status));
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
if !stdout.is_empty() {
|
||||
lines.push("stdout:".into());
|
||||
lines.push(stdout);
|
||||
}
|
||||
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
|
||||
if !stderr.is_empty() {
|
||||
lines.push("stderr:".into());
|
||||
lines.push(stderr);
|
||||
}
|
||||
|
||||
lines.push(String::new());
|
||||
Self::append_log_entry(&lines)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn append_log_entry(lines: &[String]) -> anyhow::Result<()> {
|
||||
let path = Self::log_file()?;
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let mut file = OpenOptions::new().create(true).append(true).open(&path)?;
|
||||
for line in lines {
|
||||
writeln!(file, "{line}")?;
|
||||
}
|
||||
Self::trim_log(&path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn trim_log(path: &Path) -> anyhow::Result<()> {
|
||||
const MAX_LINES: usize = 800;
|
||||
let contents = fs::read_to_string(path)?;
|
||||
let mut lines: Vec<&str> = contents.lines().collect();
|
||||
if lines.len() > MAX_LINES {
|
||||
lines = lines.split_off(lines.len() - MAX_LINES);
|
||||
fs::write(path, lines.join("\n") + "\n")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue