Skip to content

Commit

Permalink
[0.11.1] Add option to write windows line endings, rather than a plai…
Browse files Browse the repository at this point in the history
…n `\n`
  • Loading branch information
emabee committed Mar 6, 2019
1 parent 72c3850 commit 2757e79
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.11.1] - 2019-03-06

Add option to write windows line endings, rather than a plain `\n`.

## [0.11.0] - 2019-03-02

Add options to cleanup rotated log files, by deleting and/or zipping older files.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flexi_logger"
version = "0.11.0"
version = "0.11.1"
authors = ["emabee <[email protected]>"]
edition = "2018"
license = "MIT/Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Add flexi_logger to the dependencies section in your project's `Cargo.toml`, wit

```toml
[dependencies]
flexi_logger = "0.11"
flexi_logger = "^0.11.1"
log = "0.4"
```

or, if you want to use the optional features, with

```toml
[dependencies]
flexi_logger = { version = "0.11", features = ["specfile", "ziplogs"] }
flexi_logger = { version = "^0.11.1", features = ["specfile", "ziplogs"] }
log = "0.4"
```

Expand Down
8 changes: 7 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Logger {
/// Makes the logger write no logs at all.
///
/// This can be useful when you want to run tests of your programs with all log-levels active
/// to ensure they log calls which are normally not active will not cause
/// to ensure the log calls which are normally not active will not cause
/// undesired side-effects when activated
/// (note that the log macros prevent arguments of inactive log-calls from being evaluated).
pub fn do_not_log(mut self) -> Logger {
Expand Down Expand Up @@ -458,6 +458,12 @@ impl Logger {
self.other_writers.insert(name.into(), writer);
self
}

/// Use Windows line endings, rather than just `\n`.
pub fn use_windows_line_ending(mut self) -> Logger {
self.flwb = self.flwb.use_windows_line_ending();
self
}
}

/// Defines the strateygy for handling of older log files.
Expand Down
16 changes: 15 additions & 1 deletion src/writers/file_log_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct FileLogWriterConfig {
rotate_over_size: Option<u64>,
cleanup: Cleanup,
create_symlink: Option<String>,
use_windows_line_ending: bool,
}
impl FileLogWriterConfig {
// Factory method; uses the same defaults as Logger.
Expand All @@ -47,6 +48,7 @@ impl FileLogWriterConfig {
cleanup: Cleanup::Never,
rotate_over_size: None,
create_symlink: None,
use_windows_line_ending: false,
}
}
}
Expand Down Expand Up @@ -161,6 +163,12 @@ impl FileLogWriterBuilder {
self
}

/// Use Windows line endings, rather than just `\n`.
pub fn use_windows_line_ending(mut self) -> FileLogWriterBuilder {
self.config.use_windows_line_ending = true;
self
}

/// Produces the FileLogWriter.
pub fn instantiate(mut self) -> Result<FileLogWriter, FlexiLoggerError> {
// make sure the folder exists or create it
Expand Down Expand Up @@ -292,6 +300,7 @@ struct FileLogWriterState {
// None if no rotation is desired, or else Some(idx) where idx is the highest existing rotate_idx
rotate_idx: Option<u32>,
path: String,
line_ending: &'static [u8],
}
impl FileLogWriterState {
// If rotate, the logger writes into a file with infix `_rCURRENT`.
Expand All @@ -313,6 +322,11 @@ impl FileLogWriterState {
path,
written_bytes,
rotate_idx,
line_ending: if config.use_windows_line_ending {
b"\r\n"
} else {
b"\n"
},
})
}

Expand Down Expand Up @@ -594,7 +608,7 @@ impl LogWriter for FileLogWriter {
}

(self.config.format)(state, record)?;
state.write_all(b"\n")
state.write_all(state.line_ending)
}

#[inline]
Expand Down
55 changes: 55 additions & 0 deletions tests/test_windows_line_ending.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use flexi_logger::{detailed_format, Logger, ReconfigurationHandle};
use log::*;

#[test]
fn test_mods() {
let handle: ReconfigurationHandle = Logger::with_env_or_str(
"info, test_windows_line_ending::mymod1=debug, test_windows_line_ending::mymod2=error",
)
.format(detailed_format)
.log_to_file()
.use_windows_line_ending()
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));

error!("This is an error message");
warn!("This is a warning");
info!("This is an info message");
debug!("This is a debug message - you must not see it!");
trace!("This is a trace message - you must not see it!");

mymod1::test_traces();
mymod2::test_traces();

handle.validate_logs(&[
("ERROR", "test_windows_line_ending", "error"),
("WARN", "test_windows_line_ending", "warning"),
("INFO", "test_windows_line_ending", "info"),
("ERROR", "test_windows_line_ending", "error"),
("WARN", "test_windows_line_ending", "warning"),
("INFO", "test_windows_line_ending", "info"),
("DEBUG", "test_windows_line_ending", "debug"),
("ERROR", "test_windows_line_ending", "error"),
]);
}

mod mymod1 {
use log::*;
pub fn test_traces() {
error!("This is an error message");
warn!("This is a warning");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message - you must not see it!");
}
}
mod mymod2 {
use log::*;
pub fn test_traces() {
error!("This is an error message");
warn!("This is a warning - you must not see it!");
info!("This is an info message - you must not see it!");
debug!("This is a debug message - you must not see it!");
trace!("This is a trace message - you must not see it!");
}
}

0 comments on commit 2757e79

Please sign in to comment.