473,748 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from std::ostream to override formatting.

AJG
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(st d::ostream *s);

This works great when used with something like
setLogStream(&s td::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_th read::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::ostream 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::ostream {
// ...
virtual void someOverridenFu nction(const char *const buffer) {
std::cerr << boost::this_thr ead::get_id() << "| " << buffer;
}
// ...
};

and then:

ThreadLogger logger;
setLogStream(&l ogger);

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

Thanks!
Jun 29 '08 #1
1 3443
On Jun 29, 4:37 am, AJG <plus....@gmail .comwrote:
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(st d::ostream *s);
This works great when used with something like
setLogStream(&s td::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_th read::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::ostream 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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12054
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do this. I have understood that there is a relation between streambuf and iostream, where one should be able to extend streambuf and override the underflow/overflow functions (and do send()/recv() for example).
8
10359
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in "prefix hallo" I need this to overwrite the "<<" operator for MyClass in a recursive way, e.g.
2
7757
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low level function which accepts a "generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have tried to make the parameter a const ostream&, but when I plug in cerr or cout I get a compiler error. What am I doing wrong? // The low...
3
2811
by: enzo | last post by:
hi all! i need in my libraries a class to deal with messages. I did: class messenger { public: messenger(const char* p, ostream& p_s) : m(p), m_stream(p_s)
103
48716
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
0
1515
by: Steven T. Hatton | last post by:
I tried to create my own manipulator that would both set the width of the subsequent output field, and cast an unsigned char to unsigned in. I came up with the following rather ugly hack. Notice that I am instantiating UI before any call to out.operator<<(). Is there a better way to accomplish this? What I'd like is something I could simply place in the output call between '<<' operators without having to instantiate it first. The...
10
9666
by: Johannes Barop | last post by:
Hi, I want to format the output of a 'std::ostream', but i dont know how to do it . Example: int main() { my_out << "Hi.\nI'am a" << " Computer."; my_out << "Nice.";
1
2526
by: Johannes Barop | last post by:
Hello, i try to implement a streambuffer. I overwrote streambuf::overflow() and streambuf::xsputn(). Both are protected and virtual (http://www.cplusplus.com/ref/iostream/streambuf/). But somehow my OutBuf::xsputn() is not beeing used. But the orginal basic_streambuf::xsputn() calls my OutBuf::overflow(). http://pastebin.com/483016 - OutBuf.h
24
2954
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.