Error.h 3.5 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14 15 16

#pragma once

17
#include <glog/logging.h>
Y
Yu Yang 已提交
18
#include <stdarg.h>
Y
Yu Yang 已提交
19
#include <stdio.h>
Y
Yu Yang 已提交
20 21
#include <memory>
#include <string>
Y
Yu Yang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * __must_check macro. It make the function's return value must be used,
 * otherwise it will raise a compile warning. And also Paddle treat all compile
 * warnings as errors.
 */
#ifdef __GNUC__
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 30400
#define __must_check __attribute__((warn_unused_result))
#else
#define __must_check
#endif
#else
#define __must_check
#endif
Y
Yu Yang 已提交
37 38 39

namespace paddle {

40
/**
Y
Yu Yang 已提交
41
 * Error is Paddle error code. It only contain a std::string as error message.
Y
Stash  
Yu Yang 已提交
42 43
 *
 *
Y
Yu Yang 已提交
44
 * There are two styles to return error in Paddle.
Y
Stash  
Yu Yang 已提交
45
 *
Y
Yu Yang 已提交
46
 * 1. Return Error
47 48 49
 *    When method return a status, the return must use `__must_check` attribute.
 *    Example as below.
 * @code{cpp}
Y
Yu Yang 已提交
50
 * Error __must_check foo();
Y
Stash  
Yu Yang 已提交
51
 *
Y
Yu Yang 已提交
52
 * Error __must_check bar() {
53
 *   // do something.
54 55
 *   Error err = foo();  // invoke other method return status.
 *   if (err) return err;
56
 *   // do something else.
57
 *   return Error();
58 59
 * }
 * @endcode{cpp}
Y
Stash  
Yu Yang 已提交
60
 *
61
 * 2. Return by parameter.
Y
Yu Yang 已提交
62
 *    It is another way to return an error, by using a pointer parameter.
63 64 65
 *    Example as below.
 *
 * @code{cpp}
Y
Yu Yang 已提交
66
 * Error bar();
67
 *
Y
Yu Yang 已提交
68
 * int foo(Error* error) {
69
 *   // Do something.
70 71
 *   Error err = bar();
 *   if (err) {
Y
Yu Yang 已提交
72
 *     *error = s;
73 74 75 76
 *     return 0;
 *   }
 *   // Do something else.
 *   if (someInternalErrorHappend) {
Y
Yu Yang 已提交
77
 *     *error = Error("Some dimension is too large, %d", dimension);
78 79 80 81 82 83
 *     return 0;
 *   }
 *   // End of method.
 *   return someValue;
 * }
 *
Y
Yu Yang 已提交
84
 * Error foobar() {
85
 *   Error err;
86
 *   // do something.
87 88
 *   foo(&err);
 *   if (err) return err;
89 90 91 92 93 94 95
 * }
 * @endcode{cpp}
 *
 *
 * Currently there is a helper method 'check' in status, because Paddle always
 * use log(FATAL) or CHECK to make program exit before. When we clean all
 * log(FATAL) and CHECK in Paddle, 'check' method will be removed.
96
 */
Y
Yu Yang 已提交
97
class Error {
Y
Yu Yang 已提交
98
public:
99
  /**
Y
Yu Yang 已提交
100
   * Construct a no-error value.
101
   */
Y
Yu Yang 已提交
102
  Error() {}
Y
Yu Yang 已提交
103

104
  /**
Y
Yu Yang 已提交
105
   * @brief Create an Error use printf syntax.
106
   */
Y
Yu Yang 已提交
107
  explicit Error(const char* fmt, ...) {
Y
Yu Yang 已提交
108 109 110
    va_list ap;
    va_start(ap, fmt);
    constexpr size_t kBufferSize = 1024;
Y
Yu Yang 已提交
111 112 113
    char buffer[kBufferSize];
    vsnprintf(buffer, kBufferSize, fmt, ap);
    this->msg_.reset(new std::string(buffer));
Y
Yu Yang 已提交
114 115 116 117
    va_end(ap);
  }

  /**
Y
Yu Yang 已提交
118
   * @brief msg will return the error message. If no error, return nullptr.
Y
Yu Yang 已提交
119
   */
Y
Yu Yang 已提交
120 121 122
  const char* msg() const {
    if (msg_) {
      return msg_->c_str();
Y
Yu Yang 已提交
123 124 125 126 127
    } else {
      return nullptr;
    }
  }

128
  /**
Y
Yu Yang 已提交
129 130 131
   * @brief check this status by glog.
   * @note It is a temp method used during cleaning Paddle code. It will be
   *       removed later.
132
   */
Y
Yu Yang 已提交
133
  void check() const { CHECK(this->isOK()) << msg(); }
Y
Yu Yang 已提交
134

135 136 137 138 139
  /**
   * @brief isOK return True if there is no error.
   * @return True if no error.
   */
  bool isOK() const { return msg_ == nullptr; }
Y
Yu Yang 已提交
140

Y
Yu Yang 已提交
141
private:
Y
Yu Yang 已提交
142
  std::shared_ptr<std::string> msg_;
Y
Yu Yang 已提交
143 144 145
};

}  // namespace paddle