Create a gist now

Instantly share code, notes, and snippets.

@stephanpareigis /logger.h Secret
Last active May 22, 2016

What would you like to do?
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#define LOG_DEBUG \
if(Logger::getLogger().getLoggingLevel() < DEBUG); \
else Logger::getLogger().log("DEBUG")
#define LOG_WARNING \
if(Logger::getLogger().getLoggingLevel() < WARNING); \
else Logger::getLogger().log("WARNING")
#define LOG_ERROR \
if(Logger::getLogger().getLoggingLevel() < ERROR); \
else Logger::getLogger().log("ERROR")
#define LOG_SET_LEVEL(value) Logger::getLogger().setLoggingLevel(value);
#define LOG_SCOPE LogScope logscope(__FUNCTION__);
using namespace std;
class Logger{
static Logger& getLogger(){// Meyers Singleton.
static Logger Instance;// Will be delete at the end.
return Instance;
ofstream& log(string s){// return ofstream, so cascading <<..<<..<< is possible.
logfile_<<time(NULL)<<" "<<s<<" ";// time ticks in seconds. Check OS for more accurate timer.
return logfile_;
void setLoggingLevel(LEVEL level){
logfile_<<"setting output level to "<<level<<endl;
logginglevel_ = level;}
LEVEL getLoggingLevel(){return logginglevel_;}
Logger()// make these private because of singleton.
: logginglevel_(DEBUG)// set default level
logfile_<<"LOGFILE. Build: "<<__DATE__<<", "<<__TIME__<<endl;
~Logger(){ logfile_<<"END OF LOGFILE"<<endl; logfile_.close();}
LEVEL logginglevel_;
ofstream logfile_;
class LogScope{
LogScope(const string& s) : logger_(Logger::getLogger()) , functionname_(s){
logger_.log("SCOPE")<<"entering "<<functionname_<<" \n";
logger_.log("SCOPE")<<"exiting "<<functionname_<<" \n";
Logger& logger_;
string functionname_;
int main()
LOG_DEBUG<<"hello world \n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment