A log.txt writer
Add log_file
to your dependencies:
[dependencies]
log_file = "0.1.1"
Import as much as you like from the log_file lib:
rust
use log_file;
Here an example for log_file::custom
:
``rust
// import everything from the
custom` 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) ```
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 the
pythagorean_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)
```
[x] [custom](https://docs.rs/logfile/0.1.1/logfile/custom/)
[x] [Log](https://docs.rs/logfile/0.1.1/logfile/custom/struct.Log.html)
[x] [LogElement](https://docs.rs/logfile/0.1.1/logfile/custom/struct.LogElement.html)
[x] [trimto1000()](https://docs.rs/logfile/0.1.1/logfile/custom/fn.trimto1000.html)
None