Skip to content

Instantly share code, notes, and snippets.

@webmaster128
Last active August 29, 2015 14:08
Show Gist options
  • Save webmaster128/03d8901b87d9bfdfb470 to your computer and use it in GitHub Desktop.
Save webmaster128/03d8901b87d9bfdfb470 to your computer and use it in GitHub Desktop.
Cpp logging API
#ifndef LIBRARYLOGGER_H
#define LIBRARYLOGGER_H
#include <iostream>
#include <memory>
#include <cassert>
#ifndef Log
#define Log LibraryLogger(__FILE__, __LINE__, __PRETTY_FUNCTION__)
#endif
class LibraryLoggerImpl
{
public:
enum LOG_TYPE {
None = 0,
Error,
Warning,
Debug
};
LibraryLoggerImpl(const char *file, int line, const char *function)
: file_(file)
, line_(line)
, function_(function)
{
}
~LibraryLoggerImpl()
{
std::cout << std::endl;
}
void setLogType(LibraryLoggerImpl::LOG_TYPE type)
{
assert(type_ == None);
type_ = type;
}
// extractor for numeric types
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
void write(T input)
{
writeHeader();
std::cout << " " << input << std::flush;
}
void write(const char *input)
{
writeHeader();
std::cout << " " << input << std::flush;
}
// The rest
template <typename T, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
void write(T input)
{
writeHeader();
std::cout << " \"" << input << "\"" << std::flush;
}
void writeHeader()
{
if (headerWritten_) return;
switch (type_) {
case Error:
std::cout << "Error";
break;
case Warning:
std::cout << "Warning";
break;
case Debug:
std::cout << "Debug";
break;
default:
break;
}
std::cout << " in file '" << file_<< ":" << line_ << "' "
<< "(method " << function_ << "):";
headerWritten_ = true;
}
private:
std::string file_;
int line_;
std::string function_;
bool headerWritten_ = false;
LOG_TYPE type_ = None;
};
class LibraryLogger
{
public:
LibraryLogger(const char *file, int line, const char *function)
: impl_(new LibraryLoggerImpl(file, line, function))
{
}
LibraryLogger &e() { impl_->setLogType(LibraryLoggerImpl::Error); return *this; }
LibraryLogger &w() { impl_->setLogType(LibraryLoggerImpl::Warning); return *this; }
LibraryLogger &d() { impl_->setLogType(LibraryLoggerImpl::Debug); return *this; }
template <typename T> LibraryLogger &operator<<(T input) { impl_->write(input); return *this; }
private:
std::unique_ptr<LibraryLoggerImpl> impl_;
};
#endif // LIBRARYLOGGER_H
#include "librarylogger.h"
int main(int argc, char *argv[])
{
std::uint64_t cracyInteger = static_cast<std::uint64_t>(-1);
Log.e() << "Hello Streams";
Log.e() << "Streaming numbers" << 12346;
Log.e() << "Streaming long numbers" << 999999L;
Log.e() << "Streaming very long numbers" << cracyInteger;
Log.w() << "Hello Streams";
Log.w() << "Streaming numbers" << 12346L;
Log.w() << "Streaming very long numbers" << cracyInteger;
Log.d() << "Hello Streams";
Log.d() << "Pi is btw. about" << 3.14;
Log.d() << "Streaming very long numbers" << cracyInteger;
Log.d() << "A multi"
<< "line thing"
<< "thing"
<< "thing"
<< "thing"
<< "thing"
<< "with a"
<< std::string("string")
<< ".";
Log.e() << "Miau";
Log.e() << "The winning number is" << 98989888;
std::string aString = "möp";
Log.d() << "Show that string" << aString;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment