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
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use libp2p::{kad::RecordKey, PeerId};
use sn_protocol::{
    messages::{Cmd, CmdResponse},
    PrettyPrintRecordKey,
};
use std::time::Duration;
// this gets us to_string easily enough
use strum::Display;

/// Public Markers for generating log output,
/// These generate appropriate log level output and consistent strings.
/// Changing these log markers is a breaking change.
#[derive(Debug, Clone, Display)]
pub enum Marker<'a> {
    /// An operation failed after timing out due to a period of no network activity
    OperationFailedAfterNetworkInactivityTimeout,

    /// The node has started
    NodeConnectedToNetwork,

    /// No network activity in some time
    NoNetworkActivity(Duration),

    /// Network Cmd message received
    NodeCmdReceived(&'a Cmd),

    /// Network Cmd message response was generated
    NodeCmdResponded(&'a CmdResponse),

    /// Peer was added to the routing table
    PeerAddedToRoutingTable(PeerId),

    /// Peer was removed from the routing table
    PeerRemovedFromRoutingTable(PeerId),

    /// Replication trigger was fired
    ReplicationTriggered,

    /// Keys of Records we are fetching to replicate locally
    FetchingKeysForReplication {
        /// fetching_keys_len: number of keys we are fetching from network
        fetching_keys_len: usize,
    },

    /// Valid non-existing Chunk record PUT from the network received and stored
    ValidChunkRecordPutFromNetwork(&'a PrettyPrintRecordKey),
    /// Valid non-existing Register record PUT from the network received and stored
    ValidRegisterRecordPutFromNetwork(&'a PrettyPrintRecordKey),
    /// Valid non-existing Spend record PUT from the network received and stored
    ValidSpendRecordPutFromNetwork(&'a PrettyPrintRecordKey),

    /// Record rejected
    RecordRejected(&'a PrettyPrintRecordKey),
}

impl<'a> Marker<'a> {
    /// Returns the string representation of the LogMarker.
    pub fn log(&self) {
        // Down the line, if some logs are noisier than others, we can
        // match the type and log a different level.
        info!("{self:?}");
    }

    /// Helper to log the FetchingKeysForReplication variant
    pub fn fetching_keys_for_replication(keys: &Vec<(PeerId, RecordKey)>) -> Self {
        Marker::FetchingKeysForReplication {
            fetching_keys_len: keys.len(),
        }
    }
}