在项目开发中,参考apollo设计了一个诊断类,可以作为返回值和输出错误信息使用,
enum ErrorCode {
OK=0,
NewSubClassFailed = -1,
CreateNodeFailed = -2,
CreateReaderFailed = -3,
MessageTypeIsEmpty = -4,
ChannelNameOrNodeNameIsEmpty = -5,
NoCloseChannel = -6
};
class Status {
public :
Status()=default;
explicit Status(ErrorCode code = ErrorCode::OK, std::string msg = "")
: code_(code),msg_(msg) {
s_code_=code_;
if (this->code_==ErrorCode::OK)
{
LOG(INFO)<<this->msg_<<std::endl;
}
else
{
LOG(ERROR)<<static_cast<int>(code_)<<": "<<this->msg_<<std::endl;
}
}
~Status()=default;
static bool getcode()
{
return (s_code_==ErrorCode::OK);
}
static ErrorCode s_code_;
private:
ErrorCode code_;
std::string msg_;
};