Git Product home page Git Product logo

tracepp's Introduction

tracepp

C++17 tracing library, via the TRACE() macro, for debugging with ease that is inspired by Rust's dbg! macro.

If NDEBUG is defined then TRACE(x) x in order for the program to function properly in non-debug mode.

Examples

The following example shows which values are checked in the for-loop and the values for each iteration:

#include "tracepp.h"

int main()
{
  for (int i = 10; TRACE(i < 13); ++i) {
    TRACE(i);
  }
  return 0;
}

Output:

example.cc:5:main(): i < 13 = true
example.cc:6:main(): i = 10
example.cc:5:main(): i < 13 = true
example.cc:6:main(): i = 11
example.cc:5:main(): i < 13 = true
example.cc:6:main(): i = 12
example.cc:5:main(): i < 13 = false

Tracepp supports containers (as long as they have cbegin() and cend()):

#include "tracepp.h"
#include <vector>

int main()
{
  std::vector<std::vector<int>> vec{{1, 2, 3}, {4, 5}, {6, 7, 8}};
  TRACE(vec);
  if (TRACE(vec.size() >= 2)) {
    TRACE(vec[1]);
  }
  return 0;
}

Output:

example2.cc:7:main(): vec = [[1, 2, 3], [4, 5], [6, 7, 8]]
example2.cc:8:main(): vec.size() >= 2 = true
example2.cc:9:main(): vec[1] = [4, 5]

Adding custom type handling

Tracepp can easily be extended to support custom types:

#include "tracepp.h"
#include <string>

struct Custom {
  Custom(const std::string &text_) : text(text_) {}
  std::string text;
};

namespace tracepp {

template <>
inline std::string toString(const Custom &val)
{
  return "\"" + val.text + "\"";
}

} // namespace tracepp

int main()
{
  Custom c("Hello, Custom World!");
  TRACE(c);
  return 0;
}

Output:

custom.cc:22:main(): c = "Hello, Custom World!"

Overriding the default print function

The type of a print function is tracepp::PrintFunc and can be overridden using tracepp::setPrintFunc():

tracepp::setPrintFunc([](const std::string &file, const int line, const std::string &func,
                         const std::string &expr, const std::string &value) {
  std::cout << "[CUSTOM] " << file << ":" << line << ":" << func << "(): " << expr << " = "
            << value << std::endl;
});

tracepp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.