C and controlling debug stuff (something, almost, like Log4j) ;)

In case you are not aware of the macros ;) I am sure you are aware, but, just in case :)

int f(int a, int b) {
  #ifdef PROFILER
  BeginProfiling("f");
  #endif
  ...
  #ifdef PROFILER
  EndProfiling();
  #endif
}

Then, you can control it by doing this

// when you want to profile
#define PROFILER

// when you don't want to profile
//#define PROFILER

In case you are not aware of function pointers ;)

#include <stdio.h>

void (*doprofiling)(void);

void profile()
{
    printf("I am profiling\n");
}

void no_profile()
{
    printf("I am not profiling\n");
}

void fun() {
  doprofiling();
}

int main()
{
    doprofiling = profile;

    fun();

    doprofiling = no_profile;

    fun();

    return 0;
}

Then, you can switch in the code dynamically

gcc -o profile ./profile.c
./profile
I am profiling
I am not profiling

Or, you can use something like this, and you can apply different decorators to different functions

#include <stdio.h>

void doprofiling(void (*proffun)(void)) {
  proffun();
}

void profile()
{
    printf("I am profiling\n");
}

void no_profile()
{
    printf("I am not profiling\n");
}

void fun_prof() {
  void (*decorator)(void) = profile;
  doprofiling(decorator);
}

void fun_no_prof() {
  void (*decorator)(void) = no_profile;
  doprofiling(decorator);
}

int main()
{
    fun_prof();
    fun_no_prof();
    return 0;
}

And, you can still dynamically apply it in the code.

    
> gcc -o ./profile ./profile.c
> ./profile
I am profiling
I am not profiling

Leave a comment

Your comment