syslog const char* to string
catch (exception e) { syslog (LOG_ERR, "exception: " + e.what()); }
Here is what im tryin开发者_JAVA百科g to do, And it aint working, ive tried using this
string ctos(const char& c){
stringstream s;
s << c;
return s.str();
}
But still lost
Any help would be apreciated Thanks.
Try this
catch (exception e)
{
syslog (LOG_ERR, "exception: %s" ,e.what());
}
Use the std::string
constructor taking const char*
, see reference. So your logging code can be corrected as follows:
catch (const exception& e) {
syslog (LOG_ERR, (std::string("exception: ") + e.what()).c_str());
}
If you are using syslog(3)
then it is defined as:
void syslog(int priority, const char *message, ...);
Either you want to use printf
-like functionality of syslog
:
catch (const std::exception& e)
{
syslog(LOG_ERR, "exception: %s", e.what());
}
Or you can convert the std::string
to const char*
, using the std::string::c_str
member function:
catch (const std::exception& e)
{
std::string message = std::string("exception: ") + e.what();
syslog(LOG_ERR, message.c_str());
}
Another note, catch by const-reference, else it is a good chance that the actual thrown exception object gets sliced.
精彩评论