473,776 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

defining a custom output facility

I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I'd like it to write to stdout, to a file, and/or call a logging
function.

So my output function might look something like this:

OutputFacility& OutputFacility: :put(const std::string& s) {
cout << s; // print to stdout
write_to_logfil e(s); // also calling logging function
return *this;
}

Now I can define the << operator to be an alias for put().

My question is: do I have to define put() for every type of input I
expect to print? I.e., I'd like this to work with all primitive types,
int, char, double, float, etc.

Right now I'm thinking that the OutputFacility class could have a
std::ostringstr eam as a private member; every version of
OutputFacility: :put() would just in turn call the ostringstream put()
method, THEN call the actual output functions (e.g. write_to_logfil e()).

It seems like there should be a simpler method of defining my put
method, i.e. instead of overloading it and defining it multiple times,
if I could just define it once with a "magical" parameter that means
"take anything that can be converted to a string".

Thanks for any advice!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email
Jul 22 '05 #1
12 3215
On Tue, 13 Jan 2004 20:11:51 GMT, Matt Garman <fa**@not-real.bogus> wrote:
OutputFacility& OutputFacility: :put(const std::string& s) {
cout << s; // print to stdout
write_to_logfil e(s); // also calling logging function
return *this;
}

My question is: do I have to define put() for every type of input I
expect to print? I.e., I'd like this to work with all primitive types,
int, char, double, float, etc.


It looks like I can get away with the following:

template <class T> OutputFacility& OutputFacility: :put(const T& x) {
std::ostringstr ing os;
os << x;
cout << os.str();
write_to_logfil e(os.str());
return *this;
}

This seems to get me "close" to what I want to do. It works for
strings, c-style strings (char arrays), and integers. But it doesn't
recognize std::endl and it truncates the fraction part of a float.

Any thoughts?

Thanks again!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email
Jul 22 '05 #2
Matt Garman wrote:
I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I'd like it to write to stdout, to a file, and/or call a logging
function.

So my output function might look something like this:

OutputFacility& OutputFacility: :put(const std::string& s) {
cout << s; // print to stdout
write_to_logfil e(s); // also calling logging function
return *this;
}

Now I can define the << operator to be an alias for put().

My question is: do I have to define put() for every type of input I
expect to print? I.e., I'd like this to work with all primitive types,
int, char, double, float, etc.

Right now I'm thinking that the OutputFacility class could have a
std::ostringstr eam as a private member; every version of
OutputFacility: :put() would just in turn call the ostringstream put()
method, THEN call the actual output functions (e.g. write_to_logfil e()).

It seems like there should be a simpler method of defining my put
method, i.e. instead of overloading it and defining it multiple times,
if I could just define it once with a "magical" parameter that means
"take anything that can be converted to a string".

Thanks for any advice!
Matt

template< typename T >
OutputFacility&
operator << ( OutputFacility& out, T const& out )
{
return out.put( lexical_cast< std::string >( s ) );
}

http://www.boost.org/libs/conversion...m#lexical_cast

Jul 22 '05 #3
Matt Garman wrote:
This seems to get me "close" to what I want to do. It works for
strings, c-style strings (char arrays), and integers. But it doesn't
recognize std::endl and it truncates the fraction part of a float.


std::endl is specific to ostreams. It is not declared as a newline, but
rather a function that writes a newline, then flushes the ostream.
Since your OutputFacility class isn't an ostream, it won't work.

I don't see the float truncation, it works for me. Post a compilable
example.

--
Andrew
Jul 22 '05 #4
On Tue, 13 Jan 2004 20:11:51 GMT in comp.lang.c++, Matt Garman
<fa**@not-real.bogus> was alleged to have written:
I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I'd like it to write to stdout, to a file, and/or call a logging
function.


C++ streams are constructed as two layers. The upper layer, derived
from iostream, is concerned with formatting objects to and from a
character representation. The lower level, derived from streambuf, is
concerned with transferring those characters to and from some
receptacle.

It is usually a mistake to try to create a customized stream class by
deriving from the stream classes. What you want instead is to derive
from streambuf, and construct a stream using your custom streambuf.

See the examples on Dietmar Kuehl's web site
http://www.informatik.uni-konstanz.d.../c++/iostream/


Jul 22 '05 #5

"David Harmon" <so****@netcom. com> wrote in message
news:40******** ******@news.wes t.earthlink.net ...
On Tue, 13 Jan 2004 20:11:51 GMT in comp.lang.c++, Matt Garman
It is usually a mistake to try to create a customized stream class by deriving from the stream classes. What you want instead is to derive from streambuf, and construct a stream using your custom streambuf.

See the examples on Dietmar Kuehl's web site
http://www.informatik.uni-konstanz.d.../c++/iostream/


I've written a library to make defining new streambufs easy. See
http://groups.yahoo.com/group/boost/...treams_lib.zip (you have
to sign up with boost to access it, I think.)

Jonathan
Jul 22 '05 #6
On Tue, 13 Jan 2004 20:44:47 GMT, Andrew Taylor <at*****@its.to > wrote:
std::endl is specific to ostreams. It is not declared as a newline,
but rather a function that writes a newline, then flushes the ostream.
Since your OutputFacility class isn't an ostream, it won't work.
That makes sense, I forgot that endl is actually a function.

So in the example I posted here, I could create my own endl function to
achieve similar functionality, right?
I don't see the float truncation, it works for me. Post a compilable
example.


I was mistaken; it appears to be a formatting issue. In other words, I
tried it with more floats, and found that it's only printing a certain
number of digits (regardless of the decimal location).

Thank you!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email
Jul 22 '05 #7
Matt Garman wrote:

On Tue, 13 Jan 2004 20:44:47 GMT, Andrew Taylor <at*****@its.to > wrote:
std::endl is specific to ostreams. It is not declared as a newline,
but rather a function that writes a newline, then flushes the ostream.
Since your OutputFacility class isn't an ostream, it won't work.


That makes sense, I forgot that endl is actually a function.

So in the example I posted here, I could create my own endl function to
achieve similar functionality, right?


You could.
But you are still doing the whole thing the wrong way.
The right way is to write a new stream buffer class
and make a standard stream use it.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #8
On Thu, 15 Jan 2004 18:56:43 +0100, Karl Heinz Buchegger
<kb******@gasca d.at> wrote:
So in the example I posted here, I could create my own endl function
to achieve similar functionality, right?


But you are still doing the whole thing the wrong way. The right way
is to write a new stream buffer class and make a standard stream use
it.


I guess I don't really understand the underlying principle(s) behind
this method. I looked over the example on Dietmar Kuehl's website (as
suggested in another post), but didn't really "get it". I think I'm
missing some more fundamental concept(s) here :)

Or perhaps I'm looking at it too much from a "vanilla" C perspective. I
mean, if I was doing this in C, I'd probably just have a function (or
possibly even a #define macro) that had a printf() like syntax, but does
the job of printf() plus writes to a logfile, to a window, etc.

I certainly want to do it the right way, though! Does anyone happen to
have any more links that explain this in greater detail?

Thanks again!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email
Jul 22 '05 #9
Matt Garman wrote:

On Thu, 15 Jan 2004 18:56:43 +0100, Karl Heinz Buchegger
<kb******@gasca d.at> wrote:
So in the example I posted here, I could create my own endl function
to achieve similar functionality, right?
But you are still doing the whole thing the wrong way. The right way
is to write a new stream buffer class and make a standard stream use
it.


I guess I don't really understand the underlying principle(s) behind
this method. I looked over the example on Dietmar Kuehl's website (as
suggested in another post), but didn't really "get it". I think I'm
missing some more fundamental concept(s) here :)


The thing is:

The stream object itself is responsible for proper formatting.
The stream buffer object inside the stream is reponsible for bringing
the already formatted data on its way, it's the transportation layer.

You want to change a string in the behaviour of the transportation
layer, thus your approach should be to write a customized stream buffer
and make the stream use that instead of the one it was born with.
Or perhaps I'm looking at it too much from a "vanilla" C perspective. I
mean, if I was doing this in C, I'd probably just have a function (or
possibly even a #define macro) that had a printf() like syntax, but does
the job of printf() plus writes to a logfile, to a window, etc.
If the system is designed flexible (with C++ it is, with C it is not), then
there would be some sort of hook in printf, which allows you to get a grasp
at the string printf produces internally before printf sends this string to the
device. In this way you don't need to change any printf calls but just
hook into it, and do whatever you want with the printf generated string.

With printf this is not possible, with C++ streams it is. In fact it
is specifically designed to be that way.

I certainly want to do it the right way, though! Does anyone happen to
have any more links that explain this in greater detail?


http://www.google.com
"streambuf custom"

http://cpptips.hyperformix.com/cpptips/tee_strm
http://www.phenix.bnl.gov/~phoncs/on...ation/Message/
http://www.codeproject.com/vcpp/stl/zipstream.asp

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #10

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

Similar topics

0
1407
by: Brian van den Broek | last post by:
Hi all, IDLE refuses to launch, and I believe it is because I attempted to define a custom key-binding that it doesn't like. I was recently defining a custom keybinding in IDLE 1.1 under Python 2.4 on WinMe. (I was using the simpler of the two binding definition interfaces.) When done, I hit the buttons for 'OK' (or however it is labelled -- I cannot currently check), but the definition dialog remained open. After several tries, I hit...
3
2401
by: Paul Bowman | last post by:
Hi All Java has a facility where you can send output to a DOS window using System.out.println. This is useful while developing as you can see what it going in your code. Does .Net provide a similar facility? I notice that when a Windows app runs there is an output window that contains messages from the compiler etc. Is it possible to 'plug' into this?
0
988
by: Samir Patel | last post by:
Hi Everybody hai i am fine what about all of u? i have a problem regarding to ASP.NET custom control , can anybody help me? my problem is when i placed my custom control on page at design time i am not able to select the child control of my custom control,how can i do this second thing how to allow custom template control's template editing facility in design mode
19
2989
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and provide an open TD with a DIV in it for the content of this formlet. (The DIV is for DHTML to hide and show the content) I've created a web page showing step by step the two problems I'm encountering. This problem is much easier to see than it...
5
1787
by: Sadeq | last post by:
Is it possible to define custom attributes for arrays? And if so, how can I retrieve them? I mean I want to define sth like: int MyArray; and then retrieve the value of the custom attribute on demand. I used the GetCustomAttributes method on MyArray, but the results "length" was
17
7648
by: rdemyan | last post by:
My app creates a building report. My users have requested that I provide functionality to e-mail these "building reports" to building managers once a month. So assuming that I have the following table that lists a building manager's name, e-mail address and their building, is there a way that I can automate this to send these monthly e-mails from Outlook. Ideally this would be a click one button type of action:
1
4814
by: rn5a | last post by:
I have created a custom control button which when clicked displays a message in the JavaScript alert dialog. I could successfully compile the VB class file into a DLL & also could add it to the Toolbox in Visual Web Developer 2005 Express Edition but the alert message isn't popping up when I am implementing this control in an ASP.NET page & clicking the Button in the ASPX page. This is the class file: Imports System Imports System.Web...
1
3231
by: rn5a | last post by:
I want to create a custom control that encapsulates a Button & a TextBox. When the Button is clicked, the user is asked a question using JavaScript confirm (which shows 2 buttons - 'OK' & 'Cancel'). Till this point, no problem. Initially, the TextBox is empty. The Button has a property named 'ConfirmMessage' so that the developer using this custom control can modify the question in the confirm dialog. If the user clicks 'OK', I want the...
0
1393
by: rn5a | last post by:
A custom control is derived from the WebControl class & encapsulates a TextBox & a Button. When the Button is clicked, the user is shown the JavaScript confirm dialog with the 'OK' & 'Cancel' buttons. If the user clicks 'OK', the TextBox gets populated with 'true' & if 'Cancel' is clicked, the TextBox gets populated with 'false'. Note that when the user first comes to the ASPX page that uses this custom control, the TextBox is empty. This...
0
9628
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
9464
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
10122
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...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7471
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
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4031
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
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.