mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Reorganising the repository - external renames and moves (#4074)
* Adding first rough ouline of the repository structure * Remove old CI stuff * add title * formatting fixes * move node-exits job's script to scripts dir * Move docs into subdir * move to bin * move maintainence scripts, configs and helpers into its own dir * add .local to ignore * move core->client * start up 'test' area * move test client * move test runtime * make test move compile * Add dependencies rule enforcement. * Fix indexing. * Update docs to reflect latest changes * Moving /srml->/paint * update docs * move client/sr-* -> primitives/ * clean old readme * remove old broken code in rhd * update lock * Step 1. * starting to untangle client * Fix after merge. * start splitting out client interfaces * move children and blockchain interfaces * Move trie and state-machine to primitives. * Fix WASM builds. * fixing broken imports * more interface moves * move backend and light to interfaces * move CallExecutor * move cli off client * moving around more interfaces * re-add consensus crates into the mix * fix subkey path * relieve client from executor * starting to pull out client from grandpa * move is_decendent_of out of client * grandpa still depends on client directly * lemme tests pass * rename srml->paint * Make it compile. * rename interfaces->client-api * Move keyring to primitives. * fixup libp2p dep * fix broken use * allow dependency enforcement to fail * move fork-tree * Moving wasm-builder * make env * move build-script-utils * fixup broken crate depdencies and names * fix imports for authority discovery * fix typo * update cargo.lock * fixing imports * Fix paths and add missing crates * re-add missing crates
This commit is contained in:
committed by
Bastian Köcher
parent
becc3b0a4f
commit
60e5011c72
@@ -0,0 +1,191 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Custom panic hook with bug report link
|
||||
//!
|
||||
//! This crate provides the [`set`] function, which wraps around [`std::panic::set_hook`] and
|
||||
//! sets up a panic hook that prints a backtrace and invites the user to open an issue to the
|
||||
//! given URL.
|
||||
//!
|
||||
//! By default, the panic handler aborts the process by calling [`std::process::exit`]. This can
|
||||
//! temporarily be disabled by using an [`AbortGuard`].
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use std::io::{self, Write};
|
||||
use std::marker::PhantomData;
|
||||
use std::panic::{self, PanicInfo};
|
||||
use std::cell::Cell;
|
||||
use std::thread;
|
||||
|
||||
thread_local! {
|
||||
static ON_PANIC: Cell<OnPanic> = Cell::new(OnPanic::Abort);
|
||||
}
|
||||
|
||||
/// Panic action.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum OnPanic {
|
||||
/// Abort when panic occurs.
|
||||
Abort,
|
||||
/// Unwind when panic occurs.
|
||||
Unwind,
|
||||
/// Always unwind even if someone changes strategy to Abort afterwards.
|
||||
NeverAbort,
|
||||
}
|
||||
|
||||
/// Set the panic hook.
|
||||
///
|
||||
/// Calls [`std::panic::set_hook`] to set up the panic hook.
|
||||
///
|
||||
/// The `bug_url` parameter is an invitation for users to visit that URL to submit a bug report
|
||||
/// in the case where a panic happens.
|
||||
pub fn set(bug_url: &'static str, version: &str) {
|
||||
panic::set_hook(Box::new({
|
||||
let version = version.to_string();
|
||||
move |c| {
|
||||
panic_hook(c, bug_url, &version)
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
macro_rules! ABOUT_PANIC {
|
||||
() => ("
|
||||
This is a bug. Please report it at:
|
||||
|
||||
{}
|
||||
")}
|
||||
|
||||
/// Set aborting flag. Returns previous value of the flag.
|
||||
fn set_abort(on_panic: OnPanic) -> OnPanic {
|
||||
ON_PANIC.with(|val| {
|
||||
let prev = val.get();
|
||||
match prev {
|
||||
OnPanic::Abort | OnPanic::Unwind => val.set(on_panic),
|
||||
OnPanic::NeverAbort => (),
|
||||
}
|
||||
prev
|
||||
})
|
||||
}
|
||||
|
||||
/// RAII guard for whether panics in the current thread should unwind or abort.
|
||||
///
|
||||
/// Sets a thread-local abort flag on construction and reverts to the previous setting when dropped.
|
||||
/// Does not implement `Send` on purpose.
|
||||
///
|
||||
/// > **Note**: Because we restore the previous value when dropped, you are encouraged to leave
|
||||
/// > the `AbortGuard` on the stack and let it destroy itself naturally.
|
||||
pub struct AbortGuard {
|
||||
/// Value that was in `ABORT` before we created this guard.
|
||||
previous_val: OnPanic,
|
||||
/// Marker so that `AbortGuard` doesn't implement `Send`.
|
||||
_not_send: PhantomData<std::rc::Rc<()>>
|
||||
}
|
||||
|
||||
impl AbortGuard {
|
||||
/// Create a new guard. While the guard is alive, panics that happen in the current thread will
|
||||
/// unwind the stack (unless another guard is created afterwards).
|
||||
pub fn force_unwind() -> AbortGuard {
|
||||
AbortGuard {
|
||||
previous_val: set_abort(OnPanic::Unwind),
|
||||
_not_send: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new guard. While the guard is alive, panics that happen in the current thread will
|
||||
/// abort the process (unless another guard is created afterwards).
|
||||
pub fn force_abort() -> AbortGuard {
|
||||
AbortGuard {
|
||||
previous_val: set_abort(OnPanic::Abort),
|
||||
_not_send: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new guard. While the guard is alive, panics that happen in the current thread will
|
||||
/// **never** abort the process (even if `AbortGuard::force_abort()` guard will be created afterwards).
|
||||
pub fn never_abort() -> AbortGuard {
|
||||
AbortGuard {
|
||||
previous_val: set_abort(OnPanic::NeverAbort),
|
||||
_not_send: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for AbortGuard {
|
||||
fn drop(&mut self) {
|
||||
set_abort(self.previous_val);
|
||||
}
|
||||
}
|
||||
|
||||
/// Function being called when a panic happens.
|
||||
fn panic_hook(info: &PanicInfo, report_url: &'static str, version: &str) {
|
||||
let location = info.location();
|
||||
let file = location.as_ref().map(|l| l.file()).unwrap_or("<unknown>");
|
||||
let line = location.as_ref().map(|l| l.line()).unwrap_or(0);
|
||||
|
||||
let msg = match info.payload().downcast_ref::<&'static str>() {
|
||||
Some(s) => *s,
|
||||
None => match info.payload().downcast_ref::<String>() {
|
||||
Some(s) => &s[..],
|
||||
None => "Box<Any>",
|
||||
}
|
||||
};
|
||||
|
||||
let thread = thread::current();
|
||||
let name = thread.name().unwrap_or("<unnamed>");
|
||||
|
||||
let backtrace = Backtrace::new();
|
||||
|
||||
let mut stderr = io::stderr();
|
||||
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(stderr, "====================");
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(stderr, "Version: {}", version);
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(stderr, "{:?}", backtrace);
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(
|
||||
stderr,
|
||||
"Thread '{}' panicked at '{}', {}:{}",
|
||||
name, msg, file, line
|
||||
);
|
||||
|
||||
let _ = writeln!(stderr, ABOUT_PANIC!(), report_url);
|
||||
ON_PANIC.with(|val| {
|
||||
if val.get() == OnPanic::Abort {
|
||||
::std::process::exit(1);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn does_not_abort() {
|
||||
set("test", "1.2.3");
|
||||
let _guard = AbortGuard::force_unwind();
|
||||
::std::panic::catch_unwind(|| panic!()).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn does_not_abort_after_never_abort() {
|
||||
set("test", "1.2.3");
|
||||
let _guard = AbortGuard::never_abort();
|
||||
let _guard = AbortGuard::force_abort();
|
||||
std::panic::catch_unwind(|| panic!()).ok();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user