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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211


#[cfg(feature = "log_time")]
use chrono::Utc;


#[macro_export]
macro_rules! log_env_name {
	() => (concat!(env!("CARGO_CRATE_NAME"), "_log"))
}

#[macro_export]
macro_rules! log_level {
	() => (match option_env!( $crate::log_env_name!() ) {
		Some("INFO") => "INFO",
		Some("WARN") => "WARN",
		None | Some("ERROR") => "ERROR",
		Some("IGNORE") => "",
		Some(_) => panic!("only INFO | WARN | ERROR | IGNORE are supported")
	})
}

#[cfg(not(feature = "log_time"))]
#[macro_export]
macro_rules! log_maybe_with_time {
	( INFO, $($arg:tt)* ) => (println!( "INFO {}", format!($($arg)*) ));
	( WARN, $($arg:tt)* ) => (println!( "WARN {}", format!($($arg)*) ));
	( ERRO, $($arg:tt)* ) => (println!( "ERRO {}", format!($($arg)*) ));
}

#[cfg(feature = "log_time")]
pub fn time() -> String {
	Utc::now().to_rfc3339()
}

#[cfg(feature = "log_time")]
#[macro_export]
macro_rules! log_maybe_with_time {
	( INFO, $($arg:tt)* ) => (println!( "{} INFO {}", $crate::time(), format!($($arg)*) ));
	( WARN, $($arg:tt)* ) => (println!( "{} WARN {}", $crate::time(), format!($($arg)*) ));
	( ERRO, $($arg:tt)* ) => (println!( "{} ERRO {}", $crate::time(), format!($($arg)*) ));
}

#[macro_export]
macro_rules! info {
	($($arg:tt)*) => (match $crate::log_level!() {
		"INFO" => {
		$crate::log_maybe_with_time!( INFO, $($arg)* );
		}, _ => {}
	})
}

#[macro_export]
macro_rules! warn {
	($($arg:tt)*) => (match $crate::log_level!() {
		"INFO" | "WARN" => {
		$crate::log_maybe_with_time!( WARN, $($arg)* );
		}, _ => {}
	})
}

#[macro_export]
macro_rules! error {
	($($arg:tt)*) => (match $crate::log_level!() {
		"INFO" | "WARN" | "ERROR" => {
		$crate::log_maybe_with_time!( ERRO, $($arg)* );
		}, _ => {}
	})
}


#[macro_export]
macro_rules! init_log_traits {
	() => (mod log_traits {

		pub trait Log {
			fn info( self, msg: &'static str ) -> Self;
			fn warn( self, msg: &'static str ) -> Self;
			fn error( self, msg: &'static str ) -> Self;
		}

		impl<T, E> Log for Result<T, E>
		where E: std::fmt::Debug {

			fn info( self, msg: &'static str ) -> Self {
				if let Err(e) = &self {
					$crate::info!( "{} {:?}", msg, e );
				}
				self
			}

			#[inline(always)]
			fn warn( self, msg: &'static str ) -> Self {
				if let Err(e) = &self {
					$crate::warn!( "{} {:?}", msg, e );
				}
				self
			}

			fn error( self, msg: &'static str ) -> Self {
				if let Err(e) = &self {
					$crate::error!( "{} {:?}", msg, e );
				}
				self
			}

		}

		impl<T> Log for Option<T> {

			fn info( self, msg: &'static str ) -> Self {
				if let None = &self {
					$crate::info!("{} {:?}", msg, std::any::type_name::<Self>() );
				}
				self
			}

			fn warn( self, msg: &'static str ) -> Self {
				if let None = &self {
					$crate::warn!("{} {:?}", msg, std::any::type_name::<Self>() );
				}
				self
			}

			fn error( self, msg: &'static str ) -> Self {
				if let None = &self {
					$crate::error!("{} {:?}", msg, std::any::type_name::<Self>() );
				}
				self
			}

		}

		impl Log for bool {

			fn info( self, msg: &'static str ) -> Self {
				if !self {
					$crate::info!("{}", msg );
				}
				self
			}

			fn warn( self, msg: &'static str ) -> Self {
				if !self {
					$crate::warn!("{}", msg );
				}
				self
			}

			fn error( self, msg: &'static str ) -> Self {
				if !self {
					$crate::error!("{}", msg );
				}
				self
			}

		}

		// Maybe rename this??
		pub trait AlwaysLog {
			fn always_info( self, msg: &'static str ) -> Self;
			fn always_warn( self, msg: &'static str ) -> Self;
			fn always_error( self, msg: &'static str ) -> Self;
		}

		impl<T> AlwaysLog for T
		where T: std::fmt::Debug {

			fn always_info( self, msg: &'static str ) -> Self {
				$crate::info!( "{} {:?}", msg, self );
				self
			}

			fn always_warn( self, msg: &'static str ) -> Self {
				$crate::warn!( "{} {:?}", msg, self );
				self
			}

			fn always_error( self, msg: &'static str ) -> Self {
				$crate::error!( "{} {:?}", msg, self );
				self
			}

		}

	})
}



#[cfg(test)]
mod tests {

	use super::*;

	add_log_trait!();

	#[test]
	fn test_env() {
		assert_eq!( "env_name {}", log_env_name!() );
		println!( "log_level {}", log_level!() );
		assert!(false);
	}

	#[test]
	fn test_log_trait() {
		let err: Result<(), _> = Err("static");
		let _ = err.warn("error detect in test_log_trait");
	}

}