Click here to Skip to main content
15,868,420 members
Articles / Programming Languages / C++
Tip/Trick

Easy Trace of Function Entry and All Exits

Rate me:
Please Sign up or sign in to vote.
3.76/5 (8 votes)
25 May 2021CPOL 5.2K   6   6
A simple struct is presented which permits the automatic display to the console of function entry and all exits
If you need to trace the function entrances and exits of your project, the method presented here makes it easy to do utilizing a simple struct which makes it all automatic.

Introduction

Here is a small C++ struct which makes it easy and effortless to automatically display via the console the entry and exits of your functions. Note that it utilizes the helpful console colors of @Jaded-Hobo previously published Add Color to Console Projects.

C++
struct cpreamble
{
    std::string m_func;
    cpreamble(const char* func) : m_func(func) 
    { std::cout << fg_yellow << func << fg_white << " entry" << std::endl; }
    ~cpreamble() { std::cout << fg_cyan << m_func << fg_white << " exit" << std::endl; }
};
#define PREAMBLE cpreamble _cpreamble(__func__);

So one can utilize it as below:

void foobar()
{
    PREAMBLE // first line in function
    // full body of function ...
}

Voila! The entry and all returns are automatically displayed to the console or wherever you wish if you modify cpreamble via the constructor and destructor of cpreamble. Of course, one can utilize __FUNCSIG__ instead of __func__ as the macro argument if one can tolerate the lengthy class type strings and overly informative function signatures which result. Such lengthy strings can of course be simplified but that is another project and tip. Please note the automatic formatting by Visual Studio of code which follows a macro which contains its own terminating semi-colon does not work as expected but in my own work, I insist on doing so. - Cheerio

History

  • 26th May, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
MS Physics Drake University
BA Mathematics Dominican University
Wrote world's first user configurable binary file editor utilizing templates to specify file structure id est to wit in particular to be specific "FileRay" unfortunately not a commercial success. Some years later Sweetscape did a better job w/ "010 Editor" and seems to be a success.
Enjoy physical simulation. Did some work in this regard in astro-physics and options market making.

Home Page
https://mypaltrythoughts.blogspot.com/

Below is my "Signature" apologies for no apologies if offends
My sympathies to the SPAM moderator
"I once put instant coffee into the microwave and went back in time." - Steven Wright
"Shut up and calculate" - apparently N. David Mermin possibly Richard Feynman
“I want to sing, I want to cry, I want to laugh. Everything together. And jump and dance. The day has arrived — yippee!” - Desmond Tutu
“When the green flag drops the bullshit stops!”
"It is cheaper to save the world than it is to ruin it."
"I must have had lessons" - Reverend Jim Ignatowski / Christopher Lloyd
"Dripping water hollows out stone, not through force, but through persistence." - Ovid, Roman poet
"... as likely as lightning striking a leprechaun whilst riding a unicorn."
"Don't worry, the planet will do just fine without us."
"We can't solve today's problems with the mentality that created them." Albert Einstein
“Much becomes obvious in ­hindsight, ... Yet it is striking how in both physics and mathematics there is a lack of ­proportion between the effort needed to understand something for the first time and the simplicity and naturalness of the solution once all the required stages have been completed.” - Giorgio Parisi, recipient of the 2021 Nobel Prize in Physics
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
"Insanity in individuals is rare, but in groups, parties, nations, it is the rule." - Nietzsche

Comments and Discussions

 
QuestionNice snippet Pin
jeffie10010-Jun-21 4:34
jeffie10010-Jun-21 4:34 
Questionhow do we replace fg_yellow in this preamble function Pin
Southmountain8-Jun-21 11:50
Southmountain8-Jun-21 11:50 
a complete example is easier to pick up....
diligent hands rule....

GeneralMy vote of 3 Pin
Vincent Radio1-Jun-21 19:56
professionalVincent Radio1-Jun-21 19:56 
SuggestionSuggestions Pin
Greg Utas26-May-21 2:15
professionalGreg Utas26-May-21 2:15 
GeneralMy vote of 5 Pin
Сергій Ярошко26-May-21 0:17
professionalСергій Ярошко26-May-21 0:17 
QuestionI have been using a similar solution for years Pin
Joerg Michels25-May-21 21:15
Joerg Michels25-May-21 21:15 

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.