1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::path::PathBuf;
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::error::Result;
use crate::event::Event;
/// A log line with its source file
#[derive(Debug, Clone)]
pub struct LogEntry {
pub content: String,
pub file: String,
}
/// Watch Laravel log directory for changes
pub struct LogWatcher {
log_dir: PathBuf,
additional_files: Vec<PathBuf>,
event_tx: mpsc::Sender<Event>,
cancel_token: CancellationToken,
}
impl LogWatcher {
pub fn new(
log_dir: PathBuf,
event_tx: mpsc::Sender<Event>,
cancel_token: CancellationToken,
) -> Self {
Self {
log_dir,
additional_files: Vec::new(),
event_tx,
cancel_token,
}
}
/// Add additional log files to watch (paths relative to project root)
pub fn with_additional_files(mut self, files: Vec<PathBuf>) -> Self {
self.additional_files = files;
self
}
/// Start watching the log directory for any .log files
pub async fn watch(self) -> Result<()> {
let log_dir = self.log_dir.clone();
let additional_files = self.additional_files.clone();
let event_tx = self.event_tx.clone();
let cancel_token = self.cancel_token.clone();
// Create channel for file system events
let (fs_tx, mut fs_rx) = mpsc::channel::<notify::Result<notify::Event>>(100);
// Create watcher
let mut watcher = RecommendedWatcher::new(
move |res| {
let _ = fs_tx.blocking_send(res);
},
Config::default(),
)?;
// Watch the log directory
watcher.watch(&log_dir, RecursiveMode::NonRecursive)?;
// Watch parent directories of additional files
let mut watched_dirs: HashSet<PathBuf> = HashSet::new();
watched_dirs.insert(log_dir.clone());
// Track which specific files we're watching (for additional files)
let watched_files: HashSet<PathBuf> = additional_files.iter().cloned().collect();
for file in &additional_files {
if let Some(parent) = file.parent() {
if parent.exists()
&& !watched_dirs.contains(parent)
&& watcher.watch(parent, RecursiveMode::NonRecursive).is_ok()
{
watched_dirs.insert(parent.to_path_buf());
}
}
}
// Track file positions for incremental reading (keyed by file path)
let mut file_positions: std::collections::HashMap<PathBuf, u64> =
std::collections::HashMap::new();
// Initialize positions for existing log files and load recent history
if let Ok(entries) = std::fs::read_dir(&log_dir) {
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
if path.extension().map(|ext| ext == "log").unwrap_or(false) {
let file_name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
// Read last 5 lines as initial history
if let Ok(recent_lines) = read_last_n_lines(&path, 5) {
if !recent_lines.is_empty() {
let entries: Vec<LogEntry> = recent_lines
.into_iter()
.map(|content| LogEntry {
content,
file: file_name.clone(),
})
.collect();
let _ = event_tx.send(Event::LogUpdate(entries)).await;
}
}
// Set position to end of file for future reads
let pos = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
file_positions.insert(path, pos);
}
}
}
// Initialize positions for additional files
for file in &additional_files {
if file.exists() {
let file_name = file
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
// Read last 5 lines as initial history
if let Ok(recent_lines) = read_last_n_lines(file, 5) {
if !recent_lines.is_empty() {
let entries: Vec<LogEntry> = recent_lines
.into_iter()
.map(|content| LogEntry {
content,
file: file_name.clone(),
})
.collect();
let _ = event_tx.send(Event::LogUpdate(entries)).await;
}
}
// Set position to end of file for future reads
let pos = std::fs::metadata(file).map(|m| m.len()).unwrap_or(0);
file_positions.insert(file.clone(), pos);
}
}
loop {
tokio::select! {
_ = cancel_token.cancelled() => {
break;
}
Some(event) = fs_rx.recv() => {
if let Ok(event) = event {
// Process any .log file events
for path in event.paths.iter() {
let is_log_file = path.extension().map(|ext| ext == "log").unwrap_or(false);
// Check if this is a file we should process:
// 1. Any .log file in the main log directory
// 2. Specifically watched additional files
let in_log_dir = path.parent() == Some(&log_dir);
let is_watched_file = watched_files.contains(path);
let should_process = (is_log_file && in_log_dir) || is_watched_file;
if should_process
&& matches!(event.kind, notify::EventKind::Modify(_) | notify::EventKind::Create(_))
{
let file_name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
// Get or create position tracker for this file
let last_pos = file_positions.entry(path.clone()).or_insert(0);
// Read new content
if let Ok(new_lines) = read_new_lines(path, last_pos) {
if !new_lines.is_empty() {
let entries: Vec<LogEntry> = new_lines
.into_iter()
.map(|content| LogEntry {
content,
file: file_name.clone(),
})
.collect();
let _ = event_tx.send(Event::LogUpdate(entries)).await;
}
}
}
}
}
}
}
}
Ok(())
}
}
/// Read the last N non-empty lines from a file
fn read_last_n_lines(path: &PathBuf, n: usize) -> std::io::Result<Vec<String>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
// Read all lines, keeping only non-empty ones
let lines: Vec<String> = reader
.lines()
.map_while(|l| l.ok())
.map(|l| l.trim_end().to_string())
.filter(|l| !l.is_empty())
.collect();
// Return last N lines
let start = lines.len().saturating_sub(n);
Ok(lines[start..].to_vec())
}
/// Read new lines from file starting at given position
fn read_new_lines(path: &PathBuf, last_pos: &mut u64) -> std::io::Result<Vec<String>> {
let file = File::open(path)?;
let metadata = file.metadata()?;
let current_size = metadata.len();
// File was truncated (rotated), start from beginning
if current_size < *last_pos {
*last_pos = 0;
}
// No new content
if current_size == *last_pos {
return Ok(Vec::new());
}
let mut reader = BufReader::new(file);
reader.seek(SeekFrom::Start(*last_pos))?;
let mut lines = Vec::new();
let mut line = String::new();
while reader.read_line(&mut line)? > 0 {
let trimmed = line.trim_end();
if !trimmed.is_empty() {
lines.push(trimmed.to_string());
}
line.clear();
}
*last_pos = current_size;
Ok(lines)
}
/// Find Laravel log directory in project
pub fn find_log_dir(working_dir: &std::path::Path) -> Option<PathBuf> {
let logs_dir = working_dir.join("storage/logs");
if logs_dir.exists() && logs_dir.is_dir() {
Some(logs_dir)
} else {
None
}
}