pxn_crate/
lib.rs

1//#==============================================================================
2//# Copyright (c) 2022 Mattsoft/PoiXson
3//# <https://mattsoft.net> <https://poixson.com>
4//# Released under the AGPL 3.0
5//#
6//# Description: Common library for PoiXson projects
7//#
8//# This program is free software: you can redistribute it and/or modify
9//# it under the terms of the GNU Affero General Public License as
10//# published by the Free Software Foundation, either version 3 of the
11//# License, or (at your option) any later version.
12//#
13//# This program is distributed in the hope that it will be useful,
14//# but WITHOUT ANY WARRANTY; without even the implied warranty of
15//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//# GNU Affero General Public License for more details.
17//#
18//# You should have received a copy of the GNU Affero General Public License
19//# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20//#==============================================================================
21
22use log::Level;
23use std::io::Write;
24
25pub mod utils;
26pub mod file_finder;
27
28
29
30pub fn stdout_flush() {
31	std::io::stdout().flush().unwrap();
32}
33
34
35
36pub fn print_type_of<T>(_: T) {
37	println!("{}", std::any::type_name::<T>())
38}
39
40
41
42pub fn pxn_init_logging(verbose: i8, quiet: i8) {
43	let verbosity: i8 = verbose - quiet;
44	{
45		let lvl = match verbosity {
46			// -qq
47			-1=> Level::Error.to_level_filter(),
48			// -q
49			0 => Level::Warn.to_level_filter(),
50			// -v
51			1 => Level::Info.to_level_filter(),
52			// -vv
53			2 => Level::Debug.to_level_filter(),
54			_ => {
55				// -qqq
56				if verbosity < -1 {
57					log::LevelFilter::Off
58				} else
59				// -vvv
60				if verbosity > 2 {
61					Level::Trace.to_level_filter()
62				} else {
63					Level::Warn.to_level_filter()
64				}
65			},
66		};
67		env_logger::Builder::new()
68			.filter_level(lvl)
69			.init();
70	}
71	log_panics::init();
72}
73
74
75
76pub fn get_timestamp() -> String {
77	chrono::Local::now()
78		.to_rfc2822()
79}