mirror of
https://github.com/obhq/obliteration.git
synced 2024-11-27 05:00:24 +00:00
Added File Logging (#258)
This commit is contained in:
parent
47b19c42a9
commit
f347c82e00
@ -19,6 +19,7 @@ llvm-sys = { version = "160.0.2", features = ["strict-versioning"] }
|
||||
param = { path = "../param" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.9"
|
||||
strip-ansi-escapes = "0.1.1"
|
||||
termcolor = "1.2.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
use std::cell::RefCell;
|
||||
use std::fs::{create_dir_all, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use strip_ansi_escapes::strip;
|
||||
use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
|
||||
|
||||
/// Encapsulate the stdout.
|
||||
@ -8,15 +12,36 @@ use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}
|
||||
/// the error before the info.
|
||||
pub struct Logger {
|
||||
writer: BufferWriter,
|
||||
file: Option<RefCell<std::fs::File>>,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
writer: BufferWriter::stdout(ColorChoice::Auto),
|
||||
file: None,
|
||||
}
|
||||
}
|
||||
|
||||
// File logging
|
||||
pub fn set_log_file<P: Into<PathBuf>>(&mut self, path: P) -> std::io::Result<()> {
|
||||
let path = path.into();
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
create_dir_all(parent)?; // Create parent directories if needed
|
||||
}
|
||||
|
||||
let file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&path)?;
|
||||
|
||||
self.file = Some(RefCell::new(file));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn info(&self) -> Buffer {
|
||||
let mut b = self.writer.buffer();
|
||||
let mut c = ColorSpec::new();
|
||||
@ -58,6 +83,17 @@ impl Logger {
|
||||
|
||||
pub fn write(&self, b: Buffer) {
|
||||
self.writer.print(&b).unwrap();
|
||||
|
||||
// Only run when File is set
|
||||
if let Some(file) = &self.file {
|
||||
let ansi_with = String::from_utf8_lossy(b.as_slice());
|
||||
let ansi_without = strip(ansi_with.as_bytes()).unwrap();
|
||||
// Mutable reference to file
|
||||
let mut file = file.borrow_mut();
|
||||
// File writer
|
||||
file.write_all(&ansi_without).unwrap();
|
||||
file.flush().unwrap(); // write immediately\
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ mod sysctl;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
// Initialize logger.
|
||||
let logger = Logger::new();
|
||||
let mut logger = Logger::new();
|
||||
|
||||
// Load arguments.
|
||||
let args = if std::env::args().any(|a| a == "--debug") {
|
||||
@ -60,6 +60,15 @@ fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
// Begin File logging
|
||||
let debug_dump_dir = PathBuf::from(&args.debug_dump);
|
||||
let log_file_path = debug_dump_dir.join("obliteration-kernel.log");
|
||||
if let Ok(_) = logger.set_log_file(&log_file_path) {
|
||||
info!(logger, "File Logging enabled");
|
||||
} else {
|
||||
warn!(logger, "Failed to set log file at: {:?}", log_file_path);
|
||||
}
|
||||
|
||||
// Show basic infomation.
|
||||
info!(logger, "Starting Obliteration kernel.");
|
||||
info!(logger, "System directory : {}", args.system.display());
|
||||
|
@ -27,7 +27,7 @@ impl<'a> Sysctl<'a> {
|
||||
&self,
|
||||
name: &[i32],
|
||||
old: Option<&mut [u8]>,
|
||||
new: Option<&[u8]>,
|
||||
_new: Option<&[u8]>,
|
||||
) -> Result<usize, InvokeError> {
|
||||
// Check arguments.
|
||||
if name.len() < 2 || name.len() > 24 {
|
||||
|
@ -11,7 +11,7 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Setup application.
|
||||
QCoreApplication::setOrganizationName("Obliteration");
|
||||
QCoreApplication::setOrganizationName("OBHQ");
|
||||
QCoreApplication::setApplicationName("Obliteration");
|
||||
|
||||
// Dark Mode for Windows
|
||||
|
BIN
src/resources.rc
BIN
src/resources.rc
Binary file not shown.
Loading…
Reference in New Issue
Block a user