hid_io_core/
logging.rs

1/* Copyright (C) 2020-2022 by Jacob Alexander
2 *
3 * This file is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This file is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/// Logging functions
18/// Handles general logging setup and other special functions
19use flexi_logger::{FileSpec, Logger};
20use std::env;
21
22/// Logging setup
23pub fn setup_logging() -> Result<(), std::io::Error> {
24    match Logger::try_with_env_or_str("")
25        .unwrap()
26        .log_to_file(FileSpec::default().directory(env::temp_dir()))
27        //.format(flexi_logger::colored_detailed_format)
28        .format(flexi_logger::colored_default_format)
29        .format_for_files(flexi_logger::colored_detailed_format)
30        .rotate(
31            flexi_logger::Criterion::Size(1_000_000),
32            flexi_logger::Naming::Numbers,
33            flexi_logger::Cleanup::KeepLogFiles(5),
34        )
35        .duplicate_to_stderr(flexi_logger::Duplicate::All)
36        .start()
37    {
38        Err(msg) => Err(std::io::Error::new(
39            std::io::ErrorKind::Other,
40            format!("Could not start logger {msg}"),
41        )),
42        Ok(_) => {
43            info!("-------------------------- HID-IO Core starting! --------------------------");
44            info!("Log location -> {:?}", env::temp_dir());
45            Ok(())
46        }
47    }
48}
49
50/// Lite logging setup
51pub fn setup_logging_lite() -> Result<(), std::io::Error> {
52    match Logger::try_with_env_or_str("")
53        .unwrap()
54        .format(flexi_logger::colored_default_format)
55        .format_for_files(flexi_logger::colored_detailed_format)
56        .duplicate_to_stderr(flexi_logger::Duplicate::All)
57        .start()
58    {
59        Err(msg) => Err(std::io::Error::new(
60            std::io::ErrorKind::Other,
61            format!("Could not start logger {msg}"),
62        )),
63        Ok(_) => Ok(()),
64    }
65}