I've been using the nifty: DEBUG(errs() << "Here is the state of things:\n"); style of optional logging, but ran into an issue where I want to dump a table of information. The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length. I've worked up a patch that adds two new methods to raw_ostream: /// write_hex - Output \arg N as ten char hexadecimal string, including /// 0x prefix (e.g. 0x12345678 or 0x00000001). raw_ostream &write_hex32(uint32_t N); /// write_hex - Output \arg N as 18 char hexadecimal string, including /// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001). raw_ostream &write_hex64(uint64_t N); Is there already some way to do this level of formatting with raw_ostream? -------------- next part -------------- A non-text attachment was scrubbed... Name: raw_ostream.patch Type: application/octet-stream Size: 2143 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120518/f81a985c/attachment.obj> -------------- next part -------------- -Nick
Jakob Stoklund Olesen
2012-May-18 20:44 UTC
[LLVMdev] Fixed with hex numbers with raw_ostream
On May 18, 2012, at 11:50 AM, Nick Kledzik <kledzik at apple.com> wrote:> I've been using the nifty: > DEBUG(errs() << "Here is the state of things:\n"); > style of optional logging, but ran into an issue where I want to dump a table of information. The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length. > > I've worked up a patch that adds two new methods to raw_ostream: > > /// write_hex - Output \arg N as ten char hexadecimal string, including > /// 0x prefix (e.g. 0x12345678 or 0x00000001). > raw_ostream &write_hex32(uint32_t N); > > /// write_hex - Output \arg N as 18 char hexadecimal string, including > /// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001). > raw_ostream &write_hex64(uint64_t N); > > > Is there already some way to do this level of formatting with raw_ostream?You can get the full awesomeness of printf with include/llvm/Support/Format.h: OS << format("%016" PRIx64, N); I don't know if there is a significant performance difference. /jakob
On May 18, 2012, at 1:44 PM, Jakob Stoklund Olesen wrote:> On May 18, 2012, at 11:50 AM, Nick Kledzik <kledzik at apple.com> wrote: > >> I've been using the nifty: >> DEBUG(errs() << "Here is the state of things:\n"); >> style of optional logging, but ran into an issue where I want to dump a table of information. The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length. >> >> I've worked up a patch that adds two new methods to raw_ostream: >> >> /// write_hex - Output \arg N as ten char hexadecimal string, including >> /// 0x prefix (e.g. 0x12345678 or 0x00000001). >> raw_ostream &write_hex32(uint32_t N); >> >> /// write_hex - Output \arg N as 18 char hexadecimal string, including >> /// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001). >> raw_ostream &write_hex64(uint64_t N); >> >> >> Is there already some way to do this level of formatting with raw_ostream? > > You can get the full awesomeness of printf with include/llvm/Support/Format.h: > > OS << format("%016" PRIx64, N);Excellent!> I don't know if there is a significant performance difference.These are in DEBUG() statements, so I don't care about performance. I've switched over to using format(). Thanks! -Nick