473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Logging class and overloaded stream operators

Hello everyone,

I have a logging class which writes program outputs to the logfile. The
class works fine as long as only C++ native data types are considered. The
problem is that I have a program with a bunch of classes which have the
output stream operators << of their own. The overloaded operators work fine
when inserted into the std:cout stream but I don't know how to make the
logging class accept them.

If I try to compile the program (see the code below) with the command

mainlog << a;

included I get the following error message:
g++ logtest.cpp


logtest.cpp: In function `int main()':
logtest.cpp:64: error: no match for 'operator<<' in 'mainlog << a'
logtest.cpp:20: error: candidates are: Logger& Logger::operator<<(const
char*)
logtest.cpp:25: error: Logger& Logger::operator<<(int)
logtest.cpp:46: error: std::ostream&
operator<<(std::ostream&,
const MyClass&)

Does this mean I have to somehow introduce the overloaded operator << of
MyClass (and every other class I use in my program) separately in the Logger
class?

Here follows the simplified example:

------------------------------------
#include <iostream>
#include <fstream>

using namespace std;

class Logger
{
public:
Logger(const char* filename = "program.log")
{
logfile = new fstream(filename, fstream::out);
}
~Logger()
{
logfile->close();
delete logfile;
}

Logger& operator<<(const char* msg)
{
*logfile << msg;
return *this;
}
Logger& operator<<(const int val)
{
*logfile << val;
return *this;
}
private:
fstream* logfile;
};

class MyClass
{
public:
MyClass(const int n)
{
data = n;
}

~MyClass()
{
}

friend ostream& operator<<(ostream& o, const MyClass& v)
{
return o << "MyClass data = " << v.data << "\n";
}

private:
int data;
};

int main()
{
Logger mainlog;
MyClass a(10);

// These work fine
cout << a;
mainlog << "Bla " << 200 << "\n";

// This doesn't work
//mainlog << a;

return 0;
}
------------------------------------

Any comments or suggestions regarding this problem are appreciated!

Riku

--
life, space, irc

Oct 16 '05 #1
7 6730
You need to define a template function in the Logger class so it can
accept all classes. Inside that function, it uses the operator<< to
output to a standard stream. This way, your classes don't need to
know specifically about a Logger class, just a standard stream.

It should be defined similiar to this:

template<class T>
friend Logger& operator<<(Logger& logger, const T& t)
{
logger.logfile_ << t;
return logger;
}

Also, check out log4cpp an open source logger based on log4j.

Oct 16 '05 #2
add
template<class T>
Logger& operator<<(const & T msg){
*logfile << msg;
return *this;
}

to your Logger class.

Or, you may inherit your Logger from std::ofstream if it does provide
some extra functionality over the Logger class you've posted (and in
this case you don't need to create any operators<<), otherwise just use
ostream

Oct 16 '05 #3
> You need to define a template function in the Logger class so it can
accept all classes. Inside that function, it uses the operator<< to
output to a standard stream. This way, your classes don't need to
know specifically about a Logger class, just a standard stream.

It should be defined similiar to this:

template<class T>
friend Logger& operator<<(Logger& logger, const T& t)
{
logger.logfile_ << t;
return logger;
}
You cannot define this function in the Logger class - it's a
freestanding function. You must declare it friend to Logger also

Also, check out log4cpp an open source logger based on log4j.


Oct 16 '05 #4
> You cannot define this function in the Logger class - it's a
freestanding function. You must declare it friend to Logger also


thanks, I was going to post a follow-up to my post but thought the
_friend_ specifier would be enough to clarify my statement.

Oct 16 '05 #5
> Logger& operator<<(const & T msg){

it should be Logger& operator<<(const T & msg){
sorry

Oct 16 '05 #6
Riku Jarvinen wrote:
Hello everyone,

I have a logging class which writes program outputs to the logfile. The
class works fine as long as only C++ native data types are considered. The
problem is that I have a program with a bunch of classes which have the
output stream operators << of their own. The overloaded operators work
fine when inserted into the std:cout stream but I don't know how to make
the logging class accept them.


Thanks guys, template class solved the presented problem. However, I might
have simplified the case a little too much and I'm not sure if I'm able to
use the solution in my program. I have to study more about templates before
I proceed in this issue.

Oct 16 '05 #7
Riku Jarvinen wrote:

Thanks guys, template class solved the presented problem. However, I might
have simplified the case a little too much and I'm not sure if I'm able to
use the solution in my program. I have to study more about templates
before I proceed in this issue.


Ok, now I think it is time to ask something more about this logging issue.

So, the basic idea is to have a compact logger class which can be used like
the std::cout stream and add extra functionality to it (line numbering, some
output counters etc.). The class should also handle the manipulators,
including special manipulators in <iomanip>. How can I make the logging
class handle all the manipulators in the same way std::cout does?

Riku

--
life, space, irc

Oct 24 '05 #8

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

Similar topics

4
by: Luis | last post by:
Below is my project. My Code is done, but when I try to compile it, my IDE crahses (again). I fixed up all the things you guys said I should, except for some which would not meet the project...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
2
by: ZHENG Zhong | last post by:
Hi, I implemented a small logging library with the API like this: logger& log = log_manager::instance().get_logger("my_logger"); log.stream(DEBUG) << "this is a debug message" << std::endl;...
2
by: Russell Warren | last post by:
I was just setting up some logging in a make script and decided to give the built-in logging module a go, but I just found out that the base StreamHandler always puts a newline at the end of each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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
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,...
0
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...

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.