Re: fstreams and rolling files
On Nov 14, 12:32 pm, "b...@blah.com" <GrahamJWa...@gmail.com> wrote:
I have a requirement to "roll" a logfile in a GUI. Namely as each
write to the file occurs, I need to do some filtering (not relevant
here) and then stream the message contents to a graphical user
interface.
Ideally, I would like a callback object/function be to be called each
time a write occurs to the file associated with the stream. Ive
checked about and I cant really see anything out of the box.
I see that there is a "ios_base::register_callback" method available
however i don't think thats what I'm looking for. It provides the
following events, copyfmt_event, erase_event and imbue_event. I don't
think any of these trigger the registered function to be called when a
"write" to the stream occurs.
Perhaps I need to look at a different structure/object. In any
case, I'd be grateful if anybody could direct me to the
correct approach for getting notified each time a write to the
file occurs.
The usual solution here is to use some sort of ostream wrapper.
Basically, a class which contains some sort of ostream, and has
template operator<< members to forward all operations to the
ostream, but which can be used as a temporary, and whose
destructor will do whatever actions you want on "write".
If you're targetting a GUI, the object would probably contain an
std::ostringstream to collect the information, with the
destructor then doing whatever is necessary to cause it to
display on the screen. More generally, however, I'd use a
standard ostream, with a custom streambuf, whose sync() function
did the necessary work; the destructor of your wrapper object
would then call sync.
Most of the time, you'll want to return the wrapper object from
a function, so the user can write something like:
log( severity ) << "a = " << a ;
In that case, the wrapper needs to be copiable, and you'll need
some sort of counting so that only the last destructor does
anything.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34