July 11, 2015

A simple C++ logger class

log.h

#ifndef __LOG1_H__

#define __LOG1_H__

#include <sstream>
#include <string>
#include <stdio.h>

class Log
{
public:
    Log(){};
    ~Log();
    std::ostringstream& Get();
protected:
    std::ostringstream os;
};

inline Log::~Log()
{
    os << std::endl;
    fprintf(stderr, "%s", os.str().c_str());
    fflush(stderr);
}

inline std::ostringstream& Log::Get()
{
    os << " " <<  ": ";
    return os;
}

#define log() Log().Get()

#endif //__LOG_H__


test.cpp:
log() << "A loop with " << count << " iterations";

No comments:

Post a Comment