Last active
August 29, 2015 14:26
-
-
Save bbkane/a01adc17e4c50b3a571f to your computer and use it in GitHub Desktop.
Logger to be improved
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 <fstream> | |
#include <string> | |
#define STR(x) #x | |
#define STRINGIFY(x) STR((x)) | |
#define ERROR_INFO __FILE__ " : " __FUNCTION__ " : " STRINGIFY(__LINE__) " : " | |
class Error_Logger { | |
private: | |
std::ostream * output_stream{ nullptr }; | |
bool using_file{ false }; | |
public: | |
bool init(std::string file) | |
{ | |
output_stream = new std::ofstream(file); | |
using_file = true; | |
return true; | |
} | |
bool init() | |
{ | |
output_stream = &std::cout; | |
using_file = false; | |
return true; | |
} | |
bool destroy() | |
{ | |
if (using_file) | |
{ | |
delete output_stream; | |
} | |
return true; | |
} | |
void log(){}; | |
template<typename T> | |
void log(T info) | |
{ | |
*output_stream << info << std::endl; | |
} | |
template<typename T, typename... Args> | |
void log(T first, Args... args) | |
{ | |
*output_stream << first << ' '; | |
log(args...); | |
} | |
template<typename T> | |
void check(bool cond, T info) | |
{ | |
if (!cond) | |
{ | |
log(info); | |
} | |
} | |
template<typename... Args> | |
void check(bool cond, Args... args) | |
{ | |
if (!cond) | |
{ | |
log(args...); | |
} | |
} | |
}; | |
class tmp { | |
int i{ 4 }; | |
friend std::ostream & operator<<(std::ostream &os, const tmp& p); | |
}; | |
std::ostream &operator<<(std::ostream &os, const tmp& p) | |
{ | |
return os << p.i; | |
} | |
int main() | |
{ | |
tmp t; | |
Error_Logger logger; | |
logger.init(); | |
logger.log(ERROR_INFO, STRINGIFY(4==3), " ", "hi"); | |
logger.check(3 == 2, "hi", "there"); | |
logger.check(2 == 4, t); | |
logger.check(3 == 4); //doesn't work | |
logger.destroy(); | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment