Skip to content

Commit d484e50

Browse files
committed
[IMP] logging: add basic logging
Added basic logging to simplify error understanding.
1 parent c80165b commit d484e50

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ tower-http = { version = "0.6.8", features = ["cors"] }
1010
zpl-forge = "0.1.1"
1111
open = "5.3.3"
1212
clap = { version = "4.5.58", features = ["derive"] }
13+
log = "0.4.29"
14+
colog = "1.4.0"

src/main.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use axum::{
33
};
44

55
use clap::Parser;
6+
use log::{error, info};
67
use open;
78
use std::{collections::HashMap, error::Error, fs::File, io::Write, time::Duration};
89
use tower_http::cors::{Any, CorsLayer};
@@ -47,24 +48,35 @@ async fn post_print(State(args): State<Args>, body: Bytes) -> impl IntoResponse
4748
Resolution::Dpi203,
4849
) {
4950
Ok(e) => e,
50-
Err(_) => return StatusCode::BAD_REQUEST,
51+
Err(_) => {
52+
error!("Invalid ZPL input");
53+
return StatusCode::BAD_REQUEST;
54+
}
5155
};
5256

5357
let png_bytes = match engine.render(PngBackend::new(), &HashMap::new()) {
5458
Ok(bytes) => bytes,
55-
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
59+
Err(_) => {
60+
error!("Failed to render png from ZPL");
61+
return StatusCode::INTERNAL_SERVER_ERROR;
62+
}
5663
};
5764

5865
match display_png(png_bytes.as_slice()) {
5966
Ok(_) => (),
60-
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
67+
Err(_) => {
68+
error!("Could not open PNG in default viewer");
69+
return StatusCode::INTERNAL_SERVER_ERROR;
70+
}
6171
}
6272

73+
info!("Successfully rendered and displayed label");
6374
StatusCode::OK
6475
}
6576

6677
#[tokio::main]
6778
async fn main() -> Result<(), Box<dyn Error>> {
79+
colog::init();
6880
let args = Args::parse();
6981

7082
let cors = CorsLayer::new()

0 commit comments

Comments
 (0)