1#![allow(non_camel_case_types)]
11#![allow(non_snake_case)]
12#![allow(non_upper_case_globals)]
13#![allow(dead_code)]
14use crate::common::errcode;
15use serde_json;
16use std::net::{self, IpAddr, SocketAddr, UdpSocket};
17use std::time;
18use crate::common::{self,rsm_time_t};
19use super::*;
20
21
22const _DEBUG: bool = false;
23
24pub fn new_xlogger(
27 moduleName: &str,
28 selfIp: &IpAddr,
29 self_port: u16,
30 servIp: &net::IpAddr,
31 servPort: u16,
32) -> xlogger_t {
33 let server_addr = SocketAddr::new(servIp.clone(), servPort);
34 let mut socket: Option<UdpSocket> = None;
35 if let Ok(conn) = UdpSocket::bind(SocketAddr::new(*selfIp, self_port)) {
36 socket = Some(conn);
37 }
38
39 let logger = xlogger_t {
40 module_name: String::from(moduleName),
41 self_ip: *selfIp,
42 server_addr: server_addr,
43 socket: socket,
44 self_port: self_port,
45 level: LOG_LEVEL_ERROR,
46 sentPackets: 0,
47 sentbytes: 0,
48 };
49
50 return logger;
51}
52
53impl xlogger_t {
54 pub fn Log(
56 &mut self,
57 level: LOG_LEVEL,
58 position: &str,
59 err: errcode::RESULT,
60 logDesc: &String,
61 ) {
62 if _DEBUG && (level<=self.level){
63 self.print_debug(level,err,position,logDesc);
64
65 }
66
67 if level > self.level {
68 return;
69 }
70 let logMsg = InnerLogMsg {
71 ModuleName: self.module_name.clone(),
72 OccureTime: rsm_time_t::now_utc(),
73 LogType: LOG_TYPE_SYSTEM,
74 LogLevel: level,
75 Position: String::from(position),
76 ErrCode: err,
77 LogDesc: logDesc.clone(),
78 Context: String::from("Null"),
79 };
80
81 self.sentLog(&logMsg);
82 }
83
84 pub fn set_log_level(&mut self, new_level: LOG_LEVEL) {
85 if new_level >= 0 && new_level <= LOG_LEVEL_MAX {
86 self.level = new_level;
87 }
88 }
89
90 fn print_debug(&self,level: LOG_LEVEL,ec:errcode::RESULT,position: &str,logDesc:&String) {
91 if ec == 0 {
92 println!(
93 "time:{},position={},msg:{}\n",
94 common::format_datetime(&time::SystemTime::now()),
95 position, logDesc
96 );
97 } else {
98 println!(
99 "time:{},level={},errcode={},position={},err={}\n",
100 common::format_datetime(&time::SystemTime::now()),
101 level, ec, position, logDesc
102 );
103 }
104 }
105 fn sentLog(&mut self, msg: &InnerLogMsg) -> errcode::RESULT {
107 let res = serde_json::to_string::<InnerLogMsg>(msg);
108 let json_str = match res {
109 Err(e) => {
110 println!("LogClient: Sent Log error, err={}\n", e);
111 return errcode::ERROR_ENCODE_MSG;
112 }
113 Ok(s) => s,
114 };
115
116 if let Some(ref c) = self.socket {
117 if let Ok(len) = c.send_to(json_str.as_bytes(), self.server_addr) {
118 self.sentbytes += len as u64;
119 self.sentPackets += 1;
120 return errcode::RESULT_SUCCESS;
121 } else {
122 return errcode::ERROR_SEND_MSG;
123 }
124 } else {
125 println!(
126 "LogClient: udp connection is not ready, ip={},port={}\n",
127 self.self_ip, self.self_port
128 );
129 return errcode::ERROR_BIND_SOCKET;
130 }
131 }
132
133 pub fn Alertf(&mut self, postion: &str, err: errcode::RESULT, logDesc: &String) {
134 self.Log(LOG_LEVEL_ALERT, postion, err, logDesc)
135 }
136
137 pub fn Errorf(&mut self, postion: &str, err: errcode::RESULT, logDesc: &String) {
138 self.Log(LOG_LEVEL_ERROR, postion, err, logDesc)
139 }
140
141 pub fn Warningf(&mut self, postion: &str, err: errcode::RESULT, logDesc: &String) {
142 self.Log(LOG_LEVEL_WARNING, postion, err, logDesc);
143 }
144
145 pub fn Infof(&mut self, postion: &str, err: errcode::RESULT, logDesc: &String) {
146 self.Log(LOG_LEVEL_INFO, postion, err, logDesc);
147 }
148
149 pub fn Debugf(&mut self, postion: &str, err: errcode::RESULT, logDesc: &String) {
150 self.Log(LOG_LEVEL_DEBUG, postion, err, logDesc);
151 }
152
153 fn GetLogStats(&self) -> (u64, u64) {
154 return (self.sentPackets, self.sentbytes);
155 }
156
157 pub fn PrintStats(&self) {
158 println!(
159 "LogClient: ClientIP={}:{}, Server={}:{}\n",
160 self.self_ip,
161 self.self_port,
162 self.server_addr.ip(),
163 self.server_addr.port()
164 );
165 println!(
166 "LogClient: sent packets={}, bytes={}\n",
167 self.sentPackets, self.sentbytes
168 );
169 }
170}
171
172#[macro_export]
173macro_rules! log {
174 ($($arg:tt)*) => (xlog::_print($crate::format_args!($($arg)*)));
175}