Error.h 3.1 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 <stdio.h>
Y
Yu Yang 已提交
19 20
#include <memory>
#include <string>
21
#include "Compiler.h"
Y
Yu Yang 已提交
22 23 24

namespace paddle {

25
/**
Y
Yu Yang 已提交
26
 * Error is Paddle error code. It only contain a std::string as error message.
Y
Stash  
Yu Yang 已提交
27 28
 *
 *
Y
Yu Yang 已提交
29
 * There are two styles to return error in Paddle.
Y
Stash  
Yu Yang 已提交
30
 *
Y
Yu Yang 已提交
31
 * 1. Return Error
32 33 34
 *    When method return a status, the return must use `__must_check` attribute.
 *    Example as below.
 * @code{cpp}
Y
Yu Yang 已提交
35
 * Error __must_check foo();
Y
Stash  
Yu Yang 已提交
36
 *
Y
Yu Yang 已提交
37
 * Error __must_check bar() {
38 39
 *   // do something.
 *   Status s = foo();  // invoke other method return status.
Y
Yu Yang 已提交
40
 *   if (!s) return s;
41 42 43 44
 *   // do something else.
 *   return Status();
 * }
 * @endcode{cpp}
Y
Stash  
Yu Yang 已提交
45
 *
46
 * 2. Return by parameter.
Y
Yu Yang 已提交
47
 *    It is another way to return an error, by using a pointer parameter.
48 49 50
 *    Example as below.
 *
 * @code{cpp}
Y
Yu Yang 已提交
51
 * Error bar();
52
 *
Y
Yu Yang 已提交
53
 * int foo(Error* error) {
54
 *   // Do something.
Y
Yu Yang 已提交
55 56 57
 *   Error s = bar();
 *   if (!s) {
 *     *error = s;
58 59 60 61
 *     return 0;
 *   }
 *   // Do something else.
 *   if (someInternalErrorHappend) {
Y
Yu Yang 已提交
62
 *     *error = Error("Some dimension is too large, %d", dimension);
63 64 65 66 67 68
 *     return 0;
 *   }
 *   // End of method.
 *   return someValue;
 * }
 *
Y
Yu Yang 已提交
69 70
 * Error foobar() {
 *   Error s;
71 72
 *   // do something.
 *   foo(&s);
Y
Yu Yang 已提交
73
 *   if (!s) return s;
74 75 76 77 78 79 80
 * }
 * @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.
81
 */
Y
Yu Yang 已提交
82
class Error final {
Y
Yu Yang 已提交
83
public:
84 85 86
  /**
   * Default Status. OK
   */
Y
Yu Yang 已提交
87
  inline Error() {}
Y
Yu Yang 已提交
88

89
  /**
Y
Yu Yang 已提交
90
   * @brief Create an Error use printf syntax.
91
   */
Y
Yu Yang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  inline explicit Error(const char* fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    constexpr size_t kBufferSize = 1024;
    this->errMsg_.reset(new std::string(kBufferSize, 0));
    auto sz = vsnprintf(&(*errMsg_)[0], kBufferSize, fmt, ap);
    this->errMsg_->resize(sz);
    this->errMsg_->shrink_to_fit();
    va_end(ap);
  }

  /**
   * @brief what will return the error message. If no error, return nullptr.
   */
  inline const char* msg() const {
Y
Yu Yang 已提交
107 108 109 110 111 112 113
    if (errMsg_) {
      return errMsg_->data();
    } else {
      return nullptr;
    }
  }

114
  /**
Y
Yu Yang 已提交
115
   * @brief operator bool, return True if there is no error.
116
   */
Y
Yu Yang 已提交
117
  inline operator bool() const { return !errMsg_; }
118 119 120 121 122
  /**
   * @brief check this status by glog.
   * @note It is a temp method used during cleaning Paddle code. It will be
   *       removed later.
   */
Y
Yu Yang 已提交
123
  inline void check() const { CHECK(*this) << msg(); }
Y
Yu Yang 已提交
124

Y
Yu Yang 已提交
125
private:
126
  std::shared_ptr<std::string> errMsg_;
Y
Yu Yang 已提交
127 128 129
};

}  // namespace paddle