syslog const char * to string

时间:2010-11-14 08:24:09

标签: c++ string pointers const

   catch (exception e) { syslog (LOG_ERR, "exception: " + e.what());    }

这是我想要做的事情,而且它不起作用,我尝试过使用这个

string ctos(const char& c){

    stringstream s;
    s << c;

    return s.str();
}

但仍然输了

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

使用std::string的{​​{1}}构造函数,请参阅reference。因此,您的日志记录代码可以按如下方式更正:

const char*

答案 1 :(得分:3)

试试这个

catch (exception e) 
{ 
         syslog (LOG_ERR, "exception: %s" ,e.what());   
}

答案 2 :(得分:2)

如果您使用syslog(3),则定义为:

void syslog(int priority, const char *message, ...);

您要使用printf - 类似syslog的功能:

catch (const std::exception& e) 
{ 
    syslog(LOG_ERR, "exception: %s", e.what());
}

或者您可以使用std::string成员函数将const char*转换为std::string::c_str

catch (const std::exception& e)
{
    std::string message = std::string("exception: ") + e.what();
    syslog(LOG_ERR, message.c_str());
}

另一个注意事项,通过const-reference捕获,否则实际抛出的异常对象很有可能获得sliced