473,738 Members | 7,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 28 '05 #1
2 2178
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********@gma il.com> дÈëÓʼþ
news:11******** *************@f 14g2000cwb.goog legroups.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_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


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<typen ame 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_FAILU RE);
os.open(fname.c _str(),std::ios ::out|std::ios: :app);

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

template<typena me T> friend L& operator << (L&
lstrm, const T& t);
friend L& operator << (L& lstrm,const Debug_Level&
l);
~L(){os<<"\n\nE ND TIME :"<<__DATE__ <<" "<< __TIME__<<"\n"; }
};
L& initL(std::stri ng str, Debug_Level dl)
{
//static L ls(str,high);
//int dl=atoi(d_level .c_str());
static L ls(str, dl);
return ls;
}
template<typena me 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_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__<<"\n"; }

};
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", recordLog::low) ;
// E& elog=initE("err orlog.txt");
l<< high <<"test" << 45;
// elog<<low<<"Err or 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
4338
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; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
6
1683
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
2057
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 following will be printed A 3 2
8
2026
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>& lst );
25
2854
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 contents" ) ; row << new MyTableCell( "cell2 text contents" ) ; table << row ; thanks,
7
14922
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 having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
11
3555
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 T> DataDest& operator<<(DataDest& d, const T& t) { t.writeTo(d);
5
3434
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
2387
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, but the return type doesn't match: Rectangle? m_textArea = null; public Rectangle TextArea { set { m_textArea = value; } get { return m_textArea; }
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.