On Tue, 24 Mar 2009 15:44:02 +0100, Balog Pal <> wrote:
> "Jorgen Grahn" <grahn+>
>>I should know this, but ...
>>
>> Is this code safe?
[It was. Thanks; that was what I suspected.]
>> - have a 'template<T> std:string to_string(const T&)' based on it
>>
>> - log using the slightly ugly syntax
>> log("error in %s: %s",
>> to_string(foo).c_str(),
>> to_string(bar).c_str());
>>
>> It's ugly and extremely wasteful, but I want to define the output
>> format in *one* place for each class -- not in every place where I might
>> want to print it.
>
> You could use some stream-like object especially if you want only strings in
> the format, not all the other format elements. Then have stuff like
> log("error in %s: %s") <<foo << bar << flush;
> or having global object
> log << format("error in %s: %s") << foo << bar; // without flush last line
> waits the next format()
Yes, I have been thinking about something like that, but most code
using the logging calls is C-like, so the benefit across the whole
code base would be fairly small => not a high priority.
> moving the to_string() code to operator <<;
Actually there was no to_string() code to speak of -- like I mentioned
above it's a template which uses operator<< to fill a string stream,
and then return the resulting std::string. I have worked with code which
used T::toString() methods exclusively, and I never want to deal with
that again. operator<< is bad enough
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!