I'm in the situation that i have several different structs around in my code, that i want to print to the console.
Three examples (of a few hundred):
typedef struct ReqCntrlT /* Request control record */
{
int connectionID;
int dbApplID;
char appDescr[MAX_APPDSCR];
int reqID;
int resubmitFlag;
unsigned int resubmitNo;
char VCIver[MAX_VCIVER];
int loginID;
} ReqCntrlT;
//---------------------------------------------
typedef struct /* Connection request data block */
{
char userID[MAX_USRID];
char password[MAX_PWDID];
} CnctReqDataT;
//---------------------------------------------
typedef struct {
char userID[LOGIN_MAX_USERID];
char closure;
int applVersion;
int authorizationDataLength;
void *authorizationData; } LoginReqDataT;
So what i want to have, is a debug function that simply takes a struct as Parameter and puts out all members of the struct, as so:
LoginReqDataT* foo = new LoginReqDataT;
foo->applVersion = 123;
//...
debugPrintMe(foo);
CnctReqDataT* bar = new CnctReqDataT;
strcpy(bar->userID, "123");
strcpy(bar->password, "mypwd");
debugPrintMe(bar);
What I currently have, is an endless function which is doing stuff like this:
template <class T>
void debugPrintMe(T myvar)
{
if (!DEBUG) return;
if (typeid(T) == typeid(ReqCntrlT*))
{
ReqCntrlT* r = (ReqCntrlT*)myvar;
cout << "reqControl: " << endl
<< "\tconnectionID: " << r->connectionID << endl
<< "\tdbApplID: " << r->dbApplID << endl
//...
<< "\tloginID: " << r->loginID << endl << endl;
}
else if (typeid(T) == typeid(CallBkAppDataT*))
{
CallBkAppDataT* c = (CallBkAppDataT*)myvar;
cout << "appData: " << endl
<< "\tappRespBlockSize " << c->appRespBlockSize << endl
//...
<< "\tstreamType: " << c->streamType << endl << endl;
}
//... and so on
}
Is there a more elegant way to do this?