Skip to content

Commit

Permalink
feat: 🎨 improve log
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 9, 2023
1 parent d75ec44 commit 2b73998
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/src/controller/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl CryptoController {
let salt = config.database.salt.as_bytes().to_vec();
Self { signer, salt }
}
#[tracing::instrument(name="crypto_hasheq_controller",level = "debug",skip_all)]
pub fn hash_eq(&self, src: &str, tar: &[u8]) -> bool {
let hashed: Vec<u8> = self.hash(src).into();
let mut result = true;
Expand All @@ -66,6 +67,7 @@ impl CryptoController {
}
result
}
#[tracing::instrument(name="crypto_hash_controller",level = "debug",skip_all)]
pub fn hash(&self, src: &str) -> HashValue {
let hashed = digest::digest(
&digest::SHA256,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/controller/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ impl DupController {
}
}
pub fn store(&self, user_id: i32, uuid: Uuid, result: i32) {
tracing::trace!(request_id=?uuid);
#[cfg(feature = "single-instance")]
self.dups.insert((user_id, uuid), result);
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn check(&self, user_id: i32, uuid: &Uuid) -> Option<i32> {
tracing::trace!(request_id=?uuid);
#[cfg(feature = "single-instance")]
if let Some(x) = self.dups.get(&(user_id, *uuid)) {
log::debug!("duplicated request_id: {}, result: {}", uuid, x);
Expand Down
10 changes: 5 additions & 5 deletions backend/src/controller/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct TokenController {
}

impl TokenController {
#[tracing::instrument(parent = span,name="token_construct",level = "info",skip_all)]
#[tracing::instrument(parent = span,name="token_construct_controller",level = "info",skip_all)]
pub fn new(span: &Span) -> Arc<Self> {
log::debug!("Setup TokenController");
#[cfg(feature = "single-instance")]
Expand All @@ -92,7 +92,7 @@ impl TokenController {
});
self_
}
#[instrument(skip_all, name="token_create",fields(user = user.id))]
#[instrument(skip_all, name="token_create_controller",level="debug",fields(user = user.id))]
pub async fn add(
&self,
user: &entity::user::Model,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl TokenController {
))
}

#[instrument(skip_all, name = "token_verify")]
#[instrument(skip_all, name = "token_verify_controller",level="debug")]
pub async fn verify(&self, token: &str) -> Result<(i32, UserPermBytes), Error> {
let now = Local::now().naive_local();
let db = DB.get().unwrap();
Expand Down Expand Up @@ -179,7 +179,7 @@ impl TokenController {

Ok((token.user_id, UserPermBytes(token.permission)))
}
#[instrument(skip_all, name="token_removal", fields(token = token))]
#[instrument(skip_all, name="token_remove_controller",level="debug", fields(token = token))]
pub async fn remove(&self, token: String) -> Result<Option<()>, Error> {
let db = DB.get().unwrap();

Expand All @@ -197,7 +197,7 @@ impl TokenController {

Ok(Some(()))
}
#[instrument(skip_all, name="token_removal", fields(uid = user_id))]
#[instrument(skip_all, name="token_removal",level="debug", fields(uid = user_id))]
pub async fn remove_by_user_id(
&self,
user_id: i32,
Expand Down
5 changes: 5 additions & 0 deletions backend/src/endpoint/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ impl TokenSet for Arc<Server> {
.await
.map_err(Into::<Error>::into)?;

tracing::trace!(token_count=tokens.len(),"retrieve_token");

Ok(Response::new(Tokens {
list: tokens.into_iter().map(Into::into).collect(),
}))
Expand All @@ -54,6 +56,8 @@ impl TokenSet for Arc<Server> {
let db = DB.get().unwrap();
let (_, req) = self.parse_request(req).await?;

tracing::debug!(username=req.username);

let model = user::Entity::find()
.filter(user::Column::Username.eq(req.username))
.one(db)
Expand All @@ -76,6 +80,7 @@ impl TokenSet for Arc<Server> {
expiry: into_prost(expiry),
}))
} else {
tracing::trace!("password_mismatch");
Err(Error::PremissionDeny("password").into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl UserSet for Arc<Server> {

let mut model: ActiveModel = Default::default();

log::info!("creating user({})", req.info.username);
tracing::debug!(username= req.info.username);

fill_active_model!(model, req.info, username);

Expand Down
5 changes: 3 additions & 2 deletions backend/src/init/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ring::digest;
use sea_orm::{
ActiveModelTrait, ActiveValue, Database, DatabaseConnection, EntityTrait, PaginatorTrait,
ActiveModelTrait, ActiveValue, Database, DatabaseConnection, EntityTrait, PaginatorTrait, DbBackend, Statement, ConnectionTrait,
};

use tokio::sync::OnceCell;
Expand All @@ -12,12 +12,13 @@ pub static DB: OnceCell<DatabaseConnection> = OnceCell::const_new();

pub async fn init(config: &config::Database) {
// sqlite://database/backend.sqlite?mode=rwc
let uri = format!("sqlite://{}", config.path.clone());
let uri = format!("sqlite://{}?mode=rwc&cache=private", config.path.clone());

let db = Database::connect(&uri)
.await
.expect("fail connecting to database");
init_user(config, &db).await;

DB.set(db).ok();
}
fn hash(config: &config::Database, src: &str) -> Vec<u8> {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Server {
}
pub async fn start(self: Arc<Self>) {
transport::Server::builder()
// .accept_http1(true)
.accept_http1(true)
.tls_config(transport::ServerTlsConfig::new().identity(self.identity.clone()))
.unwrap()
.max_frame_size(Some(MAX_FRAME_SIZE))
Expand All @@ -90,6 +90,7 @@ impl Server {
.add_service(tonic_web::enable(TestcaseSetServer::new(self.clone())))
.add_service(tonic_web::enable(SubmitSetServer::new(self.clone())))
.serve(self.config.bind_address.clone().parse().unwrap())
.instrument(tracing::info_span!("server_serve"))
.await
.unwrap();
}
Expand Down

0 comments on commit 2b73998

Please sign in to comment.