trying to make it work...

This commit is contained in:
2014-09-09 17:16:51 +02:00
parent c62594c7ab
commit 811bac16ec

View File

@ -10,30 +10,25 @@ inline std::string NowTime();
enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4}; enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
template <typename T> template <typename T>
class Log class Log{
{ public:
public: Log();
Log(); virtual ~Log();
virtual ~Log(); std::ostringstream& Get(TLogLevel level = logINFO);
std::ostringstream& Get(TLogLevel level = logINFO); public:
public: static TLogLevel& ReportingLevel();
static TLogLevel& ReportingLevel(); static std::string ToString(TLogLevel level);
static std::string ToString(TLogLevel level); static TLogLevel FromString(const std::string& level);
static TLogLevel FromString(const std::string& level); protected:
protected: std::ostringstream os;
std::ostringstream os; private:
private: Log(const Log&);
Log(const Log&); Log& operator =(const Log&);
Log& operator =(const Log&);
}; };
template <typename T> template <typename T> Log<T>::Log() {}
Log<T>::Log()
{
}
template <typename T> template <typename T> std::ostringstream& Log<T>::Get(TLogLevel level)
std::ostringstream& Log<T>::Get(TLogLevel level)
{ {
os << "- " << NowTime(); os << "- " << NowTime();
os << " " << ToString(level) << ": "; os << " " << ToString(level) << ": ";
@ -41,29 +36,25 @@ std::ostringstream& Log<T>::Get(TLogLevel level)
return os; return os;
} }
template <typename T> template <typename T> Log<T>::~Log()
Log<T>::~Log()
{ {
os << std::endl; os << std::endl;
T::Output(os.str()); T::Output(os.str());
} }
template <typename T> template <typename T> TLogLevel& Log<T>::ReportingLevel()
TLogLevel& Log<T>::ReportingLevel()
{ {
static TLogLevel reportingLevel = logDEBUG4; static TLogLevel reportingLevel = logDEBUG4;
return reportingLevel; return reportingLevel;
} }
template <typename T> template <typename T> std::string Log<T>::ToString(TLogLevel level)
std::string Log<T>::ToString(TLogLevel level)
{ {
static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"}; static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"};
return buffer[level]; return buffer[level];
} }
template <typename T> template <typename T> TLogLevel Log<T>::FromString(const std::string& level)
TLogLevel Log<T>::FromString(const std::string& level)
{ {
if (level == "DEBUG4") if (level == "DEBUG4")
return logDEBUG4; return logDEBUG4;
@ -85,15 +76,13 @@ TLogLevel Log<T>::FromString(const std::string& level)
return logINFO; return logINFO;
} }
class Output2FILE class Output2FILE {
{
public: public:
static FILE*& Stream(); static FILE*& Stream();
static void Output(const std::string& msg); static void Output(const std::string& msg);
}; };
inline FILE*& Output2FILE::Stream() inline FILE*& Output2FILE::Stream() {
{
static FILE* pStream = stderr; static FILE* pStream = stderr;
return pStream; return pStream;
} }
@ -136,6 +125,7 @@ class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
#include <windows.h> #include <windows.h>
inline std::string NowTime() inline std::string NowTime()
{ {
const int MAX_LEN = 200; const int MAX_LEN = 200;
char buffer[MAX_LEN]; char buffer[MAX_LEN];