Skip to content

Commit

Permalink
Create the Logger type to log data to the terminal
Browse files Browse the repository at this point in the history
cargo has some fancy ways to print out things to the terminal and show
status. Eventually we'll get there as we add more things like
dependencies etc. However, we have to start somewhere with outputting
things in a way that's not just println!. This commit starts that
process by creating a Logger type that will print out what it's
compiling and when it's done!
  • Loading branch information
mgattozzi committed Jul 26, 2023
1 parent 5966b05 commit 2c1a862
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/lib.rs
@@ -1,6 +1,8 @@
mod config;
mod logger;

use config::Manifest;
use logger::Logger;
use std::env;
use std::error::Error;
use std::fmt;
Expand All @@ -14,6 +16,7 @@ pub type Result<T> = std::result::Result<T, BoxError>;
pub type BoxError = Box<dyn Error>;

pub fn build() -> Result<()> {
let mut logger = Logger::new();
let root_dir = root_dir()?;
let manifest = Manifest::parse_from_file(root_dir.join("Freight.toml"))?;

Expand All @@ -23,8 +26,8 @@ pub fn build() -> Result<()> {
let target_debug = target.join("debug");
fs::create_dir_all(&target_debug)?;

let lib_compile = || -> Result<()> {
println!("Compiling lib.rs");
let lib_compile = |logger: &mut Logger| -> Result<()> {
logger.compiling_crate(&manifest.crate_name);
Rustc::builder()
.edition(manifest.edition)
.crate_type(CrateType::Lib)
Expand All @@ -33,12 +36,12 @@ pub fn build() -> Result<()> {
.lib_dir(target_debug.clone())
.done()
.run(lib_rs.to_str().unwrap())?;
println!("Compiling lib.rs -- Done");
logger.done_compiling();
Ok(())
};

let bin_compile = |externs: Vec<&str>| -> Result<()> {
println!("Compiling main.rs");
let bin_compile = |logger: &mut Logger, externs: Vec<&str>| -> Result<()> {
logger.compiling_bin(&manifest.crate_name);
let mut builder = Rustc::builder()
.edition(manifest.edition)
.crate_type(CrateType::Bin)
Expand All @@ -51,20 +54,20 @@ pub fn build() -> Result<()> {
}

builder.done().run(main_rs.to_str().unwrap())?;
println!("Compiling main.rs -- Done");
logger.done_compiling();
Ok(())
};

match (lib_rs.exists(), main_rs.exists()) {
(true, true) => {
lib_compile()?;
bin_compile(vec![&manifest.crate_name])?;
lib_compile(&mut logger)?;
bin_compile(&mut logger, vec![&manifest.crate_name])?;
}
(true, false) => {
lib_compile()?;
lib_compile(&mut logger)?;
}
(false, true) => {
bin_compile(vec![])?;
bin_compile(&mut logger, vec![])?;
}
(false, false) => return Err("There is nothing to compile".into()),
}
Expand Down
27 changes: 27 additions & 0 deletions src/logger.rs
@@ -0,0 +1,27 @@
use std::io;
use std::io::Write;

pub struct Logger {
out: io::StdoutLock<'static>,
}

impl Logger {
pub fn new() -> Self {
Self {
out: io::stdout().lock(),
}
}
pub fn compiling_crate(&mut self, crate_name: &str) {
let _ = self
.out
.write_all(format!("Compiling crate {crate_name}...").as_bytes());
}
pub fn compiling_bin(&mut self, crate_name: &str) {
let _ = self
.out
.write_all(format!("Compiling bin {crate_name}...").as_bytes());
}
pub fn done_compiling(&mut self) {
let _ = self.out.write_all(b"Done\n");
}
}

0 comments on commit 2c1a862

Please sign in to comment.