2014-07-31 00:35:19 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
2014-09-10 01:54:53 +00:00
|
|
|
#pragma once
|
2014-07-31 00:35:19 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2014-08-05 23:13:55 +00:00
|
|
|
namespace osquery {
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A utility class which is used to express the state of operations.
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @code{.cpp}
|
|
|
|
* osquery::Status foobar() {
|
|
|
|
* auto na = doSomeWork();
|
|
|
|
* if (na->itWorked()) {
|
|
|
|
* return osquery::Status(0, "OK");
|
|
|
|
* } else {
|
|
|
|
* return osquery::Status(1, na->getErrorString());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @endcode
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
class Status {
|
2014-08-15 07:25:30 +00:00
|
|
|
public:
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Default constructor
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* Note that the default constructor initialized an osquery::Status instance
|
|
|
|
* to a state such that a successful operation is indicated.
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-09-06 10:41:10 +00:00
|
|
|
Status() : code_(0), message_("OK") {}
|
2014-09-09 18:02:17 +00:00
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A constructor which can be used to concisely express the status of
|
|
|
|
* an operation.
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @param c a status code. The idiom is that a zero status code indicates a
|
|
|
|
* successful operation and a non-zero status code indicates a failed
|
|
|
|
* operation.
|
|
|
|
* @param m a message indicating some extra detail regarding the operation.
|
|
|
|
* If all operations were successful, this message should be "OK".
|
|
|
|
* Otherwise, it doesn't matter what the string is, as long as both the
|
|
|
|
* setter and caller agree.
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
Status(int c, std::string m) : code_(c), message_(m) {}
|
2014-08-15 07:25:30 +00:00
|
|
|
|
|
|
|
public:
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A getter for the status code property
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return an integer representing the status code of the operation.
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-09-09 18:02:17 +00:00
|
|
|
int getCode() const { return code_; }
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A getter for the message property
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return a string representing arbitrary additional information about the
|
|
|
|
* success or failure of an operation. On successful operations, the idiom
|
|
|
|
* is for the message to be "OK"
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-09-09 18:02:17 +00:00
|
|
|
std::string getMessage() const { return message_; }
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A convenience method to check if the return code is 0
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @code{.cpp}
|
|
|
|
* auto s = doSomething();
|
|
|
|
* if (s.ok()) {
|
|
|
|
* LOG(INFO) << "doing work";
|
|
|
|
* } else {
|
|
|
|
* LOG(ERROR) << s.toString();
|
|
|
|
* }
|
|
|
|
* @endcode
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return a boolean which is true if the status code is 0, false otherwise.
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-09-09 18:02:17 +00:00
|
|
|
bool ok() const { return getCode() == 0; }
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A synonym for osquery::Status::getMessage()
|
2014-09-15 20:23:28 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @see getMessage()
|
2014-09-15 20:23:28 +00:00
|
|
|
*/
|
2014-09-09 18:02:17 +00:00
|
|
|
std::string toString() const { return getMessage(); }
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
private:
|
2014-09-15 20:23:28 +00:00
|
|
|
/// the internal storage of the status code
|
2014-08-15 07:25:30 +00:00
|
|
|
int code_;
|
2014-09-15 20:23:28 +00:00
|
|
|
|
|
|
|
/// the internal storage of the status message
|
2014-08-15 07:25:30 +00:00
|
|
|
std::string message_;
|
|
|
|
};
|
2014-08-05 23:13:55 +00:00
|
|
|
}
|