Overload operator << question.

H

Harry

Hi all,
I am writing a logger program which can take any datatype.

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

class L {
std::eek:fstream 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::eek:ut|std::ios::app);

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

template<typename T> friend std::eek:stream& operator << (L&
lstrm, const T& t);
friend std::eek:stream& 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::eek:stream& operator << (L& lstrm, const T&
t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm.os;
}
*/

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

class E {
std::eek:fstream 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::eek:stream& operator << (E& lstrm,
const T& t);
friend std::eek:stream& 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::eek:stream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::eek:stream& 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
 
S

sods

Harry said:
Hi all,
I am writing a logger program which can take any datatype.

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

class L {
std::eek:fstream 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::eek:ut|std::ios::app);

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

template<typename T> friend std::eek:stream& operator << (L&
lstrm, const T& t);
friend std::eek:stream& 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::eek:stream& operator << (L& lstrm, const T&
t)
{
if(lstrm.cdl <= lstrm.ddl)
lstrm.os << t;
return lstrm.os;
}
*/

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

class E {
std::eek:fstream 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::eek:stream& operator << (E& lstrm,
const T& t);
friend std::eek:stream& 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::eek:stream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::eek:stream& 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::eek:stream& operator << (L& lstrm,const Debug_Level& l);

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

so though you commented out your method "template<typename T> std::eek:stream&
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::eek:fstream 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::eek:ut|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::eek:fstream 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::eek:stream& operator << (E& lstrm,
const T& t);
friend std::eek:stream& 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::eek:stream& operator << (E& lstrm, const T&
t)
{
std::cout<<typeid(T).name()<<std::endl;
lstrm.os << t;

return lstrm.os;
}
*/

std::eek:stream& 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top