Skip to content

Commit

Permalink
feat: add panic hook to log panic backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Zvicii committed Dec 29, 2023
1 parent b60b725 commit 45f210a
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 8 deletions.
192 changes: 192 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
name: ne-s3-actions
on: [push, pull_request]
jobs:
x86_64-pc-windows-msvc:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: x86_64-pc-windows-msvc
- run: cargo build --release --target x86_64-pc-windows-msvc
- uses: actions/upload-artifact@v4
with:
name: x86_64-pc-windows-msvc
path: |
target/x86_64-pc-windows-msvc/release/ne_s3.dll
target/x86_64-pc-windows-msvc/release/ne_s3.dll.lib
target/x86_64-pc-windows-msvc/release/ne_s3.pdb
target/x86_64-pc-windows-msvc/release/ne-s3-client.exe
i686-pc-windows-msvc:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: i686-pc-windows-msvc
- run: cargo build --release --target i686-pc-windows-msvc
- uses: actions/upload-artifact@v4
with:
name: i686-pc-windows-msvc
path: |
resources/cacert.pem
resources/ne_s3.h
target/i686-pc-windows-msvc/release/ne_s3.dll
target/i686-pc-windows-msvc/release/ne_s3.dll.lib
target/i686-pc-windows-msvc/release/ne_s3.pdb
target/i686-pc-windows-msvc/release/ne-s3-client.exe
x86_64-unknown-linux-gnu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: x86_64-unknown-linux-gnu
- run: cargo build --release --target x86_64-unknown-linux-gnu
- uses: actions/upload-artifact@v4
with:
name: x86_64-unknown-linux-gnu
path: |
resources/cacert.pem
resources/ne_s3.h
target/x86_64-unknown-linux-gnu/release/libne_s3.so
target/x86_64-unknown-linux-gnu/release/ne-s3-client
aarch64-unknown-linux-gnu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-unknown-linux-gnu
- run: cargo build --release --target aarch64-unknown-linux-gnu
- uses: actions/upload-artifact@v4
with:
name: aarch64-unknown-linux-gnu
path: |
resources/cacert.pem
resources/ne_s3.h
target/aarch64-unknown-linux-gnu/release/libne_s3.so
target/aarch64-unknown-linux-gnu/release/ne-s3-client
x86_64-apple-darwin:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: x86_64-apple-darwin
- run: cargo build --release --target x86_64-apple-darwin
- uses: actions/upload-artifact@v4
with:
name: x86_64-apple-darwin
path: |
resources/cacert.pem
resources/ne_s3.h
target/x86_64-apple-darwin/release/libne_s3.dylib
target/x86_64-apple-darwin/release/ne-s3-client
aarch64-apple-darwin:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-apple-darwin
- run: cargo build --release --target aarch64-apple-darwin
- uses: actions/upload-artifact@v4
with:
name: aarch64-apple-darwin
path: |
resources/cacert.pem
resources/ne_s3.h
target/aarch64-apple-darwin/release/libne_s3.dylib
target/aarch64-apple-darwin/release/ne-s3-client
aarch64-linux-android:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-linux-android
- run: cargo build --release --target aarch64-linux-android
- uses: actions/upload-artifact@v4
with:
name: aarch64-linux-android
path: |
resources/cacert.pem
resources/ne_s3.h
target/aarch64-linux-android/release/libne_s3.so
target/aarch64-linux-android/release/ne-s3-client
armv7-linux-androideabi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: armv7-linux-androideabi
- run: cargo build --release --target armv7-linux-androideabi
- uses: actions/upload-artifact@v4
with:
name: armv7-linux-androideabi
path: |
resources/cacert.pem
resources/ne_s3.h
target/armv7-linux-androideabi/release/libne_s3.so
target/armv7-linux-androideabi/release/ne-s3-client
aarch64-apple-ios:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-apple-ios
- run: cargo build --release --target aarch64-apple-ios
- uses: actions/upload-artifact@v4
with:
name: aarch64-apple-ios
path: |
resources/cacert.pem
resources/ne_s3.h
target/aarch64-apple-ios/release/libne_s3.dylib
target/aarch64-apple-ios/release/ne-s3-client
aarch64-apple-ios-sim:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-apple-ios-sim
- run: cargo build --release --target aarch64-apple-ios-sim
- uses: actions/upload-artifact@v4
with:
name: aarch64-apple-ios-sim
path: |
resources/cacert.pem
resources/ne_s3.h
target/aarch64-apple-ios-sim/release/libne_s3.dylib
target/aarch64-apple-ios-sim/release/ne-s3-client
x86_64-apple-ios:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: x86_64-apple-ios
- run: cargo build --release --target x86_64-apple-ios
- uses: actions/upload-artifact@v4
with:
name: x86_64-apple-ios
path: |
resources/cacert.pem
resources/ne_s3.h
target/x86_64-apple-ios/release/libne_s3.dylib
target/x86_64-apple-ios/release/ne-s3-client
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ path = "src/lib.rs"
crate-type = ["cdylib", "rlib"]

[[bin]]
name = "ne-s3"
name = "ne-s3-client"
path = "src/main.rs"
41 changes: 34 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! simple s3 client with C interfaces
pub use basic::S3Params;
use flexi_logger::{with_thread, Age, Cleanup, Criterion, FileSpec, Logger, Naming, WriteMode};
use log::{error, info};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{sync::{Mutex, Arc}, path::Path};
use std::{
path::Path,
sync::{Arc, Mutex},
};
use sysinfo::System;
use urlencoding::decode;
pub use basic::S3Params;
mod basic;
mod download;
mod upload;
Expand All @@ -27,14 +30,26 @@ pub fn init(params: String) {
if !runtime.is_none() {
return;
}
std::panic::set_hook(Box::new(|info| {
let backtrace = std::backtrace::Backtrace::force_capture();
let payload = info.payload();
if let Some(string) = payload.downcast_ref::<String>() {
error!("panic captured: {string}");
} else if let Some(str) = payload.downcast_ref::<&'static str>() {
error!("panic captured: {str}")
} else {
error!("panic captured: {payload:?}")
}
error!("panic backtrace:\n{:?}", backtrace);
}));
let parsed_params = match serde_json::from_str::<InitParams>(&params) {
Ok(result) => result,
Err(err) => {
panic!("parse init params failed: {}", err);
}
};
let log_path = parsed_params.log_path.as_ref();
if log_path.is_some_and(|path| Path::new(path).exists()) {
if log_path.is_some_and(|path| !path.is_empty() && (Path::new(path).exists()) || std::fs::create_dir_all(path).is_ok()) {
let log_path = log_path.unwrap();
let _logger = Logger::try_with_str("info")
.unwrap()
Expand Down Expand Up @@ -159,7 +174,11 @@ pub fn upload(
#[no_mangle]
pub extern "cdecl" fn ne_s3_upload(
params: *const std::os::raw::c_char,
result_callback: extern "C" fn(success: bool, message: *const std::os::raw::c_char, user_data: *const std::os::raw::c_char),
result_callback: extern "C" fn(
success: bool,
message: *const std::os::raw::c_char,
user_data: *const std::os::raw::c_char,
),
progress_callback: extern "C" fn(progress: f32, user_data: *const std::os::raw::c_char),
user_data: *const std::os::raw::c_char,
) {
Expand All @@ -173,7 +192,11 @@ pub extern "cdecl" fn ne_s3_upload(
let progress_callback = move |progress: f32| {
progress_callback(progress, user_data.as_ptr());
};
upload(params.to_string(), Box::new(result_callback), Arc::new(Mutex::new(progress_callback)));
upload(
params.to_string(),
Box::new(result_callback),
Arc::new(Mutex::new(progress_callback)),
);
}

/// download file from s3
Expand Down Expand Up @@ -233,7 +256,11 @@ pub fn download(params: String, result_callback: basic::ResultCallback) {
#[no_mangle]
pub extern "cdecl" fn ne_s3_download(
params: *const std::os::raw::c_char,
result_callback: extern "C" fn(success: bool, message: *const std::os::raw::c_char, user_data: *const std::os::raw::c_char),
result_callback: extern "C" fn(
success: bool,
message: *const std::os::raw::c_char,
user_data: *const std::os::raw::c_char,
),
user_data: *const std::os::raw::c_char,
) {
let params = unsafe { std::ffi::CStr::from_ptr(params as *mut _) };
Expand All @@ -244,4 +271,4 @@ pub extern "cdecl" fn ne_s3_download(
result_callback(success, message.as_ptr(), user_data.as_ptr());
};
download(params.to_string(), Box::new(result_callback));
}
}

0 comments on commit 45f210a

Please sign in to comment.