Skip to content

Instantly share code, notes, and snippets.

@spoorcc
Created March 7, 2017 17:06
Show Gist options
  • Save spoorcc/dff4b8fea1b41fa02b499d8f5c32ff7a to your computer and use it in GitHub Desktop.
Save spoorcc/dff4b8fea1b41fa02b499d8f5c32ff7a to your computer and use it in GitHub Desktop.
C++11 tracing utility
#include "trace_util.hpp"
/** To build under gcc:
* g++ main.cpp -stdc=c++11
*/
auto main() -> int
{
TRACE("I would love some %f\n", 3.1415);
ERROR("Some error %d\n", 42);
NOTIFY("Somebody notice me\n");
}
#if __cplusplus < 201103L
#error "The tracing utility depends on C++11 language features"
#endif
#include <stdio.h>
/**
* This file is a simple C++11 tracing utility that traces with color. I
* created it mainly to use variadic template class feature
*/
#define TRACE(...) trace_func(__FILE__, __LINE__, COLOR_GREEN, ##__VA_ARGS__)
#define ERROR(...) trace_func(__FILE__, __LINE__, COLOR_RED, ##__VA_ARGS__)
#define NOTIFY(...) trace_func(__FILE__, __LINE__, COLOR_BG_GRAY, ##__VA_ARGS__)
#define COLOR_RED "\033[91m"
#define COLOR_GREEN "\033[92m"
#define COLOR_BG_GRAY "\033[100m"
#define COLOR_END "\033[0m"
template<typename... Args>
auto inline trace_func(char const* file,
const int line,
char const* color,
char const * format,
Args... args) -> void
{
#define PREFORMAT "%s>> %s[%d]: "
fprintf(stderr, PREFORMAT, color, file, line);
#pragma GCC diagnostic ignored "-Wformat-security"
fprintf(stderr, format, args...);
#pragma GCC diagnostic pop
fprintf(stderr, COLOR_END);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment