Skip to content

Commit

Permalink
Prep work for logging
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <[email protected]>
  • Loading branch information
SimonBrandner committed Sep 5, 2024
1 parent c566e57 commit e1bf9fa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
27 changes: 26 additions & 1 deletion 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 crates/oblichey-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ serde_with = "3.9.0"
toml = "0.8.19"
mockall = "0.13.0"
mockall_double = "0.3.1"
log = "0.4.22"
flexi_logger = "0.29.0"

[features]
rgb-webcam = []
Expand Down
27 changes: 26 additions & 1 deletion crates/oblichey-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod store;
use camera::Frame;
use clap::Parser;
use config::Config;
use flexi_logger::{FileSpec, Logger, WriteMode};
use processors::auth_processor::AuthProcessor;
use processors::face::{FaceEmbedding, FaceForGUI};
use processors::face_processor::FaceProcessor;
Expand All @@ -18,7 +19,10 @@ use std::process::ExitCode;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self};
use store::{load_face_embeddings, remove_face_embedding, save_face_embedding};
use store::{get_log_directory, load_face_embeddings, remove_face_embedding, save_face_embedding};

const LOG_LEVEL: &str = "trace";
const LOG_FILE_BASE_NAME: &str = "oblichey";

#[derive(PartialEq, Eq, Debug, Clone, clap::Subcommand)]
enum Command {
Expand Down Expand Up @@ -49,6 +53,27 @@ struct Args {
}

fn main() -> ExitCode {
let log_directory = get_log_directory().expect("Failed to get log directory");
let log_spec = match Logger::try_with_str(LOG_LEVEL) {
Ok(s) => s,
Err(e) => {
println!("Failed to create log spec: {e}");
return ExitCode::FAILURE;
}
};
if let Err(e) = log_spec
.log_to_file(
FileSpec::default()
.basename(LOG_FILE_BASE_NAME)
.directory(log_directory),
)
.write_mode(WriteMode::BufferAndFlush)
.start()
{
println!("Failed to start logger: {e}");
return ExitCode::FAILURE;
};

let args = Args::parse();
let command = args.command;
let config = match Config::load() {
Expand Down
11 changes: 9 additions & 2 deletions crates/oblichey-cli/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
path::PathBuf,
};

const EMBEDDINGS_DIRECTORY: &str = "oblichey";
const OBLICHEY_DIRECTORY_NAME: &str = "oblichey";

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -48,7 +48,7 @@ impl From<bincode::Error> for Error {

fn get_embeddings_directory() -> Result<PathBuf, Error> {
let state_dir = env::var("XDG_STATE_HOME")?;
let embeddings_dir_path = PathBuf::from(state_dir).join(EMBEDDINGS_DIRECTORY);
let embeddings_dir_path = PathBuf::from(state_dir).join(OBLICHEY_DIRECTORY_NAME);

if let Err(e) = create_dir(embeddings_dir_path.clone()) {
if e.kind() != io::ErrorKind::AlreadyExists {
Expand All @@ -63,6 +63,13 @@ fn get_face_embedding_file_path(name: &str) -> Result<PathBuf, Error> {
Ok(get_embeddings_directory()?.join(name))
}

pub fn get_log_directory() -> Result<PathBuf, Error> {
let cache_dir = env::var("XDG_CACHE_HOME")?;
let log_dir_path = PathBuf::from(cache_dir).join(OBLICHEY_DIRECTORY_NAME);

Ok(log_dir_path)
}

pub fn save_face_embedding(name: &str, face_embedding: &FaceEmbedding) -> Result<(), Error> {
let path = get_face_embedding_file_path(name)?;
let serialized: Vec<u8> = serialize(&face_embedding)?;
Expand Down

0 comments on commit e1bf9fa

Please sign in to comment.