Click here to Skip to main content
13,355,335 members (73,708 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 15 Aug 2003

Function Calling Sequence Tracer Class

, 15 Aug 2003
Rate this:
Please Sign up or sign in to vote.
A class to trace the sequence at which functions are called.

Sample image


Sometimes in complex applications large number of messages are to be processed and there are a large number of functions for handling requests and events. Sometimes it is confusing to see what are the functions called by a given function. What are the functions that will lead to a call to a particular function. What is the sequence of calling of all these functions. If some programmer is supposed to continue an unfinished work of another programmer, in a badly documented system it will be a nightmare. Even the same programmer who had previously made all this can be confused. I experienced this problem in one of my projects. I searched for a solution on the Internet but I was unlucky, so I started to do it myself. I wrote a class to trace methods in the application and log all the sequence to a file in a readable format.

The idea

The idea of my solution is to use a global object of a small class having some static member functions and static member variables. In an early point in the application (in the constructor of the application class, for example) we specify the file that will hold the sequence log. This is done using a static function. The class contains some private static member variables to maintain the current level of the call. At the beginning of a function an instance of this class is instantiated in the stack using the constructor, which takes as a parameter the text that should be logged. At the end of the function, this class instance will be out of scope so the destructor of the class will be called and the level will decrease.


The following is the header file of the SequenceTracer class:

// SequenceTracer.h


#include <fstream>  // for std::ofstream

class SequenceTracer  
    static void Start(const char * fileName); // Set the file name and start
    SequenceTracer(const char * text); // The only constructor
    virtual ~SequenceTracer();
    static int Stack[256]; // The stack maintaing sequence numbers
    static int Level; // The current level in the stack
    static std::ofstream * OutStream; // The always open output stream



And this is the implementation file for the class:

// SequenceTracer.cpp

#include "SequenceTracer.h"
int SequenceTracer::Level;
std::ofstream * SequenceTracer::OutStream;
int SequenceTracer::Stack[256];
void SequenceTracer::Start(const char *fileName)
    OutStream = new std::ofstream (fileName);
SequenceTracer::SequenceTracer(const char * text)
    if (!OutStream) // Outstream should be initialized

    for (int i = 0; i < Level; i++)
        *OutStream << '\t'; // Make indentation

    Stack[Level]++; // Increment the sequence number

    for (i = 0; i < Level, Stack[i] > 0; i++)
        *OutStream << Stack[i] << '.'; // Print the sequence number

    *OutStream << " "; // Add a space
    *OutStream << text << "\n"; // Write the text followed by newline
    Level++; // Increase the level
    Stack[Level] = 0; // Reset the sequence number
    Level--; // Decrease the level
    if (Level < 0) // If we are finished
        delete OutStream; // Write to the file and delete stream
        OutStream = 0; // Reset the stream pointer

Using the class

Using this class is very easy simply in three steps:

  1. Include the SequencTracer.h in CPP files in which you want to trace function sequence
  2. In an early point in the application (The application constructor for example) call the static function Start and give this function the name of the file to be created and will store all the sequence
  3. In the beginning of any function you would like to figure out the usage, instantiate an object of the class in the stack

After you run the application and the level is less than 0, you can see the contents of the sequence file. It will look like:

Sample image

I made an application specially to test the class. I made two helper macros to further simplify using the class:

#ifdef _DEBUG
#define SEQ_TRACE(txt) SequenceTracer unique_variable_name##__LINE__ (txt)
#define SEQ_START(fileName) SequenceTracer::Start(fileName)
#define  SEQ_TRACE(txt)
#define SEQ_START(fileName)
// Thanks to Michael Dunn for ##__LINE__


  • 16-8-2003: Article submitted, minor updates.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mohamed Abdel-Monem
Chief Technology Officer
Egypt Egypt
No Biography provided

You may also be interested in...

Comments and Discussions

General~SequenceTracer() may goes some mistake Pin
WangZHua5-Jan-09 6:17
memberWangZHua5-Jan-09 6:17 
GeneralUse this tool, Auto display Function Calling Sequence Pin
autodebug3-Feb-04 21:33
memberautodebug3-Feb-04 21:33 
GeneralEven better suggestion Pin
Dan Clark19-Aug-03 17:52
memberDan Clark19-Aug-03 17:52 
GeneralRe: Even better suggestion Pin
barto22-Aug-03 23:58
memberbarto22-Aug-03 23:58 
GeneralRe: Even better suggestion Pin
Dan Clark24-Aug-03 13:10
memberDan Clark24-Aug-03 13:10 
GeneralCouple of suggestions Pin
Michael Dunn16-Aug-03 6:34
sitebuilderMichael Dunn16-Aug-03 6:34 
I use a similar class that I wrote myself at work (although with just a plain number instead of 6.1.1 to indicate the level). I make sure the variable name is unique like this:
#define SEQ_TRACE(txt) SequenceTracer unique_variable_name##__LINE__ (txt)
That prevents errors if two SEQ_TRACE macros end up either accidentally or purposely in the same scope.

Also, why not use std::stack instead of a hard-coded limit of 256 levels?

Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--=
GeneralRe: Couple of suggestions Pin
Mohamed Abdel-Monem16-Aug-03 8:54
memberMohamed Abdel-Monem16-Aug-03 8:54 
GeneralRe: Couple of suggestions Pin
Michael Dunn16-Aug-03 9:05
sitebuilderMichael Dunn16-Aug-03 9:05 
GeneralRe: Couple of suggestions Pin
Mohamed Abdel-Monem16-Aug-03 10:37
memberMohamed Abdel-Monem16-Aug-03 10:37 
GeneralThreads Pin
dog_spawn16-Aug-03 4:15
memberdog_spawn16-Aug-03 4:15 
GeneralRe: Threads Pin
Mohamed Abdel-Monem16-Aug-03 8:55
memberMohamed Abdel-Monem16-Aug-03 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 16 Aug 2003
Article Copyright 2003 by Mohamed Abdel-Monem
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid