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.
- 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
stdoutto a file (InRedirect2File,OutRedirect2File).
- C++14 compiler.
- Windows-only out of the box because
CppTrace.hincludes<windows.h>and usesGetLocalTime. traceArraccepts 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).
- Go to the GitHub releases page.
- Expand
Assetsand download theCppTrace.hheader file. - Include the header file in your project.
The repository includes main.cpp as a usage demo. On Windows, you can build it with CMake:
cmake -S . -B build
cmake --build buildInclude 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.
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]
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"GPL-3.0 License © ruiyangzhou01
