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_FAILU RE);
os.open(fname.c _str(),std::ios ::out|std::ios: :app);
os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\ n";
}
template<typena me T> friend std::ostream& operator << (L&
lstrm, const T& t);
friend std::ostream& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"\n\nE ND TIME :"<<__DATE__ <<" "<< __TIME__;}
};
L& initL(std::stri ng str)
{
//static L ls(str,high);
int dl=atoi(d_level .c_str());
static L ls(str,static_c ast<Debug_Level >(dl));
return ls;
}
/*
template<typena me 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_pat h+filename;
if(!os)
exit(EXIT_FAILU RE);
os.open(fname.c _str(),std::ios ::app);
os<<"START TIME :" <<__DATE__<<" "<<__TIME__<<"\ n";
}
template<typena me 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::stri ng str)
{
static E ls(str,high);
return ls;
}
/*
template<typena me T> std::ostream& operator << (E& lstrm, const T&
t)
{
std::cout<<type id(T).name()<<s td::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("harilo gger.txt");
E& elog=initE("err orlog.txt");
l<< high <<"test" << 45;
elog<<low<<"Err or 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<<"Err or 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