Created
November 26, 2012 01:56
-
-
Save icholy/4146189 to your computer and use it in GitHub Desktop.
logging abstraction
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
// | |
// main.cpp | |
// test | |
// | |
// Created by Ilia Choly on 12-11-20. | |
// Copyright (c) 2012 Ilia Choly. All rights reserved. | |
// | |
#include <iostream> | |
#include <functional> | |
#define ACCIPITER_LOGGING_ENABLED | |
namespace logging { | |
struct Logger { | |
inline void error (char const* msg) { | |
std::cerr << msg << std::flush; | |
} | |
}; | |
namespace { | |
struct Singleton { | |
static Logger logger; | |
}; | |
struct Sink { | |
virtual inline void message (char const*) = 0; | |
inline Sink & operator<< (std::string const& msg) { | |
#ifdef ACCIPITER_LOGGING_ENABLED | |
message(msg.c_str()); | |
return *this; | |
#endif | |
} | |
inline Sink & operator<< (char c) { | |
#ifdef ACCIPITER_LOGGING_ENABLED | |
message(std::string(1, c).c_str()); | |
return *this; | |
#endif | |
} | |
}; | |
struct ErrorSink : public Sink { | |
inline void message (char const* msg) { | |
Singleton::logger.error(msg); | |
} | |
}; | |
} | |
struct log { | |
enum { | |
endl = '\n', | |
tab = '\t' | |
}; | |
static ErrorSink error; | |
}; | |
ErrorSink log::error; | |
Logger Singleton::logger; | |
} | |
using ::logging::log; | |
int main() { | |
log::error << "this is a test" << log::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment