Skip to content

Instantly share code, notes, and snippets.

@bbkane
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbkane/a01adc17e4c50b3a571f to your computer and use it in GitHub Desktop.
Save bbkane/a01adc17e4c50b3a571f to your computer and use it in GitHub Desktop.
Logger to be improved
#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