Skip to content

Commit

Permalink
feat: switch to tracing for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Leikoe committed Jan 2, 2025
1 parent 63e597a commit a4c8882
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
103 changes: 103 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ default-run = "crabe_async"
tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "time", "macros", "signal"] }
futures-util = "0.3.31"

# logging
tracing = "0.1.41"
tracing-subscriber = "0.3.19"

# path planning
rrt = "0.7.0"
rand = "0.8.5"
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crabe_async::{
};
use std::time::Duration;
use tokio::{join, select, time::sleep};
use tracing::{info, Level};

// #[derive(Debug, Clone, Copy)]
// enum HaltedState {
Expand Down Expand Up @@ -327,8 +328,14 @@ async fn update_world_with_vision_forever(mut world: World, real: bool) {
/// Simulation of a real control loop
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();

// options which will come from cli
let color = TeamColor::Blue;
let real = false;

info!("Starting up Coral (color: {:?}, real: {})", color, real);

let world = World::default_with_team_color(color);
let gc = GameController::new(None, None);
let controller = SimRobotController::new(color).await;
Expand Down
7 changes: 4 additions & 3 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use tokio::{
net::{TcpListener, TcpStream},
sync::Notify,
};
use tracing::info;

use crate::{
math::{Point2, Vec2},
Expand Down Expand Up @@ -171,7 +172,7 @@ pub async fn init() {
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");
println!("Listening on: {}", addr);
info!("viewer server listening at: {}", addr);

let new_frame_notify_clone = new_frame_notify.clone();
tokio::spawn(async move {
Expand All @@ -193,13 +194,13 @@ async fn accept_connection(new_frame_notifier: Arc<Notify>, stream: TcpStream) {
let addr = stream
.peer_addr()
.expect("connected streams should have a peer address");
println!("Peer address: {}", addr);
info!(%addr, "new viewer client connected");

let mut ws_stream = tokio_tungstenite::accept_async(stream)
.await
.expect("Error during the websocket handshake occurred");

println!("New WebSocket connection: {}", addr);
info!(%addr, "upgraded to websocket");

while !ws_stream.is_terminated() {
new_frame_notifier.notified().await;
Expand Down
3 changes: 2 additions & 1 deletion src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use ball::Ball;
pub use robot::{AllyRobot, AvoidanceMode, EnnemyRobot, GotoError, RobotId};
use serde::{Deserialize, Serialize};
use tokio::{sync::Notify, time::sleep};
use tracing::warn;

use crate::{
league_protocols::vision_packet::SslGeometryFieldSize,
Expand Down Expand Up @@ -78,7 +79,7 @@ impl World {

pub async fn allies_detection(&self) {
while self.team.lock().unwrap_ignore_poison().is_empty() {
println!("[WARNING] not detecting any ally robots yet, waiting 1s.");
warn!("not detecting any ally robots yet, waiting 1s.");
sleep(Duration::from_secs(1)).await;
}
}
Expand Down

0 comments on commit a4c8882

Please sign in to comment.