Browse Source

Use log crate

David Peter 1 year ago
parent
commit
92c0a257d1

+ 33 - 2
Cargo.lock

@@ -498,6 +498,19 @@ version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d"
 
+[[package]]
+name = "env_logger"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
+dependencies = [
+ "humantime",
+ "is-terminal",
+ "log",
+ "regex",
+ "termcolor",
+]
+
 [[package]]
 name = "equivalent"
 version = "1.0.1"
@@ -638,6 +651,12 @@ dependencies = [
  "itoa",
 ]
 
+[[package]]
+name = "humantime"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
+
 [[package]]
 name = "iana-time-zone"
 version = "0.1.60"
@@ -775,9 +794,9 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
 
 [[package]]
 name = "log"
-version = "0.4.20"
+version = "0.4.21"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
+checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
 
 [[package]]
 name = "memchr"
@@ -889,6 +908,7 @@ dependencies = [
  "insta",
  "itertools 0.12.0",
  "libc",
+ "log",
  "mendeleev",
  "num-format",
  "num-integer",
@@ -921,6 +941,7 @@ dependencies = [
  "itertools 0.12.0",
  "numbat",
  "predicates",
+ "pretty_env_logger",
  "rustyline",
  "serde",
  "terminal_size",
@@ -1079,6 +1100,16 @@ dependencies = [
  "ryu_floating_decimal",
 ]
 
+[[package]]
+name = "pretty_env_logger"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
+dependencies = [
+ "env_logger",
+ "log",
+]
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.78"

+ 1 - 0
numbat-cli/Cargo.toml

@@ -23,6 +23,7 @@ toml = { version = "0.8.8", features = ["parse"] }
 serde = { version = "1.0.195", features = ["derive"] }
 terminal_size = "0.3.0"
 chrono-tz = "0.8.5"
+pretty_env_logger = "0.5.0"
 
 [dependencies.clap]
 version = "4"

+ 2 - 0
numbat-cli/src/main.rs

@@ -603,6 +603,8 @@ fn generate_config() -> Result<()> {
 }
 
 fn main() {
+    pretty_env_logger::init();
+
     let args = Args::parse();
 
     if args.generate_config {

+ 1 - 0
numbat/Cargo.toml

@@ -38,6 +38,7 @@ rand = "0.8.5"
 strfmt = "0.2.4"
 indexmap = "2.2.6"
 mendeleev = "0.8.0"
+log = "0.4.21"
 
 [features]
 default = ["fetch-exchangerates"]

+ 13 - 11
numbat/src/typechecker/constraints.rs

@@ -4,6 +4,8 @@ use super::substitutions::{ApplySubstitution, Substitution, SubstitutionError};
 use crate::type_variable::TypeVariable;
 use crate::typed_ast::{DType, DTypeFactor, Type};
 
+use log::debug;
+
 #[derive(Debug, Clone, Error, PartialEq, Eq)]
 pub enum ConstraintSolverError {
     #[error("Could not solve the following constraints:\n{0}")]
@@ -97,7 +99,7 @@ impl ConstraintSet {
 
                         substitution.extend(new_substitution);
 
-                        println!(
+                        debug!(
                             "    New constraints:\n{}",
                             new_constraint_set.pretty_print(6)
                         );
@@ -229,11 +231,11 @@ impl Constraint {
     fn try_satisfy(&self) -> Option<Satisfied> {
         match self {
             Constraint::Equal(t1, t2) if t1 == t2 => {
-                println!("  (1) SOLVING: {} ~ {} trivially ", t1, t2);
+                debug!("  (1) SOLVING: {} ~ {} trivially ", t1, t2);
                 Some(Satisfied::trivially())
             }
             Constraint::Equal(Type::TVar(x), t) if !t.contains(x) => {
-                println!(
+                debug!(
                     "  (2) SOLVING: {x} ~ {t} with substitution {x} := {t}",
                     x = x.name(),
                     t = t
@@ -244,7 +246,7 @@ impl Constraint {
                 )))
             }
             Constraint::Equal(s, Type::TVar(x)) if !s.contains(x) => {
-                println!(
+                debug!(
                     "  (3) SOLVING: {s} ~ {x} with substitution {x} := {s}",
                     s = s,
                     x = x.name()
@@ -255,7 +257,7 @@ impl Constraint {
                 )))
             }
             // Constraint::Equal(t @ Type::TArr(s1, s2), s @ Type::TArr(t1, t2)) => {
-            //     println!(
+            //     debug!(
             //         "  (4) SOLVING: {t} ~ {s} with new constraints {s1} ~ {t1} and {s2} ~ {t2}",
             //         t = t,
             //         s = s,
@@ -270,7 +272,7 @@ impl Constraint {
             //     ]))
             // }
             Constraint::Equal(s @ Type::List(s1), t @ Type::List(t1)) => {
-                println!(
+                debug!(
                     "  (5) SOLVING: {s} ~ {t} with new constraint {s1} ~ {t1}",
                     s = s,
                     t = t,
@@ -284,7 +286,7 @@ impl Constraint {
             }
             Constraint::Equal(Type::TVar(tv), Type::Dimension(d))
             | Constraint::Equal(Type::Dimension(d), Type::TVar(tv)) => {
-                println!(
+                debug!(
                     "  (6) SOLVING: {tv} ~ {d} by lifting the type variable to a DType",
                     tv = tv.name(),
                     d = d
@@ -297,7 +299,7 @@ impl Constraint {
             }
             Constraint::Equal(Type::Dimension(d1), Type::Dimension(d2)) => {
                 let d_result = d1.divide(d2);
-                println!(
+                debug!(
                     "  (7) SOLVING: {} ~ {} with new constraint d_result = Scalar",
                     d1.pretty_print(),
                     d2.pretty_print()
@@ -313,7 +315,7 @@ impl Constraint {
                     .iter()
                     .map(|tv| Constraint::IsDType(Type::TVar(tv.clone())))
                     .collect();
-                println!(
+                debug!(
                     "  (8) SOLVING: {} : DType through new constraints: {:?}",
                     inner.pretty_print(),
                     new_constraints
@@ -322,7 +324,7 @@ impl Constraint {
             }
             Constraint::IsDType(_) => None,
             Constraint::EqualScalar(d) if d == &DType::scalar() => {
-                println!("  (9) SOLVING: Scalar = Scalar trivially");
+                debug!("  (9) SOLVING: Scalar = Scalar trivially");
                 Some(Satisfied::trivially())
             }
             Constraint::EqualScalar(dtype) => match dtype.split_first_factor() {
@@ -333,7 +335,7 @@ impl Constraint {
                             .map(|(f, j)| (f.clone(), -j / k))
                             .collect::<Vec<_>>(),
                     );
-                    println!(
+                    debug!(
                         "  (10) SOLVING: {dtype} = Scalar with substitution {tv} := {result}",
                         dtype = dtype.pretty_print(),
                         tv = tv.name(),

+ 10 - 9
numbat/src/typechecker/mod.rs

@@ -26,6 +26,7 @@ use const_evaluation::evaluate_const_expr;
 use constraints::{Constraint, ConstraintSet, TrivialResultion};
 use environment::{Environment, FunctionMetadata, FunctionSignature};
 use itertools::Itertools;
+use log::info;
 use name_generator::NameGenerator;
 use num_traits::Zero;
 
@@ -1415,14 +1416,14 @@ impl TypeChecker {
         // in (after constraint solving).
         let mut elaborated_statement = self.elaborate_statement(&statement)?;
 
-        println!("=========================================");
-        println!("Elaborated statements:");
-        println!("{}", elaborated_statement.pretty_print());
-        println!();
+        info!("=========================================");
+        info!("Elaborated statements:");
+        info!("{}", elaborated_statement.pretty_print());
+        info!("");
 
-        println!("Constraints:");
-        println!("{}", self.constraints.pretty_print(2));
-        println!();
+        info!("Constraints:");
+        info!("{}", self.constraints.pretty_print(2));
+        info!("");
 
         // Solve constraints
         let (substitution, dtype_variables) = self
@@ -1456,8 +1457,8 @@ impl TypeChecker {
         //     }
         // }
 
-        println!("Final statement:");
-        println!("{}", elaborated_statement.pretty_print());
+        info!("Final statement:");
+        info!("{}", elaborated_statement.pretty_print());
 
         Ok(elaborated_statement)
     }