Procházet zdrojové kódy

Use the log crate.

Mathias Gottschlag před 5 roky
rodič
revize
16166da916
2 změnil soubory, kde provedl 14 přidání a 12 odebrání
  1. 10
    6
      src/bin/client.rs
  2. 4
    6
      src/network/client.rs

+ 10
- 6
src/bin/client.rs Zobrazit soubor

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

+ 4
- 6
src/network/client.rs Zobrazit soubor

@@ -7,6 +7,7 @@ use futures::future::{self, Either, Future};
7 7
 use futures::sink::{Sink, SinkExt};
8 8
 use futures::stream::{Stream, StreamExt};
9 9
 use futures::task::{Context, Poll};
10
+use log::error;
10 11
 use rmps::Serializer;
11 12
 use serde::de::DeserializeOwned;
12 13
 use serde::Serialize;
@@ -86,8 +87,7 @@ async fn client_connection_task<StreamType, ErrorType, PacketType>(
86 87
                                         free_ids.free(deserialized.id);
87 88
                                         call.send(Ok(deserialized.payload)).ok();
88 89
                                     } else {
89
-                                        // TODO: Use proper logging functionality.
90
-                                        eprintln!(
90
+                                        error!(
91 91
                                             "Received a reply for an unknown call ({:?})!",
92 92
                                             deserialized.id
93 93
                                         );
@@ -98,14 +98,12 @@ async fn client_connection_task<StreamType, ErrorType, PacketType>(
98 98
                                 // We received an invalid packet. We cannot determine for which
99 99
                                 // call the packet was, so we just ignore it. The corresponding
100 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 105
                     Some(Err(e)) => {
107
-                        // TODO: Use proper logging functionality.
108
-                        eprintln!("Network error: {:?}", e);
106
+                        error!("Network error: {:?}", e);
109 107
                     }
110 108
                     None => {
111 109
                         // Stream closed. We still need to return errors for any

Loading…
Zrušit
Uložit