Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Custom polymorphic logging #2366

catamorphism opened this Issue May 7, 2012 · 8 comments


None yet
7 participants

catamorphism commented May 7, 2012

The example I have in mind is:

#debug("%s", ty_to_str(tcx, t));

where t: ty::t. It would be great if I could write:

#debug("%?", tcx, t);

and not have to write out the call to ty_to_str. perhaps there could be an RTS hook to say "for type ty::t, call ty_to_str? I guess this could potentially be very hard since at runtime, tys probably aren't distinguished from ints. Alternately, (though not quite as awesomely), there could be another format string specifier or something for types. Basically, I just want to be able to pretty-print a type in a #fmt call without writing out ty_to_str, because that's a lot of characters :-) (And slightly more seriously, then I have to import ty_to_str, which requires remembering what module it lives in...)


nikomatsakis commented May 9, 2012

I think we should not have smarter polymorphic logging but rather a to_str interface that various types can implement. Then you should be able to do #debug["%s", val] for any value that has a to_str() method defined.

Types could make use of this if we embedded a back pointer to the tck in the type box.


Dretch commented May 9, 2012

FWIW there is already a to_str iface in core::to_str.

On a slightly related note I consider that it might be nice if the + operator on strings called .to_str() on its arguments, like in Java/Javascript, because it makes code like "the value of x is " + uint::str(x) a bit more concise.


catamorphism commented Nov 8, 2012

Not quite done yet: making %s work for anything that implements to_str would be a great enhancement.


metajack commented May 16, 2013

This may get fixed by the new logging system in mozilla#3309 which is already nominated for a maturity milestone.

I do think whatever we end up with should support to_str(), so nominating this for production ready.


nikomatsakis commented May 29, 2013

Just a note: in the compiler itself, for now, I suggest writing debug!("%s", foo.repr(tcx)). I added the repr trait and implemented it for many common types. If you find it doesn't work, add an impl for your own favorite type.


graydon commented Jun 6, 2013

accepted for backwards-compatible milestone

@brson brson referenced this issue Jul 11, 2013


Redesign the logging subsystem #3309

10 of 13 tasks complete

alexcrichton commented Aug 27, 2013

@catamorphism, would you consider this closed with the new formatting traits of format!? Now, any type which implements the corresponding trait can be formatted with that character.


thestinger commented Sep 1, 2013

I think we consider this fixed now, although we still need to migrate away from the old formatting.

@thestinger thestinger closed this Sep 1, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment