1#![warn(missing_docs)]
304#![no_std]
305
306#[cfg(not(feature = "std"))]
307extern crate alloc;
308#[macro_use]
309#[cfg(feature = "std")]
310extern crate std;
311
312mod key;
313pub use self::key::Key;
314#[cfg(not(feature = "std"))]
315use alloc::sync::Arc;
316#[cfg(not(feature = "std"))]
317use alloc::boxed::Box;
318#[cfg(not(feature = "std"))]
319use alloc::rc::Rc;
320#[cfg(not(feature = "std"))]
321use alloc::string::String;
322
323#[cfg(feature = "nested-values")]
324extern crate erased_serde;
325
326use core::{convert, fmt, result};
327use core::str::FromStr;
328#[cfg(feature = "std")]
329use std::boxed::Box;
330#[cfg(feature = "std")]
331use std::panic::{RefUnwindSafe, UnwindSafe};
332#[cfg(feature = "std")]
333use std::rc::Rc;
334#[cfg(feature = "std")]
335use std::string::String;
336#[cfg(feature = "std")]
337use std::sync::Arc;
338#[macro_export(local_inner_macros)]
357macro_rules! o(
358 ($($args:tt)*) => {
359 $crate::OwnedKV(kv!($($args)*))
360 };
361);
362
363#[macro_export(local_inner_macros)]
367macro_rules! slog_o(
368 ($($args:tt)*) => {
369 $crate::OwnedKV(slog_kv!($($args)*))
370 };
371);
372
373#[macro_export(local_inner_macros)]
379macro_rules! b(
380 ($($args:tt)*) => {
381 $crate::BorrowedKV(&kv!($($args)*))
382 };
383);
384
385#[macro_export(local_inner_macros)]
387macro_rules! slog_b(
388 ($($args:tt)*) => {
389 $crate::BorrowedKV(&slog_kv!($($args)*))
390 };
391);
392
393#[macro_export(local_inner_macros)]
397macro_rules! kv(
398 (@ $args_ready:expr; $k:expr => %$v:expr) => {
399 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{}", $v))), $args_ready); )
400 };
401 (@ $args_ready:expr; $k:expr => %$v:expr, $($args:tt)* ) => {
402 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{}", $v))), $args_ready); $($args)* )
403 };
404 (@ $args_ready:expr; $k:expr => #%$v:expr) => {
405 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#}", $v))), $args_ready); )
406 };
407 (@ $args_ready:expr; $k:expr => #%$v:expr, $($args:tt)* ) => {
408 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#}", $v))), $args_ready); $($args)* )
409 };
410 (@ $args_ready:expr; $k:expr => ?$v:expr) => {
411 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:?}", $v))), $args_ready); )
412 };
413 (@ $args_ready:expr; $k:expr => ?$v:expr, $($args:tt)* ) => {
414 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:?}", $v))), $args_ready); $($args)* )
415 };
416 (@ $args_ready:expr; $k:expr => #?$v:expr) => {
417 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); )
418 };
419 (@ $args_ready:expr; $k:expr => #?$v:expr, $($args:tt)* ) => {
420 kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); $($args)* )
421 };
422 (@ $args_ready:expr; $k:expr => #$v:expr) => {
423 kv!(@ ($crate::SingleKV::from(($k, $crate::ErrorValue($v))), $args_ready); )
424 };
425 (@ $args_ready:expr; $k:expr => $v:expr) => {
426 kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); )
427 };
428 (@ $args_ready:expr; $k:expr => $v:expr, $($args:tt)* ) => {
429 kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); $($args)* )
430 };
431 (@ $args_ready:expr; $kv:expr) => {
432 kv!(@ ($kv, $args_ready); )
433 };
434 (@ $args_ready:expr; $kv:expr, $($args:tt)* ) => {
435 kv!(@ ($kv, $args_ready); $($args)* )
436 };
437 (@ $args_ready:expr; ) => {
438 $args_ready
439 };
440 (@ $args_ready:expr;, ) => {
441 $args_ready
442 };
443 ($($args:tt)*) => {
444 kv!(@ (); $($args)*)
445 };
446);
447
448#[macro_export(local_inner_macros)]
450macro_rules! slog_kv(
451 (@ $args_ready:expr; $k:expr => %$v:expr) => {
452 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{}", $v))), $args_ready); )
453 };
454 (@ $args_ready:expr; $k:expr => %$v:expr, $($args:tt)* ) => {
455 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{}", $v))), $args_ready); $($args)* )
456 };
457 (@ $args_ready:expr; $k:expr => #%$v:expr) => {
458 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#}", $v))), $args_ready); )
459 };
460 (@ $args_ready:expr; $k:expr => #%$v:expr, $($args:tt)* ) => {
461 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#}", $v))), $args_ready); $($args)* )
462 };
463 (@ $args_ready:expr; $k:expr => ?$v:expr) => {
464 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:?}", $v))), $args_ready); )
465 };
466 (@ $args_ready:expr; $k:expr => ?$v:expr, $($args:tt)* ) => {
467 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:?}", $v))), $args_ready); $($args)* )
468 };
469 (@ $args_ready:expr; $k:expr => #?$v:expr) => {
470 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); )
471 };
472 (@ $args_ready:expr; $k:expr => #?$v:expr, $($args:tt)* ) => {
473 slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); $($args)* )
474 };
475 (@ $args_ready:expr; $k:expr => $v:expr) => {
476 slog_kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); )
477 };
478 (@ $args_ready:expr; $k:expr => $v:expr, $($args:tt)* ) => {
479 slog_kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); $($args)* )
480 };
481 (@ $args_ready:expr; $slog_kv:expr) => {
482 slog_kv!(@ ($slog_kv, $args_ready); )
483 };
484 (@ $args_ready:expr; $slog_kv:expr, $($args:tt)* ) => {
485 slog_kv!(@ ($slog_kv, $args_ready); $($args)* )
486 };
487 (@ $args_ready:expr; ) => {
488 $args_ready
489 };
490 (@ $args_ready:expr;, ) => {
491 $args_ready
492 };
493 ($($args:tt)*) => {
494 slog_kv!(@ (); $($args)*)
495 };
496);
497
498#[macro_export(local_inner_macros)]
499macro_rules! record_static(
501 ($lvl:expr, $tag:expr,) => { record_static!($lvl, $tag) };
502 ($lvl:expr, $tag:expr) => {{
503 static LOC : $crate::RecordLocation = $crate::RecordLocation {
504 file: __slog_builtin!(@file),
505 line: __slog_builtin!(@line),
506 column: __slog_builtin!(@column),
507 function: "",
508 module: __slog_builtin!(@module_path),
509 };
510 $crate::RecordStatic {
511 location : &LOC,
512 level: $lvl,
513 tag : $tag,
514 }
515 }};
516);
517
518#[macro_export(local_inner_macros)]
519macro_rules! slog_record_static(
521 ($lvl:expr, $tag:expr,) => { slog_record_static!($lvl, $tag) };
522 ($lvl:expr, $tag:expr) => {{
523 static LOC : $crate::RecordLocation = $crate::RecordLocation {
524 file: __slog_builtin!(@file),
525 line: __slog_builtin!(@line),
526 column: __slog_builtin!(@column),
527 function: "",
528 module: __slog_builtin!(@module_path),
529 };
530 $crate::RecordStatic {
531 location : &LOC,
532 level: $lvl,
533 tag: $tag,
534 }
535 }};
536);
537
538#[macro_export(local_inner_macros)]
539macro_rules! record(
545 ($lvl:expr, $tag:expr, $args:expr, $b:expr,) => {
546 record!($lvl, $tag, $args, $b)
547 };
548 ($lvl:expr, $tag:expr, $args:expr, $b:expr) => {{
549 #[allow(dead_code)]
550 static RS : $crate::RecordStatic<'static> = record_static!($lvl, $tag);
551 $crate::Record::new(&RS, $args, $b)
552 }};
553);
554
555#[macro_export(local_inner_macros)]
556macro_rules! slog_record(
558 ($lvl:expr, $tag:expr, $args:expr, $b:expr,) => {
559 slog_record!($lvl, $tag, $args, $b)
560 };
561 ($lvl:expr, $tag:expr, $args:expr, $b:expr) => {{
562 static RS : $crate::RecordStatic<'static> = slog_record_static!($lvl,
563 $tag);
564 $crate::Record::new(&RS, $args, $b)
565 }};
566);
567
568#[macro_export(local_inner_macros)]
753macro_rules! log(
754 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr) => {
756 $crate::Logger::log(&$l, &record!($lvl, $tag, &__slog_builtin!(@format_args $msg_fmt, $($fmt)*), b!($($kv)*)))
757 };
758 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr,) => {
759 log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
760 };
761 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr;) => {
762 log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
763 };
764 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $($args:tt)*) => {
765 log!(2 @ { $($fmt)* }, { $($kv)* $($args)*}, $l, $lvl, $tag, $msg_fmt)
766 };
767 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr) => {
770 log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
771 };
772 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr;) => {
773 log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
774 };
775 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr,) => {
776 log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
777 };
778 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr; $($args:tt)*) => {
779 log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt, $($args)*)
780 };
781 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr, $($args:tt)*) => {
782 log!(1 @ { $($fmt)* $k = $v, }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt, $($args)*)
783 };
784 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr,) => {
786 log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
787 };
788 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr) => {
789 log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
790 };
791 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, ; $($args:tt)*) => {
792 log!(1 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt; $($args)*)
793 };
794 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr; $($args:tt)*) => {
795 log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt, $($args)*)
796 };
797 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $f:tt $($args:tt)*) => {
799 log!(1 @ { $($fmt)* $f }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt, $($args)*)
800 };
801 ($l:expr, $lvl:expr, $tag:expr, $($args:tt)*) => {
802 if $lvl.as_usize() <= $crate::__slog_static_max_level().as_usize() {
803 log!(1 @ { }, { }, $l, $lvl, $tag, $($args)*)
804 }
805 };
806);
807
808#[macro_export(local_inner_macros)]
826macro_rules! slog_log(
827 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr) => {
829 $crate::Logger::log(&$l, &slog_record!($lvl, $tag, &__slog_builtin!(@format_args $msg_fmt, $($fmt)*), slog_b!($($kv)*)))
830 };
831 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr,) => {
832 slog_log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
833 };
834 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr;) => {
835 slog_log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
836 };
837 (2 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $($args:tt)*) => {
838 slog_log!(2 @ { $($fmt)* }, { $($kv)* $($args)*}, $l, $lvl, $tag, $msg_fmt)
839 };
840 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr) => {
843 slog_log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
844 };
845 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr;) => {
846 slog_log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
847 };
848 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr,) => {
849 slog_log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt)
850 };
851 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr; $($args:tt)*) => {
852 slog_log!(2 @ { $($fmt)* $k = $v }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt, $($args)*)
853 };
854 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $k:ident = $v:expr, $($args:tt)*) => {
855 slog_log!(1 @ { $($fmt)* $k = $v, }, { $($kv)* __slog_builtin!(@stringify $k) => $v, }, $l, $lvl, $tag, $msg_fmt, $($args)*)
856 };
857 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr,) => {
859 slog_log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
860 };
861 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr) => {
862 slog_log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt)
863 };
864 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, ; $($args:tt)*) => {
865 slog_log!(1 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt; $($args)*)
866 };
867 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr; $($args:tt)*) => {
868 slog_log!(2 @ { $($fmt)* }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt, $($args)*)
869 };
870 (1 @ { $($fmt:tt)* }, { $($kv:tt)* }, $l:expr, $lvl:expr, $tag:expr, $msg_fmt:expr, $f:tt $($args:tt)*) => {
872 slog_log!(1 @ { $($fmt)* $f }, { $($kv)* }, $l, $lvl, $tag, $msg_fmt, $($args)*)
873 };
874 ($l:expr, $lvl:expr, $tag:expr, $($args:tt)*) => {
875 if $lvl.as_usize() <= $crate::__slog_static_max_level().as_usize() {
876 slog_log!(1 @ { }, { }, $l, $lvl, $tag, $($args)*)
877 }
878 };
879);
880#[macro_export(local_inner_macros)]
884macro_rules! crit(
885 ($l:expr, #$tag:expr, $($args:tt)+) => {
886 log!($l, $crate::Level::Critical, $tag, $($args)+)
887 };
888 ($l:expr, $($args:tt)+) => {
889 log!($l, $crate::Level::Critical, "", $($args)+)
890 };
891);
892
893#[macro_export(local_inner_macros)]
900macro_rules! slog_crit(
901 ($l:expr, #$tag:expr, $($args:tt)+) => {
902 slog_log!($l, $crate::Level::Critical, $tag, $($args)+)
903 };
904 ($l:expr, $($args:tt)+) => {
905 slog_log!($l, $crate::Level::Critical, "", $($args)+)
906 };
907);
908
909#[macro_export(local_inner_macros)]
913macro_rules! error(
914 ($l:expr, #$tag:expr, $($args:tt)+) => {
915 log!($l, $crate::Level::Error, $tag, $($args)+)
916 };
917 ($l:expr, $($args:tt)+) => {
918 log!($l, $crate::Level::Error, "", $($args)+)
919 };
920);
921
922#[macro_export(local_inner_macros)]
929macro_rules! slog_error(
930 ($l:expr, #$tag:expr, $($args:tt)+) => {
931 slog_log!($l, $crate::Level::Error, $tag, $($args)+)
932 };
933 ($l:expr, $($args:tt)+) => {
934 slog_log!($l, $crate::Level::Error, "", $($args)+)
935 };
936);
937
938#[macro_export(local_inner_macros)]
942macro_rules! warn(
943 ($l:expr, #$tag:expr, $($args:tt)+) => {
944 log!($l, $crate::Level::Warning, $tag, $($args)+)
945 };
946 ($l:expr, $($args:tt)+) => {
947 log!($l, $crate::Level::Warning, "", $($args)+)
948 };
949);
950
951#[macro_export(local_inner_macros)]
958macro_rules! slog_warn(
959 ($l:expr, #$tag:expr, $($args:tt)+) => {
960 slog_log!($l, $crate::Level::Warning, $tag, $($args)+)
961 };
962 ($l:expr, $($args:tt)+) => {
963 slog_log!($l, $crate::Level::Warning, "", $($args)+)
964 };
965);
966
967#[macro_export(local_inner_macros)]
971macro_rules! info(
972 ($l:expr, #$tag:expr, $($args:tt)*) => {
973 log!($l, $crate::Level::Info, $tag, $($args)*)
974 };
975 ($l:expr, $($args:tt)*) => {
976 log!($l, $crate::Level::Info, "", $($args)*)
977 };
978);
979
980#[macro_export(local_inner_macros)]
987macro_rules! slog_info(
988 ($l:expr, #$tag:expr, $($args:tt)+) => {
989 slog_log!($l, $crate::Level::Info, $tag, $($args)+)
990 };
991 ($l:expr, $($args:tt)+) => {
992 slog_log!($l, $crate::Level::Info, "", $($args)+)
993 };
994);
995
996#[macro_export(local_inner_macros)]
1000macro_rules! debug(
1001 ($l:expr, #$tag:expr, $($args:tt)+) => {
1002 log!($l, $crate::Level::Debug, $tag, $($args)+)
1003 };
1004 ($l:expr, $($args:tt)+) => {
1005 log!($l, $crate::Level::Debug, "", $($args)+)
1006 };
1007);
1008
1009#[macro_export(local_inner_macros)]
1016macro_rules! slog_debug(
1017 ($l:expr, #$tag:expr, $($args:tt)+) => {
1018 slog_log!($l, $crate::Level::Debug, $tag, $($args)+)
1019 };
1020 ($l:expr, $($args:tt)+) => {
1021 slog_log!($l, $crate::Level::Debug, "", $($args)+)
1022 };
1023);
1024
1025#[macro_export(local_inner_macros)]
1029macro_rules! trace(
1030 ($l:expr, #$tag:expr, $($args:tt)+) => {
1031 log!($l, $crate::Level::Trace, $tag, $($args)+)
1032 };
1033 ($l:expr, $($args:tt)+) => {
1034 log!($l, $crate::Level::Trace, "", $($args)+)
1035 };
1036);
1037
1038#[macro_export(local_inner_macros)]
1045macro_rules! slog_trace(
1046 ($l:expr, #$tag:expr, $($args:tt)+) => {
1047 slog_log!($l, $crate::Level::Trace, $tag, $($args)+)
1048 };
1049 ($l:expr, $($args:tt)+) => {
1050 slog_log!($l, $crate::Level::Trace, "", $($args)+)
1051 };
1052 ($($args:tt)+) => {
1053 slog_log!($crate::Level::Trace, $($args)+)
1054 };
1055);
1056
1057#[doc(hidden)]
1060#[macro_export]
1061macro_rules! __slog_builtin {
1062 (@format_args $($t:tt)*) => ( format_args!($($t)*) );
1063 (@stringify $($t:tt)*) => ( stringify!($($t)*) );
1064 (@file) => ( file!() );
1065 (@line) => ( line!() );
1066 (@column) => ( column!() );
1067 (@module_path) => ( module_path!() );
1068}
1069
1070#[derive(Clone)]
1109pub struct Logger<D = Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>>
1110where
1111 D: SendSyncUnwindSafeDrain<Ok = (), Err = Never>,
1112{
1113 drain: D,
1114 list: OwnedKVList,
1115}
1116
1117impl<D> Logger<D>
1118where
1119 D: SendSyncUnwindSafeDrain<Ok = (), Err = Never>,
1120{
1121 pub fn root<T>(drain: D, values: OwnedKV<T>) -> Logger
1148 where
1149 D: 'static + SendSyncRefUnwindSafeDrain<Err = Never, Ok = ()>,
1150 T: SendSyncRefUnwindSafeKV + 'static,
1151 {
1152 Logger {
1153 drain: Arc::new(drain)
1154 as Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>,
1155 list: OwnedKVList::root(values),
1156 }
1157 }
1158
1159 pub fn root_typed<T>(drain: D, values: OwnedKV<T>) -> Logger<D>
1173 where
1174 D: 'static + SendSyncUnwindSafeDrain<Err = Never, Ok = ()> + Sized,
1175 T: SendSyncRefUnwindSafeKV + 'static,
1176 {
1177 Logger {
1178 drain: drain,
1179 list: OwnedKVList::root(values),
1180 }
1181 }
1182
1183 #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
1216 pub fn new<T>(&self, values: OwnedKV<T>) -> Logger<D>
1217 where
1218 T: SendSyncRefUnwindSafeKV + 'static,
1219 D: Clone,
1220 {
1221 Logger {
1222 drain: self.drain.clone(),
1223 list: OwnedKVList::new(values, self.list.node.clone()),
1224 }
1225 }
1226
1227 #[inline]
1232 pub fn log(&self, record: &Record) {
1233 let _ = self.drain.log(record, &self.list);
1234 }
1235
1236 pub fn list(&self) -> &OwnedKVList {
1238 &self.list
1239 }
1240
1241 pub fn into_erased(
1251 self,
1252 ) -> Logger<Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>>
1253 where
1254 D: SendRefUnwindSafeDrain + 'static,
1255 {
1256 Logger {
1257 drain: Arc::new(self.drain)
1258 as Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>,
1259 list: self.list,
1260 }
1261 }
1262
1263 pub fn to_erased(
1267 &self,
1268 ) -> Logger<Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>>
1269 where
1270 D: SendRefUnwindSafeDrain + 'static + Clone,
1271 {
1272 self.clone().into_erased()
1273 }
1274}
1275
1276impl<D> fmt::Debug for Logger<D>
1277where
1278 D: SendSyncUnwindSafeDrain<Ok = (), Err = Never>,
1279{
1280 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1281 try!(write!(f, "Logger{:?}", self.list));
1282 Ok(())
1283 }
1284}
1285
1286impl<D> Drain for Logger<D>
1287where
1288 D: SendSyncUnwindSafeDrain<Ok = (), Err = Never>,
1289{
1290 type Ok = ();
1291 type Err = Never;
1292
1293 fn log(
1294 &self,
1295 record: &Record,
1296 values: &OwnedKVList,
1297 ) -> result::Result<Self::Ok, Self::Err> {
1298 let chained = OwnedKVList {
1299 node: Arc::new(MultiListNode {
1300 next_node: values.node.clone(),
1301 node: self.list.node.clone(),
1302 }),
1303 };
1304 self.drain.log(record, &chained)
1305 }
1306
1307 #[inline]
1308 fn is_enabled(&self, level: Level) -> bool {
1309 self.drain.is_enabled(level)
1310 }
1311}
1312
1313pub trait Drain {
1330 type Ok;
1336 type Err;
1338 fn log(
1350 &self,
1351 record: &Record,
1352 values: &OwnedKVList,
1353 ) -> result::Result<Self::Ok, Self::Err>;
1354
1355 #[inline]
1388 fn is_enabled(&self, level: Level) -> bool {
1389 level.as_usize() <= ::__slog_static_max_level().as_usize()
1390 }
1391
1392 #[inline]
1394 fn is_critical_enabled(&self) -> bool {
1395 self.is_enabled(Level::Critical)
1396 }
1397
1398 #[inline]
1400 fn is_error_enabled(&self) -> bool {
1401 self.is_enabled(Level::Error)
1402 }
1403
1404 #[inline]
1406 fn is_warning_enabled(&self) -> bool {
1407 self.is_enabled(Level::Warning)
1408 }
1409
1410 #[inline]
1412 fn is_info_enabled(&self) -> bool {
1413 self.is_enabled(Level::Info)
1414 }
1415
1416 #[inline]
1418 fn is_debug_enabled(&self) -> bool {
1419 self.is_enabled(Level::Debug)
1420 }
1421
1422 #[inline]
1424 fn is_trace_enabled(&self) -> bool {
1425 self.is_enabled(Level::Trace)
1426 }
1427
1428 fn map<F, R>(self, f: F) -> R
1441 where
1442 Self: Sized,
1443 F: FnOnce(Self) -> R,
1444 {
1445 f(self)
1446 }
1447
1448 fn filter<F>(self, f: F) -> Filter<Self, F>
1455 where
1456 Self: Sized,
1457 F: FilterFn,
1458 {
1459 Filter::new(self, f)
1460 }
1461
1462 fn filter_level(self, level: Level) -> LevelFilter<Self>
1469 where
1470 Self: Sized,
1471 {
1472 LevelFilter(self, level)
1473 }
1474
1475 fn map_err<F, E>(self, f: F) -> MapError<Self, E>
1480 where
1481 Self: Sized,
1482 F: MapErrFn<Self::Err, E>,
1483 {
1484 MapError::new(self, f)
1485 }
1486
1487 fn ignore_res(self) -> IgnoreResult<Self>
1491 where
1492 Self: Sized,
1493 {
1494 IgnoreResult::new(self)
1495 }
1496
1497 fn fuse(self) -> Fuse<Self>
1501 where
1502 Self::Err: fmt::Debug,
1503 Self: Sized,
1504 {
1505 self.map(Fuse)
1506 }
1507}
1508
1509impl<'a, D: Drain + 'a> Drain for &'a D {
1510 type Ok = D::Ok;
1511 type Err = D::Err;
1512 #[inline]
1513 fn log(
1514 &self,
1515 record: &Record,
1516 values: &OwnedKVList,
1517 ) -> result::Result<Self::Ok, Self::Err> {
1518 (**self).log(record, values)
1519 }
1520 #[inline]
1521 fn is_enabled(&self, level: Level) -> bool {
1522 (**self).is_enabled(level)
1523 }
1524}
1525
1526impl<'a, D: Drain + 'a> Drain for &'a mut D {
1527 type Ok = D::Ok;
1528 type Err = D::Err;
1529 #[inline]
1530 fn log(
1531 &self,
1532 record: &Record,
1533 values: &OwnedKVList,
1534 ) -> result::Result<Self::Ok, Self::Err> {
1535 (**self).log(record, values)
1536 }
1537 #[inline]
1538 fn is_enabled(&self, level: Level) -> bool {
1539 (**self).is_enabled(level)
1540 }
1541}
1542
1543#[cfg(feature = "std")]
1544pub trait SendSyncUnwindSafe: Send + Sync + UnwindSafe {}
1549
1550#[cfg(feature = "std")]
1551impl<T> SendSyncUnwindSafe for T
1552where
1553 T: Send + Sync + UnwindSafe + ?Sized,
1554{
1555}
1556
1557#[cfg(feature = "std")]
1558pub trait SendSyncUnwindSafeDrain: Drain + Send + Sync + UnwindSafe {}
1563
1564#[cfg(feature = "std")]
1565impl<T> SendSyncUnwindSafeDrain for T
1566where
1567 T: Drain + Send + Sync + UnwindSafe + ?Sized,
1568{
1569}
1570
1571#[cfg(feature = "std")]
1572pub trait SendSyncRefUnwindSafeDrain: Drain + Send + Sync + RefUnwindSafe {}
1577
1578#[cfg(feature = "std")]
1579impl<T> SendSyncRefUnwindSafeDrain for T
1580where
1581 T: Drain + Send + Sync + RefUnwindSafe + ?Sized,
1582{
1583}
1584
1585#[cfg(feature = "std")]
1586pub trait MapErrFn<EI, EO>
1588 : 'static + Sync + Send + UnwindSafe + RefUnwindSafe + Fn(EI) -> EO {
1589}
1590
1591#[cfg(feature = "std")]
1592impl<T, EI, EO> MapErrFn<EI, EO> for T
1593where
1594 T: 'static
1595 + Sync
1596 + Send
1597 + ?Sized
1598 + UnwindSafe
1599 + RefUnwindSafe
1600 + Fn(EI) -> EO,
1601{
1602}
1603
1604#[cfg(feature = "std")]
1605pub trait FilterFn
1607 : 'static + Sync + Send + UnwindSafe + RefUnwindSafe + Fn(&Record) -> bool {
1608}
1609
1610#[cfg(feature = "std")]
1611impl<T> FilterFn for T
1612where
1613 T: 'static
1614 + Sync
1615 + Send
1616 + ?Sized
1617 + UnwindSafe
1618 + RefUnwindSafe
1619 + Fn(&Record) -> bool,
1620{
1621}
1622
1623#[cfg(not(feature = "std"))]
1624pub trait SendSyncUnwindSafeDrain: Drain + Send + Sync {}
1629
1630#[cfg(not(feature = "std"))]
1631impl<T> SendSyncUnwindSafeDrain for T
1632where
1633 T: Drain + Send + Sync + ?Sized,
1634{
1635}
1636
1637#[cfg(not(feature = "std"))]
1638pub trait SendSyncRefUnwindSafeDrain: Drain + Send + Sync {}
1643
1644#[cfg(not(feature = "std"))]
1645impl<T> SendSyncRefUnwindSafeDrain for T
1646where
1647 T: Drain + Send + Sync + ?Sized,
1648{
1649}
1650
1651#[cfg(feature = "std")]
1652pub trait SendRefUnwindSafeDrain: Drain + Send + RefUnwindSafe {}
1654
1655#[cfg(feature = "std")]
1656impl<T> SendRefUnwindSafeDrain for T
1657where
1658 T: Drain + Send + RefUnwindSafe + ?Sized,
1659{
1660}
1661
1662#[cfg(not(feature = "std"))]
1663pub trait SendRefUnwindSafeDrain: Drain + Send {}
1665
1666#[cfg(not(feature = "std"))]
1667impl<T> SendRefUnwindSafeDrain for T
1668where
1669 T: Drain + Send + ?Sized,
1670{
1671}
1672
1673#[cfg(not(feature = "std"))]
1674pub trait MapErrFn<EI, EO>: 'static + Sync + Send + Fn(EI) -> EO {}
1676
1677#[cfg(not(feature = "std"))]
1678impl<T, EI, EO> MapErrFn<EI, EO> for T
1679where
1680 T: 'static + Sync + Send + ?Sized + Fn(EI) -> EO,
1681{
1682}
1683
1684#[cfg(not(feature = "std"))]
1685pub trait FilterFn: 'static + Sync + Send + Fn(&Record) -> bool {}
1687
1688#[cfg(not(feature = "std"))]
1689impl<T> FilterFn for T
1690where
1691 T: 'static + Sync + Send + ?Sized + Fn(&Record) -> bool,
1692{
1693}
1694
1695impl<D: Drain + ?Sized> Drain for Box<D> {
1696 type Ok = D::Ok;
1697 type Err = D::Err;
1698 fn log(
1699 &self,
1700 record: &Record,
1701 o: &OwnedKVList,
1702 ) -> result::Result<Self::Ok, D::Err> {
1703 (**self).log(record, o)
1704 }
1705 #[inline]
1706 fn is_enabled(&self, level: Level) -> bool {
1707 (**self).is_enabled(level)
1708 }
1709}
1710
1711impl<D: Drain + ?Sized> Drain for Arc<D> {
1712 type Ok = D::Ok;
1713 type Err = D::Err;
1714 fn log(
1715 &self,
1716 record: &Record,
1717 o: &OwnedKVList,
1718 ) -> result::Result<Self::Ok, D::Err> {
1719 (**self).log(record, o)
1720 }
1721 #[inline]
1722 fn is_enabled(&self, level: Level) -> bool {
1723 (**self).is_enabled(level)
1724 }
1725}
1726
1727#[derive(Debug, Copy, Clone)]
1731pub struct Discard;
1732
1733impl Drain for Discard {
1734 type Ok = ();
1735 type Err = Never;
1736 fn log(&self, _: &Record, _: &OwnedKVList) -> result::Result<(), Never> {
1737 Ok(())
1738 }
1739 #[inline]
1740 fn is_enabled(&self, _1: Level) -> bool {
1741 false
1742 }
1743}
1744
1745#[derive(Debug, Clone)]
1750pub struct Filter<D: Drain, F>(pub D, pub F)
1751where
1752 F: Fn(&Record) -> bool + 'static + Send + Sync;
1753
1754impl<D: Drain, F> Filter<D, F>
1755where
1756 F: FilterFn,
1757{
1758 pub fn new(drain: D, cond: F) -> Self {
1760 Filter(drain, cond)
1761 }
1762}
1763
1764impl<D: Drain, F> Drain for Filter<D, F>
1765where
1766 F: FilterFn,
1767{
1768 type Ok = Option<D::Ok>;
1769 type Err = D::Err;
1770 fn log(
1771 &self,
1772 record: &Record,
1773 logger_values: &OwnedKVList,
1774 ) -> result::Result<Self::Ok, Self::Err> {
1775 if (self.1)(record) {
1776 Ok(Some(self.0.log(record, logger_values)?))
1777 } else {
1778 Ok(None)
1779 }
1780 }
1781 #[inline]
1782 fn is_enabled(&self, level: Level) -> bool {
1783 self.0.is_enabled(level)
1789 }
1790}
1791
1792#[derive(Debug, Clone)]
1802pub struct LevelFilter<D: Drain>(pub D, pub Level);
1803
1804impl<D: Drain> LevelFilter<D> {
1805 pub fn new(drain: D, level: Level) -> Self {
1807 LevelFilter(drain, level)
1808 }
1809}
1810
1811impl<D: Drain> Drain for LevelFilter<D> {
1812 type Ok = Option<D::Ok>;
1813 type Err = D::Err;
1814 fn log(
1815 &self,
1816 record: &Record,
1817 logger_values: &OwnedKVList,
1818 ) -> result::Result<Self::Ok, Self::Err> {
1819 if record.level().is_at_least(self.1) {
1820 Ok(Some(self.0.log(record, logger_values)?))
1821 } else {
1822 Ok(None)
1823 }
1824 }
1825 #[inline]
1826 fn is_enabled(&self, level: Level) -> bool {
1827 level.is_at_least(self.1) && self.0.is_enabled(level)
1828 }
1829}
1830
1831pub struct MapError<D: Drain, E> {
1835 drain: D,
1836 map_fn: Box<MapErrFn<D::Err, E, Output = E>>,
1838}
1839
1840impl<D: Drain, E> MapError<D, E> {
1841 pub fn new<F>(drain: D, map_fn: F) -> Self
1843 where
1844 F: MapErrFn<<D as Drain>::Err, E>,
1845 {
1846 MapError {
1847 drain: drain,
1848 map_fn: Box::new(map_fn),
1849 }
1850 }
1851}
1852
1853impl<D: Drain, E> Drain for MapError<D, E> {
1854 type Ok = D::Ok;
1855 type Err = E;
1856 fn log(
1857 &self,
1858 record: &Record,
1859 logger_values: &OwnedKVList,
1860 ) -> result::Result<Self::Ok, Self::Err> {
1861 self.drain
1862 .log(record, logger_values)
1863 .map_err(|e| (self.map_fn)(e))
1864 }
1865 #[inline]
1866 fn is_enabled(&self, level: Level) -> bool {
1867 self.drain.is_enabled(level)
1868 }
1869}
1870
1871#[derive(Debug, Clone)]
1875pub struct Duplicate<D1: Drain, D2: Drain>(pub D1, pub D2);
1876
1877impl<D1: Drain, D2: Drain> Duplicate<D1, D2> {
1878 pub fn new(drain1: D1, drain2: D2) -> Self {
1880 Duplicate(drain1, drain2)
1881 }
1882}
1883
1884impl<D1: Drain, D2: Drain> Drain for Duplicate<D1, D2> {
1885 type Ok = (D1::Ok, D2::Ok);
1886 type Err = (
1887 result::Result<D1::Ok, D1::Err>,
1888 result::Result<D2::Ok, D2::Err>,
1889 );
1890 fn log(
1891 &self,
1892 record: &Record,
1893 logger_values: &OwnedKVList,
1894 ) -> result::Result<Self::Ok, Self::Err> {
1895 let res1 = self.0.log(record, logger_values);
1896 let res2 = self.1.log(record, logger_values);
1897
1898 match (res1, res2) {
1899 (Ok(o1), Ok(o2)) => Ok((o1, o2)),
1900 (r1, r2) => Err((r1, r2)),
1901 }
1902 }
1903 #[inline]
1904 fn is_enabled(&self, level: Level) -> bool {
1905 self.0.is_enabled(level) || self.1.is_enabled(level)
1906 }
1907}
1908
1909#[derive(Debug, Clone)]
1917pub struct Fuse<D: Drain>(pub D)
1918where
1919 D::Err: fmt::Debug;
1920
1921impl<D: Drain> Fuse<D>
1922where
1923 D::Err: fmt::Debug,
1924{
1925 pub fn new(drain: D) -> Self {
1927 Fuse(drain)
1928 }
1929}
1930
1931impl<D: Drain> Drain for Fuse<D>
1932where
1933 D::Err: fmt::Debug,
1934{
1935 type Ok = ();
1936 type Err = Never;
1937 fn log(
1938 &self,
1939 record: &Record,
1940 logger_values: &OwnedKVList,
1941 ) -> result::Result<Self::Ok, Never> {
1942 let _ = self.0
1943 .log(record, logger_values)
1944 .unwrap_or_else(|e| panic!("slog::Fuse Drain: {:?}", e));
1945 Ok(())
1946 }
1947 #[inline]
1948 fn is_enabled(&self, level: Level) -> bool {
1949 self.0.is_enabled(level)
1950 }
1951}
1952
1953#[derive(Clone)]
1959pub struct IgnoreResult<D: Drain> {
1960 drain: D,
1961}
1962
1963impl<D: Drain> IgnoreResult<D> {
1964 pub fn new(drain: D) -> Self {
1966 IgnoreResult { drain: drain }
1967 }
1968}
1969
1970impl<D: Drain> Drain for IgnoreResult<D> {
1971 type Ok = ();
1972 type Err = Never;
1973 fn log(
1974 &self,
1975 record: &Record,
1976 logger_values: &OwnedKVList,
1977 ) -> result::Result<(), Never> {
1978 let _ = self.drain.log(record, logger_values);
1979 Ok(())
1980 }
1981
1982 #[inline]
1983 fn is_enabled(&self, level: Level) -> bool {
1984 self.drain.is_enabled(level)
1985 }
1986}
1987
1988#[cfg(feature = "std")]
1990#[derive(Clone)]
1991pub enum MutexDrainError<D: Drain> {
1992 Mutex,
1994 Drain(D::Err),
1996}
1997
1998#[cfg(feature = "std")]
1999impl<D> fmt::Debug for MutexDrainError<D>
2000where
2001 D: Drain,
2002 D::Err: fmt::Debug,
2003{
2004 fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
2005 match *self {
2006 MutexDrainError::Mutex => write!(f, "MutexDrainError::Mutex"),
2007 MutexDrainError::Drain(ref e) => e.fmt(f),
2008 }
2009 }
2010}
2011
2012#[cfg(feature = "std")]
2013impl<D> std::error::Error for MutexDrainError<D>
2014where
2015 D: Drain,
2016 D::Err: fmt::Debug + fmt::Display + std::error::Error,
2017{
2018 fn description(&self) -> &str {
2019 match *self {
2020 MutexDrainError::Mutex => "Mutex acquire failed",
2021 MutexDrainError::Drain(ref e) => e.description(),
2022 }
2023 }
2024
2025 fn cause(&self) -> Option<&std::error::Error> {
2026 match *self {
2027 MutexDrainError::Mutex => None,
2028 MutexDrainError::Drain(ref e) => Some(e),
2029 }
2030 }
2031}
2032
2033#[cfg(feature = "std")]
2034impl<'a, D: Drain> From<std::sync::PoisonError<std::sync::MutexGuard<'a, D>>>
2035 for MutexDrainError<D> {
2036 fn from(
2037 _: std::sync::PoisonError<std::sync::MutexGuard<'a, D>>,
2038 ) -> MutexDrainError<D> {
2039 MutexDrainError::Mutex
2040 }
2041}
2042
2043#[cfg(feature = "std")]
2044impl<D: Drain> fmt::Display for MutexDrainError<D>
2045where
2046 D::Err: fmt::Display,
2047{
2048 fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
2049 match *self {
2050 MutexDrainError::Mutex => write!(f, "MutexError"),
2051 MutexDrainError::Drain(ref e) => write!(f, "{}", e),
2052 }
2053 }
2054}
2055
2056#[cfg(feature = "std")]
2057impl<D: Drain> Drain for std::sync::Mutex<D> {
2058 type Ok = D::Ok;
2059 type Err = MutexDrainError<D>;
2060 fn log(
2061 &self,
2062 record: &Record,
2063 logger_values: &OwnedKVList,
2064 ) -> result::Result<Self::Ok, Self::Err> {
2065 let d = self.lock()?;
2066 d.log(record, logger_values).map_err(MutexDrainError::Drain)
2067 }
2068 #[inline]
2069 fn is_enabled(&self, level: Level) -> bool {
2070 self.lock().ok().map_or(true, |lock| lock.is_enabled(level))
2071 }
2072}
2073pub static LOG_LEVEL_NAMES: [&'static str; 7] =
2080 ["OFF", "CRITICAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
2081
2082pub static LOG_LEVEL_SHORT_NAMES: [&'static str; 7] =
2086 ["OFF", "CRIT", "ERRO", "WARN", "INFO", "DEBG", "TRCE"];
2087
2088#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2090pub enum Level {
2091 Critical,
2093 Error,
2095 Warning,
2097 Info,
2099 Debug,
2101 Trace,
2103}
2104
2105#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2107pub enum FilterLevel {
2108 Off,
2110 Critical,
2112 Error,
2114 Warning,
2116 Info,
2118 Debug,
2120 Trace,
2122}
2123
2124impl Level {
2125 pub fn as_short_str(&self) -> &'static str {
2127 LOG_LEVEL_SHORT_NAMES[self.as_usize()]
2128 }
2129
2130 pub fn as_str(&self) -> &'static str {
2132 LOG_LEVEL_NAMES[self.as_usize()]
2133 }
2134
2135 #[inline]
2139 pub fn as_usize(&self) -> usize {
2140 match *self {
2141 Level::Critical => 1,
2142 Level::Error => 2,
2143 Level::Warning => 3,
2144 Level::Info => 4,
2145 Level::Debug => 5,
2146 Level::Trace => 6,
2147 }
2148 }
2149
2150 #[inline]
2154 pub fn from_usize(u: usize) -> Option<Level> {
2155 match u {
2156 1 => Some(Level::Critical),
2157 2 => Some(Level::Error),
2158 3 => Some(Level::Warning),
2159 4 => Some(Level::Info),
2160 5 => Some(Level::Debug),
2161 6 => Some(Level::Trace),
2162 _ => None,
2163 }
2164 }
2165}
2166
2167impl FilterLevel {
2168 pub fn as_short_str(&self) -> &'static str {
2170 LOG_LEVEL_SHORT_NAMES[self.as_usize()]
2171 }
2172
2173 pub fn as_str(&self) -> &'static str {
2175 LOG_LEVEL_NAMES[self.as_usize()]
2176 }
2177
2178 #[inline]
2182 pub fn as_usize(&self) -> usize {
2183 match *self {
2184 FilterLevel::Off => 0,
2185 FilterLevel::Critical => 1,
2186 FilterLevel::Error => 2,
2187 FilterLevel::Warning => 3,
2188 FilterLevel::Info => 4,
2189 FilterLevel::Debug => 5,
2190 FilterLevel::Trace => 6,
2191 }
2192 }
2193
2194 #[inline]
2198 pub fn from_usize(u: usize) -> Option<FilterLevel> {
2199 match u {
2200 0 => Some(FilterLevel::Off),
2201 1 => Some(FilterLevel::Critical),
2202 2 => Some(FilterLevel::Error),
2203 3 => Some(FilterLevel::Warning),
2204 4 => Some(FilterLevel::Info),
2205 5 => Some(FilterLevel::Debug),
2206 6 => Some(FilterLevel::Trace),
2207 _ => None,
2208 }
2209 }
2210
2211 #[inline]
2213 pub fn max() -> Self {
2214 FilterLevel::Trace
2215 }
2216
2217 #[inline]
2219 pub fn min() -> Self {
2220 FilterLevel::Off
2221 }
2222
2223 pub fn accepts(self, level: Level) -> bool {
2225 self.as_usize() >= level.as_usize()
2226 }
2227}
2228
2229impl FromStr for Level {
2230 type Err = ();
2231 fn from_str(name: &str) -> core::result::Result<Level, ()> {
2232 index_of_log_level_name(name)
2233 .and_then(|idx| Level::from_usize(idx))
2234 .ok_or(())
2235 }
2236}
2237
2238impl FromStr for FilterLevel {
2239 type Err = ();
2240 fn from_str(name: &str) -> core::result::Result<FilterLevel, ()> {
2241 index_of_log_level_name(name)
2242 .and_then(|idx| FilterLevel::from_usize(idx))
2243 .ok_or(())
2244 }
2245}
2246
2247fn index_of_log_level_name(name: &str) -> Option<usize> {
2248 index_of_str_ignore_case(&LOG_LEVEL_NAMES, name)
2249 .or_else(|| index_of_str_ignore_case(&LOG_LEVEL_SHORT_NAMES, name))
2250}
2251
2252fn index_of_str_ignore_case(haystack: &[&str], needle: &str) -> Option<usize> {
2253 if needle.is_empty() {
2254 return None;
2255 }
2256 haystack.iter()
2257 .map(|hay| &hay[..needle.len().min(hay.len())])
2259 .position(|hay| hay.eq_ignore_ascii_case(needle))
2260}
2261
2262impl fmt::Display for Level {
2263 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2264 write!(f, "{}", self.as_short_str())
2265 }
2266}
2267
2268impl fmt::Display for FilterLevel {
2269 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2270 write!(f, "{}", self.as_short_str())
2271 }
2272}
2273
2274impl Level {
2275 #[inline]
2277 pub fn is_at_least(&self, level: Self) -> bool {
2278 self.as_usize() <= level.as_usize()
2279 }
2280}
2281
2282#[test]
2283fn level_at_least() {
2284 assert!(Level::Debug.is_at_least(Level::Debug));
2285 assert!(Level::Debug.is_at_least(Level::Trace));
2286 assert!(!Level::Debug.is_at_least(Level::Info));
2287}
2288
2289#[test]
2290fn filter_level_sanity() {
2291 assert!(Level::Critical.as_usize() > FilterLevel::Off.as_usize());
2292 assert!(Level::Critical.as_usize() == FilterLevel::Critical.as_usize());
2293 assert!(Level::Trace.as_usize() == FilterLevel::Trace.as_usize());
2294}
2295
2296#[test]
2297fn level_from_str() {
2298 refute_from_str::<Level>("off");
2299 assert_from_str(Level::Critical, "critical");
2300 assert_from_str(Level::Critical, "crit");
2301 assert_from_str(Level::Error, "error");
2302 assert_from_str(Level::Error, "erro");
2303 assert_from_str(Level::Warning, "warn");
2304 assert_from_str(Level::Info, "info");
2305 assert_from_str(Level::Debug, "debug");
2306 assert_from_str(Level::Debug, "debg");
2307 assert_from_str(Level::Trace, "trace");
2308 assert_from_str(Level::Trace, "trce");
2309
2310 assert_from_str(Level::Info, "Info");
2311 assert_from_str(Level::Info, "INFO");
2312 assert_from_str(Level::Info, "iNfO");
2313
2314 refute_from_str::<Level>("");
2315 assert_from_str(Level::Info, "i");
2316 assert_from_str(Level::Info, "in");
2317 assert_from_str(Level::Info, "inf");
2318 refute_from_str::<Level>("infor");
2319
2320 refute_from_str::<Level>("?");
2321 refute_from_str::<Level>("info ");
2322 refute_from_str::<Level>(" info");
2323 refute_from_str::<Level>("desinfo");
2324}
2325
2326#[test]
2327fn filter_level_from_str() {
2328 assert_from_str(FilterLevel::Off, "off");
2329 assert_from_str(FilterLevel::Critical, "critical");
2330 assert_from_str(FilterLevel::Critical, "crit");
2331 assert_from_str(FilterLevel::Error, "error");
2332 assert_from_str(FilterLevel::Error, "erro");
2333 assert_from_str(FilterLevel::Warning, "warn");
2334 assert_from_str(FilterLevel::Info, "info");
2335 assert_from_str(FilterLevel::Debug, "debug");
2336 assert_from_str(FilterLevel::Debug, "debg");
2337 assert_from_str(FilterLevel::Trace, "trace");
2338 assert_from_str(FilterLevel::Trace, "trce");
2339
2340 assert_from_str(FilterLevel::Info, "Info");
2341 assert_from_str(FilterLevel::Info, "INFO");
2342 assert_from_str(FilterLevel::Info, "iNfO");
2343
2344 refute_from_str::<FilterLevel>("");
2345 assert_from_str(FilterLevel::Info, "i");
2346 assert_from_str(FilterLevel::Info, "in");
2347 assert_from_str(FilterLevel::Info, "inf");
2348 refute_from_str::<FilterLevel>("infor");
2349
2350 refute_from_str::<FilterLevel>("?");
2351 refute_from_str::<FilterLevel>("info ");
2352 refute_from_str::<FilterLevel>(" info");
2353 refute_from_str::<FilterLevel>("desinfo");
2354}
2355
2356#[cfg(test)]
2357fn assert_from_str<T>(expected: T, level_str: &str)
2358 where
2359 T: FromStr + fmt::Debug + PartialEq,
2360 T::Err: fmt::Debug {
2361 let result = T::from_str(level_str);
2362
2363 let actual = result.unwrap_or_else(|e| {
2364 panic!("Failed to parse filter level '{}': {:?}", level_str, e)
2365 });
2366 assert_eq!(expected, actual, "Invalid filter level parsed from '{}'", level_str);
2367}
2368
2369#[cfg(test)]
2370fn refute_from_str<T>(level_str: &str)
2371 where
2372 T: FromStr + fmt::Debug {
2373 let result = T::from_str(level_str);
2374
2375 if let Ok(level) = result {
2376 panic!("Parsing filter level '{}' succeeded: {:?}", level_str, level)
2377 }
2378}
2379
2380#[cfg(feature = "std")]
2381#[test]
2382fn level_to_string_and_from_str_are_compatible() {
2383 assert_to_string_from_str(Level::Critical);
2384 assert_to_string_from_str(Level::Error);
2385 assert_to_string_from_str(Level::Warning);
2386 assert_to_string_from_str(Level::Info);
2387 assert_to_string_from_str(Level::Debug);
2388 assert_to_string_from_str(Level::Trace);
2389}
2390
2391#[cfg(feature = "std")]
2392#[test]
2393fn filter_level_to_string_and_from_str_are_compatible() {
2394 assert_to_string_from_str(FilterLevel::Off);
2395 assert_to_string_from_str(FilterLevel::Critical);
2396 assert_to_string_from_str(FilterLevel::Error);
2397 assert_to_string_from_str(FilterLevel::Warning);
2398 assert_to_string_from_str(FilterLevel::Info);
2399 assert_to_string_from_str(FilterLevel::Debug);
2400 assert_to_string_from_str(FilterLevel::Trace);
2401}
2402
2403#[cfg(all(test, feature = "std"))]
2404fn assert_to_string_from_str<T>(expected: T)
2405 where
2406 T: std::string::ToString + FromStr + PartialEq + fmt::Debug,
2407 <T as FromStr>::Err: fmt::Debug {
2408 let string = expected.to_string();
2409
2410 let actual = T::from_str(&string)
2411 .expect(&format!("Failed to parse string representation of {:?}", expected));
2412
2413 assert_eq!(expected, actual, "Invalid value parsed from string representation of {:?}", actual);
2414}
2415
2416#[test]
2417fn filter_level_accepts_tests() {
2418 assert_eq!(true, FilterLevel::Warning.accepts(Level::Error));
2419 assert_eq!(true, FilterLevel::Warning.accepts(Level::Warning));
2420 assert_eq!(false, FilterLevel::Warning.accepts(Level::Info));
2421 assert_eq!(false, FilterLevel::Off.accepts(Level::Critical));
2422}
2423#[doc(hidden)]
2427#[derive(Clone, Copy)]
2428pub struct RecordLocation {
2429 pub file: &'static str,
2431 pub line: u32,
2433 pub column: u32,
2435 pub function: &'static str,
2437 pub module: &'static str,
2439}
2440pub struct RecordStatic<'a> {
2445 #[doc(hidden)]
2447 pub location: &'a RecordLocation,
2448 #[doc(hidden)]
2450 pub tag: &'a str,
2451 #[doc(hidden)]
2453 pub level: Level,
2454}
2455
2456pub struct Record<'a> {
2465 rstatic: &'a RecordStatic<'a>,
2466 msg: &'a fmt::Arguments<'a>,
2467 kv: BorrowedKV<'a>,
2468}
2469
2470impl<'a> Record<'a> {
2471 #[inline]
2481 pub fn new(
2482 s: &'a RecordStatic<'a>,
2483 msg: &'a fmt::Arguments<'a>,
2484 kv: BorrowedKV<'a>,
2485 ) -> Self {
2486 Record {
2487 rstatic: s,
2488 msg: msg,
2489 kv: kv,
2490 }
2491 }
2492
2493 pub fn msg(&self) -> &fmt::Arguments {
2495 self.msg
2496 }
2497
2498 pub fn level(&self) -> Level {
2500 self.rstatic.level
2501 }
2502
2503 pub fn line(&self) -> u32 {
2505 self.rstatic.location.line
2506 }
2507
2508 pub fn location(&self) -> &RecordLocation {
2510 self.rstatic.location
2511 }
2512
2513 pub fn column(&self) -> u32 {
2515 self.rstatic.location.column
2516 }
2517
2518 pub fn file(&self) -> &'static str {
2520 self.rstatic.location.file
2521 }
2522
2523 pub fn tag(&self) -> &str {
2533 self.rstatic.tag
2534 }
2535
2536 pub fn module(&self) -> &'static str {
2538 self.rstatic.location.module
2539 }
2540
2541 pub fn function(&self) -> &'static str {
2549 self.rstatic.location.function
2550 }
2551
2552 pub fn kv(&self) -> BorrowedKV {
2554 BorrowedKV(self.kv.0)
2555 }
2556}
2557#[cfg(macro_workaround)]
2562macro_rules! impl_default_as_fmt{
2563 (#[$m:meta] $($t:tt)+) => {
2564 #[$m]
2565 impl_default_as_fmt!($($t)*);
2566 };
2567 ($t:ty => $f:ident) => {
2568 #[allow(missing_docs)]
2569 fn $f(&mut self, key : Key, val : $t)
2570 -> Result {
2571 self.emit_arguments(key, &format_args!("{}", val))
2572 }
2573 };
2574}
2575
2576#[cfg(not(macro_workaround))]
2577macro_rules! impl_default_as_fmt{
2578 ($(#[$m:meta])* $t:ty => $f:ident) => {
2579 $(#[$m])*
2580 fn $f(&mut self, key : Key, val : $t)
2581 -> Result {
2582 self.emit_arguments(key, &format_args!("{}", val))
2583 }
2584 };
2585}
2586
2587#[cfg(feature = "nested-values")]
2592struct SerializerForward<'a, T: 'a + ?Sized>(&'a mut T);
2593
2594#[cfg(feature = "nested-values")]
2595impl<'a, T: Serializer + 'a + ?Sized> Serializer for SerializerForward<'a, T> {
2596 fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> Result {
2597 self.0.emit_arguments(key, val)
2598 }
2599
2600 #[cfg(feature = "nested-values")]
2601 fn emit_serde(&mut self, _key: Key, _value: &SerdeValue) -> Result {
2602 panic!();
2603 }
2604}
2605
2606pub trait Serializer {
2611 impl_default_as_fmt! {
2612 usize => emit_usize
2614 }
2615 impl_default_as_fmt! {
2616 isize => emit_isize
2618 }
2619 impl_default_as_fmt! {
2620 bool => emit_bool
2622 }
2623 impl_default_as_fmt! {
2624 char => emit_char
2626 }
2627 impl_default_as_fmt! {
2628 u8 => emit_u8
2630 }
2631 impl_default_as_fmt! {
2632 i8 => emit_i8
2634 }
2635 impl_default_as_fmt! {
2636 u16 => emit_u16
2638 }
2639 impl_default_as_fmt! {
2640 i16 => emit_i16
2642 }
2643 impl_default_as_fmt! {
2644 u32 => emit_u32
2646 }
2647 impl_default_as_fmt! {
2648 i32 => emit_i32
2650 }
2651 impl_default_as_fmt! {
2652 f32 => emit_f32
2654 }
2655 impl_default_as_fmt! {
2656 u64 => emit_u64
2658 }
2659 impl_default_as_fmt! {
2660 i64 => emit_i64
2662 }
2663 impl_default_as_fmt! {
2664 f64 => emit_f64
2666 }
2667 impl_default_as_fmt! {
2668 #[cfg(integer128)]
2670 u128 => emit_u128
2671 }
2672 impl_default_as_fmt! {
2673 #[cfg(integer128)]
2675 i128 => emit_i128
2676 }
2677 impl_default_as_fmt! {
2678 &str => emit_str
2680 }
2681
2682 fn emit_unit(&mut self, key: Key) -> Result {
2684 self.emit_arguments(key, &format_args!("()"))
2685 }
2686
2687 fn emit_none(&mut self, key: Key) -> Result {
2689 self.emit_arguments(key, &format_args!(""))
2690 }
2691
2692 fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> Result;
2698
2699 #[cfg(feature = "nested-values")]
2709 fn emit_serde(&mut self, key: Key, value: &SerdeValue) -> Result {
2710 value.serialize_fallback(key, &mut SerializerForward(self))
2711 }
2712
2713 #[cfg(feature = "std")]
2726 fn emit_error(&mut self, key: Key, error: &(std::error::Error + 'static)) -> Result {
2727 self.emit_arguments(key, &format_args!("{}", ErrorAsFmt(error)))
2728 }
2729}
2730
2731struct AsFmtSerializer<F>(pub F)
2735where
2736 F: for<'a> FnMut(Key, fmt::Arguments<'a>) -> Result;
2737
2738impl<F> Serializer for AsFmtSerializer<F>
2739where
2740 F: for<'a> FnMut(Key, fmt::Arguments<'a>) -> Result,
2741{
2742 fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> Result {
2743 (self.0)(key, *val)
2744 }
2745}
2746
2747#[cfg(feature = "std")]
2752struct ErrorAsFmt<'a>(pub &'a (std::error::Error + 'static));
2753
2754#[cfg(feature = "std")]
2755impl<'a> fmt::Display for ErrorAsFmt<'a> {
2756 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2757 #![allow(deprecated)]
2760 write!(f, "{}", self.0)?;
2761 let mut error = self.0.cause();
2762 while let Some(source) = error {
2763 write!(f, ": {}", source)?;
2764 error = source.cause();
2765 }
2766 Ok(())
2767 }
2768}
2769
2770#[cfg(feature = "nested-values")]
2777pub trait SerdeValue: erased_serde::Serialize + Value {
2778 fn serialize_fallback(
2788 &self,
2789 _key: Key,
2790 _serializer: &mut Serializer,
2791 ) -> Result<()> {
2792 Err(Error::Other)
2793 }
2794
2795 fn as_serde(&self) -> &erased_serde::Serialize;
2798
2799 fn to_sendable(&self) -> Box<SerdeValue + Send + 'static>;
2803}
2804
2805pub trait Value {
2831 fn serialize(
2836 &self,
2837 record: &Record,
2838 key: Key,
2839 serializer: &mut Serializer,
2840 ) -> Result;
2841}
2842
2843impl<'a, V> Value for &'a V
2844where
2845 V: Value + ?Sized,
2846{
2847 fn serialize(
2848 &self,
2849 record: &Record,
2850 key: Key,
2851 serializer: &mut Serializer,
2852 ) -> Result {
2853 (*self).serialize(record, key, serializer)
2854 }
2855}
2856
2857macro_rules! impl_value_for{
2858 ($t:ty, $f:ident) => {
2859 impl Value for $t {
2860 fn serialize(&self,
2861 _record : &Record,
2862 key : Key,
2863 serializer : &mut Serializer
2864 ) -> Result {
2865 serializer.$f(key, *self)
2866 }
2867 }
2868 };
2869}
2870
2871impl_value_for!(usize, emit_usize);
2872impl_value_for!(isize, emit_isize);
2873impl_value_for!(bool, emit_bool);
2874impl_value_for!(char, emit_char);
2875impl_value_for!(u8, emit_u8);
2876impl_value_for!(i8, emit_i8);
2877impl_value_for!(u16, emit_u16);
2878impl_value_for!(i16, emit_i16);
2879impl_value_for!(u32, emit_u32);
2880impl_value_for!(i32, emit_i32);
2881impl_value_for!(f32, emit_f32);
2882impl_value_for!(u64, emit_u64);
2883impl_value_for!(i64, emit_i64);
2884impl_value_for!(f64, emit_f64);
2885#[cfg(integer128)]
2886impl_value_for!(u128, emit_u128);
2887#[cfg(integer128)]
2888impl_value_for!(i128, emit_i128);
2889
2890impl Value for () {
2891 fn serialize(
2892 &self,
2893 _record: &Record,
2894 key: Key,
2895 serializer: &mut Serializer,
2896 ) -> Result {
2897 serializer.emit_unit(key)
2898 }
2899}
2900
2901impl Value for str {
2902 fn serialize(
2903 &self,
2904 _record: &Record,
2905 key: Key,
2906 serializer: &mut Serializer,
2907 ) -> Result {
2908 serializer.emit_str(key, self)
2909 }
2910}
2911
2912impl<'a> Value for fmt::Arguments<'a> {
2913 fn serialize(
2914 &self,
2915 _record: &Record,
2916 key: Key,
2917 serializer: &mut Serializer,
2918 ) -> Result {
2919 serializer.emit_arguments(key, self)
2920 }
2921}
2922
2923impl Value for String {
2924 fn serialize(
2925 &self,
2926 _record: &Record,
2927 key: Key,
2928 serializer: &mut Serializer,
2929 ) -> Result {
2930 serializer.emit_str(key, self.as_str())
2931 }
2932}
2933
2934impl<T: Value> Value for Option<T> {
2935 fn serialize(
2936 &self,
2937 record: &Record,
2938 key: Key,
2939 serializer: &mut Serializer,
2940 ) -> Result {
2941 match *self {
2942 Some(ref s) => s.serialize(record, key, serializer),
2943 None => serializer.emit_none(key),
2944 }
2945 }
2946}
2947
2948impl<T> Value for Box<T>
2949where
2950 T: Value + ?Sized,
2951{
2952 fn serialize(
2953 &self,
2954 record: &Record,
2955 key: Key,
2956 serializer: &mut Serializer,
2957 ) -> Result {
2958 (**self).serialize(record, key, serializer)
2959 }
2960}
2961impl<T> Value for Arc<T>
2962where
2963 T: Value + ?Sized,
2964{
2965 fn serialize(
2966 &self,
2967 record: &Record,
2968 key: Key,
2969 serializer: &mut Serializer,
2970 ) -> Result {
2971 (**self).serialize(record, key, serializer)
2972 }
2973}
2974
2975impl<T> Value for Rc<T>
2976where
2977 T: Value,
2978{
2979 fn serialize(
2980 &self,
2981 record: &Record,
2982 key: Key,
2983 serializer: &mut Serializer,
2984 ) -> Result {
2985 (**self).serialize(record, key, serializer)
2986 }
2987}
2988
2989impl<T> Value for core::num::Wrapping<T>
2990where
2991 T: Value,
2992{
2993 fn serialize(
2994 &self,
2995 record: &Record,
2996 key: Key,
2997 serializer: &mut Serializer,
2998 ) -> Result {
2999 self.0.serialize(record, key, serializer)
3000 }
3001}
3002
3003#[cfg(feature = "std")]
3004impl<'a> Value for std::path::Display<'a> {
3005 fn serialize(
3006 &self,
3007 _record: &Record,
3008 key: Key,
3009 serializer: &mut Serializer,
3010 ) -> Result {
3011 serializer.emit_arguments(key, &format_args!("{}", *self))
3012 }
3013}
3014
3015#[cfg(feature = "std")]
3016impl Value for std::net::SocketAddr {
3017 fn serialize(
3018 &self,
3019 _record: &Record,
3020 key: Key,
3021 serializer: &mut Serializer,
3022 ) -> Result {
3023 serializer.emit_arguments(key, &format_args!("{}", self))
3024 }
3025}
3026
3027#[cfg(feature = "std")]
3028impl Value for std::io::Error {
3029 fn serialize(
3030 &self,
3031 _record: &Record,
3032 key: Key,
3033 serializer: &mut Serializer,
3034 ) -> Result {
3035 serializer.emit_error(key, self)
3036 }
3037}
3038
3039pub struct FnValue<V: Value, F>(pub F)
3041where
3042 F: for<'c, 'd> Fn(&'c Record<'d>) -> V;
3043
3044impl<'a, V: 'a + Value, F> Value for FnValue<V, F>
3045where
3046 F: 'a + for<'c, 'd> Fn(&'c Record<'d>) -> V,
3047{
3048 fn serialize(
3049 &self,
3050 record: &Record,
3051 key: Key,
3052 serializer: &mut Serializer,
3053 ) -> Result {
3054 (self.0)(record).serialize(record, key, serializer)
3055 }
3056}
3057
3058#[deprecated(note = "Renamed to `PushFnValueSerializer`")]
3059pub type PushFnSerializer<'a> = PushFnValueSerializer<'a>;
3061
3062pub struct PushFnValueSerializer<'a> {
3067 record: &'a Record<'a>,
3068 key: Key,
3069 serializer: &'a mut Serializer,
3070 done: bool,
3071}
3072
3073impl<'a> PushFnValueSerializer<'a> {
3074 #[deprecated(note = "Renamed to `emit`")]
3075 pub fn serialize<'b, S: 'b + Value>(self, s: S) -> Result {
3077 self.emit(s)
3078 }
3079
3080 pub fn emit<'b, S: 'b + Value>(mut self, s: S) -> Result {
3084 self.done = true;
3085 s.serialize(self.record, self.key.clone(), self.serializer)
3086 }
3087}
3088
3089impl<'a> Drop for PushFnValueSerializer<'a> {
3090 fn drop(&mut self) {
3091 if !self.done {
3092 let _ = self.serializer.emit_unit(self.key.clone());
3094 }
3095 }
3096}
3097
3098pub struct PushFnValue<F>(pub F)
3134where
3135 F: 'static
3136 + for<'c, 'd> Fn(&'c Record<'d>, PushFnValueSerializer<'c>) -> Result;
3137
3138impl<F> Value for PushFnValue<F>
3139where
3140 F: 'static
3141 + for<'c, 'd> Fn(&'c Record<'d>, PushFnValueSerializer<'c>) -> Result,
3142{
3143 fn serialize(
3144 &self,
3145 record: &Record,
3146 key: Key,
3147 serializer: &mut Serializer,
3148 ) -> Result {
3149 let ser = PushFnValueSerializer {
3150 record: record,
3151 key: key,
3152 serializer: serializer,
3153 done: false,
3154 };
3155 (self.0)(record, ser)
3156 }
3157}
3158
3159#[cfg(feature = "std")]
3168pub struct ErrorValue<E: std::error::Error>(pub E);
3169
3170#[cfg(feature = "std")]
3171impl<E> Value for ErrorValue<E>
3172where
3173 E: 'static + std::error::Error,
3174{
3175 fn serialize(
3176 &self,
3177 _record: &Record,
3178 key: Key,
3179 serializer: &mut Serializer,
3180 ) -> Result {
3181 serializer.emit_error(key, &self.0)
3182 }
3183}
3184
3185pub trait KV {
3239 fn serialize(&self, record: &Record, serializer: &mut Serializer)
3244 -> Result;
3245}
3246
3247impl<'a, T> KV for &'a T
3248where
3249 T: KV,
3250{
3251 fn serialize(
3252 &self,
3253 record: &Record,
3254 serializer: &mut Serializer,
3255 ) -> Result {
3256 (**self).serialize(record, serializer)
3257 }
3258}
3259
3260#[cfg(feature = "nothreads")]
3261pub trait SendSyncRefUnwindSafeKV: KV {}
3265
3266#[cfg(feature = "nothreads")]
3267impl<T> SendSyncRefUnwindSafeKV for T where T: KV + ?Sized {}
3268
3269#[cfg(all(not(feature = "nothreads"), feature = "std"))]
3270pub trait SendSyncRefUnwindSafeKV: KV + Send + Sync + RefUnwindSafe {}
3272
3273#[cfg(all(not(feature = "nothreads"), feature = "std"))]
3274impl<T> SendSyncRefUnwindSafeKV for T where T: KV + Send + Sync + RefUnwindSafe + ?Sized {}
3275
3276#[cfg(all(not(feature = "nothreads"), not(feature = "std")))]
3277pub trait SendSyncRefUnwindSafeKV: KV + Send + Sync {}
3279
3280#[cfg(all(not(feature = "nothreads"), not(feature = "std")))]
3281impl<T> SendSyncRefUnwindSafeKV for T where T: KV + Send + Sync + ?Sized {}
3282
3283pub struct SingleKV<V>(pub Key, pub V)
3285where
3286 V: Value;
3287
3288#[cfg(feature = "dynamic-keys")]
3289impl<V: Value> From<(String, V)> for SingleKV<V> {
3290 fn from(x: (String, V)) -> SingleKV<V> {
3291 SingleKV(Key::from(x.0), x.1)
3292 }
3293}
3294#[cfg(feature = "dynamic-keys")]
3295impl<V: Value> From<(&'static str, V)> for SingleKV<V> {
3296 fn from(x: (&'static str, V)) -> SingleKV<V> {
3297 SingleKV(Key::from(x.0), x.1)
3298 }
3299}
3300#[cfg(not(feature = "dynamic-keys"))]
3301impl<V: Value> From<(&'static str, V)> for SingleKV<V> {
3302 fn from(x: (&'static str, V)) -> SingleKV<V> {
3303 SingleKV(x.0, x.1)
3304 }
3305}
3306
3307impl<V> KV for SingleKV<V>
3308where
3309 V: Value,
3310{
3311 fn serialize(
3312 &self,
3313 record: &Record,
3314 serializer: &mut Serializer,
3315 ) -> Result {
3316 self.1.serialize(record, self.0.clone(), serializer)
3317 }
3318}
3319
3320impl KV for () {
3321 fn serialize(
3322 &self,
3323 _record: &Record,
3324 _serializer: &mut Serializer,
3325 ) -> Result {
3326 Ok(())
3327 }
3328}
3329
3330impl<T: KV, R: KV> KV for (T, R) {
3331 fn serialize(
3332 &self,
3333 record: &Record,
3334 serializer: &mut Serializer,
3335 ) -> Result {
3336 try!(self.0.serialize(record, serializer));
3337 self.1.serialize(record, serializer)
3338 }
3339}
3340
3341impl<T> KV for Box<T>
3342where
3343 T: KV + ?Sized,
3344{
3345 fn serialize(
3346 &self,
3347 record: &Record,
3348 serializer: &mut Serializer,
3349 ) -> Result {
3350 (**self).serialize(record, serializer)
3351 }
3352}
3353
3354impl<T> KV for Arc<T>
3355where
3356 T: KV + ?Sized,
3357{
3358 fn serialize(
3359 &self,
3360 record: &Record,
3361 serializer: &mut Serializer,
3362 ) -> Result {
3363 (**self).serialize(record, serializer)
3364 }
3365}
3366
3367impl<T> KV for OwnedKV<T>
3368where
3369 T: SendSyncRefUnwindSafeKV + ?Sized,
3370{
3371 fn serialize(
3372 &self,
3373 record: &Record,
3374 serializer: &mut Serializer,
3375 ) -> Result {
3376 self.0.serialize(record, serializer)
3377 }
3378}
3379
3380impl<'a> KV for BorrowedKV<'a> {
3381 fn serialize(
3382 &self,
3383 record: &Record,
3384 serializer: &mut Serializer,
3385 ) -> Result {
3386 self.0.serialize(record, serializer)
3387 }
3388}
3389pub struct OwnedKV<T>(
3401 #[doc(hidden)]
3402 pub T,
3406)
3407where
3408 T: SendSyncRefUnwindSafeKV + ?Sized;
3409pub struct BorrowedKV<'a>(
3421 #[doc(hidden)]
3426 pub &'a KV,
3427);
3428
3429struct OwnedKVListNode<T>
3433where
3434 T: SendSyncRefUnwindSafeKV + 'static,
3435{
3436 next_node: Arc<SendSyncRefUnwindSafeKV + 'static>,
3437 kv: T,
3438}
3439
3440struct MultiListNode {
3441 next_node: Arc<SendSyncRefUnwindSafeKV + 'static>,
3442 node: Arc<SendSyncRefUnwindSafeKV + 'static>,
3443}
3444
3445#[derive(Clone)]
3447pub struct OwnedKVList {
3448 node: Arc<SendSyncRefUnwindSafeKV + 'static>,
3449}
3450
3451impl<T> KV for OwnedKVListNode<T>
3452where
3453 T: SendSyncRefUnwindSafeKV + 'static,
3454{
3455 fn serialize(
3456 &self,
3457 record: &Record,
3458 serializer: &mut Serializer,
3459 ) -> Result {
3460 try!(self.kv.serialize(record, serializer));
3461 try!(self.next_node.serialize(record, serializer));
3462
3463 Ok(())
3464 }
3465}
3466
3467impl KV for MultiListNode {
3468 fn serialize(
3469 &self,
3470 record: &Record,
3471 serializer: &mut Serializer,
3472 ) -> Result {
3473 try!(self.next_node.serialize(record, serializer));
3474 try!(self.node.serialize(record, serializer));
3475
3476 Ok(())
3477 }
3478}
3479
3480impl KV for OwnedKVList {
3481 fn serialize(
3482 &self,
3483 record: &Record,
3484 serializer: &mut Serializer,
3485 ) -> Result {
3486 try!(self.node.serialize(record, serializer));
3487
3488 Ok(())
3489 }
3490}
3491
3492impl fmt::Debug for OwnedKVList {
3493 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3494 try!(write!(f, "("));
3495 let mut i = 0;
3496
3497 {
3498 let mut as_str_ser = AsFmtSerializer(|key, _val| {
3499 if i != 0 {
3500 try!(write!(f, ", "));
3501 }
3502
3503 try!(write!(f, "{}", key));
3504 i += 1;
3505 Ok(())
3506 });
3507 let record_static = record_static!(Level::Trace, "");
3508
3509 try!(
3510 self.node
3511 .serialize(
3512 &Record::new(
3513 &record_static,
3514 &format_args!(""),
3515 BorrowedKV(&STATIC_TERMINATOR_UNIT)
3516 ),
3517 &mut as_str_ser
3518 )
3519 .map_err(|_| fmt::Error)
3520 );
3521 }
3522
3523 try!(write!(f, ")"));
3524 Ok(())
3525 }
3526}
3527
3528impl OwnedKVList {
3529 fn root<T>(values: OwnedKV<T>) -> Self
3531 where
3532 T: SendSyncRefUnwindSafeKV + 'static,
3533 {
3534 OwnedKVList {
3535 node: Arc::new(OwnedKVListNode {
3536 next_node: Arc::new(()),
3537 kv: values.0,
3538 }),
3539 }
3540 }
3541
3542 fn new<T>(
3544 values: OwnedKV<T>,
3545 next_node: Arc<SendSyncRefUnwindSafeKV + 'static>,
3546 ) -> Self
3547 where
3548 T: SendSyncRefUnwindSafeKV + 'static,
3549 {
3550 OwnedKVList {
3551 node: Arc::new(OwnedKVListNode {
3552 next_node: next_node,
3553 kv: values.0,
3554 }),
3555 }
3556 }
3557}
3558
3559impl<T> convert::From<OwnedKV<T>> for OwnedKVList
3560where
3561 T: SendSyncRefUnwindSafeKV + 'static,
3562{
3563 fn from(from: OwnedKV<T>) -> Self {
3564 OwnedKVList::root(from)
3565 }
3566}
3567#[derive(Debug)]
3571#[cfg(feature = "std")]
3572pub enum Error {
3574 Io(std::io::Error),
3576 Fmt(std::fmt::Error),
3578 Other,
3580}
3581
3582#[derive(Debug)]
3583#[cfg(not(feature = "std"))]
3584pub enum Error {
3586 Fmt(core::fmt::Error),
3588 Other,
3590}
3591
3592pub type Result<T = ()> = result::Result<T, Error>;
3594
3595#[cfg(feature = "std")]
3596impl From<std::io::Error> for Error {
3597 fn from(err: std::io::Error) -> Error {
3598 Error::Io(err)
3599 }
3600}
3601
3602impl From<core::fmt::Error> for Error {
3603 fn from(_: core::fmt::Error) -> Error {
3604 Error::Other
3605 }
3606}
3607
3608#[cfg(feature = "std")]
3609impl From<Error> for std::io::Error {
3610 fn from(e: Error) -> std::io::Error {
3611 match e {
3612 Error::Io(e) => e,
3613 Error::Fmt(_) => std::io::Error::new(
3614 std::io::ErrorKind::Other,
3615 "formatting error",
3616 ),
3617 Error::Other => {
3618 std::io::Error::new(std::io::ErrorKind::Other, "other error")
3619 }
3620 }
3621 }
3622}
3623
3624#[cfg(feature = "std")]
3625impl std::error::Error for Error {
3626 fn description(&self) -> &str {
3627 match *self {
3628 Error::Io(ref e) => e.description(),
3629 Error::Fmt(_) => "formatting error",
3630 Error::Other => "serialization error",
3631 }
3632 }
3633
3634 fn cause(&self) -> Option<&std::error::Error> {
3635 match *self {
3636 Error::Io(ref e) => Some(e),
3637 Error::Fmt(ref e) => Some(e),
3638 Error::Other => None,
3639 }
3640 }
3641}
3642
3643#[cfg(feature = "std")]
3644impl core::fmt::Display for Error {
3645 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> std::fmt::Result {
3646 match *self {
3647 Error::Io(ref e) => e.fmt(fmt),
3648 Error::Fmt(ref e) => e.fmt(fmt),
3649 Error::Other => fmt.write_str("Other serialization error"),
3650 }
3651 }
3652}
3653#[doc(hidden)]
3660pub type Never = private::NeverStruct;
3661
3662mod private {
3663 #[doc(hidden)]
3664 #[derive(Clone, Debug)]
3665 pub struct NeverStruct(());
3666}
3667
3668#[doc(hidden)]
3670pub static STATIC_TERMINATOR_UNIT: () = ();
3671
3672#[allow(unknown_lints)]
3673#[allow(inline_always)]
3674#[inline(always)]
3675#[doc(hidden)]
3676pub fn __slog_static_max_level() -> FilterLevel {
3682 if !cfg!(debug_assertions) {
3683 if cfg!(feature = "release_max_level_off") {
3684 return FilterLevel::Off;
3685 } else if cfg!(feature = "release_max_level_error") {
3686 return FilterLevel::Error;
3687 } else if cfg!(feature = "release_max_level_warn") {
3688 return FilterLevel::Warning;
3689 } else if cfg!(feature = "release_max_level_info") {
3690 return FilterLevel::Info;
3691 } else if cfg!(feature = "release_max_level_debug") {
3692 return FilterLevel::Debug;
3693 } else if cfg!(feature = "release_max_level_trace") {
3694 return FilterLevel::Trace;
3695 }
3696 }
3697 if cfg!(feature = "max_level_off") {
3698 FilterLevel::Off
3699 } else if cfg!(feature = "max_level_error") {
3700 FilterLevel::Error
3701 } else if cfg!(feature = "max_level_warn") {
3702 FilterLevel::Warning
3703 } else if cfg!(feature = "max_level_info") {
3704 FilterLevel::Info
3705 } else if cfg!(feature = "max_level_debug") {
3706 FilterLevel::Debug
3707 } else if cfg!(feature = "max_level_trace") {
3708 FilterLevel::Trace
3709 } else {
3710 if !cfg!(debug_assertions) {
3711 FilterLevel::Info
3712 } else {
3713 FilterLevel::Debug
3714 }
3715 }
3716}
3717
3718#[deprecated(note = "Renamed to `Value`")]
3722pub type Serialize = Value;
3724
3725#[deprecated(note = "Renamed to `PushFnValue`")]
3726pub type PushLazy<T> = PushFnValue<T>;
3728
3729#[deprecated(note = "Renamed to `PushFnValueSerializer`")]
3730pub type ValueSerializer<'a> = PushFnValueSerializer<'a>;
3732
3733#[deprecated(note = "Renamed to `OwnedKVList`")]
3734pub type OwnedKeyValueList = OwnedKVList;
3736
3737#[deprecated(note = "Content of ser module moved to main namespace")]
3738pub mod ser {
3740 #[allow(deprecated)]
3741 pub use super::{OwnedKeyValueList, PushLazy, Serialize, Serializer,
3742 ValueSerializer};
3743}
3744#[cfg(test)]
3748mod tests;
3749
3750