slog_notify/
lib.rs

1/*Copyright 2017 bjorn3
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.*/
14
15extern crate slog;
16extern crate notify_rust;
17
18use notify_rust::Notification;
19use notify_rust::hints::NotificationHint;
20
21pub struct Notify{
22    name: &'static str
23}
24
25impl slog::Drain for Notify{
26    type Err = String;
27    type Ok = ();
28    
29    fn log(&self, info: &slog::Record, _: &slog::OwnedKVList) -> Result<Self::Ok, Self::Err>{
30        let summary = format!("{:?} {}@{}:{}", info.level(), info.file(), info.line(), info.column());
31        let body = format!("{}", info.msg());
32        
33        let mut notification = Notification::new();
34        notification
35            .appname(self.name)
36            .summary(&summary)
37            .body(&body);
38        
39        if info.level().is_at_least(slog::Level::Warning){
40            notification.hint(NotificationHint::Resident(true));
41        }
42        
43        notification
44            .show()
45            .unwrap();
46        Ok(())
47    }
48}
49
50pub fn simple(name: &'static str) -> Notify {
51    Notify{
52        name: name
53    }
54}