By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,833 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,833 IT Pros & Developers. It's quick & easy.

Overload operator << question.

P: n/a
Hi all,
I am writing a logger program which can take any datatype.

namespace recordLog {
enum Debug_Level {low, midium, high};

class L {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
L(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=log_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::out|std::ios::app) ;

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}

template<typename T> friend std::ostream& operator << (L&
lstrm, const T& t);
friend std::ostream& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__;}
};
L& initL(std::string str)
{
//static L ls(str,high);
int dl=atoi(d_level.c_str());
static L ls(str,static_cast<Debug_Level>(dl));
return ls;
}
/*
template<typename T> std::ostream& operator << (L& lstrm, const T&
t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm.os;
}
*/

std::ostream& operator << (L& lstrm,const Debug_Level& l)
{
std::cout<<"I AM LOG"<<std::endl;
lstrm.cdl = l;
return lstrm.os;
}

class E {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
E(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=error_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::app);

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}
template<typename T> friend std::ostream& operator << (E& lstrm,
const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__; }

};
E& initE(std::string str)
{
static E ls(str,high);
return ls;
}

/*
template<typename T> std::ostream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::ostream& operator << (E& lstrm,const Debug_Level& l)
{
lstrm.cdl = l;
return lstrm.os;
}

}
int main()
{
using namespace recordLog;
L& l=initL("harilogger.txt");
E& elog=initE("errorlog.txt");
l<< high <<"test" << 45;
elog<<low<<"Error in this line"<<56;

}

My question is still I commented out the template part..
My program is still writing to the files harilogger.txt and
errorlog.txt.My program will be such as that I will set a debug level
based on which data's will be displayed.

if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;

if current debug level is less then or equal to desired debug level it
should write the data else it should not.

elog<<low<<"Error in this line"<<56;
Here first I am setting the debug level i,e low and based on which data
<<"Error in this line"<<56;
will be written..
Thanks in advance..
Cheer's
HPS

Nov 28 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
More information on operator overloading and the << operator can be found
here:

http://www.vias.org/cppcourse/chap15_08.html

This would not solve your specific problem but gives a good introduction to
that topic.

Regards, Hans
Nov 28 '05 #2

P: n/a

"Harry" <ha********@gmail.com> дʼ
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi all,
I am writing a logger program which can take any datatype.

namespace recordLog {
enum Debug_Level {low, midium, high};

class L {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
L(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=log_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::out|std::ios::app) ;

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}

template<typename T> friend std::ostream& operator << (L&
lstrm, const T& t);
friend std::ostream& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__;}
};
L& initL(std::string str)
{
//static L ls(str,high);
int dl=atoi(d_level.c_str());
static L ls(str,static_cast<Debug_Level>(dl));
return ls;
}
/*
template<typename T> std::ostream& operator << (L& lstrm, const T&
t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm.os;
}
*/

std::ostream& operator << (L& lstrm,const Debug_Level& l)
{
std::cout<<"I AM LOG"<<std::endl;
lstrm.cdl = l;
return lstrm.os;
}

class E {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
E(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=error_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::app);

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}
template<typename T> friend std::ostream& operator << (E& lstrm,
const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__; }

};
E& initE(std::string str)
{
static E ls(str,high);
return ls;
}

/*
template<typename T> std::ostream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::ostream& operator << (E& lstrm,const Debug_Level& l)
{
lstrm.cdl = l;
return lstrm.os;
}

}
int main()
{
using namespace recordLog;
L& l=initL("harilogger.txt");
E& elog=initE("errorlog.txt");
l<< high <<"test" << 45;
elog<<low<<"Error in this line"<<56;

}

My question is still I commented out the template part..
My program is still writing to the files harilogger.txt and
errorlog.txt.My program will be such as that I will set a debug level
based on which data's will be displayed.

if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;

if current debug level is less then or equal to desired debug level it
should write the data else it should not.

elog<<low<<"Error in this line"<<56;
Here first I am setting the debug level i,e low and based on which data
<<"Error in this line"<<56;
will be written..
Thanks in advance..
Cheer's
HPS


Hi,

I think the key is the operator <<.
l << high , call your overloaded method,
std::ostream& operator << (L& lstrm,const Debug_Level& l);

but the following << "test" is really call
std::ostream& operator<< (ostream& os, const char * s), isn't it.

so though you commented out your method "template<typename T> std::ostream&
operator << (L& lstrm, const T& t)",
it's nothing.

maybe you need overload L& operator << (L& lstrm,const const T& l) instead.

try this.
#include <iostream>
#include <fstream>
#include <string>

namespace recordLog {

enum Debug_Level {low, midium, high};

std::string log_path = "E:\\";
std::string error_path = "E:\\";
class L {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
L(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=log_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::out|std::ios::app) ;

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}

template<typename T> friend L& operator << (L&
lstrm, const T& t);
friend L& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__<<"\n";}
};
L& initL(std::string str, Debug_Level dl)
{
//static L ls(str,high);
//int dl=atoi(d_level.c_str());
static L ls(str, dl);
return ls;
}
template<typename T> L& operator << (L& lstrm, const T&
t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm;
}
L& operator << (L& lstrm,const Debug_Level& l)
{
std::cout<<"I AM LOG"<<std::endl;
lstrm.cdl = l;
return lstrm;
}

class E {
std::ofstream os;
Debug_Level cdl;
const Debug_Level ddl;
public:
E(std::string filename, const Debug_Level& desired)
: ddl(desired)
{
std::string fname=error_path+filename;
if(!os)
exit(EXIT_FAILURE);
os.open(fname.c_str(),std::ios::app);

os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\n";
}
template<typename T> friend std::ostream& operator << (E& lstrm,
const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"\n\nEND TIME :"<<__DATE__<<" "<< __TIME__<<"\n"; }

};
E& initE(std::string str)
{
static E ls(str,high);
return ls;
}

/*
template<typename T> std::ostream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::ostream& operator << (E& lstrm,const Debug_Level& l)
{
lstrm.cdl = l;
return lstrm.os;
}

}
int main()
{
using namespace recordLog;
L& l=initL("harilogger.txt", recordLog::low);
// E& elog=initE("errorlog.txt");
l<< high <<"test" << 45;
// elog<<low<<"Error in this line"<<56;

}
sods.
Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.