log-watcher 0.2.1

Real-time log file monitoring with pattern highlighting and desktop notifications
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use anyhow::{Context, Result};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

/// Read all lines from a file
pub fn read_file_from_end<P: AsRef<Path>>(path: P, _buffer_size: usize) -> Result<Vec<String>> {
    let file = File::open(&path)
        .with_context(|| format!("Failed to open file: {}", path.as_ref().display()))?;

    let reader = BufReader::new(file);
    let mut lines = Vec::new();

    for line_result in reader.lines() {
        let line = line_result?;
        if !line.trim().is_empty() {
            lines.push(line.trim().to_string());
        }
    }

    Ok(lines)
}

/// Check if a file exists and is readable
pub fn is_file_readable<P: AsRef<Path>>(path: P) -> bool {
    File::open(path).is_ok()
}

/// Get file size
pub fn get_file_size<P: AsRef<Path>>(path: P) -> Result<u64> {
    let metadata = std::fs::metadata(path).with_context(|| "Failed to get file metadata")?;
    Ok(metadata.len())
}

/// Check if a file has been rotated (size decreased)
pub fn is_file_rotated<P: AsRef<Path>>(path: P, previous_size: u64) -> Result<bool> {
    let current_size = get_file_size(path)?;
    Ok(current_size < previous_size)
}

/// Validate that all files exist and are readable
pub fn validate_files<P: AsRef<Path> + Clone>(files: &[P]) -> Result<Vec<P>> {
    let mut valid_files = Vec::new();
    let mut errors = Vec::new();

    for file in files {
        if is_file_readable(file) {
            valid_files.push(file.clone());
        } else {
            errors.push(format!("File not readable: {}", file.as_ref().display()));
        }
    }

    if valid_files.is_empty() {
        return Err(anyhow::anyhow!(
            "No valid files to watch: {}",
            errors.join(", ")
        ));
    }

    if !errors.is_empty() {
        eprintln!(
            "Warning: Some files are not accessible: {}",
            errors.join(", ")
        );
    }

    Ok(valid_files)
}

/// Format file size in human-readable format
pub fn format_file_size(size: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
    let mut size = size as f64;
    let mut unit_index = 0;

    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
        size /= 1024.0;
        unit_index += 1;
    }

    if unit_index == 0 {
        format!("{} {}", size as u64, UNITS[unit_index])
    } else {
        format!("{:.1} {}", size, UNITS[unit_index])
    }
}

/// Get filename from path
pub fn get_filename<P: AsRef<Path>>(path: P) -> String {
    path.as_ref()
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("unknown")
        .to_string()
}

/// Check if a path is a symlink
pub fn is_symlink<P: AsRef<Path>>(path: P) -> bool {
    path.as_ref()
        .symlink_metadata()
        .map(|metadata| metadata.file_type().is_symlink())
        .unwrap_or(false)
}

/// Resolve symlink to actual path
pub fn resolve_symlink<P: AsRef<Path>>(path: P) -> Result<std::path::PathBuf> {
    let resolved = path
        .as_ref()
        .read_link()
        .with_context(|| format!("Failed to read symlink: {}", path.as_ref().display()))?;
    Ok(resolved)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_format_file_size() {
        assert_eq!(format_file_size(1024), "1.0 KB");
        assert_eq!(format_file_size(1536), "1.5 KB");
        assert_eq!(format_file_size(1048576), "1.0 MB");
        assert_eq!(format_file_size(1023), "1023 B");
    }

    #[test]
    fn test_get_filename() {
        assert_eq!(get_filename("/path/to/file.log"), "file.log");
        assert_eq!(get_filename("file.log"), "file.log");
        assert_eq!(get_filename("/"), "unknown");
    }

    #[test]
    fn test_validate_files() {
        // Create a temporary file
        let temp_file = NamedTempFile::new().unwrap();
        let temp_path = temp_file.path();

        // Test with valid file
        let valid_files = vec![temp_path];
        let result = validate_files(&valid_files);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 1);

        // Test with invalid file
        let invalid_files = vec![std::path::Path::new("/nonexistent/file.log")];
        let result = validate_files(&invalid_files);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_file_from_end() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "line 1").unwrap();
        writeln!(temp_file, "line 2").unwrap();
        writeln!(temp_file, "line 3").unwrap();
        temp_file.flush().unwrap();

        let lines = read_file_from_end(temp_file.path(), 1024).unwrap();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "line 1");
        assert_eq!(lines[1], "line 2");
        assert_eq!(lines[2], "line 3");
    }

    #[test]
    fn test_read_file_from_end_with_empty_lines() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "line 1").unwrap();
        writeln!(temp_file).unwrap(); // Empty line
        writeln!(temp_file, "   ").unwrap(); // Whitespace only line
        writeln!(temp_file, "line 2").unwrap();
        temp_file.flush().unwrap();

        let lines = read_file_from_end(temp_file.path(), 1024).unwrap();
        assert_eq!(lines.len(), 2); // Empty lines should be filtered out
        assert_eq!(lines[0], "line 1");
        assert_eq!(lines[1], "line 2");
    }

    #[test]
    fn test_read_file_from_end_with_nonexistent_file() {
        let result = read_file_from_end("/nonexistent/file.log", 1024);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to open file"));
    }

    #[test]
    fn test_is_file_readable() {
        let temp_file = NamedTempFile::new().unwrap();
        assert!(is_file_readable(temp_file.path()));

        assert!(!is_file_readable("/nonexistent/file.log"));
    }

    #[test]
    fn test_get_file_size() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test content").unwrap();
        temp_file.flush().unwrap();

        let size = get_file_size(temp_file.path()).unwrap();
        assert!(size > 0);
    }

    #[test]
    fn test_get_file_size_nonexistent() {
        let result = get_file_size("/nonexistent/file.log");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to get file metadata"));
    }

    #[test]
    fn test_is_file_rotated() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "initial content").unwrap();
        temp_file.flush().unwrap();

        let initial_size = get_file_size(temp_file.path()).unwrap();

        // Add more content
        writeln!(temp_file, "more content").unwrap();
        temp_file.flush().unwrap();

        // File should not be rotated (size increased)
        assert!(!is_file_rotated(temp_file.path(), initial_size).unwrap());

        // Simulate file rotation by truncating
        temp_file.as_file_mut().set_len(0).unwrap();
        temp_file.flush().unwrap();

        // File should be detected as rotated (size decreased)
        assert!(is_file_rotated(temp_file.path(), initial_size).unwrap());
    }

    #[test]
    fn test_validate_files_with_mixed_validity() {
        let temp_file = NamedTempFile::new().unwrap();
        let temp_path = temp_file.path();

        let files = vec![
            temp_path,
            std::path::Path::new("/nonexistent/file1.log"),
            std::path::Path::new("/nonexistent/file2.log"),
        ];

        let result = validate_files(&files);
        assert!(result.is_ok());

        let valid_files = result.unwrap();
        assert_eq!(valid_files.len(), 1);
        assert_eq!(valid_files[0], temp_path);
    }

    #[test]
    fn test_validate_files_all_invalid() {
        let files = vec![
            std::path::Path::new("/nonexistent/file1.log"),
            std::path::Path::new("/nonexistent/file2.log"),
        ];

        let result = validate_files(&files);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No valid files to watch"));
    }

    #[test]
    fn test_format_file_size_edge_cases() {
        // Test bytes
        assert_eq!(format_file_size(0), "0 B");
        assert_eq!(format_file_size(1), "1 B");
        assert_eq!(format_file_size(1023), "1023 B");

        // Test kilobytes
        assert_eq!(format_file_size(1024), "1.0 KB");
        assert_eq!(format_file_size(1536), "1.5 KB");
        assert_eq!(format_file_size(2048), "2.0 KB");

        // Test megabytes
        assert_eq!(format_file_size(1048576), "1.0 MB");
        assert_eq!(format_file_size(1572864), "1.5 MB");

        // Test gigabytes
        assert_eq!(format_file_size(1073741824), "1.0 GB");

        // Test terabytes
        assert_eq!(format_file_size(1099511627776), "1.0 TB");
    }

    #[test]
    fn test_get_filename_edge_cases() {
        assert_eq!(get_filename("/path/to/file.log"), "file.log");
        assert_eq!(get_filename("file.log"), "file.log");
        assert_eq!(get_filename("/"), "unknown");
        assert_eq!(get_filename(""), "unknown");
        // For paths ending with /, the last component is actually "to", not "unknown"
        assert_eq!(get_filename("/path/to/"), "to");
    }

    #[test]
    fn test_is_symlink() {
        // Test regular file
        let temp_file = NamedTempFile::new().unwrap();
        assert!(!is_symlink(temp_file.path()));

        // Test nonexistent file
        assert!(!is_symlink("/nonexistent/file.log"));
    }

    #[test]
    fn test_resolve_symlink() {
        // Test with regular file (not a symlink)
        let temp_file = NamedTempFile::new().unwrap();
        let result = resolve_symlink(temp_file.path());
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to read symlink"));

        // Test with nonexistent file
        let result = resolve_symlink("/nonexistent/file.log");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to read symlink"));
    }

    #[test]
    fn test_read_file_from_end_coverage_line_11() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "Line 1").unwrap();
        writeln!(temp_file, "Line 2").unwrap();
        writeln!(temp_file, "Line 3").unwrap();
        temp_file.flush().unwrap();

        // Test read_file_from_end to cover line 11 (BufReader::new)
        let result = read_file_from_end(temp_file.path(), 1024);
        assert!(result.is_ok());
        let lines = result.unwrap();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "Line 1");
        assert_eq!(lines[1], "Line 2");
        assert_eq!(lines[2], "Line 3");
    }

    #[test]
    fn test_get_file_size_coverage_line_32() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "Test content").unwrap();
        temp_file.flush().unwrap();

        // Test get_file_size to cover line 32 (metadata.len())
        let result = get_file_size(temp_file.path());
        assert!(result.is_ok());
        let size = result.unwrap();
        assert!(size > 0);
    }

    #[test]
    fn test_is_file_rotated_coverage_line_38() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "Test content").unwrap();
        temp_file.flush().unwrap();

        // Test is_file_rotated to cover line 38 (current_size < previous_size)
        let result = is_file_rotated(temp_file.path(), 1000); // Previous size larger than current
        assert!(result.is_ok());
        let is_rotated = result.unwrap();
        assert!(is_rotated); // File is smaller than previous size
    }

    #[test]
    fn test_format_file_size_coverage_line_68() {
        // Test format_file_size to cover line 68 (unit_index < UNITS.len() - 1)
        let result = format_file_size(1536); // 1.5 KB
        assert_eq!(result, "1.5 KB");

        // Test with larger size to trigger the while loop
        let result = format_file_size(1048576); // 1 MB
        assert_eq!(result, "1.0 MB");
    }

    #[test]
    fn test_resolve_symlink_coverage_line_112() {
        // Test resolve_symlink to cover line 112 (resolved.clone())
        let result = resolve_symlink("/nonexistent/file.log");
        assert!(result.is_err());
        // The error should contain the context message
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Failed to read symlink"));
    }
}