Skip to main content

asterust_junos/system/syslog/
file.rs

1//! `file`
2//! `system { syslog { file <name> { ... } } }`.
3//!
4//! [`SyslogTarget`](crate::system::SyslogTarget) is shared with the `user` destinations.
5//!
6//! # See also
7//!
8//! - [file (System Logging)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/file-edit-system-syslog.html)
9//! - [match-strings](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/match-string-edit-system-syslog-file.html)
10
11use super::archive::{parse_archive, SyslogArchive};
12use super::{syslog_rules, SyslogRule};
13use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children};
14use asterust::config::ConfigLine;
15
16/// A `structured-data` statement on a syslog file.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct SyslogStructuredData {
20    /// Omit the message text.
21    pub brief: bool,
22}
23
24/// A `file <name>` or `user <name>` destination.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct SyslogTarget {
28    /// File name, or user name (`*` for all users).
29    pub name: String,
30    pub rules: Vec<SyslogRule>,
31    /// Only files have an archive.
32    pub archive: Option<SyslogArchive>,
33    pub allow_duplicates: bool,
34    /// The regular expression from `match`.
35    pub match_regex: Option<String>,
36    /// The substrings from `match-strings`; a message is logged if it contains any of them.
37    pub match_strings: Vec<String>,
38    /// Only files have `explicit-priority`.
39    pub explicit_priority: bool,
40    /// Only files have `structured-data`.
41    pub structured_data: Option<SyslogStructuredData>,
42}
43
44pub(super) fn targets(syslog: &ConfigLine, keyword: &str) -> Vec<SyslogTarget> {
45    named_children(syslog, keyword)
46        .map(|(name, target)| SyslogTarget {
47            name: name.to_string(),
48            rules: syslog_rules(target),
49            archive: parse_archive(target),
50            allow_duplicates: has_leaf(target, "allow-duplicates"),
51            match_regex: arg_of(target, "match").map(str::to_string),
52            match_strings: args_of(target, "match-strings")
53                .into_iter()
54                .flat_map(list_values)
55                .collect(),
56            explicit_priority: has_leaf(target, "explicit-priority"),
57            structured_data: child_block(target, "structured-data").map(|data| SyslogStructuredData {
58                brief: has_leaf(data, "brief"),
59            }),
60        })
61        .collect()
62}
63
64pub(super) fn files(syslog: &ConfigLine) -> Vec<SyslogTarget> {
65    targets(syslog, "file")
66}