Questa versione per C non mi piace molto. Meglio questa per C++:
codice:
#include <iostream>
#ifdef __GNUC__
extern "C"
{
#include <sys/types.h>
}
#else
typedef unsigned long u_long;
#define __attribute__(x)
#endif
class Debugging
{
public:
Debugging(const char*, const u_long, const u_long, const char*) throw();
~Debugging(void) throw();
void debug(void) throw() __attribute__((noreturn, weak));
private:
// evita la copia
Debugging(const Debugging&);
Debugging operator=(Debugging);
char *name;
u_long numline;
u_long numargs;
char *valret;
};
Debugging::Debugging(const char *n, const u_long nu, const u_long ar, const char*val) throw() :
numline(nu), numargs(ar)
{
u_long namecounter(0), valcounter(0);
while (n[namecounter++]);
while (val[valcounter++]);
name = new char[namecounter + 1];
valret = new char[valcounter + 1];
for (int i(0); i != namecounter; ++i)
name[i] = n[i];
for (int i(0); i != valcounter; ++i)
valret[i] = val[i];
name[namecounter] = '\0';
valret[valcounter] = '\0';
}
Debugging::~Debugging() throw()
{
delete[] name;
delete[] valret;
}
void Debugging::debug() throw()
{
std::cout << "nome della funzione:\t" << name << std::endl;
std::cout << "numero di argomenti:\t" << numline << std::endl;
std::cout << "numero di linee:\t" << numargs << std::endl;
std::cout << "valore di ritorno:\t" << valret << std::endl;
_exit(1);
}
Come avrai ben capito gli stub del preprocessore servono per farla funzionare bene anche su altri ambienti fuori da gcc.
si usa cosi:
codice:
int main()
{
Debugging var("main", 7, 0, "int");
int a = 10;
// ammesso che a == 10 indica un errore
if (a == 10)
var.debug();
}
Ok?
bye