Skip to content

Instantly share code, notes, and snippets.

@icholy
Created November 26, 2012 01:56
Show Gist options
  • Save icholy/4146189 to your computer and use it in GitHub Desktop.
Save icholy/4146189 to your computer and use it in GitHub Desktop.
logging abstraction
//
// 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