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
use std::fmt::{Display, Error, Formatter};

use bson::Document;
use error::Error as MongoError;
use separator::Separatable;

/// Contains the information about a given command that started.
pub struct CommandStarted {
    pub command: Document,
    pub database_name: String,
    pub command_name: String,
    pub request_id: i64,
    pub connection_string: String,
}

impl Display for CommandStarted {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        fmt.write_fmt(format_args!("COMMAND.{} {} STARTED: {}",
                                   self.command_name,
                                   self.connection_string,
                                   self.command))
    }
}

/// Contains the information about a given command that completed.
pub enum CommandResult<'a> {
    Success {
        duration: u64,
        reply: Document,
        command_name: String,
        request_id: i64,
        connection_string: String,
    },
    Failure {
        duration: u64,
        command_name: String,
        failure: &'a MongoError,
        request_id: i64,
        connection_string: String,
    },
}

impl<'a> Display for CommandResult<'a> {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        match *self {
            CommandResult::Success { duration,
                                     ref reply,
                                     ref command_name,
                                     ref connection_string,
                                     .. } => {
                fmt.write_fmt(format_args!("COMMAND.{} {} COMPLETED: {} ({} ns)",
                                           command_name,
                                           connection_string,
                                           reply,
                                           duration.separated_string()))
            }
            CommandResult::Failure { duration,
                                     ref command_name,
                                     failure,
                                     ref connection_string,
                                     .. } => {
                fmt.write_fmt(format_args!("COMMAND.{} {} FAILURE: {} ({} ns)",
                                           command_name,
                                           connection_string,
                                           failure,
                                           duration.separated_string()))
            }
        }
    }
}