Search code examples
rustsubstrate

Where is the type Log defined inside of the Substrate runtime crate?


Take a look at this implementation:

impl consensus::Trait for Runtime {
    type Log = Log;
    type SessionKey = AuthorityId;

    // The Aura module handles offline-reports internally
    // rather than using an explicit report system.
    type InherentOfflineReport = ();
}

How is Log defined? There is no use clause for importing this symbol.

Running

cargo rustc -- -Z unstable-options --pretty=expanded

does not show any Log entry with type clause. It does show other macro declarations after expanding macros at level 0, but I am not sure if this relevant.

I tried using the Atom IDE because it automatically parses the files and lets you find the definition of the symbols, but it did not help.

How can I find how Log is defined?


Solution

  • Log is defined by construct_runtime macro. Here are some relevant code:

    construct_runtime macro:

    https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L79

    macro_rules! construct_runtime {
        (
            pub enum $runtime:ident with Log ($log_internal:ident: DigestItem<$( $log_genarg:ty ),+>)
                where
                    Block = $block:ident,
                    NodeBlock = $node_block:ty,
                    UncheckedExtrinsic = $uncheckedextrinsic:ident
            {
                $( $rest:tt )*
            }
        )
    

    calling __decl_outer_log

    https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L267

        $crate::__decl_outer_log!(
            $runtime;
            $log_internal < $( $log_genarg ),* >;
            {};
            $(
                $name: $module:: $( < $module_instance >:: )? { $( $modules $( ( $( $modules_args )* ) )* )* }
            )*
        );
    

    __decl_outer_log macro

    https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L706

    macro_rules! __decl_outer_log {
        (
            $runtime:ident;
            $log_internal:ident <$( $log_genarg:ty ),+>;
            { $( $parsed:tt )* };
            $name:ident: $module:ident:: $(<$module_instance:ident>::)? {
                Log ( $( $args:ident )* ) $( $modules:ident $( ( $( $modules_args:ident )* ) )* )*
            }
            $( $rest:tt )*
        ) => {
    

    calling impl_outer_log

    https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L763

    (
        $runtime:ident;
        $log_internal:ident <$( $log_genarg:ty ),+>;
        { $(
            $parsed_modules:ident $(< $parsed_instance:ident >)? ( $( $parsed_args:ident )* )
        )* };
    ) => {
        $crate::paste::item! {
            $crate::runtime_primitives::impl_outer_log!(
                pub enum Log($log_internal: DigestItem<$( $log_genarg ),*>) for $runtime {
                    $( [< $parsed_modules $(_ $parsed_instance)? >] $(< $parsed_modules::$parsed_instance >)? ( $( $parsed_args ),* ) ),*
                }
            );
        }
    };
    

    impl_outer_log macro: https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/core/sr-primitives/src/lib.rs#L630

    macro_rules! impl_outer_log {
        (
            $(#[$attr:meta])*
            pub enum $name:ident ($internal:ident: DigestItem<$( $genarg:ty ),*>) for $trait:ident {
                $( $module:ident $(<$instance:path>)? ( $( $sitem:ident ),* ) ),*
            }
        )
    

    which actually declare and implement the Log struct

    You should be able to see the result when cargo expand the runtime crate.