Skip to content

Commit

Permalink
replace println with tracing info
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra authored and aochagavia committed Jul 3, 2023
1 parent 9a7ccce commit 6ce282b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
1 change: 1 addition & 0 deletions crates/libsolv_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme.workspace = true
itertools = "0.11.0"
petgraph = "0.6.3"
rattler_conda_types = { version = "0.5.0", path = "../rattler_conda_types" }
tracing = "0.1.37"

[dev-dependencies]
insta = "1.29.0"
22 changes: 14 additions & 8 deletions crates/libsolv_rs/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Problem {

let candidates = &solver.pool().match_spec_to_candidates[match_spec_id];
if candidates.is_empty() {
println!(
tracing::info!(
"{package_id:?} requires {match_spec_id:?}, which has no candidates"
);
graph.add_edge(
Expand All @@ -64,7 +64,7 @@ impl Problem {
);
} else {
for &candidate_id in candidates {
println!("{package_id:?} requires {candidate_id:?}");
tracing::info!("{package_id:?} requires {candidate_id:?}");

let candidate_node =
Self::add_node(&mut graph, &mut nodes, candidate_id);
Expand Down Expand Up @@ -237,8 +237,13 @@ pub struct ProblemGraph {
}

impl ProblemGraph {
/// Returns a graph in its graphviz representation
pub fn graphviz(&self, pool: &Pool, simplify: bool) {
/// Writes a graphviz graph that represents this instance to the specified output.
pub fn graphviz(
&self,
f: &mut impl std::io::Write,
pool: &Pool,
simplify: bool,
) -> Result<(), std::io::Error> {
let graph = &self.graph;

let merged_nodes = if simplify {
Expand All @@ -247,7 +252,7 @@ impl ProblemGraph {
HashMap::new()
};

println!("digraph {{");
write!(f, "digraph {{")?;
for nx in graph.node_indices() {
let id = match graph.node_weight(nx).as_ref().unwrap() {
ProblemNode::Solvable(id) => *id,
Expand Down Expand Up @@ -303,14 +308,15 @@ impl ProblemGraph {
ProblemNode::UnresolvedDependency => "unresolved".to_string(),
};

println!(
write!(
f,
"\"{}\" -> \"{}\"[color={color}, label=\"{label}\"];",
solvable.display(),
target
);
)?;
}
}
println!("}}");
write!(f, "}}")
}

fn simplify(&self, pool: &Pool) -> HashMap<SolvableId, Rc<MergedProblemNode>> {
Expand Down
80 changes: 45 additions & 35 deletions crates/libsolv_rs/src/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::solvable::SolvableInner;
use crate::solve_jobs::SolveJobs;
use crate::transaction::Transaction;

use itertools::Itertools;
use rattler_conda_types::MatchSpec;
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -281,7 +282,7 @@ impl<'a> Solver<'a> {
_locked_solvables: &[SolvableId],
_top_level_requirements: &[MatchSpec],
) -> Result<(), ClauseId> {
println!("=== Deciding assertions for requires without candidates");
tracing::info!("=== Deciding assertions for requires without candidates");

for (i, clause) in self.clauses.iter().enumerate() {
if let Clause::Requires(solvable_id, _) = clause.kind {
Expand All @@ -295,7 +296,7 @@ impl<'a> Solver<'a> {
.map_err(|_| clause_id)?;

if decided {
println!(
tracing::info!(
"Set {} = false",
self.pool.resolve_solvable_inner(solvable_id).display()
);
Expand Down Expand Up @@ -390,7 +391,7 @@ impl<'a> Solver<'a> {
) -> Result<u32, Problem> {
level += 1;

println!(
tracing::info!(
"=== Install {} at level {level} (required by {})",
self.pool.resolve_solvable_inner(solvable).display(),
self.pool.resolve_solvable_inner(required_by).display(),
Expand All @@ -404,38 +405,40 @@ impl<'a> Solver<'a> {
let r = self.propagate(level);
let Err((conflicting_solvable, attempted_value, conflicting_clause)) = r else {
// Propagation succeeded
println!("=== Propagation succeeded");
tracing::info!("=== Propagation succeeded");
break;
};

{
let solvable = self
.pool
.resolve_solvable_inner(conflicting_solvable)
.display();
println!(
"=== Propagation conflicted: could not set {solvable} to {attempted_value}"
tracing::info!(
"=== Propagation conflicted: could not set {solvable} to {attempted_value}",
solvable = self
.pool
.resolve_solvable_inner(conflicting_solvable)
.display()
);
println!(
tracing::info!(
"During unit propagation for clause: {:?}",
self.clauses[conflicting_clause.index()].debug(&self.pool)
);

let decision = self
.decision_tracker
.stack()
.iter()
.find(|d| d.solvable_id == conflicting_solvable)
.unwrap();
println!(
tracing::info!(
"Previously decided value: {}. Derived from: {:?}",
!attempted_value,
self.clauses[decision.derived_from.index()].debug(&self.pool),
self.clauses[self
.decision_tracker
.stack()
.iter()
.find(|d| d.solvable_id == conflicting_solvable)
.unwrap()
.derived_from
.index()]
.debug(&self.pool),
);
}

if level == 1 {
println!("=== UNSOLVABLE");
tracing::info!("=== UNSOLVABLE");
for decision in self.decision_tracker.stack() {
let clause = &self.clauses[decision.derived_from.index()];
let level = self.decision_tracker.level(decision.solvable_id);
Expand All @@ -446,7 +449,7 @@ impl<'a> Solver<'a> {
continue;
}

println!(
tracing::info!(
"* ({level}) {action} {}. Reason: {:?}",
self.pool
.resolve_solvable_inner(decision.solvable_id)
Expand All @@ -462,7 +465,7 @@ impl<'a> Solver<'a> {
self.analyze(level, conflicting_solvable, conflicting_clause);
level = new_level;

println!("=== Backtracked to level {level}");
tracing::info!("=== Backtracked to level {level}");

// Optimization: propagate right now, since we know that the clause is a unit clause
let decision = literal.satisfying_value();
Expand All @@ -472,7 +475,7 @@ impl<'a> Solver<'a> {
level,
)
.expect("bug: solvable was already decided!");
println!(
tracing::info!(
"=== Propagate after learn: {} = {decision}",
self.pool
.resolve_solvable_inner(literal.solvable_id)
Expand Down Expand Up @@ -519,8 +522,13 @@ impl<'a> Solver<'a> {
.map_err(|_| (literal.solvable_id, decision, clause_id))?;

if decided {
let s = self.pool.resolve_solvable_inner(literal.solvable_id);
println!("Propagate assertion {} = {}", s.display(), decision);
tracing::info!(
"Propagate assertion {} = {}",
self.pool
.resolve_solvable_inner(literal.solvable_id)
.display(),
decision
);
}
}

Expand Down Expand Up @@ -613,7 +621,7 @@ impl<'a> Solver<'a> {
// Skip logging for ForbidMultipleInstances, which is so noisy
Clause::ForbidMultipleInstances(..) => {}
_ => {
println!(
tracing::info!(
"Propagate {} = {}. {:?}",
self.pool
.resolve_solvable_inner(remaining_watch.solvable_id)
Expand Down Expand Up @@ -676,7 +684,7 @@ impl<'a> Solver<'a> {

let mut problem = Problem::default();

println!("=== ANALYZE UNSOLVABLE");
tracing::info!("=== ANALYZE UNSOLVABLE");

let mut involved = HashSet::new();
self.clauses[clause_id.index()].kind.visit_literals(
Expand Down Expand Up @@ -846,14 +854,16 @@ impl<'a> Solver<'a> {
// Store it
self.clauses.push(clause);

println!("Learnt disjunction:");
for lit in learnt {
let yes_no = if lit.negate { "NOT " } else { "" };
println!(
"- {yes_no}{}",
self.pool.resolve_solvable_inner(lit.solvable_id).display()
);
}
tracing::info!(
"Learnt disjunction:\n{}",
learnt
.into_iter()
.format_with("\n", |lit, f| f(&format_args!(
"- {}{}",
if lit.negate { "NOT " } else { "" },
self.pool.resolve_solvable_inner(lit.solvable_id).display()
)))
);

// Should revert at most to the root level
let target_level = back_track_to.max(1);
Expand Down

0 comments on commit 6ce282b

Please sign in to comment.