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
/*
Astrolog, a logging framework for Rust
Copyright (C) 2019 Alessandro Pellizzari

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#[macro_use]
extern crate lazy_static;

use std::error::Error;

use serde::Serialize;
use std::sync::RwLock;

pub mod errors;
pub mod formatter;
pub mod handler;
pub mod logger;

pub use crate::logger::{Context, Level, Logger, Record};

pub mod prelude {
	pub use crate::formatter::{ContextFormatting, DateFormat, DateFormatting, LevelFormat, LevelFormatting};
	pub use crate::formatter::{HtmlFormatter, JsonFormatter, LineFormatter};
	pub use crate::handler::LevelAware;
	pub use crate::logger::{Level, Logger};
}

pub mod handler_prelude {
	pub use crate::errors::{HandlerError, HandlerResult};
	pub use crate::formatter::*;
	pub use crate::handler::{Handler, LevelAware};
	pub use crate::logger::{Context, Level, Record};
}

lazy_static! {
	static ref ASTROLOG_GLOBAL: RwLock<Logger> = RwLock::new(Logger::new());
}

pub fn reset_global_logger() {
	let mut global_logger = ASTROLOG_GLOBAL.write().unwrap();
	(*global_logger) = Logger::new();
}

pub fn config<F>(f: F)
where
	F: FnOnce(&mut Logger),
{
	let mut l = ASTROLOG_GLOBAL.write().unwrap();
	f(&mut l);
}

pub fn with<V>(k: &str, v: V) -> logger::Item
where
	V: Serialize,
{
	logger::Item::new_global().with(k, v)
}

pub fn with_error(e: &Error) -> logger::Item {
	logger::Item::new_global().with_error(e)
}

pub fn with_new_context<V:Into<Context>>(ctx: V) -> logger::Item<'static> {
	logger::Item::new_global().with_new_context(ctx)
}

pub fn log<S: ToString>(level: Level, msg: S) {
	ASTROLOG_GLOBAL.read().unwrap().log(level, msg);
}

pub fn trace<S: ToString>(msg: S) {
	log(Level::Trace, msg);
}
pub fn profile<S: ToString>(msg: S) {
	log(Level::Profile, msg);
}
pub fn debug<S: ToString>(msg: S) {
	log(Level::Debug, msg);
}
pub fn info<S: ToString>(msg: S) {
	log(Level::Info, msg);
}
pub fn notice<S: ToString>(msg: S) {
	log(Level::Notice, msg);
}
pub fn warning<S: ToString>(msg: S) {
	log(Level::Warning, msg);
}
pub fn error<S: ToString>(msg: S) {
	log(Level::Error, msg);
}
pub fn critical<S: ToString>(msg: S) {
	log(Level::Critical, msg);
}
pub fn alert<S: ToString>(msg: S) {
	log(Level::Alert, msg);
}
pub fn emergency<S: ToString>(msg: S) {
	log(Level::Emergency, msg);
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::thread;

	#[test]
	fn multi_thread() {
		use super::handler::vec::VecHandler;
		let handler = VecHandler::new();
		let store = handler.get_store();

		config(|l| {l.push_handler(handler);});

		let th = thread::spawn(|| {
			debug("Thread debug message");
		});

		warning("Main warning message");

		let _ = th.join();

		let logs = store.take();

		assert!(logs.iter().position(|v| v.msg.contains("Thread debug")).is_some());
		assert!(logs.iter().position(|v| v.msg.contains("Main warning")).is_some());
	}
}