Skip to content

Commit

Permalink
Replace all print calls with log crate calls
Browse files Browse the repository at this point in the history
  • Loading branch information
friedkiwi committed Sep 24, 2024
1 parent a877ff2 commit e06937d
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 35 deletions.
102 changes: 100 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ lazy-regex = "3.1.0"
tub = "0.3.7"
tokio-util = { version = "0.7.10", features=["futures-io", "futures-util", "codec"]}
futures = "0.3.30"
log = "0.4.22"
env_logger = "0.11.5"

# Compilation optimizations for release builds
# Increases compile time but typically produces a faster and smaller binary. Suitable for final releases but not for debug builds.
Expand Down
25 changes: 13 additions & 12 deletions src/jobs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::SinkExt;
use rquickjs::{async_with, AsyncContext, AsyncRuntime};
use std::{num::NonZeroUsize, sync::Arc, thread::available_parallelism, time::SystemTime};
use log::{debug, error};
use tokio::{runtime::Handle, sync::Mutex, task::block_in_place};
use tub::Pool;

Expand Down Expand Up @@ -164,11 +165,11 @@ pub async fn process_decrypt_n_signature<W>(
Ok(x) => x,
Err(n) => {
if n.is_exception() {
println!("JavaScript interpreter error (nsig code): {:?}", ctx.catch().as_exception());
error!("JavaScript interpreter error (nsig code): {:?}", ctx.catch().as_exception());
} else {
println!("JavaScript interpreter error (nsig code): {}", n);
error!("JavaScript interpreter error (nsig code): {}", n);
}
println!("Code: {}", player_info.nsig_function_code.clone());
debug!("Code: {}", player_info.nsig_function_code.clone());
writer = cloned_writer.lock().await;
let _ = writer.send(OpcodeResponse {
opcode: JobOpcode::DecryptNSignature,
Expand All @@ -192,11 +193,11 @@ pub async fn process_decrypt_n_signature<W>(
Ok(x) => x,
Err(n) => {
if n.is_exception() {
println!("JavaScript interpreter error (nsig code): {:?}", ctx.catch().as_exception());
error!("JavaScript interpreter error (nsig code): {:?}", ctx.catch().as_exception());
} else {
println!("JavaScript interpreter error (nsig code): {}", n);
error!("JavaScript interpreter error (nsig code): {}", n);
}
println!("Code: {}", call_string.clone());
debug!("Code: {}", call_string.clone());
writer = cloned_writer.lock().await;
let _ = writer.send(OpcodeResponse {
opcode: JobOpcode::DecryptNSignature,
Expand Down Expand Up @@ -243,11 +244,11 @@ pub async fn process_decrypt_signature<W>(
Ok(x) => x,
Err(n) => {
if n.is_exception() {
println!("JavaScript interpreter error (sig code): {:?}", ctx.catch().as_exception());
error!("JavaScript interpreter error (sig code): {:?}", ctx.catch().as_exception());
} else {
println!("JavaScript interpreter error (sig code): {}", n);
error!("JavaScript interpreter error (sig code): {}", n);
}
println!("Code: {}", player_info.sig_function_code.clone());
debug!("Code: {}", player_info.sig_function_code.clone());
writer = cloned_writer.lock().await;
let _ = writer.send(OpcodeResponse {
opcode: JobOpcode::DecryptSignature,
Expand All @@ -274,11 +275,11 @@ pub async fn process_decrypt_signature<W>(
Ok(x) => x,
Err(n) => {
if n.is_exception() {
println!("JavaScript interpreter error (sig code): {:?}", ctx.catch().as_exception());
error!("JavaScript interpreter error (sig code): {:?}", ctx.catch().as_exception());
} else {
println!("JavaScript interpreter error (sig code): {}", n);
error!("JavaScript interpreter error (sig code): {}", n);
}
println!("Code: {}", call_string.clone());
debug!("Code: {}", call_string.clone());
writer = cloned_writer.lock().await;
let _ = writer.send(OpcodeResponse {
opcode: JobOpcode::DecryptSignature,
Expand Down
20 changes: 12 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use jobs::{process_decrypt_n_signature, process_fetch_update, GlobalState, JobOp
use opcode::OpcodeDecoder;
use player::fetch_update;
use std::{env::args, sync::Arc};
use env_logger::Env;
use tokio::{
fs::remove_file,
io::{AsyncReadExt, AsyncWrite},
net::{TcpListener, UnixListener},
sync::Mutex,
};
use tokio_util::codec::Framed;
use log::{info, error, debug};

use crate::jobs::{
process_decrypt_signature, process_get_signature_timestamp, process_player_status,
Expand All @@ -24,11 +26,11 @@ use crate::jobs::{

macro_rules! loop_main {
($i:ident, $s:ident) => {
println!("Fetching player");
info!("Fetching player");
match fetch_update($s.clone()).await {
Ok(()) => println!("Successfully fetched player"),
Ok(()) => info!("Successfully fetched player"),
Err(x) => {
println!("Error occured while trying to fetch the player: {:?}", x);
error!("Error occured while trying to fetch the player: {:?}", x);
}
}
loop {
Expand All @@ -43,6 +45,8 @@ macro_rules! loop_main {
}
#[tokio::main]
async fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

let args: Vec<String> = args().collect();
let socket_url: &str = match args.get(1) {
Some(stringref) => stringref,
Expand All @@ -60,14 +64,14 @@ async fn main() {
let tcp_socket = match TcpListener::bind(socket_tcp_url).await {
Ok(x) => x,
Err(x) => {
println!("Error occurred while trying to bind: {}", x);
error!("Error occurred while trying to bind: {}", x);
return;
}
};
loop_main!(tcp_socket, state);
} else if socket_url == "--test" {
// TODO: test the API aswell, this only tests the player script extractor
println!("Fetching player");
info!("Fetching player");
match fetch_update(state.clone()).await {
Ok(()) => std::process::exit(0),
Err(_x) => std::process::exit(-1),
Expand All @@ -80,7 +84,7 @@ async fn main() {
remove_file(socket_url).await;
UnixListener::bind(socket_url).unwrap()
} else {
println!("Error occurred while trying to bind: {}", x);
error!("Error occurred while trying to bind: {}", x);
return;
}
}
Expand All @@ -102,7 +106,7 @@ where
while let Some(opcode_res) = stream.next().await {
match opcode_res {
Ok(opcode) => {
//println!("Received job: {}", opcode.opcode);
debug!("Received job: {}", opcode.opcode);

match opcode.opcode {
JobOpcode::ForceUpdate => {
Expand Down Expand Up @@ -177,7 +181,7 @@ where
}
}
Err(x) => {
println!("I/O error: {:?}", x);
error!("I/O error: {:?}", x);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/opcode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::ErrorKind;

use log::debug;
use tokio_util::{
bytes::{Buf, BufMut},
codec::{Decoder, Encoder},
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Decoder for OpcodeDecoder {
&mut self,
src: &mut tokio_util::bytes::BytesMut,
) -> Result<Option<Self::Item>, Self::Error> {
//println!("Decoder length: {}", src.len());
debug!("Decoder length: {}", src.len());
if 5 > src.len() {
return Ok(None);
}
Expand Down
Loading

0 comments on commit e06937d

Please sign in to comment.