473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing classes which are "streamable"

Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn't work (of course):

log << "This will create two log messages" << " why isn't this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael

Dec 5 '05 #1
10 2228
Mikael wrote:
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);
Are you sure that your function needs to modify str? If not, why isn't it a
const reference?
So now I can write:

log << "Only strings are supported";

However this doesn't work (of course):

log << "This will create two log messages" << " why isn't this
concatenated?";
What do you mean by "doesn't work", and why "of course"?
I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.


You could derive it from std::ostream, which will automatically give you all
the stream operators defined for the standard output streams.

Dec 5 '05 #2
Geo

Mikael wrote:
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn't work (of course):
Post more code, what does

Log& operator<< (Log& log, std::string& str);

return ???
log << "This will create two log messages" << " why isn't this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael


Dec 5 '05 #3

The operator<< is defined as follows (I have added the const as you
pointed out):

Log& operator<< (Log& log, std::string const& str)
{
log.logMessage(str.c_str());
return log;
}

Sorry if my question was too unspecific, I will try to be more verbose
the next time.

The above function generates two log messages when I write the
following code:

log << "message 1" << "message 2";

because operator<<() calls log.logMessage() directly.

If I derive from std::ostream, how do I actually get the formatted
string into my log? If you could please clarify how it should work
then?

I am sorry if these are stupid questions, I am pretty new to streams (I
have only used them sofar).
With kind regards,

Mikael

Dec 5 '05 #4
Geo

Mikael wrote:
The operator<< is defined as follows (I have added the const as you
pointed out):

Log& operator<< (Log& log, std::string const& str)
{
log.logMessage(str.c_str());
return log;
}

Sorry if my question was too unspecific, I will try to be more verbose
the next time.

The above function generates two log messages when I write the
following code:

log << "message 1" << "message 2";

because operator<<() calls log.logMessage() directly.

If I derive from std::ostream, how do I actually get the formatted
string into my log? If you could please clarify how it should work
then?

I am sorry if these are stupid questions, I am pretty new to streams (I
have only used them sofar).
With kind regards,

Mikael


Sorry, I'm not clear what you're question is anymore, if

log << "message 1" << "message 2";

generates two messages, then it would seem to be working correctly,
what did you hope it would do ?

You need to give an example of the output you are getting, the output
you hoped to get and some detail of what logMessage(...) does if what
you are seeing is not what you expected.

Dec 5 '05 #5
Mikael wrote:
I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.


Google for articles "James Kanze" or I have written about "streambuf".
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 5 '05 #6
Mikael wrote:

The operator<< is defined as follows (I have added the const as you
pointed out):

Log& operator<< (Log& log, std::string const& str)
{
log.logMessage(str.c_str());
return log;
}

Sorry if my question was too unspecific, I will try to be more verbose
the next time.

The above function generates two log messages when I write the
following code:

log << "message 1" << "message 2";
Yes exactly, which is exactly the behaviour you are expected to get.
because operator<<() calls log.logMessage() directly.
Yes of course. The above statement is (nearly) equivalent to the following
two:

log << "message 1";
log << "message 2";

I can only guess, but I think you belive that the "<<" operator concanates
two char arrays? right? Well it does not. It sends a char array to a
stream, and returns the stream. So what happens when you do
log << "message 1" << "message 2";
is:
Log& tmplog = operator<<(log, "message 1");
operator<<(tmplog, "message 2");
And not:
operator<<(log,"message1message2")
as you suposedly expected.

If I derive from std::ostream, how do I actually get the formatted
string into my log? If you could please clarify how it should work
then?


If you use std::strings instead of char arrays you can use the + operator:
log << std::string("message 1") + "message 2";

Or if you insist on using "<<", you have to change your Log::logMessage()
member function to accumulate everything that it gets until you give it a
special object, like (I have not checked if this compiles, just to get the
idea):

struct new_log_entry {};

Log& operator<< (Log& log, new_log_entry const& d)
{
log.flush();
return log;
}

....
log << "message 1" << "message 2" << new_log_entry;
HTH

Fabio


Dec 5 '05 #7
Mikael <Mi*************@gmail.com> wrote:
The operator<< is defined as follows (I have added the const as you
pointed out):

Log& operator<< (Log& log, std::string const& str)
{
log.logMessage(str.c_str());
return log;
}

Sorry if my question was too unspecific, I will try to be more verbose
the next time.

The above function generates two log messages when I write the
following code:

log << "message 1" << "message 2";

because operator<<() calls log.logMessage() directly.


My guess would be to check your log.logMessage() function to see if it
is automatically adding a newline at the end of the message.

--
Marcus Kwok
Dec 5 '05 #8
Okay, I will try your proposal with the special object which flushes
the accumulated string.

I was also wondering if I could subclass or encapsulate a certain class
to get all the different <<-operators for free as well as all
modifiers, so that my log class has all formatting features from the
ostream-class.

I will try this approach:

class Log : std::strstream
{
struct endl {};

// other members

friend Log& operator <<(Log& log, endl const& e);
}

Log& operator <<(Log& log, endl const& e)
{
char *message = str();
log.logMessage (message);
delete message;

return log;
}

// test

Log log;

log << "This is a test i=" << 5 << Log::endl;
^_ std::strstream<<
^_ std::stream<<
^_ prints message
With kind regards,

Mikael

Dec 6 '05 #9
Mikael wrote:
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn't work (of course):

log << "This will create two log messages" << " why isn't this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael


What you need is a function prototype like this

std::ostream &operator << (std::ostream &str, Log &inp);

then handle via a member function of Log or directly by making it a
friend of Log.

Once you've done this Log objects can be streamed via fstream, std::cout
and stringstream.

Similarly for input streaming use the following:-

std::istream &operator >> (std::ostream &str, Log &inp);

JB
Dec 6 '05 #10
n2xssvv g02gfr12930 wrote:
Mikael wrote:
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn't work (of course):

log << "This will create two log messages" << " why isn't this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael


What you need is a function prototype like this

std::ostream &operator << (std::ostream &str, Log &inp);

then handle via a member function of Log or directly by making it a
friend of Log.

Once you've done this Log objects can be streamed via fstream, std::cout
and stringstream.

Similarly for input streaming use the following:-

std::istream &operator >> (std::ostream &str, Log &inp);

JB

Just to clarify, these will enable to do things like this this:-

Log Val;
std::cin >> Log;
std::cout << "Log value is " << Val << " OK" << std::endl;

JB
Dec 6 '05 #11

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

Similar topics

11
2202
by: lawrence | last post by:
I asked a lot of questions in May about how to organize OO code. I didn't get great answers here, but someone here suggested that I look the Eclipse library, which was a good tip. Looking at its...
12
7390
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I...
8
1485
by: Christian Tismer | last post by:
Hi Pythonistas, just as a small comment on the side-effects of the rather new concept of local functions with access to their scope: This concept can be used to avoid having extra classes...
0
5365
by: sedefo | last post by:
I ran into this Microsoft Patterns & Practices Enterprise Library while i was researching how i can write a database independent data access layer. In my company we already use Data Access...
2
1233
by: van | last post by:
Writing classes of some algorithms, some parameters needs to be passed into the classes, the actual algorithms are contained in member functions of classes. What I am thinking is having some...
4
1799
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
1
1103
by: Lane | last post by:
I am developing a systemusing asp and c# and need to pass values from forms on one page to be accessed on another page, Session variables are easy but are they robust enough to be usefull or is it...
9
2712
by: TomC | last post by:
Is there any resource where someone who is familiar with Java might find which .NET classes are similar to classes they are familiar with in Java? For example, I have a project that I'd like to...
6
4014
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
7133
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...
0
7405
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...
1
7066
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...
0
5643
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,...
1
5059
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...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
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...
0
1568
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 ...
0
435
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...

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.