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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! # Pipe Logger
//! Stores, rotates, compresses process logs.

extern crate clap;
extern crate byte_unit;
extern crate path_absolutize;
extern crate chrono;
extern crate regex;
extern crate xz2;

use std::env;
use std::path::{Path, PathBuf};
use std::fs::{self, OpenOptions, File};
use std::io::{self, Read, Write};
use std::thread;

use regex::Regex;

use byte_unit::Byte;
use byte_unit::ByteError;

use path_absolutize::*;

use chrono::prelude::*;

use xz2::write::XzEncoder;

use clap::{App, Arg};

// TODO -----Config START-----

const APP_NAME: &str = "Pipe Logger";
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_PKG_AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
const DEFAULT_LOG_NAME: &str = "logfile.log";
const BUFFER_SIZE: usize = 4096 * 4;

#[derive(Debug)]
pub enum RotateMethod {
    FileSize(u64)
}

#[derive(Debug)]
pub struct Config {
    pub rotate: Option<RotateMethod>,
    pub count: Option<usize>,
    pub log_path: String,
    pub compress: bool,
    pub err: bool,
}

impl Config {
    pub fn from_cli() -> Result<Config, String> {
        let arg0 = env::args().next().unwrap();
        let arg0 = Path::new(&arg0).file_stem().unwrap().to_str().unwrap();

        let default_log_path = {
            let path = Path::new(&arg0);
            match path.parent() {
                Some(p) => {
                    Path::join(p, DEFAULT_LOG_NAME)
                }
                None => {
                    PathBuf::from(DEFAULT_LOG_NAME)
                }
            }
        };

        let examples = vec![
            "/path/to/out.log                        # Stores log into /path/to/out.log",
            "/path/to/out.log -r 10M                 # The same as above, plus if its size is over than 10MB, it will be rotated and renamed.",
            "/path/to/out.log -r 10M -c 4            # The same as above, plus the max count of log files is 4. The oldest ones will be removed when the quota is exhausted.",
            "/path/to/out.log -r 10M -c 4 --compress # The same as above, plus the rotated log files are compressed by xz.",
        ];

        let matches = App::new(APP_NAME)
            .version(CARGO_PKG_VERSION)
            .author(CARGO_PKG_AUTHORS)
            .about(format!("Stores, rotates, compresses process logs.\n\nEXAMPLES:\n{}", examples.iter()
                .map(|e| format!("  {} {}\n", arg0, e))
                .collect::<Vec<String>>()
                .concat()
            ).as_str()
            )
            .arg(Arg::with_name("ROTATE")
                .long("rotate")
                .short("r")
                .help("Rotates the log file.")
                .takes_value(true)
            )
            .arg(Arg::with_name("COUNT")
                .long("count")
                .short("c")
                .help("Assigns the max count of log files.")
                .takes_value(true)
            )
            .arg(Arg::with_name("COMPRESS")
                .long("--compress")
                .help("Compresses the rotated log files.")
            )
            .arg(Arg::with_name("ERR")
                .long("--err")
                .help("Re-outputs logs through stderr.")
            )
            .arg(Arg::with_name("LOG_PATH")
                .required(true)
                .help("The path that you want to store your logs.")
                .takes_value(true)
                .default_value(default_log_path.to_str().unwrap())
            )
            .after_help("Enjoy it! https://magiclen.org")
            .get_matches();

        let rotate = match matches.value_of("ROTATE") {
            Some(r) => {
                match Byte::from_string(r) {
                    Ok(byte) => {
                        let b = byte.get_bytes();
                        if b > <u64>::max_value() as u128 {
                            return Err("The file size of rotation is too large.".to_string());
                        } else if b < 2 {
                            return Err("The file size of rotation is too small.".to_string());
                        }
                        Some(RotateMethod::FileSize(b as u64))
                    }
                    Err(err) => {
                        match err {
                            ByteError::ParseError => {
                                // TODO other methods
                                return Err("You need to input a file size for log rotation.".to_string());
                            }
                            ByteError::ValueIncorrect => {
                                return Err("The file size of rotation is incorrect.".to_string());
                            }
                            ByteError::UnitIncorrect => {
                                return Err("The unit of the file size is incorrect.".to_string());
                            }
                        }
                    }
                }
            }
            None => {
                None
            }
        };

        let count = if rotate.is_some() {
            match matches.value_of("COUNT") {
                Some(c) => {
                    match c.parse::<usize>() {
                        Ok(c) => {
                            if c < 1 {
                                return Err("The count of log files is too small.".to_string());
                            }
                            Some(c)
                        }
                        Err(err) => {
                            return Err(err.to_string());
                        }
                    }
                }
                None => {
                    None
                }
            }
        } else {
            None
        };

        let compress = rotate.is_some() && matches.is_present("COMPRESS");

        let err = matches.is_present("ERR");

        let log_path = match Path::new(matches.value_of("LOG_PATH").unwrap()).absolutize() {
            Ok(p) => {
                p
            }
            Err(err) => {
                return Err(err.to_string());
            }
        };

        if log_path.exists() {
            if log_path.is_dir() {
                return Err(format!("`{}` is a directory.", log_path.to_str().unwrap()));
            }
            match fs::metadata(&log_path) {
                Ok(m) => {
                    let p = m.permissions();
                    if p.readonly() {
                        return Err(format!("`{}` is readonly.", log_path.to_str().unwrap()));
                    }
                }
                Err(err) => {
                    return Err(err.to_string());
                }
            }

            if rotate.is_some() {
                match log_path.parent() {
                    Some(parent) => {
                        match fs::metadata(&parent) {
                            Ok(m) => {
                                let p = m.permissions();
                                if p.readonly() {
                                    return Err(format!("`{}` is readonly.", parent.to_str().unwrap()));
                                }
                            }
                            Err(err) => {
                                return Err(err.to_string());
                            }
                        }
                    }
                    None => {
                        panic!("impossible");
                    }
                }
            }
        } else {
            match log_path.parent() {
                Some(parent) => {
                    match fs::metadata(&parent) {
                        Ok(m) => {
                            let p = m.permissions();
                            if p.readonly() {
                                return Err(format!("`{}` is readonly.", parent.to_str().unwrap()));
                            }
                        }
                        Err(err) => {
                            return Err(err.to_string());
                        }
                    }
                }
                None => {
                    return Err(format!("`{}`'s parent does not exist.", log_path.to_str().unwrap()));
                }
            };
        }

        let log_path = log_path.to_str().unwrap().to_string();

        Ok(
            Config {
                rotate,
                count,
                log_path,
                compress,
                err,
            }
        )
    }
}

// TODO -----Config END-----

pub fn run(config: Config) -> Result<i32, String> {
    let file_path = Path::new(&config.log_path);

    let file_parent = file_path.parent().unwrap();

    let mut file_size = match fs::metadata(&file_path) {
        Ok(m) => {
            m.len()
        }
        Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
            0
        }
        Err(err) => {
            return Err(err.to_string());
        }
    };

    let file_name = Path::new(&file_path).file_name().unwrap().to_str().unwrap();

    let file_name_point_index = match file_name.rfind(".") {
        Some(index) => {
            index
        }
        None => {
            file_name.len()
        }
    };

    let file_name_without_extension = Path::new(&file_path).file_stem().unwrap().to_str().unwrap();

    let mut rotated_log_file_names = Vec::new();

    let re = Regex::new("^-[1-2][0-9]{3}(-[0-5][0-9]){5}-[0-9]{3}$").unwrap(); // -%Y-%m-%d-%H-%M-%S + $.3f

    for entry in file_parent.read_dir().unwrap().filter_map(|entry| entry.ok()) {
        let rotated_log_file_path = entry.path();

        if !rotated_log_file_path.is_file() {
            continue;
        }

        let rotated_log_file_name = Path::new(&rotated_log_file_path).file_name().unwrap().to_str().unwrap();

        if !rotated_log_file_name.starts_with(file_name_without_extension) {
            continue;
        }

        let rotated_log_file_name_point_index = match rotated_log_file_name.rfind(".") {
            Some(index) => {
                index
            }
            None => {
                rotated_log_file_name.len()
            }
        };

        if rotated_log_file_name_point_index < file_name_point_index + 24 { // -%Y-%m-%d-%H-%M-%S + $.3f
            continue;
        }

        let file_name_without_extension_len = file_name_without_extension.len();

        if !re.is_match(&rotated_log_file_name[file_name_without_extension_len..file_name_without_extension_len + 24]) {  // -%Y-%m-%d-%H-%M-%S + $.3f
            continue;
        }

        let ext = &rotated_log_file_name[rotated_log_file_name_point_index..];

        if ext.eq(&file_name[file_name_point_index..]) {
            rotated_log_file_names.push(rotated_log_file_name.to_string());
        } else if ext.eq(".xz") && rotated_log_file_name[..rotated_log_file_name_point_index].ends_with(&file_name[file_name_point_index..]) {
            rotated_log_file_names.push(rotated_log_file_name[..rotated_log_file_name_point_index].to_string());
        }
    }

    rotated_log_file_names.sort();

    let mut file = match OpenOptions::new()
        .create(true)
        .write(true)
        .append(true)
        .open(&file_path) {
        Ok(f) => {
            f
        }
        Err(err) => {
            return Err(err.to_string());
        }
    };

    let mut input = String::new();
    let stdin = &io::stdin();
    loop {
        match stdin.read_line(&mut input) {
            Ok(n) => {
                if n == 0 {
                    break;
                }
                if config.err {
                    eprint!("{}", input);
                } else {
                    print!("{}", input);
                }

                match file.write(input.as_bytes()) {
                    Ok(n) => {
                        if n != input.len() {
                            return Err("The space is not enough.".to_string());
                        }
                    }
                    Err(err) => {
                        return Err(err.to_string());
                    }
                }

                file_size += input.len() as u64;

                if let Some(rotate) = &config.rotate {
                    match rotate {
                        RotateMethod::FileSize(size) => {
                            if file_size >= *size {
                                let utc: DateTime<Utc> = Utc::now();
                                let timestamp = utc.format("%Y-%m-%d-%H-%M-%S").to_string();
                                let millisecond = utc.format("%.3f").to_string();

                                if let Err(err) = file.flush() {
                                    return Err(err.to_string());
                                }
                                if let Err(err) = file.sync_all() {
                                    return Err(err.to_string());
                                }
                                drop(file);

                                let rotated_log_file_name = format!("{}-{}-{}{}", file_name_without_extension, timestamp, &millisecond[1..], &file_name[file_name_point_index..]);

                                let rotated_log_file = Path::join(file_parent, Path::new(&rotated_log_file_name));

                                if let Err(err) = fs::copy(&file_path, &rotated_log_file) {
                                    return Err(err.to_string());
                                }

                                if config.compress {
                                    let rotated_log_file_name_compressed = format!("{}.xz", rotated_log_file_name);
                                    let rotated_log_file_compressed = Path::join(file_parent, Path::new(&rotated_log_file_name_compressed));
                                    let config_err = config.err;

                                    thread::spawn(move || {
                                        match File::create(&rotated_log_file_compressed) {
                                            Ok(file_w) => {
                                                match File::open(&rotated_log_file) {
                                                    Ok(mut file_r) => {
                                                        let mut compressor = XzEncoder::new(file_w, 9);
                                                        let mut buffer = [0u8; BUFFER_SIZE];
                                                        loop {
                                                            match file_r.read(&mut buffer) {
                                                                Ok(c) => {
                                                                    if c == 0 {
                                                                        if let Err(_) = fs::remove_file(&rotated_log_file) {}
                                                                        break;
                                                                    }
                                                                    match compressor.write(&buffer[..c]) {
                                                                        Ok(cc) => {
                                                                            if c != cc {
                                                                                if config_err {
                                                                                    println!("The space is not enough.");
                                                                                } else {
                                                                                    eprintln!("The space is not enough.");
                                                                                }
                                                                                break;
                                                                            }
                                                                        }
                                                                        Err(err) => {
                                                                            if config_err {
                                                                                println!("{}", err.to_string());
                                                                            } else {
                                                                                eprintln!("{}", err.to_string());
                                                                            }
                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                                Err(err) => {
                                                                    if config_err {
                                                                        println!("{}", err.to_string());
                                                                    } else {
                                                                        eprintln!("{}", err.to_string());
                                                                    }
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    Err(err) => {
                                                        if config_err {
                                                            println!("{}", err.to_string());
                                                        } else {
                                                            eprintln!("{}", err.to_string());
                                                        }
                                                    }
                                                }
                                            }
                                            Err(err) => {
                                                if config_err {
                                                    println!("{}", err.to_string());
                                                } else {
                                                    eprintln!("{}", err.to_string());
                                                }
                                            }
                                        };
                                    });
                                }

                                rotated_log_file_names.push(rotated_log_file_name);

                                if let Some(count) = config.count {
                                    while rotated_log_file_names.len() >= count {
                                        let rotated_log_file_name = rotated_log_file_names.remove(0);
                                        if let Err(_) = fs::remove_file(Path::join(file_parent, Path::new(&rotated_log_file_name))) {}

                                        let p_compressed_name = format!("{}.xz", rotated_log_file_name);

                                        let p_compressed = Path::join(file_parent, Path::new(&p_compressed_name));
                                        if let Err(_) = fs::remove_file(&p_compressed) {}
                                    }
                                }

                                file = match OpenOptions::new()
                                    .write(true)
                                    .truncate(true)
                                    .open(&file_path) {
                                    Ok(f) => {
                                        f
                                    }
                                    Err(err) => {
                                        return Err(err.to_string());
                                    }
                                };
                            }
                        }
                    }
                }
            }
            Err(err) => {
                return Err(err.to_string());
            }
        }
        input.clear();
    }

    Ok(0)
}