浏览代码

Use the log crate.

父节点
当前提交
16166da916
共有 2 个文件被更改,包括 14 次插入12 次删除
  1. 10
    6
      src/bin/client.rs
  2. 4
    6
      src/network/client.rs

+ 10
- 6
src/bin/client.rs 查看文件

2
 use std::fs::{self, create_dir_all, remove_file};
2
 use std::fs::{self, create_dir_all, remove_file};
3
 use std::sync::Arc;
3
 use std::sync::Arc;
4
 
4
 
5
+use env_logger::Env;
5
 use fslock::LockFile;
6
 use fslock::LockFile;
7
+use log::{error, info};
6
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
8
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
7
 use tokio::net::{UnixListener, UnixStream};
9
 use tokio::net::{UnixListener, UnixStream};
10
+use tokio::signal;
8
 use tokio::stream::StreamExt;
11
 use tokio::stream::StreamExt;
9
 use tokio::sync::{mpsc, Mutex};
12
 use tokio::sync::{mpsc, Mutex};
10
 
13
 
51
 
54
 
52
 #[tokio::main]
55
 #[tokio::main]
53
 async fn main() {
56
 async fn main() {
57
+    env_logger::from_env(Env::default().default_filter_or("info")).init();
58
+
54
     // Create the data directories if necessary.
59
     // Create the data directories if necessary.
55
     create_dir_all(paths::config_dir()).unwrap();
60
     create_dir_all(paths::config_dir()).unwrap();
56
     create_dir_all(paths::client_data()).unwrap();
61
     create_dir_all(paths::client_data()).unwrap();
59
     fs::write(&paths::client_lock(), b"").ok();
64
     fs::write(&paths::client_lock(), b"").ok();
60
     let mut lock = LockFile::open(&paths::client_lock()).expect("cannot open lockfile");
65
     let mut lock = LockFile::open(&paths::client_lock()).expect("cannot open lockfile");
61
     if !lock.try_lock().expect("error while locking the lock file") {
66
     if !lock.try_lock().expect("error while locking the lock file") {
62
-        eprintln!("Client already running, exiting...");
67
+        error!("Client already running, exiting...");
63
         return;
68
         return;
64
     }
69
     }
65
 
70
 
90
         tokio::select! {
95
         tokio::select! {
91
             result = &mut ctrl_c => {
96
             result = &mut ctrl_c => {
92
                 result.expect("could not listen for Ctrl+C");
97
                 result.expect("could not listen for Ctrl+C");
93
-                println!("Ctrl+C pressed, terminating...");
98
+                info!("Ctrl+C pressed, terminating...");
94
                 // Ctrl+C was pressed, so we terminate the program.
99
                 // Ctrl+C was pressed, so we terminate the program.
95
                 break;
100
                 break;
96
             }
101
             }
111
                         };
116
                         };
112
                     }
117
                     }
113
                     Some(Err(err)) => {
118
                     Some(Err(err)) => {
114
-                        eprintln!("Error while listening on the local unix socket: {:?}", err);
119
+                        error!("Error while listening on the local unix socket: {:?}", err);
115
                         break;
120
                         break;
116
                     }
121
                     }
117
                     None => {
122
                     None => {
118
-                        eprintln!("listening for CLI connections failed");
123
+                        error!("listening for CLI connections failed");
119
                         break;
124
                         break;
120
                     }
125
                     }
121
                 }
126
                 }
122
             }
127
             }
123
             error = errors.next() => {
128
             error = errors.next() => {
124
                 let error = error.unwrap();
129
                 let error = error.unwrap();
125
-                eprintln!("error: {:?}", error.error);
130
+                error!("Synchronization error: {:?}", error.error);
126
                 for directory in directories.iter_mut() {
131
                 for directory in directories.iter_mut() {
127
                     if directory.path == error.directory {
132
                     if directory.path == error.directory {
128
                         // In theory, there could be a race condition here where we pause a
133
                         // In theory, there could be a race condition here where we pause a
204
 
209
 
205
     Ok(())
210
     Ok(())
206
 }
211
 }
207
-

+ 4
- 6
src/network/client.rs 查看文件

7
 use futures::sink::{Sink, SinkExt};
7
 use futures::sink::{Sink, SinkExt};
8
 use futures::stream::{Stream, StreamExt};
8
 use futures::stream::{Stream, StreamExt};
9
 use futures::task::{Context, Poll};
9
 use futures::task::{Context, Poll};
10
+use log::error;
10
 use rmps::Serializer;
11
 use rmps::Serializer;
11
 use serde::de::DeserializeOwned;
12
 use serde::de::DeserializeOwned;
12
 use serde::Serialize;
13
 use serde::Serialize;
86
                                         free_ids.free(deserialized.id);
87
                                         free_ids.free(deserialized.id);
87
                                         call.send(Ok(deserialized.payload)).ok();
88
                                         call.send(Ok(deserialized.payload)).ok();
88
                                     } else {
89
                                     } else {
89
-                                        // TODO: Use proper logging functionality.
90
-                                        eprintln!(
90
+                                        error!(
91
                                             "Received a reply for an unknown call ({:?})!",
91
                                             "Received a reply for an unknown call ({:?})!",
92
                                             deserialized.id
92
                                             deserialized.id
93
                                         );
93
                                         );
98
                                 // We received an invalid packet. We cannot determine for which
98
                                 // We received an invalid packet. We cannot determine for which
99
                                 // call the packet was, so we just ignore it. The corresponding
99
                                 // call the packet was, so we just ignore it. The corresponding
100
                                 // call will freeze until the connection is dropped.
100
                                 // call will freeze until the connection is dropped.
101
-                                // TODO: Use proper logging functionality.
102
-                                eprintln!("Invalid packet received: {:?}", e);
101
+                                error!("Invalid packet received: {:?}", e);
103
                             }
102
                             }
104
                         };
103
                         };
105
                     }
104
                     }
106
                     Some(Err(e)) => {
105
                     Some(Err(e)) => {
107
-                        // TODO: Use proper logging functionality.
108
-                        eprintln!("Network error: {:?}", e);
106
+                        error!("Network error: {:?}", e);
109
                     }
107
                     }
110
                     None => {
108
                     None => {
111
                         // Stream closed. We still need to return errors for any
109
                         // Stream closed. We still need to return errors for any

正在加载...
取消
保存