PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Inheriting from std::ostream to override formatting.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Inheriting from std::ostream to override formatting.

Réponse
 
LinkBack Outils de la discussion
Vieux 29/06/2008, 03h37   #1
AJG
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Inheriting from std::ostream to override formatting.

Hi there. I am using a library called SOCI that has a method to set a
stream which it uses to log SQL queries. The signature is as follows:

void setLogStream(std:stream *s);

This works great when used with something like
setLogStream(&std::cerr). However, I would like to add more context to
the query logged. Specifically, I'd like to add the thread ID. So,
instead of the query logged looking like:

select * from foo;

it would look like:

<thread-id>| select * from foo;

I already have the function to get the thread id
(boost::this_thread::get_id()), but I need to find a way to pass an
object to setLogStream that will do the formatting for me. As far as I
can tell, the only way to do this is inheriting from std:stream and
somehow overriding the default formatting. Except I'm not sure which
of its methods are virtual and meant to be overridden. Any clues?

Basically, I figure what I need might look something like:

struct ThreadLogger : public std:stream {
// ...
virtual void someOverridenFunction(const char *const buffer) {
std::cerr << boost::this_thread::get_id() << "| " << buffer;
}
// ...
};

and then:

ThreadLogger logger;
setLogStream(&logger);

Is what I'm looking to do possible? Should I look into IO manipulators
instead? Any greatly appreciated .

Thanks!


  Réponse avec citation
Vieux 29/06/2008, 10h57   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Inheriting from std::ostream to override formatting.

On Jun 29, 4:37 am, AJG <plus....@gmail.com> wrote:
> Hi there. I am using a library called SOCI that has a method
> to set a stream which it uses to log SQL queries. The
> signature is as follows:


> void setLogStream(std:stream *s);


> This works great when used with something like
> setLogStream(&std::cerr). However, I would like to add more
> context to the query logged. Specifically, I'd like to add the
> thread ID. So, instead of the query logged looking like:


> select * from foo;


> it would look like:


> <thread-id>| select * from foo;


> I already have the function to get the thread id
> (boost::this_thread::get_id()), but I need to find a way to
> pass an object to setLogStream that will do the formatting for
> me.


You also need a way of ensuring correct locking on the output
stream object.

> As far as I can tell, the only way to do this is inheriting
> from std:stream and somehow overriding the default
> formatting. Except I'm not sure which of its methods are
> virtual and meant to be overridden.


None of the functions (except the destructor) are virtual and
are meant to be overridden. That's not how ostream works.

> Any clues?


You could use a filtering streambuf to insert the thread id at
the start of each line, but that still leaves the issue of
locking. The usual solution is to combine a filtering streambuf
(to "decorate" the output, i.e. adding timestamp, thread id,
line and filename...) and an output stream wrapper (to manage
the lock, and more generally the notion of a "record", and to
ensure that each record ends with a new line, is immediately
flushed, and is atomic, e.g. when the output stream sends emails
or writes to syslog).

For more information on filtering streambufs, see
http://kanze.james.neuf.fr/articles-en.html; an implementation
is also available there, or you can use boost::iostream (if e.g.
you're already using Boost). I'm not aware of any good article
on output stream wrappers, but the concept is simple enough that
it might not be necessary. Again, an implementation is
available at my site, but if you don't feel like installing the
entire library (and I've made no effort to date to make it
fractionable), it's small enough and simple enough that you
should be able to just copy the relevant code and manage it as
your own.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09520 seconds with 10 queries