-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to use Error refactor LOG(FATAL)/CHECK #1067
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ limitations under the License. */ | |
|
||
#pragma once | ||
|
||
#include <paddle/utils/Error.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按照 https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes,这里应该写 `#include "paddle/utils/Error.h" 。只有system header files 才用尖括号。 |
||
#include <paddle/utils/Logging.h> | ||
#include <paddle/utils/Util.h> | ||
#include <memory> | ||
|
@@ -32,19 +33,17 @@ class DataConfig; | |
* | ||
* The all operation to TrainerConfig object should use this object. It remove | ||
* many copy & paste code in trainer. | ||
* | ||
* @TODO(yuyang18): Make cmake check compiler support keyword 'final' or not. | ||
* Define a macro to unify 'final' keyword | ||
*/ | ||
class TrainerConfigHelper /*final*/ { | ||
class TrainerConfigHelper final { | ||
public: | ||
DISABLE_COPY(TrainerConfigHelper); | ||
|
||
/** | ||
* @brief Ctor, Create a TrainerConfig from config file | ||
* @param configFilePath Config file path. | ||
*/ | ||
explicit TrainerConfigHelper(const std::string& configFilePath); | ||
explicit TrainerConfigHelper(const std::string& configFilePath) throw( | ||
ErrorPtr&); | ||
explicit TrainerConfigHelper(const TrainerConfig& config); | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少空行。下面有些地方也缺少了空行。 |
||
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. */ | ||
|
||
#include "Error.h" | ||
|
||
namespace paddle { | ||
void paddle::Error::throwError(const std::string &what) throw(ErrorPtr &) { | ||
throw std::unique_ptr<std::exception>(new Error(what)); | ||
} | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少空行。 |
||
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. */ | ||
|
||
#pragma once | ||
#include <memory> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #pragma 和 #include 之间缺少空行 |
||
#include <string> | ||
namespace paddle { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #include 和 namespace 之间缺少空行 |
||
typedef std::unique_ptr<std::exception> ErrorPtr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少空行 |
||
class Error : public std::exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少空行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按照C++ style guide的要求,我们不应该使用exceptions,为什么要定义这个exception type? |
||
public: | ||
explicit inline Error(const std::string& what) noexcept : what_(what) {} | ||
virtual const char* what() const noexcept { return this->what_.c_str(); } | ||
|
||
static void throwError(const std::string& what) throw(ErrorPtr&); | ||
|
||
private: | ||
std::string what_; | ||
}; | ||
|
||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ limitations under the License. */ | |
#include <gflags/gflags.h> | ||
|
||
#include "CustomStackTrace.h" | ||
#include "Error.h" | ||
#include "Logging.h" | ||
#include "StringUtil.h" | ||
#include "Thread.h" | ||
|
@@ -143,7 +144,19 @@ void runInitFunctions() { | |
}); | ||
} | ||
|
||
static void logAllUnhandledExceptions() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果按照style guide不用exceptions的话,貌似也就不需要log exceptions了. |
||
try { | ||
throw; | ||
} catch (ErrorPtr& err) { | ||
std::cerr << err->what() << "\n"; | ||
} catch (...) { | ||
std::cerr << "Unsupported error occured"; | ||
} | ||
exit(1); | ||
} | ||
|
||
void initMain(int argc, char** argv) { | ||
std::set_terminate(logAllUnhandledExceptions); | ||
initializeLogging(argc, argv); | ||
installLayerStackTracer(); | ||
std::string line; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://google.github.io/styleguide/cppguide.html#Exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不过似乎构造函数里的Error,就只能用Exception返回了。。我再想想其他办法。