1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate chrono;
extern crate regex;
extern crate rand;
extern crate openssl;
#[cfg(test)]
extern crate tempdir;
extern crate serde;
extern crate serde_yaml;
extern crate opcua_types;
pub mod comms;
pub mod crypto;
pub fn init_logging() {
use std::env;
const ANSI_ERROR: &str = "\x1b[37m\x1b[41m";
const ANSI_WARN: &str = "\x1b[33m";
const ANSI_INFO: &str = "\x1b[36m";
const ANSI_RESET: &str = "\x1b[0m";
let result = {
let mut builder = env_logger::LogBuilder::new();
builder.format(|record: &log::LogRecord| {
use chrono;
let now = chrono::UTC::now();
let time_fmt = now.format("%Y-%m-%d %H:%M:%S%.3f");
match record.metadata().level() {
log::LogLevel::Error => {
format!("{} - {}{}{} - {} - {}", time_fmt, ANSI_ERROR, record.level(), ANSI_RESET, record.location().module_path(), record.args())
}
log::LogLevel::Warn => {
format!("{} - {}{}{} - {} - {}", time_fmt, ANSI_WARN, record.level(), ANSI_RESET, record.location().module_path(), record.args())
}
log::LogLevel::Info => {
format!("{} - {}{}{} - {} - {}", time_fmt, ANSI_INFO, record.level(), ANSI_RESET, record.location().module_path(), record.args())
}
_ => {
format!("{} - {} - {} - {}", time_fmt, record.level(), record.location().module_path(), record.args())
}
}
});
let filters = if let Ok(env_filters) = env::var("RUST_OPCUA_LOG") {
env_filters
} else {
"info".to_string()
};
builder.parse(&filters);
builder.init()
};
if result.is_err() {
println!("Logger error, check error = {}", result.unwrap_err());
} else {
info!("Logging is enabled, use RUST_OPCUA_LOG environment variable to control filtering, logging level");
}
}
pub mod debug {
pub const SUBSCRIPTION: &'static str = "subscription";
pub fn log_buffer(message: &str, buf: &[u8]) {
use log;
if !log_enabled!(log::LogLevel::Trace) {
return;
}
let line_len = 32;
let len = buf.len();
let last_line_padding = ((len / line_len) + 1) * line_len - len;
trace!("{}", message);
let mut char_line = String::new();
let mut hex_line = format!("{:08x}: ", 0);
for (i, b) in buf.iter().enumerate() {
let value = *b as u8;
if i > 0 && i % line_len == 0 {
trace!(target: "hex", "{} {}", hex_line, char_line);
hex_line = format!("{:08}: ", i);
char_line.clear();
}
hex_line = format!("{} {:02x}", hex_line, value);
char_line.push(if value >= 32 && value <= 126 { value as char } else { '.' });
}
if last_line_padding > 0 {
for _ in 0..last_line_padding {
hex_line.push_str(" ");
}
trace!(target: "hex", "{} {}", hex_line, char_line);
}
}
}
#[cfg(test)]
mod tests;
pub mod config;
pub mod prelude {
pub use opcua_types::*;
pub use comms::prelude::*;
pub use crypto::*;
pub use config::Config;
}