Skip to content

Commit

Permalink
Merge pull request #146 from NethermindEth/decrease_logging
Browse files Browse the repository at this point in the history
feat: fine grain control on logging, remove unused packages
  • Loading branch information
stranger80 authored Oct 12, 2023
2 parents 64eb492 + 5983348 commit 3a701b1
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 84 deletions.
58 changes: 17 additions & 41 deletions api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ rocket = { version = "0.5.0-rc.2", features = ["json"] }
rand = "0.8"
serde = { version = "1.0.152", features = ["derive"] }
tracing = "^0.1"
tracing-subscriber = "^0.3"
tracing-subscriber = { version = "^0.3.17", features=["env-filter", "json", "registry", "smallvec"]}
tracing-appender = "0.2"
log = "0.4.20"
env_logger = "0.10.0"
tracing-log = "0.1.3"
crossbeam = "0.8.2"
uuid = { version = "1.4.1", features = ["serde", "v4"] }
futures = "0.3.28"
crossbeam-queue = "0.3.8"
crossbeam-skiplist = "0.1.1"
fmt = "0.1.0"
yansi = "0.5.1"
10 changes: 8 additions & 2 deletions api/src/handlers/cairo_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ use crate::utils::lib::CAIRO_DIR;
use crate::worker::WorkerEngine;
use rocket::State;
use std::process::{Command, Stdio};
use tracing::instrument;
use tracing::{error, info, instrument};

// Read the version from the cairo Cargo.toml file.
#[instrument]
#[get("/cairo_version")]
pub async fn cairo_version() -> String {
info!("/cairo_version");
do_cairo_version().unwrap_or_else(|e| e)
}

// Read the version from the cairo Cargo.toml file.
#[instrument]
#[get("/cairo_version_async")]
pub async fn cairo_version_async(engine: &State<WorkerEngine>) -> String {
info!("/cairo_version_async");
do_process_command(ApiCommand::CairoVersion, engine)
}

#[instrument]
#[get("/cairo_version_result/<process_id>")]
pub async fn get_cairo_version_result(process_id: String, engine: &State<WorkerEngine>) -> String {
info!("/cairo_version_result/{:?}", process_id);
fetch_process_result(process_id, engine, |result| match result {
ApiCommandResult::CairoVersion(version) => version.to_string(),
_ => String::from("Result not available"),
Expand Down Expand Up @@ -51,6 +54,9 @@ pub fn do_cairo_version() -> Result<String, String> {
.stdout,
) {
Ok(version) => Ok(version),
Err(e) => Err(e.to_string()),
Err(e) => {
error!("{:?}", e.to_string());
Err(e.to_string())
}
}
}
20 changes: 14 additions & 6 deletions api/src/handlers/compile_casm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@ use rocket::serde::json;
use rocket::serde::json::Json;
use rocket::tokio::fs;
use rocket::State;
use tracing::info;
use tracing::instrument;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

#[instrument]
#[get("/compile-to-casm/<remix_file_path..>")]
pub async fn compile_to_casm(remix_file_path: PathBuf) -> Json<CompileResponse> {
info!("/compile-to-casm/{:?}", remix_file_path);
do_compile_to_casm(remix_file_path).await
}

#[instrument]
#[get("/compile-to-casm-async/<remix_file_path..>")]
pub async fn compile_to_casm_async(
remix_file_path: PathBuf,
engine: &State<WorkerEngine>,
) -> String {
info!("/compile-to-casm-async/{:?}", remix_file_path);
do_process_command(ApiCommand::CasmCompile(remix_file_path), engine)
}

#[instrument]
#[get("/compile-to-casm-result/<process_id>")]
pub async fn copmile_to_casm_result(process_id: String, engine: &State<WorkerEngine>) -> String {
info!("/compile-to-casm-result/{:?}", process_id);
fetch_process_result(process_id, engine, |result| match result {
ApiCommandResult::CasmCompile(casm_result) => json::to_string(&casm_result).unwrap(),
_ => String::from("Result not available"),
Expand All @@ -48,10 +56,10 @@ pub async fn do_compile_to_casm(remix_file_path: PathBuf) -> Json<CompileRespons
// check if the file has .sierra extension
match get_file_ext(&remix_file_path) {
ext if ext == "sierra" => {
println!("LOG: File extension is sierra");
debug!("LOG: File extension is sierra");
}
_ => {
println!("LOG: File extension not supported");
debug!("LOG: File extension not supported");
return Json(CompileResponse {
file_content: "".to_string(),
message: "File extension not supported".to_string(),
Expand All @@ -73,14 +81,14 @@ pub async fn do_compile_to_casm(remix_file_path: PathBuf) -> Json<CompileRespons
match casm_path.parent() {
Some(parent) => match fs::create_dir_all(parent).await {
Ok(_) => {
println!("LOG: Created directory: {:?}", parent);
debug!("LOG: Created directory: {:?}", parent);
}
Err(e) => {
println!("LOG: Error creating directory: {:?}", e);
debug!("LOG: Error creating directory: {:?}", e);
}
},
None => {
println!("LOG: Error creating directory");
debug!("LOG: Error creating directory");
}
}

Expand All @@ -96,7 +104,7 @@ pub async fn do_compile_to_casm(remix_file_path: PathBuf) -> Json<CompileRespons
.spawn()
.expect("Failed to execute starknet-sierra-compile");

println!("LOG: ran command:{:?}", compile);
debug!("LOG: ran command:{:?}", compile);

let output = result.wait_with_output().expect("Failed to wait on child");

Expand Down
19 changes: 13 additions & 6 deletions api/src/handlers/compile_sierra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use rocket::serde::json;
use rocket::serde::json::Json;
use rocket::tokio::fs;
use rocket::State;
use tracing::{debug, instrument};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

#[instrument]
#[get("/compile-to-sierra/<remix_file_path..>")]
pub async fn compile_to_sierra(remix_file_path: PathBuf) -> Json<CompileResponse> {
info!("/compile-to-sierra");
do_compile_to_sierra(remix_file_path)
.await
.unwrap_or(Json::from(CompileResponse {
Expand All @@ -21,16 +24,20 @@ pub async fn compile_to_sierra(remix_file_path: PathBuf) -> Json<CompileResponse
}))
}

#[instrument]
#[get("/compile-to-sierra-async/<remix_file_path..>")]
pub async fn compile_to_siera_async(
remix_file_path: PathBuf,
engine: &State<WorkerEngine>,
) -> String {
info!("/compile-to-sierra-async");
do_process_command(ApiCommand::SierraCompile(remix_file_path), engine)
}

#[instrument]
#[get("/compile-to-sierra-result/<process_id>")]
pub async fn get_siera_compile_result(process_id: String, engine: &State<WorkerEngine>) -> String {
info!("/compile-to-sierra-result");
fetch_process_result(process_id, engine, |result| match result {
ApiCommandResult::SierraCompile(sierra_result) => json::to_string(&sierra_result).unwrap(),
_ => String::from("Result not available"),
Expand All @@ -56,10 +63,10 @@ pub async fn do_compile_to_sierra(
// check if the file has .cairo extension
match get_file_ext(&remix_file_path) {
ext if ext == "cairo" => {
println!("LOG: File extension is cairo");
debug!("LOG: File extension is cairo");
}
_ => {
println!("LOG: File extension not supported");
debug!("LOG: File extension not supported");
return Ok(Json(CompileResponse {
file_content: "".to_string(),
message: "File extension not supported".to_string(),
Expand All @@ -82,14 +89,14 @@ pub async fn do_compile_to_sierra(
match sierra_path.parent() {
Some(parent) => match fs::create_dir_all(parent).await {
Ok(_) => {
println!("LOG: Created directory: {:?}", parent);
debug!("LOG: Created directory: {:?}", parent);
}
Err(e) => {
println!("LOG: Error creating directory: {:?}", e);
debug!("LOG: Error creating directory: {:?}", e);
}
},
None => {
println!("LOG: Error creating directory");
debug!("LOG: Error creating directory");
}
}

Expand All @@ -107,7 +114,7 @@ pub async fn do_compile_to_sierra(
.spawn()
.expect("Failed to execute starknet-compile");

println!("LOG: ran command:{:?}", compile);
debug!("LOG: ran command:{:?}", compile);

let output = result.wait_with_output().expect("Failed to wait on child");

Expand Down
3 changes: 3 additions & 0 deletions api/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ use crate::handlers::compile_sierra::do_compile_to_sierra;
use crate::handlers::scarb_compile::do_scarb_compile;
use crate::handlers::types::{ApiCommand, ApiCommandResult, FileContentMap};
use rocket::serde::json::Json;
use tracing::info;
use std::path::Path;
use tracing::instrument;

#[instrument]
#[get("/health")]
pub async fn health() -> &'static str {
info!("/health");
"OK"
}

#[instrument]
#[get("/")]
pub async fn who_is_this() -> &'static str {
info!("/who_is_this");
"Who are you?"
}

Expand Down
3 changes: 3 additions & 0 deletions api/src/handlers/process.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::handlers::types::{ApiCommand, ApiCommandResult};
use crate::worker::{ProcessState, WorkerEngine};
use rocket::State;
use tracing::{info, instrument};
use uuid::Uuid;

#[instrument]
#[get("/process_status/<process_id>")]
pub async fn get_process_status(process_id: String, engine: &State<WorkerEngine>) -> String {
info!("/process_status/{:?}", process_id);
// get status of process by ID
match Uuid::parse_str(&process_id) {
Ok(process_uuid) => {
Expand Down
Loading

0 comments on commit 3a701b1

Please sign in to comment.