One Debug-Print function to rule them all

2019-04-13 15:53发布

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?

4条回答
疯言疯语
2楼-- · 2019-04-13 16:28

Rather than branching on the typeid, I'd use a very basic C++ feature that doesn't have any runtime overhead: function overloading! Since you're writing the code to print the function anyways, just seperate it into seperate functions:

void debugPrintMe(ReqCntrlT const& r){
  // ...
}

void debugPrintMe(CallBkAppDataT const& c){
  // ...
}

// others
查看更多
仙女界的扛把子
3楼-- · 2019-04-13 16:36

Since you are already willing to use a template function that checks against typeid(T), you may wish to just specialize implementations of TheOneTrueDebugFunction:

template <class T>
void debugPrintMe(const T& myvar);

template <>
void debugPrintMe< ReqCntrlT* >(T)
{
   // implementation to print debug messages
}

template <>
void debugPrintMe< CallBkAppDataT* >(T)
{
   // implementation
}

As you noticed, you have to play games with typeid(T)==typeid(XYZ*) to get it to print the correct types in response to whether the type is Struct, Struct*, const Struct *, and so on. You'll want to explore using more generic type traits to avoid much more duplicated code.

查看更多
▲ chillily
4楼-- · 2019-04-13 16:39

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 structs. 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;
}
查看更多
爷的心禁止访问
5楼-- · 2019-04-13 16:43

I don't think this is easily doable in a language with no builtin introspection, so you're probably better off just overloading operator<< for each of your structs to print thme to an ostream.

查看更多
登录 后发表回答