Qt 5 Logging - definig category with QLoggingCategory -


i have problem qloggingcategory(const char * category).

when use like:

qstring rt = "3"; qstring sv = "p"; qloggingcategory dailylogger(qstring(rt+sv+"logger").tostdstring().c_str()); 

it doesn't work (my custom message handler doesn't recognize category).

but when use:

qloggingcategory dailylogger("3plogger"); 

message handler sees category.

here's handler function:

void mymessageoutput(qtmsgtype type, const qmessagelogcontext &context, const qstring &msg) {     if (qstring(context.category).contains("3"))     {         //some code     } } 

why computed category name not work?

qloggingcategory retains copy of char* pointer it's constructed. pointed-to characters must remain valid , constant life of qloggingcategory instance.

in failing example, c_str() returns temporary no longer valid after std::string goes out of scope. qloggingcategory pointing @ invalid memory (giving undefined behaviour).

in second example, pass string literal, valid lifetime of program, no lifetime issues arise.

you need copy string data char array lifetime outlives logging category.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -