readme/
readme.rs

1struct Resp {
2    body: Vec<u8>,
3    code: u16,
4}
5
6fn http_req(_url: &str) -> Result<Resp, loga::Error> {
7    panic!();
8}
9
10fn launch_satellite(_body: Vec<u8>) -> Result<(), loga::Error> {
11    panic!();
12}
13
14fn shutdown_server() -> Result<(), loga::Error> {
15    panic!();
16}
17
18use loga::{
19    ea,
20    ResultContext,
21    INFO,
22};
23
24fn main1() -> Result<(), loga::Error> {
25    // All errors stacked from this will have "system = main"
26    let log = &loga::Log::new_root(INFO).fork(ea!(system = "main"));
27
28    // Convert the error result to `loga::Error`, add all the logger's attributes, add
29    // a message, and add additional attributes.
30    let res =
31        http_req(
32            "https://example.org",
33        ).stack_context_with(log, "Example.org is down", ea!(process = "get_weather"))?;
34    match launch_satellite(res.body) {
35        Ok(_) => (),
36        Err(e) => {
37            let e = e.stack_context(log, "Failed to launch satellite");
38            if let Err(e2) = shutdown_server() {
39                // Attach incidental errors
40                return Err(e.also(e2.into()));
41            }
42            return Err(e);
43        },
44    }
45    if res.code == 295 {
46        return Err(loga::err("Invalid response"));
47    }
48    log.log(INFO, "Obtained weather");
49    return Ok(());
50}
51
52fn main() {
53    match main1() {
54        Ok(_) => (),
55        Err(e) => loga::fatal(e),
56    }
57}