rich_strong_lib/
config.rs

1use std::{fs::File, io::Write};
2
3use backtrace::Backtrace;
4use configparser::ini::Ini;
5
6use crate::log::Log;
7
8pub struct Config;
9
10static mut WRITE_LOG: bool = false;
11static mut MININUM_WORKER: u8 = 0;
12static mut WORKING_CALLER: u8 = 0;
13static mut PROXY_SERVER_ADDR : String = String::new();
14
15impl Config {
16    pub fn init() -> (String,String) {
17        Config::set_panic_hook();
18        let r = Config::load();
19        let app = r.get("common","app").unwrap();
20        let http = r.get("common","http").unwrap();
21        let write_log = r.getbool("common", "write_log").unwrap().unwrap();
22        if write_log {
23            Config::turn_on()
24        }
25        (app,http)
26    }
27
28    pub fn init_client_side_setting() {
29        let r = Config::load();
30        let n:u8 = r.getuint("client","minimum_worker").unwrap().unwrap() as u8;
31        let addr = r.get("client","proxy_server_addr").unwrap();
32        Config::set_minimum_worker(n);
33        Config::set_proxy_server_addr(addr);
34    }
35
36    
37}
38
39//[Private]
40impl Config {
41    fn load() -> Ini {
42        let mut config = Ini::new();
43        config.load("theshy.ini".to_string()).unwrap();
44        config
45    }
46
47    fn set_panic_hook() {
48        std::panic::set_hook(Box::new(|_| {
49            let bt = Backtrace::new();
50            let mut f = File::options().append(true).open(Log::panic_file()).unwrap();
51            f.write(format!("{:?}",bt).as_bytes()).unwrap();
52        }));
53    }
54}
55
56impl Config {
57    pub fn turn_on() {
58        unsafe{ WRITE_LOG = true }
59    }
60
61    pub fn log_off() -> bool {
62        unsafe{ if !WRITE_LOG { return true; } }
63        false
64    }
65
66    pub fn working_caller_count() -> u8 {
67        unsafe{ WORKING_CALLER }
68    }
69
70    pub fn set_working_caller_count(n:u8) {
71        unsafe{ WORKING_CALLER = n; }
72    }
73
74    pub fn minimum_worker() -> u8 {
75        unsafe{ MININUM_WORKER }
76    }
77
78    pub fn set_minimum_worker(n:u8) {
79        assert!(n < u8::MAX/4);
80        unsafe{ MININUM_WORKER = n; }
81    }
82
83    pub fn proxy_server_addr() -> String {
84        unsafe{ PROXY_SERVER_ADDR.clone() }
85    }
86
87    pub fn set_proxy_server_addr(str:String) {
88        unsafe{ PROXY_SERVER_ADDR = str }
89    }
90
91}