Skip to content

Instantly share code, notes, and snippets.

@sirisian
Created April 12, 2015 01:34
Show Gist options
  • Save sirisian/d2f838bc1f0600ee93e5 to your computer and use it in GitHub Desktop.
Save sirisian/d2f838bc1f0600ee93e5 to your computer and use it in GitHub Desktop.
#include "Log.hpp"
#include <windows.h>
Section::Section(const char* name) : Name(name)
{
}
LogProxy::LogProxy(const char* level) : message(new std::stringstream)
{
*message << level;
}
LogProxy::LogProxy(const char* level, const Section& section) : message(new std::stringstream)
{
*message << section.Name << " - " << level;
}
LogProxy::~LogProxy()
{
*message << std::endl;
std::unique_lock<std::mutex>(mutex);
OutputDebugString(message->str().c_str());
}
std::mutex LogProxy::mutex;
LogProxy Log::DebugStream::operator()()
{
return LogProxy("Debug: ");
}
LogProxy Log::DebugStream::operator()(const ::Section& section)
{
return LogProxy("Debug: ", section);
}
LogProxy Log::WarningStream::operator()()
{
return LogProxy("Warning: ");
}
LogProxy Log::WarningStream::operator()(const ::Section& section)
{
return LogProxy("Warning: ", section);
}
LogProxy Log::ErrorStream::operator()()
{
return LogProxy("Error: ");
}
LogProxy Log::ErrorStream::operator()(const ::Section& section)
{
return LogProxy("Error: ", section);
}
LogProxy& Log::operator()(const char* level)
{
return LogProxy(level);
}
Section Log::Section(const char* name)
{
return ::Section(name);
}
Log::DebugStream Log::Debug;
Log::WarningStream Log::Warning;
Log::ErrorStream Log::Error;
#pragma once
#include <iostream>
#include <sstream>
#include <mutex>
#include <memory>
struct Section
{
const char* Name;
Section(const char* name);
};
class LogProxy
{
static std::mutex mutex;
std::shared_ptr<std::stringstream> message;
public:
LogProxy(const char* level);
LogProxy(const char* level, const Section& section);
~LogProxy();
template<typename T>
LogProxy& operator<<(const T& other)
{
*message << other;
return *this;
}
};
class Log
{
public:
static struct DebugStream
{
LogProxy operator()();
LogProxy operator()(const Section& section);
} Debug;
static struct WarningStream
{
LogProxy operator()();
LogProxy operator()(const Section& section);
} Warning;
static struct ErrorStream
{
LogProxy operator()();
LogProxy operator()(const Section& section);
} Error;
LogProxy& operator()(const char* level);
static Section Section(const char* name);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment