A
Adrian
Hi all,
I am having some problems deriving a logging class from
std::stringstream and I dont understand why it is not work. Main goal
of this I am trying to replace 1000's of lines in some old code that
is of the style
Lock();
stream << "some message";
Unlock();
Main questions are:
1. Why does the temporary version not work with class Wibble (compiler
cannot match an operator<< func)
2. Why does the temporary version output address of char * rather then
the string
Do I need to add operator<< for all the standard types?
[Output]
dluadrianc:/home/adrianc> g++ -g -Wall -ansi -pedantic -Wextra log.cc
dluadrianc:/home/adrianc> a.out
try char* apple try int 23
BEGIN
23
I am wibbles output friend
This is a test
END
BEGIN
42
0x8049540
END
//code
//#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstdarg>
class LockedLog
{
public:
static LockedLog &Instance()
{
if(instance==0)
{
instance=new LockedLog();
}
return *instance;
}
void log(const char *fmt, ...) //__attribute__((format(printf,
2,3)))
{
va_list ap;
va_start(ap, fmt);
// pthread_mutex_lock(&m_LogMutex);
vprintf(fmt, ap);
// pthread_mutex_unlock(&m_LogMutex);
va_end(ap);
}
private:
LockedLog() { /*pthread_mutex_init(&m_LogMutex, 0);*/ }
static LockedLog *instance;
pthread_mutex_t m_LogMutex;
};
class LogStreamer : public std:stringstream
{
public:
LogStreamer() :m_logger(LockedLog::Instance()) {}
~LogStreamer()
{
m_logger.log("%s", str().c_str());
}
private:
LockedLog &m_logger;
};
LockedLog *LockedLog::instance=0;
class Wibble
{
public:
friend std:stream &operator<<(std:stream &os, const Wibble
&) { os << "I am wibbles output friend"; return os; }
};
int main(int, char *[])
{
LockedLog::Instance().log("try char* %s try int %d\n", "apple",
23);
std::cout << "BEGIN\n";
{
LogStreamer strm;
strm << 23 << std::endl;
strm << Wibble() << std::endl;
strm << "This is a test" << std::endl;
}
std::cout << "END\n";
std::cout << "BEGIN\n";
LogStreamer() << 42 << std::endl;
// doesnt compile
// LogStreamer() << Wibble() << std::endl;
LogStreamer() << "This is NOT a test" << std::endl;
std::cout << "END\n";
return 0;
}
I am having some problems deriving a logging class from
std::stringstream and I dont understand why it is not work. Main goal
of this I am trying to replace 1000's of lines in some old code that
is of the style
Lock();
stream << "some message";
Unlock();
Main questions are:
1. Why does the temporary version not work with class Wibble (compiler
cannot match an operator<< func)
2. Why does the temporary version output address of char * rather then
the string
Do I need to add operator<< for all the standard types?
[Output]
dluadrianc:/home/adrianc> g++ -g -Wall -ansi -pedantic -Wextra log.cc
dluadrianc:/home/adrianc> a.out
try char* apple try int 23
BEGIN
23
I am wibbles output friend
This is a test
END
BEGIN
42
0x8049540
END
//code
//#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstdarg>
class LockedLog
{
public:
static LockedLog &Instance()
{
if(instance==0)
{
instance=new LockedLog();
}
return *instance;
}
void log(const char *fmt, ...) //__attribute__((format(printf,
2,3)))
{
va_list ap;
va_start(ap, fmt);
// pthread_mutex_lock(&m_LogMutex);
vprintf(fmt, ap);
// pthread_mutex_unlock(&m_LogMutex);
va_end(ap);
}
private:
LockedLog() { /*pthread_mutex_init(&m_LogMutex, 0);*/ }
static LockedLog *instance;
pthread_mutex_t m_LogMutex;
};
class LogStreamer : public std:stringstream
{
public:
LogStreamer() :m_logger(LockedLog::Instance()) {}
~LogStreamer()
{
m_logger.log("%s", str().c_str());
}
private:
LockedLog &m_logger;
};
LockedLog *LockedLog::instance=0;
class Wibble
{
public:
friend std:stream &operator<<(std:stream &os, const Wibble
&) { os << "I am wibbles output friend"; return os; }
};
int main(int, char *[])
{
LockedLog::Instance().log("try char* %s try int %d\n", "apple",
23);
std::cout << "BEGIN\n";
{
LogStreamer strm;
strm << 23 << std::endl;
strm << Wibble() << std::endl;
strm << "This is a test" << std::endl;
}
std::cout << "END\n";
std::cout << "BEGIN\n";
LogStreamer() << 42 << std::endl;
// doesnt compile
// LogStreamer() << Wibble() << std::endl;
LogStreamer() << "This is NOT a test" << std::endl;
std::cout << "END\n";
return 0;
}