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
//! Unified error logging system that respects verbosity levels
//!
//! This module provides centralized error logging that:
//! - Always saves errors to a report file (unless --no-report-errors)
//! - Shows errors on console with -v
//! - Shows all operations with -vv (no progress bar)
//! - Integrates with the existing ErrorReporter
use crate::error_report::{ErrorReportHandle, ErrorReporter};
use crate::options::SyncOptions;
use crate::sync_stats::SyncStats;
use anyhow::Result;
use chrono::Local;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
/// Unified error logger that handles console output and file logging
pub struct ErrorLogger {
options: SyncOptions,
error_reporter: Option<ErrorReporter>,
error_handle: Option<ErrorReportHandle>,
operation_log: Arc<Mutex<Vec<String>>>,
}
impl ErrorLogger {
/// Create a new error logger
pub fn new(options: SyncOptions, source: &Path, destination: &Path) -> Self {
let (error_reporter, error_handle) = if options.no_report_errors {
(None, None)
} else {
let reporter = ErrorReporter::new(source, destination, &options);
let handle = reporter.get_handle();
(Some(reporter), Some(handle))
};
Self {
options,
error_reporter,
error_handle,
operation_log: Arc::new(Mutex::new(Vec::new())),
}
}
/// Get a handle for error logging that can be cloned and shared
pub fn get_handle(&self) -> ErrorLogHandle {
ErrorLogHandle {
verbose: self.options.verbose,
error_handle: self.error_handle.clone(),
operation_log: Arc::clone(&self.operation_log),
}
}
/// Log an error (saves to file and optionally prints to console)
pub fn log_error(&self, path: &Path, message: &str, operation: &str) {
// Always save to error report (unless disabled)
if let Some(ref handle) = self.error_handle {
let full_message = format!("{operation}: {message}");
handle.add_error(path, &full_message);
}
// Never print errors to console during execution - they break the progress bar
// Errors are always saved to the error report file
// Log operation if verbose >= 2
if self.options.verbose >= 2 {
self.log_operation(&format!(
"ERROR {}: {} - {}",
operation,
path.display(),
message
));
}
}
/// Log a warning (saves to file and optionally prints to console)
pub fn log_warning(&self, path: &Path, message: &str, operation: &str) {
// Always save to error report (unless disabled)
if let Some(ref handle) = self.error_handle {
let full_message = format!("{operation}: {message}");
handle.add_warning(path, &full_message);
}
// Never print warnings to console during execution - they break the progress bar
// Warnings are always saved to the error report file
// Log operation if verbose >= 2
if self.options.verbose >= 2 {
self.log_operation(&format!(
"WARNING {}: {} - {}",
operation,
path.display(),
message
));
}
}
/// Log a successful operation (only shown with -vv)
pub fn log_success(&self, path: &Path, operation: &str) {
if self.options.verbose >= 2 {
let timestamp = Local::now().format("%H:%M:%S");
println!("[{}] {} {}", timestamp, operation, path.display());
self.log_operation(&format!("{}: {}", operation, path.display()));
}
}
/// Log any operation (for -vv mode)
pub fn log_operation(&self, message: &str) {
if self.options.verbose >= 2 {
if let Ok(mut log) = self.operation_log.lock() {
log.push(format!("[{}] {}", Local::now().format("%H:%M:%S"), message));
}
}
}
/// Should we display progress bars?
pub fn should_show_progress(&self) -> bool {
// Show progress bar with --progress (unless -vv)
self.options.verbose < 2 && self.options.show_progress
}
/// Write the error report file if needed
pub fn finalize(&self) -> Result<Option<PathBuf>> {
if let Some(ref reporter) = self.error_reporter {
reporter.write_report()
} else {
Ok(None)
}
}
/// Write the error report file with details from SyncStats
pub fn finalize_with_stats(&self, stats: &SyncStats) -> Result<Option<PathBuf>> {
if let Some(ref reporter) = self.error_reporter {
// Add all error details from SyncStats to the error reporter
for error_detail in stats.get_error_details() {
reporter.add_error(
&error_detail.path,
&format!("{}: {}", error_detail.operation, error_detail.message),
);
}
// Add all structured errors from SyncStats
// Add all structured errors from SyncStats
for structured_error in stats.get_structured_errors() {
// Extract path from error if available
let path = match &structured_error.error {
crate::error::RoboSyncError::Io { path: Some(p), .. } => p.clone(),
crate::error::RoboSyncError::Permission { path, .. } => path.clone(),
crate::error::RoboSyncError::NotFound { path } => path.clone(),
crate::error::RoboSyncError::SyncFailed {
source_path: Some(p),
..
} => p.clone(),
crate::error::RoboSyncError::SyncFailed {
dest_path: Some(p), ..
} => p.clone(),
crate::error::RoboSyncError::DeltaFailed { file_path, .. } => file_path.clone(),
crate::error::RoboSyncError::ChecksumMismatch { path, .. } => path.clone(),
crate::error::RoboSyncError::PatternError { path: Some(p), .. } => p.clone(),
_ => PathBuf::from("unknown"),
};
reporter.add_error(
&path,
&format!("{}: {}", structured_error.context, structured_error.error),
);
}
reporter.write_report()
} else {
Ok(None)
}
}
/// Get error count
pub fn error_count(&self) -> usize {
self.error_reporter
.as_ref()
.map(|r| r.error_count())
.unwrap_or(0)
}
/// Get warning count
pub fn warning_count(&self) -> usize {
self.error_reporter
.as_ref()
.map(|r| r.warning_count())
.unwrap_or(0)
}
}
/// Handle for error logging that can be cloned and shared across threads
#[derive(Clone)]
pub struct ErrorLogHandle {
verbose: u8,
error_handle: Option<ErrorReportHandle>,
operation_log: Arc<Mutex<Vec<String>>>,
}
impl ErrorLogHandle {
/// Log an error (saves to file and optionally prints to console)
pub fn log_error(&self, path: &Path, message: &str, operation: &str) {
// Always save to error report (unless disabled)
if let Some(ref handle) = self.error_handle {
let full_message = format!("{operation}: {message}");
handle.add_error(path, &full_message);
}
// Never print errors to console during execution - they break the progress bar
// Errors are always saved to the error report file
// Log operation if verbose >= 2
if self.verbose >= 2 {
self.log_operation(&format!(
"ERROR {}: {} - {}",
operation,
path.display(),
message
));
}
}
/// Log a warning (saves to file and optionally prints to console)
pub fn log_warning(&self, path: &Path, message: &str, operation: &str) {
// Always save to error report (unless disabled)
if let Some(ref handle) = self.error_handle {
let full_message = format!("{operation}: {message}");
handle.add_warning(path, &full_message);
}
// Never print warnings to console during execution - they break the progress bar
// Warnings are always saved to the error report file
// Log operation if verbose >= 2
if self.verbose >= 2 {
self.log_operation(&format!(
"WARNING {}: {} - {}",
operation,
path.display(),
message
));
}
}
/// Log a successful operation (only shown with -vv)
pub fn log_success(&self, path: &Path, operation: &str) {
if self.verbose >= 2 {
let timestamp = Local::now().format("%H:%M:%S");
println!("[{}] {} {}", timestamp, operation, path.display());
self.log_operation(&format!("{}: {}", operation, path.display()));
}
}
/// Log any operation (for -vv mode)
fn log_operation(&self, message: &str) {
if self.verbose >= 2 {
if let Ok(mut log) = self.operation_log.lock() {
log.push(format!("[{}] {}", Local::now().format("%H:%M:%S"), message));
}
}
}
}