Skip to content

Commit b2bd20a

Browse files
committed
Move all println's after logger setup to the log facade
1 parent 815b5ae commit b2bd20a

4 files changed

Lines changed: 51 additions & 25 deletions

File tree

rust/impls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bytes = "1.4.0"
1212
tokio = { version = "1.38.0", default-features = false, features = ["rt", "macros"] }
1313
native-tls = { version = "0.2.14", default-features = false }
1414
postgres-native-tls = { version = "0.5.2", default-features = false, features = ["runtime"] }
15+
log = "0.4.29"
1516

1617
[dev-dependencies]
1718
tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "macros"] }

rust/impls/src/postgres_store.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use tokio::sync::Mutex;
1717
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
1818
use tokio_postgres::{error, Client, NoTls, Socket, Transaction};
1919

20+
use log::{info, error, warn, debug};
21+
2022
pub use native_tls::Certificate;
2123

2224
pub(crate) struct VssDbRecord {
@@ -104,6 +106,7 @@ where
104106

105107
async fn ensure_connected(&self, client: &mut Client) -> Result<(), Error> {
106108
if client.is_closed() || client.check_connection().await.is_err() {
109+
debug!("Rotating connection to the postgres database");
107110
let new_client =
108111
make_db_connection(&self.endpoint, &self.db_name, self.tls.clone()).await?;
109112
*client = new_client;
@@ -145,7 +148,7 @@ where
145148
// Connection must be driven on a separate task, and will resolve when the client is dropped
146149
tokio::spawn(async move {
147150
if let Err(e) = connection.await {
148-
eprintln!("Connection error: {}", e);
151+
warn!("Connection error: {}", e);
149152
}
150153
});
151154
Ok(client)
@@ -173,7 +176,7 @@ where
173176
client.execute(&stmt, &[]).await.map_err(|e| {
174177
Error::new(ErrorKind::Other, format!("Failed to create database {}: {}", db_name, e))
175178
})?;
176-
println!("Created database {}", db_name);
179+
info!("Created database {}", db_name);
177180
}
178181

179182
Ok(())
@@ -291,7 +294,7 @@ where
291294
panic!("We do not allow downgrades");
292295
}
293296

294-
println!("Applying migration(s) {} through {}", migration_start, migrations.len() - 1);
297+
info!("Applying migration(s) {} through {}", migration_start, migrations.len() - 1);
295298

296299
for (idx, &stmt) in (&migrations[migration_start..]).iter().enumerate() {
297300
let _num_rows = tx.execute(stmt, &[]).await.map_err(|e| {

rust/server/src/main.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tokio::signal::unix::SignalKind;
1717
use hyper::server::conn::http1;
1818
use hyper_util::rt::TokioIo;
1919

20-
use log::error;
20+
use log::{error, info, warn};
2121

2222
use api::auth::{Authorizer, NoopAuthorizer};
2323
use api::kv_store::KvStore;
@@ -52,7 +52,7 @@ fn main() {
5252
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
5353
Ok(runtime) => Arc::new(runtime),
5454
Err(e) => {
55-
eprintln!("Failed to setup tokio runtime: {}", e);
55+
error!("Failed to setup tokio runtime: {}", e);
5656
std::process::exit(-1);
5757
},
5858
};
@@ -62,15 +62,15 @@ fn main() {
6262
let mut sighup_stream = match tokio::signal::unix::signal(SignalKind::hangup()) {
6363
Ok(stream) => stream,
6464
Err(e) => {
65-
eprintln!("Failed to register SIGHUP handler: {e}");
65+
error!("Failed to register SIGHUP handler: {e}");
6666
std::process::exit(-1);
6767
}
6868
};
6969

7070
let mut sigterm_stream = match tokio::signal::unix::signal(SignalKind::terminate()) {
7171
Ok(stream) => stream,
7272
Err(e) => {
73-
println!("Failed to register for SIGTERM stream: {}", e);
73+
error!("Failed to register for SIGTERM stream: {}", e);
7474
std::process::exit(-1);
7575
},
7676
};
@@ -81,11 +81,11 @@ fn main() {
8181
if let Some(rsa_pem) = config.rsa_pem {
8282
authorizer = match JWTAuthorizer::new(&rsa_pem).await {
8383
Ok(auth) => {
84-
println!("Configured JWT authorizer with RSA public key");
84+
info!("Configured JWT authorizer with RSA public key");
8585
Some(Arc::new(auth))
8686
},
8787
Err(e) => {
88-
println!("Failed to configure JWT authorizer: {}", e);
88+
error!("Failed to configure JWT authorizer: {}", e);
8989
std::process::exit(-1);
9090
},
9191
};
@@ -94,14 +94,14 @@ fn main() {
9494
#[cfg(feature = "sigs")]
9595
{
9696
if authorizer.is_none() {
97-
println!("Configured signature-validating authorizer");
97+
info!("Configured signature-validating authorizer");
9898
authorizer = Some(Arc::new(SignatureValidatingAuthorizer));
9999
}
100100
}
101101
let authorizer = if let Some(auth) = authorizer {
102102
auth
103103
} else {
104-
println!("No authentication method configured, all storage with the same store id will be commingled.");
104+
warn!("No authentication method configured, all storage with the same store id will be commingled.");
105105
Arc::new(NoopAuthorizer {})
106106
};
107107

@@ -114,10 +114,10 @@ fn main() {
114114
)
115115
.await
116116
.unwrap_or_else(|e| {
117-
println!("Failed to start postgres TLS backend: {}", e);
117+
error!("Failed to start postgres TLS backend: {}", e);
118118
std::process::exit(-1);
119119
});
120-
println!(
120+
info!(
121121
"Connected to PostgreSQL TLS backend with DSN: {}/{}",
122122
config.postgresql_prefix, config.vss_db
123123
);
@@ -130,21 +130,21 @@ fn main() {
130130
)
131131
.await
132132
.unwrap_or_else(|e| {
133-
println!("Failed to start postgres plaintext backend: {}", e);
133+
error!("Failed to start postgres plaintext backend: {}", e);
134134
std::process::exit(-1);
135135
});
136-
println!(
136+
info!(
137137
"Connected to PostgreSQL plaintext backend with DSN: {}/{}",
138138
config.postgresql_prefix, config.vss_db
139139
);
140140
Arc::new(postgres_plaintext_backend)
141141
};
142142

143143
let rest_svc_listener = TcpListener::bind(&config.bind_address).await.unwrap_or_else(|e| {
144-
println!("Failed to bind listening port: {}", e);
144+
error!("Failed to bind listening port: {}", e);
145145
std::process::exit(-1);
146146
});
147-
println!("Listening for incoming connections on {}{}", config.bind_address, crate::vss_service::BASE_PATH_PREFIX);
147+
info!("Listening for incoming connections on {}{}", config.bind_address, crate::vss_service::BASE_PATH_PREFIX);
148148

149149
loop {
150150
tokio::select! {
@@ -155,15 +155,15 @@ fn main() {
155155
let vss_service = VssService::new(Arc::clone(&store), Arc::clone(&authorizer));
156156
runtime.spawn(async move {
157157
if let Err(err) = http1::Builder::new().serve_connection(io_stream, vss_service).await {
158-
eprintln!("Failed to serve connection: {}", err);
158+
warn!("Failed to serve connection: {}", err);
159159
}
160160
});
161161
},
162-
Err(e) => eprintln!("Failed to accept connection: {}", e),
162+
Err(e) => warn!("Failed to accept connection: {}", e),
163163
}
164164
}
165165
_ = tokio::signal::ctrl_c() => {
166-
println!("Received CTRL-C, shutting down..");
166+
info!("Received CTRL-C, shutting down..");
167167
break;
168168
}
169169
_ = sighup_stream.recv() => {
@@ -172,7 +172,7 @@ fn main() {
172172
}
173173
}
174174
_ = sigterm_stream.recv() => {
175-
println!("Received SIGTERM, shutting down..");
175+
info!("Received SIGTERM, shutting down..");
176176
break;
177177
}
178178
}

rust/server/src/vss_service.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use std::future::Future;
1818
use std::pin::Pin;
1919
use std::sync::Arc;
2020

21+
use log::{debug, trace};
22+
2123
#[derive(Clone)]
2224
pub struct VssService {
2325
store: Arc<dyn KvStore>,
@@ -73,22 +75,42 @@ impl Service<Request<Incoming>> for VssService {
7375
async fn handle_get_object_request(
7476
store: Arc<dyn KvStore>, user_token: String, request: GetObjectRequest,
7577
) -> Result<GetObjectResponse, VssError> {
76-
store.get(user_token, request).await
78+
trace!("handling GetObject request with store_id {}", request.store_id);
79+
let result = store.get(user_token, request).await;
80+
if let Err(ref e) = result {
81+
debug!("GetObject request with store_id {} failed", e);
82+
}
83+
result
7784
}
7885
async fn handle_put_object_request(
7986
store: Arc<dyn KvStore>, user_token: String, request: PutObjectRequest,
8087
) -> Result<PutObjectResponse, VssError> {
81-
store.put(user_token, request).await
88+
trace!("handling PutObject request with store_id {}", request.store_id);
89+
let result = store.put(user_token, request).await;
90+
if let Err(ref e) = result {
91+
debug!("PutObject request with store_id {} failed", e);
92+
}
93+
result
8294
}
8395
async fn handle_delete_object_request(
8496
store: Arc<dyn KvStore>, user_token: String, request: DeleteObjectRequest,
8597
) -> Result<DeleteObjectResponse, VssError> {
86-
store.delete(user_token, request).await
98+
trace!("handling DeleteObject request with store_id {}", request.store_id);
99+
let result = store.delete(user_token, request).await;
100+
if let Err(ref e) = result {
101+
debug!("DeleteObject request with store_id {} failed", e);
102+
}
103+
result
87104
}
88105
async fn handle_list_object_request(
89106
store: Arc<dyn KvStore>, user_token: String, request: ListKeyVersionsRequest,
90107
) -> Result<ListKeyVersionsResponse, VssError> {
91-
store.list_key_versions(user_token, request).await
108+
trace!("handling ListKeyVersions request with store_id {}", request.store_id);
109+
let result = store.list_key_versions(user_token, request).await;
110+
if let Err(ref e) = result {
111+
debug!("ListKeyVersions request with store_id {} failed", e);
112+
}
113+
result
92114
}
93115
async fn handle_request<
94116
T: Message + Default,

0 commit comments

Comments
 (0)