Skip to content

Commit

Permalink
Use fmt::Show for outputting debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed May 6, 2014
1 parent 1879bf9 commit 61b7a38
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 105 deletions.
31 changes: 18 additions & 13 deletions src/components/gfx/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use libc::uintptr_t;
use servo_net::image::base::Image;
use servo_util::geometry::Au;
use servo_util::range::Range;
use std::fmt;
use std::mem;
use std::slice::Items;
use style::computed_values::border_style;
Expand Down Expand Up @@ -638,23 +639,27 @@ impl DisplayItem {
for _ in range(0, level) {
indent.push_str("| ")
}
debug!("{}+ {}", indent, self.debug_str());
debug!("{}+ {}", indent, self);
for child in self.children() {
child.debug_with_level(level + 1);
}
}
}

pub fn debug_str(&self) -> ~str {
let class = match *self {
SolidColorDisplayItemClass(_) => "SolidColor",
TextDisplayItemClass(_) => "Text",
ImageDisplayItemClass(_) => "Image",
BorderDisplayItemClass(_) => "Border",
LineDisplayItemClass(_) => "Line",
ClipDisplayItemClass(_) => "Clip",
PseudoDisplayItemClass(_) => "Pseudo",
};
format!("{} @ {} ({:x})", class, self.base().bounds, self.base().node.id())
impl fmt::Show for DisplayItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{} @ {} ({:x})",
match *self {
SolidColorDisplayItemClass(_) => "SolidColor",
TextDisplayItemClass(_) => "Text",
ImageDisplayItemClass(_) => "Image",
BorderDisplayItemClass(_) => "Border",
LineDisplayItemClass(_) => "Line",
ClipDisplayItemClass(_) => "Clip",
PseudoDisplayItemClass(_) => "Pseudo",
},
self.base().bounds,
self.base().node.id(),
)
}
}

24 changes: 13 additions & 11 deletions src/components/main/layout/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use gfx::render_task::RenderLayer;
use servo_msg::compositor_msg::{FixedPosition, LayerId, Scrollable};
use servo_util::geometry::Au;
use servo_util::geometry;
use std::fmt;
use std::mem;
use std::num::Zero;
use style::computed_values::{LPA_Auto, LPA_Length, LPA_Percentage, LPN_Length, LPN_None};
Expand Down Expand Up @@ -1690,22 +1691,23 @@ impl Flow for BlockFlow {
LayerId(self.box_.node.id(), fragment_index)
}

fn debug_str(&self) -> ~str {
let txt = if self.is_float() {
"FloatFlow: ".to_owned()
} else if self.is_root() {
"RootFlow: ".to_owned()
} else {
"BlockFlow: ".to_owned()
};
txt.append(self.box_.debug_str())
}

fn is_absolute_containing_block(&self) -> bool {
self.is_positioned()
}
}

impl fmt::Show for BlockFlow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_float() {
write!(f.buf, "FloatFlow: {}", self.box_)
} else if self.is_root() {
write!(f.buf, "RootFlow: {}", self.box_)
} else {
write!(f.buf, "BlockFlow: {}", self.box_)
}
}
}

/// The inputs for the widths-and-margins constraint equation.
pub struct WidthConstraintInput {
pub computed_width: MaybeAuto,
Expand Down
63 changes: 34 additions & 29 deletions src/components/main/layout/box_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use servo_util::namespace;
use servo_util::smallvec::SmallVec;
use servo_util::str::is_whitespace;
use std::cast;
use std::fmt;
use std::from_str::FromStr;
use std::iter::AdditiveIterator;
use std::mem;
Expand Down Expand Up @@ -838,10 +839,10 @@ impl Box {
// Box position wrt to the owning flow.
let box_bounds = self.border_box;
let absolute_box_bounds = box_bounds.translate(&flow_origin);
debug!("Box::build_display_list at rel={}, abs={}: {:s}",
debug!("Box::build_display_list at rel={}, abs={}: {}",
box_bounds,
absolute_box_bounds,
self.debug_str());
self);
debug!("Box::build_display_list: dirty={}, flow_origin={}",
layout_context.dirty,
flow_origin);
Expand Down Expand Up @@ -1396,40 +1397,21 @@ impl Box {
self.style().Box.get().overflow == overflow::hidden
}

/// Returns a debugging string describing this box.
pub fn debug_str(&self) -> ~str {
let class_name = match self.specific {
GenericBox => "GenericBox",
IframeBox(_) => "IframeBox",
ImageBox(_) => "ImageBox",
ScannedTextBox(_) => "ScannedTextBox",
TableBox => "TableBox",
TableCellBox => "TableCellBox",
TableColumnBox(_) => "TableColumnBox",
TableRowBox => "TableRowBox",
TableWrapperBox => "TableWrapperBox",
UnscannedTextBox(_) => "UnscannedTextBox",
};

format!("({}{}{})",
class_name,
self.side_offsets_debug_string("bp", self.border_padding),
self.side_offsets_debug_string("m", self.margin))
}

/// A helper function to return a debug string describing the side offsets for one of the rect
/// box model properties (border, padding, or margin).
fn side_offsets_debug_string(&self, name: &str, value: SideOffsets2D<Au>) -> ~str {
let zero: SideOffsets2D<Au> = Zero::zero();
if value == zero {
return "".to_str()
}
format!(" {}{},{},{},{}",
fn side_offsets_debug_fmt(&self, name: &str,
value: SideOffsets2D<Au>,
f: &mut fmt::Formatter) -> fmt::Result {
if value.is_zero() {
Ok(())
} else {
write!(f.buf, "{}{},{},{},{}",
name,
value.top,
value.right,
value.bottom,
value.left)
}
}

/// Sends the size and position of this iframe box to the constellation. This is out of line to
Expand All @@ -1456,6 +1438,29 @@ impl Box {
}
}

impl fmt::Show for Box {
/// Outputs a debugging string describing this box.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, "({} ",
match self.specific {
GenericBox => "GenericBox",
IframeBox(_) => "IframeBox",
ImageBox(_) => "ImageBox",
ScannedTextBox(_) => "ScannedTextBox",
TableBox => "TableBox",
TableCellBox => "TableCellBox",
TableColumnBox(_) => "TableColumnBox",
TableRowBox => "TableRowBox",
TableWrapperBox => "TableWrapperBox",
UnscannedTextBox(_) => "UnscannedTextBox",
}));
try!(self.side_offsets_debug_fmt("bp", self.border_padding, f));
try!(write!(f.buf, " "));
try!(self.side_offsets_debug_fmt("m", self.margin, f));
write!(f.buf, ")")
}
}

/// An object that accumulates display lists of child flows, applying a clipping rect if necessary.
pub struct ChildDisplayListAccumulator {
clip_display_item: Option<~ClipDisplayItem>,
Expand Down
10 changes: 3 additions & 7 deletions src/components/main/layout/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use gfx::render_task::RenderLayer;
use servo_msg::compositor_msg::LayerId;
use servo_util::geometry::Au;
use std::cast;
use std::fmt;
use std::iter::Zip;
use std::num::Zero;
use std::sync::atomics::Relaxed;
Expand All @@ -65,7 +66,7 @@ use style::computed_values::{clear, position, text_align};
///
/// Note that virtual methods have a cost; we should not overuse them in Servo. Consider adding
/// methods to `ImmutableFlowUtils` or `MutableFlowUtils` before adding more methods here.
pub trait Flow {
pub trait Flow: fmt::Show + ToStr {
// RTTI
//
// TODO(pcwalton): Use Rust's RTTI, once that works.
Expand Down Expand Up @@ -267,11 +268,6 @@ pub trait Flow {
LayerId(pointer, fragment_id)
}
}

/// Returns a debugging string describing this flow.
fn debug_str(&self) -> ~str {
"???".to_owned()
}
}

// Base access
Expand Down Expand Up @@ -915,7 +911,7 @@ impl<'a> ImmutableFlowUtils for &'a Flow {
for _ in range(0, level) {
indent.push_str("| ")
}
debug!("{}+ {}", indent, self.debug_str());
debug!("{}+ {}", indent, self.to_str());
for kid in imm_child_iter(self) {
kid.dump_with_level(level + 1)
}
Expand Down
30 changes: 17 additions & 13 deletions src/components/main/layout/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use servo_util::geometry::Au;
use servo_util::geometry;
use servo_util::range::Range;
use std::iter::Enumerate;
use std::fmt;
use std::mem;
use std::slice::{Items, MutItems};
use std::u16;
Expand Down Expand Up @@ -235,8 +236,8 @@ impl LineboxScanner {
// try_append_to_line.
match first_box.split_to_width(line_bounds.size.width, line_is_empty) {
CannotSplit => {
error!("LineboxScanner: Tried to split unsplittable render box! {:s}",
first_box.debug_str());
error!("LineboxScanner: Tried to split unsplittable render box! {}",
first_box);
return (line_bounds, first_box_size.width);
}
SplitDidFit(left, right) => {
Expand Down Expand Up @@ -356,11 +357,11 @@ impl LineboxScanner {
}

debug!("LineboxScanner: Trying to append box to line {:u} (box size: {}, green zone: \
{}): {:s}",
{}): {}",
self.lines.len(),
in_box.border_box.size,
self.pending_line.green_zone,
in_box.debug_str());
in_box);

let green_zone = self.pending_line.green_zone;

Expand Down Expand Up @@ -400,8 +401,8 @@ impl LineboxScanner {
let split = in_box.split_to_width(available_width, line_is_empty);
let (left, right) = match (split, line_is_empty) {
(CannotSplit, _) => {
debug!("LineboxScanner: Tried to split unsplittable render box! {:s}",
in_box.debug_str());
debug!("LineboxScanner: Tried to split unsplittable render box! {}",
in_box);
self.work_list.push_front(in_box);
return false
}
Expand Down Expand Up @@ -756,7 +757,7 @@ impl Flow for InlineFlow {

let mut intrinsic_widths = IntrinsicWidths::new();
for (fragment, context) in self.boxes.mut_iter() {
debug!("Flow: measuring {:s}", fragment.debug_str());
debug!("Flow: measuring {}", *fragment);

let box_intrinsic_widths = fragment.intrinsic_widths(Some(context));
intrinsic_widths.minimum_width = geometry::max(intrinsic_widths.minimum_width,
Expand Down Expand Up @@ -954,16 +955,19 @@ impl Flow for InlineFlow {
self.base.floats = scanner.floats();
self.base.floats.translate(Point2D(Au::new(0), -self.base.position.size.height));
}
}

fn debug_str(&self) -> ~str {
let mut string = "InlineFlow: ".to_str();
impl fmt::Show for InlineFlow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, "InlineFlow"));
for (i, (fragment, _)) in self.boxes.iter().enumerate() {
if i != 0 {
string.push_str(", ")
if i == 0 {
try!(write!(f.buf, ": {}", fragment))
} else {
try!(write!(f.buf, ", {}", fragment))
}
string.push_str(fragment.debug_str())
}
string
Ok(())
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/components/main/layout/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use layout::wrapper::ThreadSafeLayoutNode;

use servo_util::geometry::Au;
use servo_util::geometry;
use std::fmt;
use style::computed_values::table_layout;

/// A table flow corresponded to the table's internal table box under a table wrapper flow.
Expand Down Expand Up @@ -287,10 +288,12 @@ impl Flow for TableFlow {
fn compute_absolute_position(&mut self) {
self.block_flow.compute_absolute_position()
}
}

fn debug_str(&self) -> ~str {
let txt = "TableFlow: ".to_owned();
txt.append(self.block_flow.box_.debug_str())
impl fmt::Show for TableFlow {
/// Outputs a debugging string describing this table flow.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "TableFlow: {}", self.block_flow)
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/components/main/layout/table_caption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use layout::context::LayoutContext;
use layout::flow::{TableCaptionFlowClass, FlowClass, Flow};
use layout::wrapper::ThreadSafeLayoutNode;

use std::fmt;

/// A table formatting context.
pub struct TableCaptionFlow {
pub block_flow: BlockFlow,
Expand Down Expand Up @@ -60,9 +62,10 @@ impl Flow for TableCaptionFlow {
fn compute_absolute_position(&mut self) {
self.block_flow.compute_absolute_position()
}
}

fn debug_str(&self) -> ~str {
let txt = "TableCaptionFlow: ".to_owned();
txt.append(self.block_flow.box_.debug_str())
impl fmt::Show for TableCaptionFlow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "TableCaptionFlow: {}", self.block_flow)
}
}
9 changes: 5 additions & 4 deletions src/components/main/layout/table_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use layout::table::InternalTable;
use layout::wrapper::ThreadSafeLayoutNode;

use servo_util::geometry::Au;
use std::fmt;

/// A table formatting context.
pub struct TableCellFlow {
Expand Down Expand Up @@ -109,10 +110,10 @@ impl Flow for TableCellFlow {
fn compute_absolute_position(&mut self) {
self.block_flow.compute_absolute_position()
}
}

fn debug_str(&self) -> ~str {
let txt = "TableCellFlow: ".to_owned();
txt.append(self.block_flow.box_.debug_str())
impl fmt::Show for TableCellFlow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "TableCellFlow: {}", self.block_flow)
}
}

9 comments on commit 61b7a38

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at brendanzab@61b7a38

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/servo/debug_str = 61b7a38 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/servo/debug_str = 61b7a38 merged ok, testing candidate = e492b6c

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at brendanzab@61b7a38

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/servo/debug_str = 61b7a38 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/servo/debug_str = 61b7a38 merged ok, testing candidate = 38bf7fd

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 38bf7fd

Please sign in to comment.