Browse Source

standard的panic!信息添加到日志

gepangpang 2 years ago
parent
commit
d116139c31

+ 15 - 3
standard/src/binance_swap.rs

@@ -7,6 +7,7 @@ use async_trait::async_trait;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
 use tokio::sync::mpsc::Sender;
+use tracing::error;
 use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, OrderCommand, utils, PositionModeEnum};
 use exchanges::binance_swap_rest::BinanceSwapRest;
 
@@ -90,6 +91,7 @@ impl Platform for BinanceSwap {
             let balance_info = res_data_json.iter().find(|item| item["asset"].as_str().unwrap().to_string() == symbol_array[1].to_string());
             match balance_info {
                 None => {
+                    error!("格式化Binance账号信息错误!\nget_account: balance_info={:?}", balance_info);
                     panic!("格式化Binance账号信息错误!\nget_account: balance_info={:?}", balance_info)
                 }
                 Some(value) => {
@@ -171,6 +173,7 @@ impl Platform for BinanceSwap {
             let market_info = symbols.iter().find(|&item| item["symbol"].as_str().unwrap() == symbol_format);
             match market_info {
                 None => {
+                    error!("Binance:获取Market信息错误!\nget_market:res_data={:?}", res_data_str);
                     panic!("Binance:获取Market信息错误!\nget_market:res_data={:?}", res_data_str)
                 }
                 Some(value) => {
@@ -211,7 +214,10 @@ impl Platform for BinanceSwap {
             let res_data_json: serde_json::Value = serde_json::from_str(res_data_str).unwrap();
 
             let status = res_data_json["status"].as_str().unwrap();
-            let custom_status = if ["CANCELED", "EXPIRED", "FILLED"].contains(&status) { "REMOVE".to_string() } else if status == "NEW" { "NEW".to_string() } else { panic!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status) };
+            let custom_status = if ["CANCELED", "EXPIRED", "FILLED"].contains(&status) { "REMOVE".to_string() } else if status == "NEW" { "NEW".to_string() } else {
+                error!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status);
+                panic!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status)
+            };
             let result = Order {
                 id: res_data_json["orderId"].to_string(),
                 custom_id: res_data_json["clientOrderId"].as_str().unwrap().parse().unwrap(),
@@ -237,7 +243,10 @@ impl Platform for BinanceSwap {
             let order_info: Vec<_> = res_data_json.iter().filter(|item| item["contract"].as_str().unwrap_or("") == self.symbol).collect();
             let result = order_info.iter().map(|&item| {
                 let status = item["status"].as_str().unwrap();
-                let custom_status = if ["CANCELED", "EXPIRED", "FILLED"].contains(&status) { "REMOVE".to_string() } else if status == "NEW" { "NEW".to_string() } else { panic!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status) };
+                let custom_status = if ["CANCELED", "EXPIRED", "FILLED"].contains(&status) { "REMOVE".to_string() } else if status == "NEW" { "NEW".to_string() } else {
+                    error!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status);
+                    panic!("Binance:格式化订单状态错误!\nget_order_detail:status={:?}", status)
+                };
                 Order {
                     id: item["orderId"].to_string(),
                     custom_id: item["clientOrderId"].as_str().unwrap().parse().unwrap(),
@@ -276,7 +285,10 @@ pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal)
         "BOTH" => PositionModeEnum::Both,
         "LONG" => PositionModeEnum::Long,
         "SHORT" => PositionModeEnum::Short,
-        _ => panic!("Binance:格式化持仓模式错误!\nformat_position_item:position_side={:?}", position["positionSide"])
+        _ => {
+            error!("Binance:格式化持仓模式错误!\nformat_position_item:position_side={:?}", position["positionSide"]);
+            panic!("Binance:格式化持仓模式错误!\nformat_position_item:position_side={:?}", position["positionSide"])
+        }
     };
     let size = Decimal::from_str(position["positionAmt"].as_str().unwrap()).unwrap();
     let amount = size * amount_size;

+ 10 - 3
standard/src/gate_handle.rs

@@ -1,7 +1,7 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
-use tracing::{debug};
+use tracing::{debug, error};
 use exchanges::response_base::ResponseData;
 use crate::{Account, MarketOrder, Order, Position, PositionModeEnum, SpecialDepth, SpecialOrder};
 use crate::exchange::ExchangeEnum;
@@ -19,6 +19,7 @@ pub fn format_account_info(data: Vec<serde_json::Value>, symbol: String) -> Acco
     let balance_info = data.iter().find(|&item| item["text"].as_str().unwrap().contains(&symbol));
     match balance_info {
         None => {
+            error!("Gate:格式化账号信息错误!\nformat_account_info: balance_info={:?}", balance_info);
             panic!("Gate:格式化账号信息错误!\nformat_account_info: balance_info={:?}", balance_info)
         }
         Some(value) => {
@@ -47,7 +48,10 @@ pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal)
         "single" => PositionModeEnum::Both,
         "dual_long" => PositionModeEnum::Long,
         "dual_short" => PositionModeEnum::Short,
-        _ => panic!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"])
+        _ => {
+            error!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"]);
+            panic!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"])
+        }
     };
     let size = Decimal::from_str(&position["size"].to_string()).unwrap();
     let amount = size * amount_size;
@@ -88,7 +92,10 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
 
     let amount = size * amount_size;
     let deal_amount = (size - left) * amount_size;
-    let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else { panic!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status) };
+    let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else {
+        error!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status);
+        panic!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status)
+    };
     let rst_order = Order {
         id: order["id"].to_string(),
         custom_id: text.replace("t-my-custom-id_", ""),

+ 10 - 2
standard/src/gate_swap.rs

@@ -144,6 +144,7 @@ impl Platform for GateSwap {
             let ticker_info = res_data_json.iter().find(|item| item["contract"].as_str().unwrap() == self.symbol);
             match ticker_info {
                 None => {
+                    error!("Gate:获取Ticker信息错误!\nget_ticker:res_data={:?}", res_data_str);
                     panic!("Gate:获取Ticker信息错误!\nget_ticker:res_data={:?}", res_data_str)
                 }
                 Some(value) => {
@@ -173,6 +174,7 @@ impl Platform for GateSwap {
             let market_info = res_data_json.iter().find(|item| item["name"].as_str().unwrap() == self.symbol);
             match market_info {
                 None => {
+                    error!("Gate:获取Market信息错误!\nget_market:market_info={:?}", market_info);
                     panic!("Gate:获取Market信息错误!\nget_market:market_info={:?}", market_info)
                 }
                 Some(value) => {
@@ -448,7 +450,10 @@ pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal)
         "single" => PositionModeEnum::Both,
         "dual_long" => PositionModeEnum::Long,
         "dual_short" => PositionModeEnum::Short,
-        _ => panic!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"])
+        _ => {
+            error!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"]);
+            panic!("Gate:格式化持仓模式错误!\nformat_position_item:position_mode={:?}", position["mode"])
+        }
     };
     let size = Decimal::from_str(&position["size"].to_string()).unwrap();
     let amount = size * amount_size;
@@ -474,7 +479,10 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
 
     let amount = size * amount_size;
     let deal_amount = (size - left) * amount_size;
-    let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else { panic!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status) };
+    let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else {
+        error!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status);
+        panic!("Gate:格式化订单状态错误!\nformat_order_item:status={:?}", status)
+    };
     let rst_order = Order {
         id: order["id"].to_string(),
         custom_id: text.replace("t-my-custom-id_", ""),

+ 19 - 9
standard/src/handle_info.rs

@@ -2,6 +2,7 @@ use std::cmp::Ordering;
 use rust_decimal::{Decimal};
 use rust_decimal::prelude::FromPrimitive;
 use rust_decimal_macros::dec;
+use tracing::error;
 use exchanges::response_base::ResponseData;
 use global::public_params;
 use crate::exchange::ExchangeEnum;
@@ -16,7 +17,8 @@ impl HandleSwapInfo {
     pub fn handle_account_info(exchange: ExchangeEnum, res_data: ResponseData, symbol: String) -> Account {
         match exchange {
             ExchangeEnum::BinanceSwap => {
-                panic!("暂未提供此交易所方法!")
+                error!("暂未提供此交易所方法!handle_account_info:{:?}", exchange);
+                panic!("暂未提供此交易所方法!handle_account_info:{:?}", exchange);
             }
             ExchangeEnum::GateSwap => {
                 gate_handle::handle_account_info(res_data, symbol)
@@ -25,7 +27,8 @@ impl HandleSwapInfo {
                 kucoin_handle::handle_account_info(res_data, symbol)
             }
             _ => {
-                panic!("参数错误!")
+                error!("参数错误!handle_account_info: {:?}",exchange);
+                panic!("参数错误!handle_account_info: {:?}",exchange);
             }
         }
     }
@@ -39,13 +42,15 @@ impl HandleSwapInfo {
                 binance_handle::handle_special_ticker(res_data)
             }
             ExchangeEnum::GateSwap => {
-                panic!("暂未提供此交易所方法!")
+                error!("暂未提供此交易所方法!handle_special_ticker:{:?}", exchange);
+                panic!("暂未提供此交易所方法!handle_special_ticker:{:?}", exchange);
             }
             ExchangeEnum::KucoinSwap => {
                 kucoin_handle::handle_special_ticker(res_data)
             }
             _ => {
-                panic!("参数错误!")
+                error!("参数错误!handle_special_ticker: {:?}",exchange);
+                panic!("参数错误!handle_special_ticker: {:?}",exchange);
             }
         }
     }
@@ -53,7 +58,8 @@ impl HandleSwapInfo {
     pub fn handle_position(exchange: ExchangeEnum, res_data: ResponseData, amount_size: Decimal) -> Vec<Position> {
         match exchange {
             ExchangeEnum::BinanceSwap => {
-                panic!("暂未提供此交易所方法!")
+                error!("暂未提供此交易所方法!handle_position:{:?}", exchange);
+                panic!("暂未提供此交易所方法!handle_position:{:?}", exchange);
             }
             ExchangeEnum::GateSwap => {
                 gate_handle::handle_position(res_data, amount_size)
@@ -62,7 +68,8 @@ impl HandleSwapInfo {
                 kucoin_handle::handle_position(res_data, amount_size)
             }
             _ => {
-                panic!("参数错误!")
+                error!("参数错误!handle_position: {:?}",exchange);
+                panic!("参数错误!handle_position: {:?}",exchange);
             }
         }
     }
@@ -70,7 +77,8 @@ impl HandleSwapInfo {
     pub fn handle_order(exchange: ExchangeEnum, res_data: ResponseData, amount_size: Decimal) -> SpecialOrder {
         match exchange {
             ExchangeEnum::BinanceSwap => {
-                panic!("暂未提供此交易所方法!")
+                error!("暂未提供此交易所方法!handle_order:{:?}", exchange);
+                panic!("暂未提供此交易所方法!handle_order:{:?}", exchange);
             }
             ExchangeEnum::GateSwap => {
                 gate_handle::handle_order(res_data, amount_size)
@@ -79,7 +87,8 @@ impl HandleSwapInfo {
                 kucoin_handle::handle_order(res_data, amount_size)
             }
             _ => {
-                panic!("参数错误!")
+                error!("参数错误!handle_order: {:?}",exchange);
+                panic!("参数错误!handle_order: {:?}",exchange);
             }
         }
     }
@@ -107,7 +116,8 @@ impl HandleSwapInfo {
                 depth_bids = kucoin_handle::format_depth_items(res_data_json["bids"].clone());
             }
             _ => {
-                panic!("参数错误!")
+                error!("参数错误!handle_special_depth: {:?}",exchange);
+                panic!("参数错误!handle_special_depth: {:?}",exchange);
             }
         }
         depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));

+ 17 - 4
standard/src/kucoin_swap.rs

@@ -168,6 +168,7 @@ impl Platform for KucoinSwap {
             let market_info = res_data_json.iter().find(|item| item["symbol"].as_str().unwrap() == symbol_format);
             match market_info {
                 None => {
+                    error!("Kucoin:获取Market信息错误!\nget_market:res_data={:?}", res_data_str);
                     panic!("Kucoin:获取Market信息错误!\nget_market:res_data={:?}", res_data_str)
                 }
                 Some(value) => {
@@ -283,7 +284,10 @@ impl Platform for KucoinSwap {
 
     async fn cancel_order(&mut self, order_id: &str, custom_id: &str) -> Result<Order, Error> {
         let res_data = self.request.cancel_order(order_id.to_string(), custom_id.to_string()).await;
-        if order_id == "" { panic!("Kucoin:撤销订单错误,该交易所为提供自定义订单号撤销订单!\ncancel_order:order_id={:?},custom_id={:?}", order_id, custom_id) }
+        if order_id == "" {
+            error!("Kucoin:撤销订单错误,该交易所为提供自定义订单号撤销订单!\ncancel_order:order_id={:?},custom_id={:?}", order_id, custom_id);
+            panic!("Kucoin:撤销订单错误,该交易所为提供自定义订单号撤销订单!\ncancel_order:order_id={:?},custom_id={:?}", order_id, custom_id)
+        }
         if res_data.code == "200" {
             let res_data_str = &res_data.data;
             let res_data_json: serde_json::Value = serde_json::from_str(res_data_str).unwrap();
@@ -330,11 +334,20 @@ impl Platform for KucoinSwap {
         }
     }
 
-    async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> { panic!("Kucoin:该交易所暂不支持此方法!"); }
+    async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
+        error!("Kucoin:该交易所暂不支持此方法!");
+        panic!("Kucoin:该交易所暂不支持此方法!");
+    }
 
-    async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> { panic!("Kucoin:该交易所暂不支持此方法!"); }
+    async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> {
+        error!("Kucoin:该交易所暂不支持此方法!");
+        panic!("Kucoin:该交易所暂不支持此方法!");
+    }
 
-    async fn wallet_transfers(&mut self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> { panic!("Kucoin:该交易所暂不支持此方法!"); }
+    async fn wallet_transfers(&mut self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> {
+        error!("Kucoin:该交易所暂不支持此方法!");
+        panic!("Kucoin:该交易所暂不支持此方法!");
+    }
 
     // 指令下单
     async fn command_order(&mut self, order_command: OrderCommand) {

+ 9 - 4
standard/tests/exchange_test.rs

@@ -6,7 +6,7 @@ use std::sync::atomic::AtomicBool;
 use std::time::Duration;
 use tokio::sync::mpsc::{channel, Receiver, Sender};
 use tokio::try_join;
-use tracing::trace;
+use tracing::{error, trace};
 use exchanges::binance_spot_ws::{BinanceSpotSubscribeType, BinanceSpotWs, BinanceSpotWsType};
 // use exchanges::kucoin_swap_ws::{KucoinSubscribeType, KucoinSwapWs, KucoinWsType};
 use exchanges::response_base::ResponseData;
@@ -110,6 +110,7 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
                                 }
                             }
                             _ => {
+                                error!("没有该命令!mode={}", mold_clone);
                                 panic!("没有该命令!mode={}", mold_clone)
                             }
                         }
@@ -119,10 +120,12 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
             try_join!(t1, t2).unwrap();
         }
         ExchangeEnum::BinanceSwap => {
-            panic!("该交易所不支持!")
+            error!("该交易所不支持!test_new_exchange_wss:{:?}",exchange);
+            panic!("该交易所不支持!test_new_exchange_wss:{:?}", exchange)
         }
         ExchangeEnum::GateSwap => {
-            panic!("该交易所不支持!")
+            error!("该交易所不支持!test_new_exchange_wss:{:?}",exchange);
+            panic!("该交易所不支持!test_new_exchange_wss:{:?}", exchange)
         }
         ExchangeEnum::KucoinSwap => {
             // let symbol_format = format!("{}M", utils::format_symbol(symbol.to_string(), ""));
@@ -180,6 +183,7 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
             //                     trace!(?result)
             //                 }
             //                 _ => {
+            //                     error!("没有该命令!mode={}", mold_clone);
             //                     panic!("没有该命令!mode={}", mold_clone)
             //                 }
             //             }
@@ -189,7 +193,8 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
             // try_join!(t1, t2).unwrap();
         }
         _ => {
-            panic!("该交易所不支持!")
+            error!("该交易所不支持!test_new_exchange_wss:{:?}",exchange);
+            panic!("该交易所不支持!test_new_exchange_wss:{:?}", exchange)
         }
     }
 }