2 releases
0.1.1 | Oct 20, 2021 |
---|---|
0.1.0 | Oct 17, 2021 |
#10 in #txt-file
50KB
126 lines
A log.txt writer
Table of contents
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:
use log_file;
Example
Log types
- custom:
structure example (custom)
Here an example for log_file::custom
:
// 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 : log_file
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 : log_file
[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 pythagorean_theorem method. Here is the implementation without log:
fn pythagorean_theorem(log : &mut log, a : f64, b : f64) -> f64 {
let a_sq = a*a;
let b_sq = b*b;
let c = (a_sq + b_sq).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:
fn pythagorean_theorem(log : &mut Log, a : f64, b : f64) -> f64 {
log.add_str("pythagorean_theorem - step 1","a*a");
let a_sq = a*a;
log.add_str("pythagorean_theorem - step 2","b*b");
let b_sq = b*b;
log.add_str("pythagorean_theorem - step 3","(a_sq + b_sq) * (a_sq + b_sq)");
let c = (a_sq + b_sq).sqrt();
return c;
}
If we use this function in main()
, it looks like this:
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 pythagorean_theorem(log : &mut Log, a : f64, b : f64) -> f64 {
// snipped //
}
The log.txt
file now contains the following text:
pythagorean_theorem - step 1 : a*a
pythagorean_theorem - step 2 : b*b
pythagorean_theorem - step 3 : (a_sq + b_sq) * (a_sq + b_sq)
Features
Requirements
None