Pre-processor macros causing segfaults
Hello,
so, I'm playing around with pre-processor macros, and I've come across behaviour I can't quite figure out. I've written these macros to trace the execution path of my program, problem is, when I activate them, the program just seg-faults and I can't for the life of me figure out what is causing this, maybe anyone on here has a clue.
Here are the two main files:
Code:
#ifndef DEBUG_HPP__
#define DEBUG_HPP__
#define DEBUG
#ifdef DEBUG
#include <fstream>
#define LOCATION "(File: " << __FILE__ << ", Function: " << __FUNCTION__ << "(...), Line: " << __LINE__ << ")"
#define SPACE std::string(lvl*2, ' ')
#define BEGIN { \
extern unsigned int lvl; \
extern std::ofstream log; \
\
{ \
log << SPACE << "--> " \
<< __FILE__ << "::" \
<< __FUNCTION__ << "(...)" \
<< std::endl; \
lvl++; \
}
#define PING \
{ \
log << SPACE \
<< "PING " \
<< LOCATION \
<< std::endl; \
}
#define PRINT(x) \
{ \
log << SPACE \
<< x << " " \
<< LOCATION \
<< std::endl; \
}
#define RETURN(x) \
{ \
lvl--; \
log << SPACE \
<< "<-- " \
<< __FILE__ << "::" << __FUNCTION__ << "(...)" \
<< std::endl; \
} \
\
return x;
#define END \
{ \
lvl--; \
log << SPACE \
<< "<-- " \
<< __FILE__ << "::" << __FUNCTION__ << "(...)" \
<< std::endl; lvl--; \
} \
}
#endif /*DEBUG*/
#ifndef DEBUG
#define BEGIN {
#define PING
#define PRINT(x)
#define RETURN(x) return x;
#define END }
#endif /*DEBUG*/
#define DELETE(x) delete x; x = 0;
#endif /*DEBUG_HPP__*/
Ye, the indentation above sucks but hope you can understand it.
Code:
#include <fstream>
#include "Debug.hpp"
#ifdef DEBUG
unsigned int lvl = 0;
std::ofstream log("log.log");
#endif /*DEBUG*/
Here is a sample function which makes use of the macros.
Code:
void function(int arg)
BEGIN
PRINT(arg)
END
Thanks in advance, Jimmy.