R
Riku Jarvinen
Hi everyone,
I asked another question regarding this same subject about a week ago. See
thread:
http://groups.google.fi/group/comp....579d6832732/530bae667a479add#530bae667a479add
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 accept the iostream manipulators, including special
manipulators in <iomanip>. How can I make the logging class handle all the
manipulators in the same way std::cout does?
If I compile the code (see below) which tries to use the manipulators with
the Logger class, I get the following error messages:
example2.cpp: In function `int main()':
example2.cpp:51: error: no match for 'operator<<' in 'mainlog <<
std::setw(int)()'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
example2.cpp:52: error: no match for 'operator<<' in '
(+(&mainlog)->Logger:perator<<(" x = "))->Logger:perator<<(x) <<
std::endl'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
example2.cpp:53: error: `precision' undeclared (first use this function)
example2.cpp:53: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
example2.cpp:54: error: no match for 'operator<<' in '
(&mainlog)->Logger:perator<<("x = ") << std::showpos'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
Simplified example code:
------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Logger
{
public:
Logger(const char* filename = "program.log")
{
logfile = new fstream(filename, fstream:ut);
}
~Logger()
{
logfile->close();
delete logfile;
}
Logger& operator<<(const char* msg)
{
*logfile << msg;
return *this;
}
Logger& operator<<(const double val)
{
*logfile << val;
return *this;
}
private:
fstream* logfile;
};
int main()
{
Logger mainlog;
double x = 12.34567;
// These work fine
cout << setw(20) << setfill('*') << setprecision(4);
cout << " x = " << x << endl;
cout.precision(2);
cout << "x = " << showpos << scientific << x << endl;
mainlog << "x = " << x << "\n";
// These don't work
//mainlog << setw(20) << setfill('*') << setprecision(4);
//mainlog << " x = " << x << endl;
//mainlog.precision(2);
//mainlog << "x = " << showpos << scientific << x << endl;
return 0;
}
I asked another question regarding this same subject about a week ago. See
thread:
http://groups.google.fi/group/comp....579d6832732/530bae667a479add#530bae667a479add
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 accept the iostream manipulators, including special
manipulators in <iomanip>. How can I make the logging class handle all the
manipulators in the same way std::cout does?
If I compile the code (see below) which tries to use the manipulators with
the Logger class, I get the following error messages:
g++ example2.cpp
example2.cpp: In function `int main()':
example2.cpp:51: error: no match for 'operator<<' in 'mainlog <<
std::setw(int)()'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
example2.cpp:52: error: no match for 'operator<<' in '
(+(&mainlog)->Logger:perator<<(" x = "))->Logger:perator<<(x) <<
std::endl'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
example2.cpp:53: error: `precision' undeclared (first use this function)
example2.cpp:53: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
example2.cpp:54: error: no match for 'operator<<' in '
(&mainlog)->Logger:perator<<("x = ") << std::showpos'
example2.cpp:22: error: candidates are: Logger& Logger:perator<<(const
char*)
example2.cpp:28: error: Logger& Logger:perator<<(double)
Simplified example code:
------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Logger
{
public:
Logger(const char* filename = "program.log")
{
logfile = new fstream(filename, fstream:ut);
}
~Logger()
{
logfile->close();
delete logfile;
}
Logger& operator<<(const char* msg)
{
*logfile << msg;
return *this;
}
Logger& operator<<(const double val)
{
*logfile << val;
return *this;
}
private:
fstream* logfile;
};
int main()
{
Logger mainlog;
double x = 12.34567;
// These work fine
cout << setw(20) << setfill('*') << setprecision(4);
cout << " x = " << x << endl;
cout.precision(2);
cout << "x = " << showpos << scientific << x << endl;
mainlog << "x = " << x << "\n";
// These don't work
//mainlog << setw(20) << setfill('*') << setprecision(4);
//mainlog << " x = " << x << endl;
//mainlog.precision(2);
//mainlog << "x = " << showpos << scientific << x << endl;
return 0;
}