multithread/multithread.rs
1/*
2Astrolog, a logging framework for Rust
3Copyright (C) 2019 Alessandro Pellizzari
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20extern crate astrolog;
21
22use std::env;
23use std::thread;
24
25use astrolog::handler::console::ConsoleHandler;
26use astrolog::prelude::*;
27
28fn main() {
29 let logger = Logger::new()
30 .with_global("OS", env::consts::OS)
31 .with_handler(ConsoleHandler::new().with_levels_range(Level::Info, Level::Emergency))
32 .into_arc();
33
34 let ths = (0..10)
35 .map(|i| {
36 let logger = logger.clone();
37 thread::spawn(move || {
38 logger
39 .with("line", line!())
40 .with("file", file!())
41 .with("thread", i)
42 .info("A simple info logging");
43
44 if i % 2 == 0 {
45 logger.with("thread", i).emergency("This thread did something bad");
46 }
47 })
48 })
49 .collect::<Vec<_>>();
50
51 for t in ths {
52 t.join().expect("Thread join failed");
53 }
54}