Add printf method to Status.

avx_docs
Yu Yang 9 years ago
parent 6c20e08b04
commit 741637eba4

@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#include <stdio.h>
#include <memory>
#include <string>
@ -45,6 +46,28 @@ public:
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.
*/

@ -26,4 +26,9 @@ TEST(Status, testAll) {
paddle::Status status2("error2");
ASSERT_FALSE(status2.isOK());
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());
}

Loading…
Cancel
Save