-
-
Save JamesWCCheng/a642fe7f5f8f1b61e37a to your computer and use it in GitHub Desktop.
Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <string> | |
using namespace std; | |
template<class Arg> | |
string ToString(const char* func, int line, const char* varname, const Arg& arg) | |
{ | |
ostringstream ss; | |
ss << "[" << func << "](" << line << ") :" << varname << " = " << arg << endl; | |
return ss.str().c_str(); | |
} | |
#define _(X) ToString(__func__,__LINE__,#X,X) | |
class EZLogger | |
{ | |
public: | |
template<class T> | |
EZLogger& operator<<(T&& other) | |
{ | |
cout << other; | |
return *this; | |
} | |
EZLogger& operator<<(ostream& (*func)(ostream&))//for std::endl | |
{ | |
cout << func; | |
return *this; | |
} | |
}; | |
EZLogger ez; | |
class Foo | |
{ | |
public: | |
Foo() { x = 123; y = 5566; z = 7788; } | |
int x; | |
int y; | |
friend ostream& operator<<(ostream& o, const Foo& f); | |
private: | |
int z; | |
}; | |
ostream& operator<<(ostream& o, const Foo& f) | |
{ | |
o << f.x << f.y << f.z << endl; | |
return o; | |
} | |
int main() | |
{ | |
int qq = 123; | |
Foo foo; | |
ez << _(qq) << _(foo) << endl << "@@@@"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <string> | |
using namespace std; | |
template<class Arg> | |
string ToString(const char* func, int line, const char* varname, const Arg& arg) | |
{ | |
ostringstream ss; | |
ss << "[" << func << "](" << line << ") :" << varname << " = " << arg << endl; | |
return ss.str().c_str(); | |
} | |
#define _(X) ToString(__func__,__LINE__,#X,X) | |
class EZLogger | |
{ | |
public: | |
template<class T> | |
EZLogger& operator<<(T&& other) | |
{ | |
cout << other; | |
return *this; | |
} | |
EZLogger& operator<<(ostream& (*func)(ostream&))//for std::endl | |
{ | |
cout << func; | |
return *this; | |
} | |
}; | |
EZLogger ez; | |
class Foo | |
{ | |
public: | |
Foo() { x = 123; y = 5566; z = 7788; } | |
int x; | |
int y; | |
friend ostream& operator<<(ostream& o, const Foo& f); | |
private: | |
int z; | |
}; | |
ostream& operator<<(ostream& o, const Foo& f) | |
{ | |
o << f.x << f.y << f.z << endl; | |
return o; | |
} | |
int main() | |
{ | |
int qq = 123; | |
Foo foo; | |
ez << _(qq) << _(foo) << endl << "@@@@"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment