tracing_core/
subscriber.rs1use crate::{span, Dispatch, Event, LevelFilter, Metadata};
3
4use crate::stdlib::{
5 any::{Any, TypeId},
6 boxed::Box,
7 sync::Arc,
8};
9
10pub trait Subscriber: 'static {
84 fn on_register_dispatch(&self, subscriber: &Dispatch) {
104 let _ = subscriber;
105 }
106
107 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
179 if self.enabled(metadata) {
180 Interest::always()
181 } else {
182 Interest::never()
183 }
184 }
185
186 fn enabled(&self, metadata: &Metadata<'_>) -> bool;
207
208 fn max_level_hint(&self) -> Option<LevelFilter> {
231 None
232 }
233
234 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id;
259
260 fn record(&self, span: &span::Id, values: &span::Record<'_>);
299
300 fn record_follows_from(&self, span: &span::Id, follows: &span::Id);
319
320 fn event_enabled(&self, event: &Event<'_>) -> bool {
327 let _ = event;
328 true
329 }
330
331 fn event(&self, event: &Event<'_>);
350
351 fn enter(&self, span: &span::Id);
360
361 fn exit(&self, span: &span::Id);
372
373 fn clone_span(&self, id: &span::Id) -> span::Id {
394 id.clone()
395 }
396
397 #[deprecated(since = "0.1.2", note = "use `Subscriber::try_close` instead")]
407 fn drop_span(&self, _id: span::Id) {}
408
409 fn try_close(&self, id: span::Id) -> bool {
446 #[allow(deprecated)]
447 self.drop_span(id);
448 false
449 }
450
451 fn current_span(&self) -> span::Current {
465 span::Current::unknown()
466 }
467
468 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
496 if id == TypeId::of::<Self>() {
497 Some(self as *const Self as *const ())
498 } else {
499 None
500 }
501 }
502}
503
504impl dyn Subscriber {
505 pub fn is<T: Any>(&self) -> bool {
507 self.downcast_ref::<T>().is_some()
508 }
509
510 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
513 unsafe {
514 let raw = self.downcast_raw(TypeId::of::<T>())?;
515 if raw.is_null() {
516 None
517 } else {
518 Some(&*(raw as *const _))
519 }
520 }
521 }
522}
523
524impl dyn Subscriber + Send {
525 pub fn is<T: Any>(&self) -> bool {
527 self.downcast_ref::<T>().is_some()
528 }
529
530 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
533 unsafe {
534 let raw = self.downcast_raw(TypeId::of::<T>())?;
535 if raw.is_null() {
536 None
537 } else {
538 Some(&*(raw as *const _))
539 }
540 }
541 }
542}
543
544impl dyn Subscriber + Sync {
545 pub fn is<T: Any>(&self) -> bool {
547 self.downcast_ref::<T>().is_some()
548 }
549
550 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
553 unsafe {
554 let raw = self.downcast_raw(TypeId::of::<T>())?;
555 if raw.is_null() {
556 None
557 } else {
558 Some(&*(raw as *const _))
559 }
560 }
561 }
562}
563
564impl dyn Subscriber + Send + Sync {
565 pub fn is<T: Any>(&self) -> bool {
567 self.downcast_ref::<T>().is_some()
568 }
569
570 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
573 unsafe {
574 let raw = self.downcast_raw(TypeId::of::<T>())?;
575 if raw.is_null() {
576 None
577 } else {
578 Some(&*(raw as *const _))
579 }
580 }
581 }
582}
583
584#[derive(Clone, Debug)]
592pub struct Interest(InterestKind);
593
594#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
595enum InterestKind {
596 Never = 0,
597 Sometimes = 1,
598 Always = 2,
599}
600
601impl Interest {
602 #[inline]
608 pub fn never() -> Self {
609 Interest(InterestKind::Never)
610 }
611
612 #[inline]
620 pub fn sometimes() -> Self {
621 Interest(InterestKind::Sometimes)
622 }
623
624 #[inline]
630 pub fn always() -> Self {
631 Interest(InterestKind::Always)
632 }
633
634 #[inline]
637 pub fn is_never(&self) -> bool {
638 matches!(self.0, InterestKind::Never)
639 }
640
641 #[inline]
644 pub fn is_sometimes(&self) -> bool {
645 matches!(self.0, InterestKind::Sometimes)
646 }
647
648 #[inline]
651 pub fn is_always(&self) -> bool {
652 matches!(self.0, InterestKind::Always)
653 }
654
655 pub(crate) fn and(self, rhs: Interest) -> Self {
662 if self.0 == rhs.0 {
663 self
664 } else {
665 Interest::sometimes()
666 }
667 }
668}
669
670#[derive(Copy, Clone, Debug, Default)]
675pub struct NoSubscriber(());
676
677impl Subscriber for NoSubscriber {
678 #[inline]
679 fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest {
680 Interest::never()
681 }
682
683 fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
684 span::Id::from_u64(0xDEAD)
685 }
686
687 fn event(&self, _event: &Event<'_>) {}
688
689 fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {}
690
691 fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {}
692
693 #[inline]
694 fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
695 false
696 }
697
698 fn enter(&self, _span: &span::Id) {}
699 fn exit(&self, _span: &span::Id) {}
700}
701
702impl NoSubscriber {
703 #[must_use]
705 pub const fn new() -> Self {
706 Self(())
707 }
708}
709
710impl<S> Subscriber for Box<S>
711where
712 S: Subscriber + ?Sized,
713{
714 #[inline]
715 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
716 self.as_ref().register_callsite(metadata)
717 }
718
719 #[inline]
720 fn enabled(&self, metadata: &Metadata<'_>) -> bool {
721 self.as_ref().enabled(metadata)
722 }
723
724 #[inline]
725 fn max_level_hint(&self) -> Option<LevelFilter> {
726 self.as_ref().max_level_hint()
727 }
728
729 #[inline]
730 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
731 self.as_ref().new_span(span)
732 }
733
734 #[inline]
735 fn record(&self, span: &span::Id, values: &span::Record<'_>) {
736 self.as_ref().record(span, values)
737 }
738
739 #[inline]
740 fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
741 self.as_ref().record_follows_from(span, follows)
742 }
743
744 #[inline]
745 fn event_enabled(&self, event: &Event<'_>) -> bool {
746 self.as_ref().event_enabled(event)
747 }
748
749 #[inline]
750 fn event(&self, event: &Event<'_>) {
751 self.as_ref().event(event)
752 }
753
754 #[inline]
755 fn enter(&self, span: &span::Id) {
756 self.as_ref().enter(span)
757 }
758
759 #[inline]
760 fn exit(&self, span: &span::Id) {
761 self.as_ref().exit(span)
762 }
763
764 #[inline]
765 fn clone_span(&self, id: &span::Id) -> span::Id {
766 self.as_ref().clone_span(id)
767 }
768
769 #[inline]
770 fn try_close(&self, id: span::Id) -> bool {
771 self.as_ref().try_close(id)
772 }
773
774 #[inline]
775 #[allow(deprecated)]
776 fn drop_span(&self, id: span::Id) {
777 self.as_ref().try_close(id);
778 }
779
780 #[inline]
781 fn current_span(&self) -> span::Current {
782 self.as_ref().current_span()
783 }
784
785 #[inline]
786 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
787 if id == TypeId::of::<Self>() {
788 return Some(self as *const Self as *const _);
789 }
790
791 self.as_ref().downcast_raw(id)
792 }
793}
794
795impl<S> Subscriber for Arc<S>
796where
797 S: Subscriber + ?Sized,
798{
799 #[inline]
800 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
801 self.as_ref().register_callsite(metadata)
802 }
803
804 #[inline]
805 fn enabled(&self, metadata: &Metadata<'_>) -> bool {
806 self.as_ref().enabled(metadata)
807 }
808
809 #[inline]
810 fn max_level_hint(&self) -> Option<LevelFilter> {
811 self.as_ref().max_level_hint()
812 }
813
814 #[inline]
815 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
816 self.as_ref().new_span(span)
817 }
818
819 #[inline]
820 fn record(&self, span: &span::Id, values: &span::Record<'_>) {
821 self.as_ref().record(span, values)
822 }
823
824 #[inline]
825 fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
826 self.as_ref().record_follows_from(span, follows)
827 }
828
829 #[inline]
830 fn event_enabled(&self, event: &Event<'_>) -> bool {
831 self.as_ref().event_enabled(event)
832 }
833
834 #[inline]
835 fn event(&self, event: &Event<'_>) {
836 self.as_ref().event(event)
837 }
838
839 #[inline]
840 fn enter(&self, span: &span::Id) {
841 self.as_ref().enter(span)
842 }
843
844 #[inline]
845 fn exit(&self, span: &span::Id) {
846 self.as_ref().exit(span)
847 }
848
849 #[inline]
850 fn clone_span(&self, id: &span::Id) -> span::Id {
851 self.as_ref().clone_span(id)
852 }
853
854 #[inline]
855 fn try_close(&self, id: span::Id) -> bool {
856 self.as_ref().try_close(id)
857 }
858
859 #[inline]
860 #[allow(deprecated)]
861 fn drop_span(&self, id: span::Id) {
862 self.as_ref().try_close(id);
863 }
864
865 #[inline]
866 fn current_span(&self) -> span::Current {
867 self.as_ref().current_span()
868 }
869
870 #[inline]
871 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
872 if id == TypeId::of::<Self>() {
873 return Some(self as *const Self as *const _);
874 }
875
876 self.as_ref().downcast_raw(id)
877 }
878}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4