-
Notifications
You must be signed in to change notification settings - Fork 18
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
Exception stack support and stacktrace improvements #24
Closed
rhoneyager-tomorrow
wants to merge
6
commits into
JCSDA:develop
from
rhoneyager-tomorrow:feature/stacktrace
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
095e4df
Adding stacktrace support
rhoneyager-tomorrow a21ff2e
Merge remote-tracking branch 'origin/develop' into feature/stacktrace
rhoneyager-tomorrow 21e3e43
.
rhoneyager-tomorrow 10b238a
Implemented a stacktrace exporter in oops.
rhoneyager-tomorrow a77f28c
oops applications should walk the C++ exception stack on error.
rhoneyager-tomorrow 19f1506
Coding norms
rhoneyager-tomorrow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* (C) Copyright 2024 The Tomorrow Companies, Inc. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
#ifndef BOOST_STACKTRACE_USE_NOOP | ||
# include <boost/stacktrace.hpp> | ||
#endif | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include "oops/util/Stacktrace.h" | ||
|
||
namespace util { | ||
|
||
std::string stacktrace_current() { | ||
#ifndef BOOST_STACKTRACE_USE_NOOP | ||
std::ostringstream s; | ||
s << boost::stacktrace::stacktrace() << std::endl; | ||
std::string sout = s.str(); | ||
return sout; | ||
#else | ||
return "Stacktrace not available"; | ||
#endif | ||
} | ||
|
||
void unwind_exception_stack(const std::exception& e, std::ostream& out, int level) { | ||
out << "Exception: level: " << level << "\n" << e.what() << std::endl; | ||
try { | ||
std::rethrow_if_nested(e); | ||
} catch (const std::exception& f) { | ||
unwind_exception_stack(f, out, level + 1); | ||
} catch (...) { | ||
out << "exception: level: " << level | ||
<< "\n\tException at this level is not derived from std::exception." << std::endl; | ||
} | ||
} | ||
|
||
} // end namespace util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* (C) Copyright 2024 The Tomorrow Companies, Inc. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
#ifndef OOPS_UTIL_STACKTRACE_H_ | ||
#define OOPS_UTIL_STACKTRACE_H_ | ||
|
||
#include <exception> | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace util { | ||
|
||
/// @brief This is a basic backport of C++23's stacktrace feature. | ||
/// @details This uses Boost instead of eckit's Backtrace code because | ||
/// the eckit implementation has a lot of platform-specific code | ||
/// and libstdc++-related code for symbol demangling that is more | ||
/// generalized by Boost. Boost's stacktrace code also provides | ||
/// line numbers whenever possible. | ||
/// @returns A string with the current stacktrace. | ||
std::string stacktrace_current(); | ||
|
||
/// @brief Print details of the exception stack. | ||
/// @details This C++11 feature was added after eckit's Exception classes | ||
/// were written. It is more general, as all of the JEDI-related exceptions | ||
/// ultimately derive from std::exception, including the eckit-related ones. | ||
/// @param e is the exception. | ||
/// @param out is the output stream. | ||
/// @param level denotes current depth in the stack. Used because | ||
/// unwind_exception_stack is a recursive function. | ||
void unwind_exception_stack(const std::exception& e, std::ostream& out = std::cerr, int level = 0); | ||
|
||
} // namespace util | ||
|
||
#endif // OOPS_UTIL_STACKTRACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* (C) Copyright 2021-2023 UCAR | ||
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. 2024 |
||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
/* | ||
* This test deliberately produces a stacktrace. | ||
*/ | ||
|
||
#include "oops/util/Stacktrace.h" | ||
|
||
#include <iostream> | ||
|
||
int main(int argc, char **argv) { | ||
std::cout << "Stacktrace:\n" << util::stacktrace_current() << std::endl; | ||
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. Just a suggestion but you could use |
||
return 0; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could be
const
.