Skip to content

Commit

Permalink
fix: #371
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Sep 27, 2024
1 parent 6f9def2 commit 660cf63
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
9 changes: 4 additions & 5 deletions screenpipe-server/src/bin/screenpipe-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ use screenpipe_audio::{
};
use screenpipe_core::find_ffmpeg_path;
use screenpipe_server::{
cli::{Cli, CliAudioTranscriptionEngine, CliOcrEngine, Command, PipeCommand}, start_continuous_recording, watch_pid, DatabaseManager, PipeManager, ResourceMonitor, Server
cli::{Cli, CliAudioTranscriptionEngine, CliOcrEngine, Command, PipeCommand}, logs::SingleFileRollingWriter, start_continuous_recording, watch_pid, DatabaseManager, PipeManager, ResourceMonitor, Server
};
use screenpipe_vision::monitor::list_monitors;
use serde_json::{json, Value};
use tokio::{runtime::Runtime, signal, sync::broadcast};
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;
Expand Down Expand Up @@ -83,12 +82,12 @@ async fn main() -> anyhow::Result<()> {
}

// Set up file appender
let file_appender =
RollingFileAppender::new(Rotation::NEVER, local_data_dir.clone(), "screenpipe.log");
let log_file_path = local_data_dir.join("screenpipe.log");
let file_writer = SingleFileRollingWriter::new(log_file_path)?;

// Create a custom layer for file logging
let file_layer = fmt::layer()
.with_writer(file_appender)
.with_writer(file_writer)
.with_ansi(false)
.with_filter(EnvFilter::new("info"));

Expand Down
60 changes: 60 additions & 0 deletions screenpipe-server/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,63 @@ impl Write for MultiWriter {
Ok(())
}
}

use std::fs::{File, OpenOptions};
use std::io::{self, Seek, SeekFrom};
use tracing_subscriber::fmt::writer::MakeWriter;

const MAX_LOG_SIZE: u64 = 100 * 1024 * 1024; // 100 MB

pub struct SingleFileRollingWriter {
file: File,
path: std::path::PathBuf,
}

impl SingleFileRollingWriter {
pub fn new(path: impl Into<std::path::PathBuf>) -> io::Result<Self> {
let path = path.into();
let file = OpenOptions::new().create(true).append(true).open(&path)?;
Ok(Self { file, path })
}

fn roll(&mut self) -> io::Result<()> {
let metadata = self.file.metadata()?;
if metadata.len() > MAX_LOG_SIZE {
let mut content = Vec::new();
self.file.seek(SeekFrom::Start(0))?;
io::copy(&mut self.file, &mut content)?;

// Truncate to half the max size
content.truncate((MAX_LOG_SIZE / 2) as usize);

// Rewrite the file with truncated content
self.file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&self.path)?;
self.file.write_all(&content)?;
self.file.seek(SeekFrom::End(0))?;
}
Ok(())
}
}

impl Write for SingleFileRollingWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.roll()?;
self.file.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.file.flush()
}
}

impl<'a> MakeWriter<'a> for SingleFileRollingWriter {
type Writer = Self;

fn make_writer(&self) -> Self::Writer {
SingleFileRollingWriter::new(&self.path).expect("Failed to create writer")
}
}

0 comments on commit 660cf63

Please sign in to comment.