一个调试打印功能来统治它们 [英] One Debug-Print function to rule them all
问题描述
我在这种情况下,我有几个不同的结构,在我的代码,我想打印到控制台。
I'm in the situation that i have several different structs around in my code, that i want to print to the console.
三个例子
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;
所以我想要的是一个调试函数,它只需要一个struct作为参数,所有成员的结构,如此:
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?
推荐答案
是的,有一个更优雅的方式来做这个code> ... else if(typeid(T)== ... ?Yuck!)你可以写一些 operator< / code>你的
结构
s。这使得你的 debugPrintMe()
函数nice和generic允许您将结构流传输到 cout
, cerr
,记录器, ostringstream
,...
Yes there most certainly is a more elegant way of doing this (... else if (typeid(T) == ...
? Yuck!). You could write some operator <<()
s for your struct
s. This makes your debugPrintMe()
function nice and generic and also allows you to stream your structs to cout
, cerr
, a logger, an ostringstream
, ...
这是一个让你开始的例子:
Here's an example to get you started:
std::ostream& operator <<(std::ostream& os, const ReqCntrlT& r)
{
os << "reqControl"
<< "\n\tconnectionID: " << r.connectionID
<< "\n\tdbApplID: " << r.dbApplID
<< "\n\tappDescr: " << r.appDescr
<< "\n\treqID: " << r.reqID
<< "\n\tresubmitFlag: " << r.resubmitFlag
<< "\n\tresubmitNo: " << r.resubmitNo
<< "\n\tVCIver: " << r.VCIver
<< "\n\tloginID: " << r.loginID
<< '\n';
return os;
}
template <class T>
void debugPrintMe(const T& myvar)
{
if (DEBUG)
{
std::cout << myvar << std::endl;
}
}
int main()
{
ReqCntrlT r;
// [...]
debugPrintMe(r);
return 0;
}
这篇关于一个调试打印功能来统治它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!