Skip to content

Commit

Permalink
Added base logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-zip committed Oct 18, 2024
1 parent 1d595dd commit 774ddac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
7 changes: 0 additions & 7 deletions witch_craft/src/core/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ use crate::core::structs::DataSet;
use chrono;
use colored::*;
use regex::Regex;
use std::collections::HashMap;
use std::env;
use std::fmt::write;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::net::IpAddr;
use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Output};
use std::str::FromStr;

use super::types::Closure;
use super::witchrc::readrc_exists;

pub fn readargs() -> Vec<String> {
env::args().collect()
Expand Down
62 changes: 56 additions & 6 deletions witch_craft/src/core/logger.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
pub fn logger(cmd_output: String, cmd_true: String, cmd_witchy: String) -> bool {
use std::fs::OpenOptions;
use crate::datetime_now;
use std::io::Write;
use super::witchrc::readrc_value;
use serde::{Serialize, Deserialize};

let now = datetime_now();
#[derive(Serialize, Deserialize)]
struct WitchyLogger {
cmd_output: String,
cmd_status: String,
cmd_error: String,
cmd_true: String,
cmd_witchy: String,
datetime_now: String,
}

impl WitchyLogger {
fn new(
cmd_output: String,
cmd_status: String,
cmd_error: String,
cmd_true: String,
cmd_witchy: String,
) -> Self {
WitchyLogger {
cmd_output,
cmd_status,
cmd_error,
cmd_true,
cmd_witchy,
datetime_now: datetime_now(),
}
}

fn empty() -> Self {
WitchyLogger {
cmd_output: String::new(),
cmd_status: String::new(),
cmd_error: String::new(),
cmd_true: String::new(),
cmd_witchy: String::new(),
datetime_now: datetime_now(),
}
}

fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}

fn save(&self) -> String {

let output = serde_json::to_string(self).unwrap();
let witchrc = readrc_value("path_log_file");

if readrc_exists("enable_logger") {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open("my-file")
.open(&witchrc)
.unwrap();

if let Err(e) = writeln!(file, "{}", cmd_output) {
if let Err(e) = writeln!(file, "{}", output) {
eprintln!("Couldn't write to file: {}", e);
}

output
}

false
}

0 comments on commit 774ddac

Please sign in to comment.