flexi_config/appender/module.rs
1/* Copyright 2016 Joshua Gentry
2 *
3 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 * option. This file may not be copied, modified, or distributed
7 * except according to those terms.
8 */
9use log::LogRecord;
10
11use appender::{Appender, Formatter, FormatResult};
12
13#[cfg(test)]
14use appender::log_test::LogTest;
15
16//*************************************************************************************************
17/// This object is used to write the name of the module the that generated the log message.
18pub struct Module
19{
20 //---------------------------------------------------------------------------------------------
21 /// The formatter to format the module.
22 formatter : Formatter
23}
24
25impl Module
26{
27 //*********************************************************************************************
28 /// Construct a new instance of the object.
29 pub fn new(
30 fmt : &str
31 ) -> FormatResult
32 {
33 match Formatter::new(fmt)
34 {
35 Ok(formatter) => FormatResult::Ok(Box::new(Module { formatter : formatter })),
36 Err((offset, fmt)) => FormatResult::Err(offset, fmt)
37
38 }
39 }
40}
41
42impl Appender for Module
43{
44 //*********************************************************************************************
45 /// Append the module to the output string.
46 fn append(
47 &self,
48 what : &LogRecord,
49 to : &mut String
50 )
51 {
52 self.formatter.append(&what.location().module_path(), to);
53 }
54
55 //*********************************************************************************************
56 /// Test method for unit tests.
57 ///
58 /// This method should do everything the append() method does and return the output string.
59 #[cfg(test)]
60 fn test(
61 &self,
62 what : LogTest
63 ) -> String
64 {
65 let mut to = String::with_capacity(64);
66
67 self.formatter.append(&what.module, &mut to);
68
69 to
70 }
71}
72
73#[cfg(test)]
74mod test {
75 use appender::log_test::LogTest;
76
77 use super::Module;
78
79 //*********************************************************************************************
80 /// Test with no special formatting.
81 #[test]
82 fn no_formatting()
83 {
84 let module = Module::new("").unwrap();
85
86 assert_eq!(module.test(LogTest::module("abc")), "abc");
87 }
88
89 //*********************************************************************************************
90 /// Test with truncation.
91 #[test]
92 fn truncate()
93 {
94 let module = Module::new("3-").unwrap();
95
96 assert_eq!(module.test(LogTest::module("a")), "a");
97 assert_eq!(module.test(LogTest::module("abc")), "abc");
98 assert_eq!(module.test(LogTest::module("abcd")), "abc");
99 }
100
101 //*********************************************************************************************
102 /// Test left alignment.
103 #[test]
104 fn left_align()
105 {
106 let module = Module::new("<3").unwrap();
107
108 assert_eq!(module.test(LogTest::module("a")), "a ");
109 assert_eq!(module.test(LogTest::module("abc")), "abc");
110 assert_eq!(module.test(LogTest::module("abcd")), "abcd");
111 }
112
113 //*********************************************************************************************
114 /// Test left alignment with truncation
115 #[test]
116 fn left_align_trunc()
117 {
118 let module = Module::new("<3-").unwrap();
119
120 assert_eq!(module.test(LogTest::module("a")), "a ");
121 assert_eq!(module.test(LogTest::module("abc")), "abc");
122 assert_eq!(module.test(LogTest::module("abcd")), "abc");
123 }
124
125 //*********************************************************************************************
126 /// Test right alignment.
127 #[test]
128 fn right_align()
129 {
130 let module = Module::new(">3").unwrap();
131
132 assert_eq!(module.test(LogTest::module("a")), " a");
133 assert_eq!(module.test(LogTest::module("abc")), "abc");
134 assert_eq!(module.test(LogTest::module("abcd")), "abcd");
135 }
136
137 //*********************************************************************************************
138 /// Test right alignment with truncation
139 #[test]
140 fn right_align_trunc()
141 {
142 let module = Module::new(">3-").unwrap();
143
144 assert_eq!(module.test(LogTest::module("a")), " a");
145 assert_eq!(module.test(LogTest::module("abc")), "abc");
146 assert_eq!(module.test(LogTest::module("abcd")), "bcd");
147 }
148}