A log.txt writer


log_file at crates.io

Table of contents

  1. Instalation
  2. Example
  3. Features
  4. Requirements

Instalation

update Cargo.toml file

Add log_file to your dependencies: [dependencies] log_file = "0.1.1"

import it

Import as much as you like from the log_file lib: rust use log_file;

Example

Log types

structure example (custom)

Here an example for log_file::custom: ``rust // import everything from thecustom` module use log_file::custom::*;

fn main() { // create the log let mut log = Log::new(false, ':');

// using it
log.add_str("Programmer","SnefDen");
log.add_str("crate name","log_file");
log.add_str("crate version","0.1.1");

// save log in log.txt
log.save("log.txt");

} The `log.txt` file then should contain the following text: Programmer : SnefDen crate name : logfile crate version : 0.1.1 If the time stamp is on (`let mut log = Log::new(true, ':');`), the result looks like this for example: [0:0:0:100] Programmer : SnefDen [0:0:0:250] crate name : logfile [0:0:0:250] crate version : 0.1.1 So the structure is the following: time(optional) title separator content The time is written in the following format and started if the log is created: s:ms:µs:ns

s = seconds ms = milliseconds (0.001s) µs = mikroseconds (0.001ms) ns = nanoseconds (0.001µs) ```

second example (custom)

For this example we use the pythagoreantheorem method. Here is the implementation without log: ```rust fn pythagoreantheorem(log : &mut log, a : f64, b : f64) -> f64 { let asq = a*a; let bsq = b*b; let c = (asq + bsq).sqrt();

return c;

} `` This time we don't create a new log. Instead we change our header, so that we can use an existing one (and don't forget to make it mutable). At last we update the log, based on the steps in thepythagorean_theorem()` method. Now the method should look like this:

```rust fn pythagoreantheorem(log : &mut Log, a : f64, b : f64) -> f64 { log.addstr("pythagoreantheorem - step 1","a*a"); let asq = aa; log.add_str("pythagorean_theorem - step 2","bb"); let bsq = b*b; log.addstr("pythagoreantheorem - step 3","(asq + bsq) * (asq + bsq)"); let c = (asq + b_sq).sqrt();

return c;

} If we use this function in `main()`, it looks like this: rust use log_file::custom::*;

fn main() { // create log let mut log = Log::new(false, ':'));

// call pythagorean_theorem() of 2 and 3
println!("{}",pythagorean_theorem(&mut log, 2, 3));

// save log in `log.txt`
log.save("log.txt");

}

fn pythagoreantheorem(log : &mut Log, a : f64, b : f64) -> f64 { // snipped // } The `log.txt` file now contains the following text: pythagoreantheorem - step 1 : aa pythagorean_theorem - step 2 : bb pythagoreantheorem - step 3 : (asq + bsq) * (asq + b_sq) ```

Features