Skip to content

ruiyangzhou01/CppTrace

Repository files navigation

CppTrace

C++

English | 简体中文 | Deutsch | Español | Français | 日本語

CppTrace is a single-header C++14 tracing helper that prints variable information to standard output. It ships as one header file (CppTrace.h) and includes helper functions for redirecting output to a log file.

Features

  • Single-header library (just CppTrace.h).
  • Trace scalar variables with trace.
  • Trace fixed-size arrays with traceArr.
  • Optional cycle counters for nested loops (std::list<int>).
  • Optional descriptions for each trace.
  • Output includes variable name, typeid(...).name(), value(s), function scope, line number, cycle indices, and description.
  • Helpers to redirect stdout to a file (InRedirect2File, OutRedirect2File).

Screenshot

screenshot

Requirements and limitations

  • C++14 compiler.
  • Windows-only out of the box because CppTrace.h includes <windows.h> and uses GetLocalTime.
  • traceArr accepts fixed-size arrays only (not pointers or STL containers). It determines the used length by scanning for \0, so arrays should be zero-initialized or null-terminated.
  • Type names come from typeid(...).name() and are compiler-specific (not demangled).

Install

  1. Go to the GitHub releases page.
  2. Expand Assets and download the CppTrace.h header file.
  3. Include the header file in your project.

Build the demo

The repository includes main.cpp as a usage demo. On Windows, you can build it with CMake:

cmake -S . -B build
cmake --build build

Usage

Include the header and use the tracing macros in your code:

#include "CppTrace.h"

int int_var = 3;
int int_arr[60] = {2, 5, 4, 9};

trace(int_var);
trace(int_var, {1, 2});
trace(int_var, {}, "This is the description");

traceArr(int_arr);
traceArr(int_arr, {1});
traceArr(int_arr, {1, 2}, "This is the description");

The optional cycleVariables argument is a std::list<int> (typically loop indices). If you only want a description, pass an empty list ({}) first.

Output format

Scalar traces:

TRACE [Var=int_var] [Type=i] [Value=3] [Fun=main, Line=16, Cycle=(1 2)] [Desc: This is the description]

Array traces:

TRACE [Array=int_arr] [Type=i, Len=4/60] [Array={2, 5, 4, 9}] [Fun=main, Line=27]

Redirect output to a file

Both helpers redirect stdout using freopen, so they affect all subsequent console output:

OutRedirect2File("Time");    // Uses a timestamp like "2026-02-10 13-05-02.log"
OutRedirect2File("trace");   // Writes to "trace.log"
InRedirect2File("main.in");  // Redirects stdout to "main.in"

License

GPL-3.0 License © ruiyangzhou01

About

A lightweight logging library to trace C++ variables.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors