asn1obj/
logger.rs

1
2use std::env;
3use std::io::{Write};
4use std::fs;
5//use std::io::prelude::*;
6use lazy_static::lazy_static;
7use chrono::{Local,Timelike,Datelike};
8
9
10
11fn _asn1_get_environ_var(envname :&str) -> String {
12	match env::var(envname) {
13		Ok(v) => {
14			format!("{}",v)
15		},
16		Err(_e) => {
17			String::from("")
18		}
19	}
20}
21
22struct LogVar {
23	level :i32,
24	nostderr : bool,
25	wfile : Option<fs::File>,
26}
27
28
29fn asn1_proc_log_init(prefix :&str) -> LogVar {
30	let mut getv :String;
31	let mut retv :i32 = 0;
32	let mut nostderr :bool = false;
33	let mut coptfile :Option<fs::File> = None;
34	let mut key :String;
35
36	key = format!("{}_LEVEL", prefix);
37	getv = _asn1_get_environ_var(&key);
38	if getv.len() > 0 {
39		match getv.parse::<i32>() {
40			Ok(v) => {
41				retv = v;
42			},
43			Err(e) => {
44				retv = 0;
45				eprintln!("can not parse [{}] error[{}]", getv,e);
46			}
47		}
48	}
49
50	key = format!("{}_NOSTDERR",prefix);
51	getv = _asn1_get_environ_var(&key);
52	if getv.len() > 0 {
53		nostderr = true;
54	}
55
56
57
58	key = format!("{}_LOGFILE",prefix);
59	getv = _asn1_get_environ_var(&key);
60	if getv.len() > 0 {
61		let fo = fs::File::create(&getv);
62		if fo.is_err() {
63			eprintln!("can not open [{}]", getv);
64		} else {
65			coptfile = Some(fo.unwrap());
66		}
67	}
68
69	return LogVar {
70		level : retv,
71		nostderr : nostderr,
72		wfile : coptfile,		
73	};
74}
75
76
77lazy_static! {
78	static ref ASN1_OBJ_LOG_LEVEL : LogVar = {
79		asn1_proc_log_init("ASN1OBJ")
80	};
81}
82
83
84pub (crate)  fn asn1obj_debug_out(level :i32, outs :&str) {
85	if ASN1_OBJ_LOG_LEVEL.level >= level {
86		let c = format!("{}\n",outs);
87		if !ASN1_OBJ_LOG_LEVEL.nostderr {
88			let _ = std::io::stderr().write_all(c.as_bytes());
89		}
90
91		if ASN1_OBJ_LOG_LEVEL.wfile.is_some() {
92			let mut wf = ASN1_OBJ_LOG_LEVEL.wfile.as_ref().unwrap();
93			let _ = wf.write(c.as_bytes());
94		}
95	}
96	return;
97}
98
99
100pub (crate) fn asn1obj_log_get_timestamp() -> String {
101	let now = Local::now();
102	return format!("{}/{}/{} {}:{}:{}",now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second());
103}
104
105
106#[macro_export]
107macro_rules! asn1obj_log_error {
108	($($arg:tt)+) => {
109		let mut c :String= format!("<ERROR>{}[{}:{}]  ",asn1obj_log_get_timestamp(),file!(),line!());
110		c.push_str(&(format!($($arg)+)[..]));
111		asn1obj_debug_out(0,&c);
112	}
113}
114
115#[macro_export]
116macro_rules! asn1obj_log_warn {
117	($($arg:tt)+) => {
118		let mut c :String= format!("<WARN>{}[{}:{}]  ",asn1obj_log_get_timestamp(),file!(),line!());
119		c.push_str(&(format!($($arg)+)[..]));
120		asn1obj_debug_out(10,&c);
121	}
122}
123
124
125#[macro_export]
126macro_rules! asn1obj_log_info {
127	($($arg:tt)+) => {
128		let mut c :String= format!("<INFO>{}[{}:{}]  ",asn1obj_log_get_timestamp(),file!(),line!());
129		c.push_str(&(format!($($arg)+)[..]));
130		asn1obj_debug_out(20,&c);
131	}
132}
133
134#[macro_export]
135macro_rules! asn1obj_log_trace {
136	($($arg:tt)+) => {
137		let mut _c :String= format!("<TRACE>{}[{}:{}]  ",asn1obj_log_get_timestamp(),file!(),line!());
138		_c.push_str(&(format!($($arg)+)[..]));
139		asn1obj_debug_out(40, &_c);
140	}
141}
142
143
144#[macro_export]
145macro_rules! asn1obj_assert {
146	($v:expr , $($arg:tt)+) => {
147		if !($v) {
148			let mut _c :String= format!("[{}:{}] ",file!(),line!());
149			_c.push_str(&(format!($($arg)+)[..]));
150			panic!("{}", _c);
151		}
152	}
153}
154
155
156#[macro_export]
157macro_rules! asn1obj_format_buffer_log {
158	($buf:expr,$len:expr,$info:tt,$iv:expr,$($arg:tt)+) => {
159		let mut c :String = format!("[{}:{}]",file!(),line!());
160		c.push_str(&format!("{} ",$info));
161		c.push_str(&asn1obj_log_get_timestamp());
162		c.push_str(": ");
163		c.push_str(&(format!($($arg)+)[..]));
164		let _ptr :*const u8 = $buf as *const u8;
165		let  mut _ci :usize;
166		let _totallen: usize = $len as usize;
167		let mut _lasti :usize = 0;
168		let mut _nb :u8;
169		c.push_str(&format!(" buffer [{:?}][{}]",_ptr,_totallen));
170		_ci = 0;
171		while _ci < _totallen {
172			if (_ci % 16) == 0 {
173				if _ci > 0 {
174					c.push_str("    ");
175					while _lasti < _ci {
176						unsafe{
177							_nb = *_ptr.offset(_lasti as isize);	
178						}
179						
180						if _nb >= 0x20 && _nb <= 0x7e {
181							c.push(_nb as char);
182						} else {
183							c.push_str(".");
184						}
185						_lasti += 1;
186					}
187				}
188				c.push_str(&format!("\n0x{:08x}:", _ci));
189			}
190			unsafe {_nb = *_ptr.offset(_ci as isize);}			
191			c.push_str(&format!(" 0x{:02x}",_nb));
192			_ci += 1;
193		}
194
195		if _lasti < _ci {
196			while (_ci % 16) != 0 {
197				c.push_str("     ");
198				_ci += 1;
199			}
200
201			c.push_str("    ");
202
203			while _lasti < _totallen {
204				unsafe {_nb = *_ptr.offset(_lasti as isize);}				
205				if _nb >= 0x20 && _nb <= 0x7e {
206					c.push(_nb as char);
207				} else {
208					c.push_str(".");
209				}
210				_lasti += 1;
211			}
212			//c.push_str("\n");
213		}
214		asn1obj_debug_out($iv,&c);
215	}
216}
217
218#[macro_export]
219macro_rules! asn1obj_debug_buffer_error {
220	($buf:expr,$len:expr,$($arg:tt)+) => {
221		asn1obj_format_buffer_log!($buf,$len,"<ERROR>",0,$($arg)+);
222	}
223}
224
225#[macro_export]
226macro_rules! asn1obj_debug_buffer_warn {
227	($buf:expr,$len:expr,$($arg:tt)+) => {
228		asn1obj_format_buffer_log!($buf,$len,"<WARN>",1,$($arg)+);
229	}
230}
231
232#[macro_export]
233macro_rules! asn1obj_debug_buffer_info {
234	($buf:expr,$len:expr,$($arg:tt)+) => {
235		asn1obj_format_buffer_log!($buf,$len,"<INFO>",2,$($arg)+);
236	}
237}
238
239#[macro_export]
240macro_rules! asn1obj_debug_buffer_debug {
241	($buf:expr,$len:expr,$($arg:tt)+) => {
242		asn1obj_format_buffer_log!($buf,$len,"<DEBUG>",3,$($arg)+);
243	}
244}
245
246#[macro_export]
247macro_rules! asn1obj_debug_buffer_trace {
248	($buf:expr,$len:expr,$($arg:tt)+) => {
249		asn1obj_format_buffer_log!($buf,$len,"<TRACE>",4,$($arg)+);
250	}
251}