An efficient and configurable logging framework for C++
tracetool is a framework created by froglogic for tracing the execution of a C or C++ program and inspecting its state. This is achieved by instrumenting the source code of the target program and linking the recompiled sources against a shared
traceliblibrary.
tracetool is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Building the framework requires
The actual build is the merely a matter of running the commands
cmake -DCMAKE_PREFIX_PATH= make
On Windows, you may want to pass
-G "NMake Makefiles"to cmake to generate Makefiles and then use
nmaketo use those Makefiles.
Here's a quick step by step guide on how to instrument a basic program so that it generates trace information. Here's the initial source code of the sample program:
#include #includeint main() { using namespace std;
string name; cout << "Please enter your name: "; cin >> name; cout << "Hello, " << name << "!" << endl;
}
First, instrument the above source code so that the
tracelib.hheader is included and insert a few calls to various tracelib macros into the source code. Here's the instrumented code:
#include #include#include "tracelib.h"
int main() { fTrace(0) << "main() entered";
using namespace std; string name; cout << "Please enter your name: "; cin >> name; fWatch(0) << fVar(name); cout << "Hello, " << name << "!" << endl; fTrace(0) << "main() finished";
}
Save the resulting file to e.g.
hello\_instrumented.cpp.
Here's a sample compile line for use with
cl.exe(the compiler which comes with Microsoft Visual Studio):
cl /EHsc hello_instrumented.cpp /I \include\tracelib \lib\tracelib.lib ws2_32.lib
The resulting binary (
hello\_instrumented.exein the above case) will behave as before. The tracing (and the resulting change to runtime performance) is only activated in case a special configuration file was detected.
Creating a configuration file is a matter of writing some XML. Save the following file as
tracelib.xmland store it in the same directory as
hello\_instrumented.exewhich was built above:
hello_instrumented
Now, running the program will generate quite a bit of additional output:
03.09.2010 16:00:56: Process 2524 [started at 03.09.2010 16:00:56] (Thread 468): [LOG] 'main() entered' hello_instrumented.cpp:8: int __cdecl main(void) Please enter your name: Max 03.09.2010 16:00:57: Process 2524 [started at 03.09.2010 16:00:56] (Thread 468): [WATCH] hello_instrumented.cpp:16: int __cdecl main(void) Hello, Max! 03.09.2010 16:00:57: Process 2524 [started at 03.09.2010 16:00:56] (Thread 468): [LOG] 'main() finished' hello_instrumented.cpp:20: int __cdecl main(void) 03.09.2010 16:00:57: Process 2524 [started at 03.09.2010 16:00:56] finished
The tracetool framework consists of multiple components:
tracelib.dll(resp.
libtracelib.soon Linux) is a dynamic library (plus accompanying header files) which has to be linked into any application which wishes to use the tracing functionality.
traceguiis a GUI for reviewing previously recorded traces as well as recording and watching the trace generated by running applications live.
tracedis a daemon process which collects and stores trace data in the background without running a GUI.Traced applications can be configured to send their output over a network connection to
traced.exe. The recorded traces can be sent to other people and reviewed later using
tracegui.
trace2xmlis a utility program for dumping a trace database generated by
traceguior
tracedinto an XML file which can then be processed by other scripts.
xml2traceperforms the reverse operation of
trace2xml: given an XML file, a
.tracefile is generated which can be loaded by
tracegui.
convertdbis a helper utility for converting earlier versions of databases with tracelib traces.
This project is really just standing on the shoulders of giants. It was made possible by and takes advantage of a few other open source projects:
tracelib.xmlfile is done using the TinyXML parser.