提交 741637eb 编写于 作者: Y Yu Yang

Add printf method to Status.

上级 6c20e08b
...@@ -14,6 +14,7 @@ limitations under the License. */ ...@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once #pragma once
#include <stdio.h>
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -45,6 +46,28 @@ public: ...@@ -45,6 +46,28 @@ public:
errMsg_.reset(new std::string(msg)); errMsg_.reset(new std::string(msg));
} }
/**
* @brief set a error message for status. Use C style printf
* @param fmt
*/
template <typename... ARGS>
inline void setByPrintf(const char* fmt, ARGS... args) noexcept {
constexpr size_t bufferSize = 4096;
char buffer[bufferSize];
snprintf(buffer, bufferSize, fmt, args...);
errMsg_.reset(new std::string(buffer));
}
/**
* create a error status by C style printf.
*/
template <typename... ARGS>
inline static Status printf(const char* fmt, ARGS... args) noexcept {
Status s;
s.setByPrintf(fmt, args...);
return s;
}
/** /**
* @brief what will return the error message. If status is OK, return nullptr. * @brief what will return the error message. If status is OK, return nullptr.
*/ */
......
...@@ -26,4 +26,9 @@ TEST(Status, testAll) { ...@@ -26,4 +26,9 @@ TEST(Status, testAll) {
paddle::Status status2("error2"); paddle::Status status2("error2");
ASSERT_FALSE(status2.isOK()); ASSERT_FALSE(status2.isOK());
ASSERT_STREQ("error2", status2.what()); ASSERT_STREQ("error2", status2.what());
int i = 3;
auto status3 = paddle::Status::printf("error%d", i);
ASSERT_FALSE(status3.isOK());
ASSERT_STREQ("error3", status3.what());
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册