Commit fcf4ae76 authored by Costa Shulyupin's avatar Costa Shulyupin

updated and fixed, compatible with clang

parent 8a9346c1
......@@ -4,7 +4,10 @@
#include <thread>
using std::string;
#if __cplusplus >= 201402L
using namespace std::literals::chrono_literals;
#endif
int main()
{
......@@ -17,7 +20,9 @@ int main()
void * p = &i;
int * ip = &i;
#if __cplusplus >= 201402L
std::this_thread::sleep_for(100ms);
#endif
trace(); // prints only file name and line
trace("error:", s); // prints literal message
trace(p);
......@@ -25,6 +30,7 @@ int main()
trace(cp);
trace(ca);
trace(i, d);
trace(i, s);
trace(i, d, s);
trace(i, d, s, cp);
trace(i, d, s, cp, ca);
......
......@@ -2,6 +2,7 @@
* Copyright (C) 2019 Constantine Shulyupin
* Original: https://gitlab.com/makelinux/lib/blob/master/tracer.hpp
*/
#pragma once
#include <string>
#include <sstream>
......@@ -19,6 +20,8 @@
#define file_line() (string(__file__) + ":" + std::to_string(__LINE__) + ":" + __func__ +" ")
#define _strlen(s) std::char_traits<char>::length(s)
namespace tracer
{
......@@ -50,39 +53,51 @@ static inline const std::string to_string(T * const p) {return to_string((void *
#endif
// prints name=value or just raw value for literal strings
#define _v(a) (strlen(#a) ? \
#define _v(a) (_strlen(#a) ? \
/* if it is literal */ \
(tracer::to_string(a) == #a) ? \
/* Assuming string literal within '"'. */ \
/* Getting just content of string literal. */ \
std::string(#a).substr(1, std::string(#a).length() - 2) + " " \
/* else */ \
: std::string(#a) + "=" + tracer::to_string(a) + " " : "")
#define _trace2(s, a, args...) do { s << _v(a); _IF (strlen(#args)) s << _v(args); } while (0)
#define _trace3(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace2(s, args); } while (0)
#define _trace4(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace3(s, args); } while (0)
#define _trace5(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace4(s, args); } while (0)
#define _trace6(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace5(s, args); } while (0)
#define _trace7(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace6(s, args); } while (0)
#define _trace8(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace7(s, args); } while (0)
#define _trace9(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace8(s, args); } while (0)
#define _traceA(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _trace9(s, args); } while (0)
#define _traceB(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _traceA(s, args); } while (0)
#define _traceC(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _traceB(s, args); } while (0)
#define _traceD(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _traceC(s, args); } while (0)
#define _traceE(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _traceD(s, args); } while (0)
#define _traceF(s, a, args...) do { s << _v(a); _IF (strlen(#args)) _traceE(s, args); } while (0)
#define _trace2(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) s << _v(args); } while (0)
#define _trace3(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace2(s, args); } while (0)
#define _trace4(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace3(s, args); } while (0)
#define _trace5(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace4(s, args); } while (0)
#define _trace6(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace5(s, args); } while (0)
#define _trace7(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace6(s, args); } while (0)
#define _trace8(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace7(s, args); } while (0)
#define _trace9(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace8(s, args); } while (0)
#define _traceA(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _trace9(s, args); } while (0)
#define _traceB(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _traceA(s, args); } while (0)
#define _traceC(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _traceB(s, args); } while (0)
#define _traceD(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _traceC(s, args); } while (0)
#define _traceE(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _traceD(s, args); } while (0)
#define _traceF(s, a, args...) do { s << _v(a); _IF (!!_strlen(#args)) _traceE(s, args); } while (0)
/** \endcond */
/**
* \brief Universal tracing function
* \brief Universal C++ tracing function
*
* Accepts from none to 16 artuments.
* Accepts from none to 16 arguments.
* Prints file and line number, arguments names and values.
*
* Example source code:
* trace(i, s);
*
* Output:
* tracer-hpp-test.cpp:33:main i=-123 s="string value"
*
* See file tracer-hpp-test.cpp for more examples.
*/
#define trace(a, args...) \
do { std::stringstream log; \
log << file_line() << _v(a); \
_IF(strlen(#args)) \
_IF(!!_strlen(#args)) \
_traceF(log, args); \
log_str(log.str()); \
} while (0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment