perspective_client/utils/
logging.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use prost::Message;
14
15use crate::proto::make_table_data::Data;
16use crate::proto::request::ClientReq;
17use crate::proto::response::ClientResp;
18use crate::proto::{
19    MakeTableData, MakeTableReq, Request, Response, TableUpdateReq, ViewOnUpdateResp,
20    ViewToArrowResp, ViewToColumnsStringResp, ViewToCsvResp, ViewToNdjsonStringResp,
21    ViewToRowsStringResp,
22};
23
24fn replace(x: Data) -> Data {
25    match x {
26        Data::FromArrow(_) => Data::FromArrow("<< redacted >>".to_string().encode_to_vec()),
27        Data::FromRows(_) => Data::FromRows("<< redacted >>".to_string()),
28        Data::FromCols(_) => Data::FromCols("<< redacted >>".to_string()),
29        Data::FromCsv(_) => Data::FromCsv("<< redacted >>".to_string()),
30        x => x,
31    }
32}
33
34/// `prost` generates `Debug` implementations that includes the `data` field,
35/// which makes logs output unreadable. This `Display` implementation hides
36/// fields that we don't want ot display in the logs.
37impl std::fmt::Display for Request {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        let mut msg = self.clone();
40        msg = match msg {
41            Request {
42                client_req:
43                    Some(ClientReq::MakeTableReq(MakeTableReq {
44                        ref options,
45                        data:
46                            Some(MakeTableData {
47                                data: Some(ref data),
48                            }),
49                    })),
50                ..
51            } => Request {
52                client_req: Some(ClientReq::MakeTableReq(MakeTableReq {
53                    options: options.clone(),
54                    data: Some(MakeTableData {
55                        data: Some(replace(data.clone())),
56                    }),
57                })),
58                ..msg.clone()
59            },
60            Request {
61                client_req:
62                    Some(ClientReq::TableUpdateReq(TableUpdateReq {
63                        port_id,
64                        data:
65                            Some(MakeTableData {
66                                data: Some(ref data),
67                            }),
68                    })),
69                ..
70            } => Request {
71                client_req: Some(ClientReq::TableUpdateReq(TableUpdateReq {
72                    port_id,
73                    data: Some(MakeTableData {
74                        data: Some(replace(data.clone())),
75                    }),
76                })),
77                ..msg.clone()
78            },
79            x => x,
80        };
81
82        write!(
83            f,
84            "{}",
85            serde_json::to_string(&msg).unwrap_or("Can't deserialize".to_string())
86        )
87    }
88}
89
90impl std::fmt::Display for Response {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        let mut msg = self.clone();
93        msg = match msg {
94            Response {
95                client_resp: Some(ClientResp::ViewToColumnsStringResp(_)),
96                ..
97            } => Response {
98                client_resp: Some(ClientResp::ViewToColumnsStringResp(
99                    ViewToColumnsStringResp {
100                        json_string: "<< redacted >>".to_owned(),
101                    },
102                )),
103                ..msg.clone()
104            },
105            Response {
106                client_resp: Some(ClientResp::ViewToRowsStringResp(_)),
107                ..
108            } => Response {
109                client_resp: Some(ClientResp::ViewToRowsStringResp(ViewToRowsStringResp {
110                    json_string: "<< redacted >>".to_owned(),
111                })),
112                ..msg.clone()
113            },
114            Response {
115                client_resp: Some(ClientResp::ViewToNdjsonStringResp(_)),
116                ..
117            } => Response {
118                client_resp: Some(ClientResp::ViewToNdjsonStringResp(ViewToNdjsonStringResp {
119                    ndjson_string: "<< redacted >>".to_owned(),
120                })),
121                ..msg.clone()
122            },
123            Response {
124                client_resp: Some(ClientResp::ViewToArrowResp(_)),
125                ..
126            } => Response {
127                client_resp: Some(ClientResp::ViewToArrowResp(ViewToArrowResp {
128                    arrow: vec![],
129                })),
130                ..msg.clone()
131            },
132            Response {
133                client_resp: Some(ClientResp::ViewToCsvResp(_)),
134                ..
135            } => Response {
136                client_resp: Some(ClientResp::ViewToCsvResp(ViewToCsvResp {
137                    csv: "<< redacted >>".to_owned(),
138                })),
139                ..msg.clone()
140            },
141            Response {
142                client_resp: Some(ClientResp::ViewOnUpdateResp(ref x)),
143                ..
144            } => Response {
145                client_resp: Some(ClientResp::ViewOnUpdateResp(ViewOnUpdateResp {
146                    delta: x.delta.as_ref().map(|_| vec![]),
147                    port_id: x.port_id,
148                })),
149                ..msg.clone()
150            },
151            x => x,
152        };
153
154        write!(f, "{}", serde_json::to_string(&msg).unwrap())
155    }
156}