fstreams and rolling files

B

bob

Hi,

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.

thanks much and have a nice day

GrahamO
 
B

BobR

Hi,
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.
thanks much and have a nice day
GrahamO

<OT>
What OS/CPU?
What 'file system' (FS)?
What GUI?
</OT>

You'll need to ask in an NG that covers one (or more) of the above.

Check the documentation at www.gnu.org. [they have utilities for (almost)
all file systems.]
 
N

Nick Keighley

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.

if istream (or ifstream) doesn't provide it can't you wrap
a stream up in a class that does do what you want?

Assuming Filter and Gui_writer are function pointers
or functors. This also means you don't have to
splatter platform specific code all over your application.

class Log_decorator
{
public:
Log_decorator (std::eek:stream& file, Filter& filter, Gui_writer&
gui_writer):
file_(file), filter_(filter), gui_writer_(gui_writer)
{}

ostream& operator<< (const std::string& txt)
{
file_ << txt;
if (filter_(txt))
gui_writer(txt);
}

private:
std::eek:stream& file_;
Filter& filter_;
Gui_writer& gui_writer_;
}
 
J

James Kanze

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::eek:stringstream 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top