Error.h 3.0 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 {
Y
Yu Yang 已提交
83
public:
84
  /**
Y
Yu Yang 已提交
85
   * Construct an no-error value.
86
   */
Y
Yu Yang 已提交
87
  Error() {}
Y
Yu Yang 已提交
88

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

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

113
  /**
Y
Yu Yang 已提交
114
   * @brief operator bool, return True if there is no error.
115
   */
Y
Yu Yang 已提交
116
  operator bool() const { return !msg_; }
117 118 119 120 121
  /**
   * @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 已提交
122
  void check() const { CHECK(*this) << msg(); }
Y
Yu Yang 已提交
123

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

}  // namespace paddle