C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overload operator << question.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Harry
Guest





PostPosted: Mon Nov 28, 2005 7:52 am    Post subject: Overload operator << question. Reply with quote



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 lstrm, const T& t);
friend std::ostream& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"nnEND 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 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"< 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 const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"nnEND TIME :"<<__DATE__<<" "<< __TIME__; }

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

/*
template t)
{
std::cout< 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<
}

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< 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

Back to top
Hans Lohninger
Guest





PostPosted: Mon Nov 28, 2005 8:33 am    Post subject: Re: Overload operator << question. Reply with quote



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


Back to top
sods
Guest





PostPosted: Thu Dec 01, 2005 3:58 am    Post subject: Re: Overload operator << question. Reply with quote




"Harry" <harimcajec (AT) gmail (DOT) com> дÈëÓʼþ
news:1133164371.423403.34660 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
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 lstrm, const T& t);
friend std::ostream& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"nnEND 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 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"< 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 const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"nnEND TIME :"<<__DATE__<<" "<< __TIME__; }

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

/*
template t)
{
std::cout< 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<
}

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< 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 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 #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 lstrm, const T& t);
friend L& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"nnEND 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 t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm;
}


L& operator << (L& lstrm,const Debug_Level& l)
{
std::cout<<"I AM LOG"< 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 const T& t);
friend std::ostream& operator << (E& lstrm,const Debug_Level&
l);
~E(){ os<<"nnEND TIME :"<<__DATE__<<" "<< __TIME__<<"n"; }

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

/*
template t)
{
std::cout< 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<
}


sods.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.