473,231 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Overload operator << question.

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
6
by: Hunter Hou | last post by:
Hi, I have this program, compiling is passed, but when i wanted to get the executable file(g++ -o output input.o), it fails. Who can lead me where the error is? Thanks, Hunter ===
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
7
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
11
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class...
5
by: David | last post by:
Hi all, I am trying to overload the < operator, but get warning class Windowinfo { protected: HWND wndhandle; //the window handle int wndId; //the window Id
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.