Skip to main content

browser_protocol/log/
mod.rs

1//! Provides access to log entries.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// Log entry.
9
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11#[serde(rename_all = "camelCase")]
12pub struct LogEntry<'a> {
13    /// Log entry source.
14    source: Cow<'a, str>,
15    /// Log entry severity.
16    level: Cow<'a, str>,
17    /// Logged text.
18    text: Cow<'a, str>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    category: Option<Cow<'a, str>>,
21    /// Timestamp when this entry was added.
22    timestamp: crate::runtime::Timestamp,
23    /// URL of the resource if known.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    url: Option<Cow<'a, str>>,
26    /// Line number in the resource.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    lineNumber: Option<i64>,
29    /// JavaScript stack trace.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    stackTrace: Option<crate::runtime::StackTrace>,
32    /// Identifier of the network request associated with this entry.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    networkRequestId: Option<crate::network::RequestId<'a>>,
35    /// Identifier of the worker associated with this entry.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    workerId: Option<Cow<'a, str>>,
38    /// Call arguments.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    args: Option<Vec<crate::runtime::RemoteObject>>,
41}
42
43impl<'a> LogEntry<'a> {
44    pub fn builder(source: impl Into<Cow<'a, str>>, level: impl Into<Cow<'a, str>>, text: impl Into<Cow<'a, str>>, timestamp: crate::runtime::Timestamp) -> LogEntryBuilder<'a> {
45        LogEntryBuilder {
46            source: source.into(),
47            level: level.into(),
48            text: text.into(),
49            category: None,
50            timestamp: timestamp,
51            url: None,
52            lineNumber: None,
53            stackTrace: None,
54            networkRequestId: None,
55            workerId: None,
56            args: None,
57        }
58    }
59    pub fn source(&self) -> &str { self.source.as_ref() }
60    pub fn level(&self) -> &str { self.level.as_ref() }
61    pub fn text(&self) -> &str { self.text.as_ref() }
62    pub fn category(&self) -> Option<&str> { self.category.as_deref() }
63    pub fn timestamp(&self) -> &crate::runtime::Timestamp { &self.timestamp }
64    pub fn url(&self) -> Option<&str> { self.url.as_deref() }
65    pub fn lineNumber(&self) -> Option<i64> { self.lineNumber }
66    pub fn stackTrace(&self) -> Option<&crate::runtime::StackTrace> { self.stackTrace.as_ref() }
67    pub fn networkRequestId(&self) -> Option<&crate::network::RequestId<'a>> { self.networkRequestId.as_ref() }
68    pub fn workerId(&self) -> Option<&str> { self.workerId.as_deref() }
69    pub fn args(&self) -> Option<&[crate::runtime::RemoteObject]> { self.args.as_deref() }
70}
71
72
73pub struct LogEntryBuilder<'a> {
74    source: Cow<'a, str>,
75    level: Cow<'a, str>,
76    text: Cow<'a, str>,
77    category: Option<Cow<'a, str>>,
78    timestamp: crate::runtime::Timestamp,
79    url: Option<Cow<'a, str>>,
80    lineNumber: Option<i64>,
81    stackTrace: Option<crate::runtime::StackTrace>,
82    networkRequestId: Option<crate::network::RequestId<'a>>,
83    workerId: Option<Cow<'a, str>>,
84    args: Option<Vec<crate::runtime::RemoteObject>>,
85}
86
87impl<'a> LogEntryBuilder<'a> {
88    pub fn category(mut self, category: impl Into<Cow<'a, str>>) -> Self { self.category = Some(category.into()); self }
89    /// URL of the resource if known.
90    pub fn url(mut self, url: impl Into<Cow<'a, str>>) -> Self { self.url = Some(url.into()); self }
91    /// Line number in the resource.
92    pub fn lineNumber(mut self, lineNumber: i64) -> Self { self.lineNumber = Some(lineNumber); self }
93    /// JavaScript stack trace.
94    pub fn stackTrace(mut self, stackTrace: crate::runtime::StackTrace) -> Self { self.stackTrace = Some(stackTrace); self }
95    /// Identifier of the network request associated with this entry.
96    pub fn networkRequestId(mut self, networkRequestId: crate::network::RequestId<'a>) -> Self { self.networkRequestId = Some(networkRequestId); self }
97    /// Identifier of the worker associated with this entry.
98    pub fn workerId(mut self, workerId: impl Into<Cow<'a, str>>) -> Self { self.workerId = Some(workerId.into()); self }
99    /// Call arguments.
100    pub fn args(mut self, args: Vec<crate::runtime::RemoteObject>) -> Self { self.args = Some(args); self }
101    pub fn build(self) -> LogEntry<'a> {
102        LogEntry {
103            source: self.source,
104            level: self.level,
105            text: self.text,
106            category: self.category,
107            timestamp: self.timestamp,
108            url: self.url,
109            lineNumber: self.lineNumber,
110            stackTrace: self.stackTrace,
111            networkRequestId: self.networkRequestId,
112            workerId: self.workerId,
113            args: self.args,
114        }
115    }
116}
117
118/// Violation configuration setting.
119
120#[derive(Debug, Clone, Serialize, Deserialize, Default)]
121#[serde(rename_all = "camelCase")]
122pub struct ViolationSetting<'a> {
123    /// Violation type.
124    name: Cow<'a, str>,
125    /// Time threshold to trigger upon.
126    threshold: f64,
127}
128
129impl<'a> ViolationSetting<'a> {
130    pub fn builder(name: impl Into<Cow<'a, str>>, threshold: f64) -> ViolationSettingBuilder<'a> {
131        ViolationSettingBuilder {
132            name: name.into(),
133            threshold: threshold,
134        }
135    }
136    pub fn name(&self) -> &str { self.name.as_ref() }
137    pub fn threshold(&self) -> f64 { self.threshold }
138}
139
140
141pub struct ViolationSettingBuilder<'a> {
142    name: Cow<'a, str>,
143    threshold: f64,
144}
145
146impl<'a> ViolationSettingBuilder<'a> {
147    pub fn build(self) -> ViolationSetting<'a> {
148        ViolationSetting {
149            name: self.name,
150            threshold: self.threshold,
151        }
152    }
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, Default)]
156pub struct ClearParams {}
157
158impl ClearParams { pub const METHOD: &'static str = "Log.clear"; }
159
160impl<'a> crate::CdpCommand<'a> for ClearParams {
161    const METHOD: &'static str = "Log.clear";
162    type Response = crate::EmptyReturns;
163}
164
165#[derive(Debug, Clone, Serialize, Deserialize, Default)]
166pub struct DisableParams {}
167
168impl DisableParams { pub const METHOD: &'static str = "Log.disable"; }
169
170impl<'a> crate::CdpCommand<'a> for DisableParams {
171    const METHOD: &'static str = "Log.disable";
172    type Response = crate::EmptyReturns;
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize, Default)]
176pub struct EnableParams {}
177
178impl EnableParams { pub const METHOD: &'static str = "Log.enable"; }
179
180impl<'a> crate::CdpCommand<'a> for EnableParams {
181    const METHOD: &'static str = "Log.enable";
182    type Response = crate::EmptyReturns;
183}
184
185/// start violation reporting.
186
187#[derive(Debug, Clone, Serialize, Deserialize, Default)]
188#[serde(rename_all = "camelCase")]
189pub struct StartViolationsReportParams<'a> {
190    /// Configuration for violations.
191    config: Vec<ViolationSetting<'a>>,
192}
193
194impl<'a> StartViolationsReportParams<'a> {
195    pub fn builder(config: Vec<ViolationSetting<'a>>) -> StartViolationsReportParamsBuilder<'a> {
196        StartViolationsReportParamsBuilder {
197            config: config,
198        }
199    }
200    pub fn config(&self) -> &[ViolationSetting<'a>] { &self.config }
201}
202
203
204pub struct StartViolationsReportParamsBuilder<'a> {
205    config: Vec<ViolationSetting<'a>>,
206}
207
208impl<'a> StartViolationsReportParamsBuilder<'a> {
209    pub fn build(self) -> StartViolationsReportParams<'a> {
210        StartViolationsReportParams {
211            config: self.config,
212        }
213    }
214}
215
216impl<'a> StartViolationsReportParams<'a> { pub const METHOD: &'static str = "Log.startViolationsReport"; }
217
218impl<'a> crate::CdpCommand<'a> for StartViolationsReportParams<'a> {
219    const METHOD: &'static str = "Log.startViolationsReport";
220    type Response = crate::EmptyReturns;
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize, Default)]
224pub struct StopViolationsReportParams {}
225
226impl StopViolationsReportParams { pub const METHOD: &'static str = "Log.stopViolationsReport"; }
227
228impl<'a> crate::CdpCommand<'a> for StopViolationsReportParams {
229    const METHOD: &'static str = "Log.stopViolationsReport";
230    type Response = crate::EmptyReturns;
231}