Error.h 3.2 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
#include "paddle/platform/must_check.h"
Y
Yu Yang 已提交
23 24 25

namespace paddle {

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

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

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

114
  /**
115
   * @brief operator bool, return True if there is something error.
116
   */
117
  operator bool() const { return !this->isOK(); }
Y
Yu Yang 已提交
118

119 120 121 122 123
  /**
   * @brief isOK return True if there is no error.
   * @return True if no error.
   */
  bool isOK() const { return msg_ == nullptr; }
Y
Yu Yang 已提交
124

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

Y
Yu Yang 已提交
132
private:
Y
Yu Yang 已提交
133
  std::shared_ptr<std::string> msg_;
Y
Yu Yang 已提交
134 135 136
};

}  // namespace paddle