Skip to content

Commit

Permalink
add log_path field to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Jan 27, 2025
1 parent 3a9b3da commit 01f310a
Show file tree
Hide file tree
Showing 27 changed files with 405 additions and 192 deletions.
1 change: 1 addition & 0 deletions c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1.19.2", features = ["rt-multi-thread"] }
once_cell = "1.12.0"
parking_lot = "0.12.1"
time = "0.3.9"
tracing = "0.1.34"

[build-dependencies]
cbindgen = "0.24.3"
4 changes: 3 additions & 1 deletion c/csrc/include/longport.h
Original file line number Diff line number Diff line change
Expand Up @@ -3703,6 +3703,7 @@ extern "C" {
* `realtime`)
* - `LONGPORT_PRINT_QUOTE_PACKAGES` - Print quote packages when connected,
* `true` or `false` (Default: `true`)
* - `LONGPORT_LOG_PATH` - Set the path of the log files (Default: `no logs`)
*/
struct lb_config_t *lb_config_from_env(struct lb_error_t **error);

Expand All @@ -3715,7 +3716,8 @@ struct lb_config_t *lb_config_new(const char *app_key,
const enum lb_language_t *language,
bool enable_overight,
const enum lb_push_candlestick_mode_t *push_candlestick_mode,
bool enable_print_quote_packages);
bool enable_print_quote_packages,
const char *log_path);

/**
* Free the config object
Expand Down
6 changes: 6 additions & 0 deletions c/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct CConfig(pub(crate) Arc<Config>);
/// `realtime`)
/// - `LONGPORT_PRINT_QUOTE_PACKAGES` - Print quote packages when connected,
/// `true` or `false` (Default: `true`)
/// - `LONGPORT_LOG_PATH` - Set the path of the log files (Default: `no logs`)
#[no_mangle]
pub unsafe extern "C" fn lb_config_from_env(error: *mut *mut CError) -> *mut CConfig {
match Config::from_env() {
Expand All @@ -63,6 +64,7 @@ pub unsafe extern "C" fn lb_config_new(
enable_overight: bool,
push_candlestick_mode: *const CPushCandlestickMode,
enable_print_quote_packages: bool,
log_path: *const c_char,
) -> *mut CConfig {
let app_key = CStr::from_ptr(app_key).to_str().expect("invalid app key");
let app_secret = CStr::from_ptr(app_secret)
Expand Down Expand Up @@ -109,6 +111,10 @@ pub unsafe extern "C" fn lb_config_new(
config = config.dont_print_quote_packages();
}

if !log_path.is_null() {
config = config.log_path(CStr::from_ptr(log_path).to_str().expect("invalid log path"));
}

Box::into_raw(Box::new(CConfig(Arc::new(config))))
}

Expand Down
61 changes: 61 additions & 0 deletions c/src/quote_context/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
ffi::{c_void, CString},
os::raw::c_char,
sync::Arc,
time::Instant,
};

use longport::{
Expand Down Expand Up @@ -127,12 +128,24 @@ pub unsafe extern "C" fn lb_quote_context_new(
..
} => {
if let Some(callback) = &state.callbacks.quote {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_quote callback");

let quote_owned: CPushQuoteOwned = (symbol, quote).into();
(callback.f)(
Arc::as_ptr(&ctx),
&quote_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_quote callback"
);
}
}
PushEvent {
Expand All @@ -141,12 +154,24 @@ pub unsafe extern "C" fn lb_quote_context_new(
..
} => {
if let Some(callback) = &state.callbacks.depth {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_depth callback");

let depth_owned: CPushDepthOwned = (symbol, depth).into();
(callback.f)(
Arc::as_ptr(&ctx),
&depth_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_depth callback"
);
}
}
PushEvent {
Expand All @@ -155,12 +180,24 @@ pub unsafe extern "C" fn lb_quote_context_new(
..
} => {
if let Some(callback) = &state.callbacks.brokers {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_brokers callback");

let brokers_owned: CPushBrokersOwned = (symbol, brokers).into();
(callback.f)(
Arc::as_ptr(&ctx),
&brokers_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_brokers callback"
);
}
}
PushEvent {
Expand All @@ -169,12 +206,24 @@ pub unsafe extern "C" fn lb_quote_context_new(
..
} => {
if let Some(callback) = &state.callbacks.trades {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_trades callback");

let trades_owned: CPushTradesOwned = (symbol, trades).into();
(callback.f)(
Arc::as_ptr(&ctx),
&trades_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_trades callback"
);
}
}
PushEvent {
Expand All @@ -183,13 +232,25 @@ pub unsafe extern "C" fn lb_quote_context_new(
..
} => {
if let Some(callback) = &state.callbacks.candlestick {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_candlestick callback");

let candlestick_owned: CPushCandlestickOwned =
(symbol, candlestick).into();
(callback.f)(
Arc::as_ptr(&ctx),
&candlestick_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_candlestick callback"
);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion c/src/trade_context/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::c_void, os::raw::c_char, sync::Arc};
use std::{ffi::c_void, os::raw::c_char, sync::Arc, time::Instant};

use longport::{
trade::{
Expand Down Expand Up @@ -98,13 +98,25 @@ pub unsafe extern "C" fn lb_trade_context_new(
match event {
PushEvent::OrderChanged(order_changed) => {
if let Some(callback) = &state.callbacks.order_changed {
let log_subscriber = ctx.ctx.log_subscriber();
let _guard =
tracing::dispatcher::set_default(&log_subscriber.into());

let s = Instant::now();
tracing::info!("begin call on_order_changed callback");

let order_changed_owned: CPushOrderChangedOwned =
order_changed.into();
(callback.f)(
Arc::as_ptr(&ctx),
&order_changed_owned.to_ffi_type(),
callback.userdata,
);

tracing::info!(
duration = ?s.elapsed(),
"after call on_order_changed callback"
);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion cpp/include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Config
* @param language Language identifer (Default: Language::EN)
* @param push_candlestick_mode Push candlestick mode (Default:
* PushCandlestickMode::Realtime)
* @param enable_print_quote_packages Print quote packages when connected
* (Default: true)
*/
Config(const std::string& app_key,
const std::string& app_secret,
Expand All @@ -48,7 +50,8 @@ class Config
bool enable_overnight = false,
const std::optional<PushCandlestickMode>& push_candlestick_mode =
std::nullopt,
bool enable_print_quote_packages = true);
bool enable_print_quote_packages = true,
const std::optional<std::string>& log_path = std::nullopt);

~Config();

Expand Down Expand Up @@ -76,6 +79,7 @@ class Config
/// `realtime`)
/// - `LONGPORT_PRINT_QUOTE_PACKAGES` - Print quote packages when connected,
/// `true` or `false` (Default: `true`)
/// - `LONGPORT_LOG_PATH` - Set the path of the log files (Default: `no logs`)
static Status from_env(Config& config);

/// Gets a new `access_token`
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Config::Config(const std::string& app_key,
const std::optional<Language>& language,
bool enable_overnight,
const std::optional<PushCandlestickMode>& push_candlestick_mode,
bool enable_print_quote_packages)
bool enable_print_quote_packages,
const std::optional<std::string>& log_path)
{
lb_language_t c_language;
if (language) {
Expand All @@ -52,7 +53,8 @@ Config::Config(const std::string& app_key,
language ? &c_language : nullptr,
enable_overnight,
push_candlestick_mode ? &c_push_candlestick_mode : nullptr,
enable_print_quote_packages);
enable_print_quote_packages,
log_path ? log_path->c_str() : nullptr);
}

Config::~Config()
Expand Down
1 change: 1 addition & 0 deletions java/javasrc/src/main/java/com/longport/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Config implements AutoCloseable {
* `realtime`)
* - `LONGPORT_PRINT_QUOTE_PACKAGES` - Print quote packages when connected,
* `true` or `false` (Default: `true`)
* - `LONGPORT_LOG_PATH` - Set the path of the log files (Default: `no logs`)
*
* @return Config object
* @throws OpenApiException If an error occurs
Expand Down
14 changes: 13 additions & 1 deletion java/javasrc/src/main/java/com/longport/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ConfigBuilder {
private boolean enableOvernight;
private PushCandlestickMode pushCandlestickMode;
private boolean enablePrintQuotePackages;
private String logPath;

/**
* Create a `Config` object builder
Expand Down Expand Up @@ -113,6 +114,17 @@ public ConfigBuilder dontPrintQuotePackages() {
return this;
}

/**
* Set the path of the log files.
*
* @param path The path of the log files (Default: `no logs`)
* @return this object
*/
public ConfigBuilder logPath(String path) {
this.logPath = path;
return this;
}

/**
* Build a Config object
*
Expand All @@ -122,6 +134,6 @@ public ConfigBuilder dontPrintQuotePackages() {
public Config build() throws OpenApiException {
return new Config(
SdkNative.newConfig(appKey, appSecret, accessToken, httpUrl, quoteWsUrl, tradeWsUrl, language,
enableOvernight, pushCandlestickMode, enablePrintQuotePackages));
enableOvernight, pushCandlestickMode, enablePrintQuotePackages, logPath));
}
}
2 changes: 1 addition & 1 deletion java/javasrc/src/main/java/com/longport/SdkNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SdkNative {

public static native long newConfig(String appKey, String appSecret, String accessToken, String httpUrl,
String quoteWsUrl, String tradeWsUrl, Language language, boolean enableOvernight,
PushCandlestickMode mode, boolean enablePrintQuotePackages);
PushCandlestickMode mode, boolean enablePrintQuotePackages, String logPath);

public static native long newConfigFromEnv();

Expand Down
5 changes: 5 additions & 0 deletions java/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub extern "system" fn Java_com_longport_SdkNative_newConfig(
enable_overnight: jboolean,
push_candlestick_mode: JObject,
enable_print_quote_packages: jboolean,
log_path: JString,
) -> jlong {
jni_result(&mut env, 0, |env| {
let app_key = String::from_jvalue(env, app_key.into())?;
Expand All @@ -33,6 +34,7 @@ pub extern "system" fn Java_com_longport_SdkNative_newConfig(
let language = <Option<Language>>::from_jvalue(env, language.into())?;
let push_candlestick_mode =
<Option<PushCandlestickMode>>::from_jvalue(env, push_candlestick_mode.into())?;
let log_path = <Option<String>>::from_jvalue(env, log_path.into())?;

let mut config = Config::new(app_key, app_secret, access_token);

Expand All @@ -57,6 +59,9 @@ pub extern "system" fn Java_com_longport_SdkNative_newConfig(
if enable_print_quote_packages == 0 {
config = config.dont_print_quote_packages();
}
if let Some(log_path) = log_path {
config = config.log_path(log_path);
}

Ok(Box::into_raw(Box::new(config)) as jlong)
})
Expand Down
6 changes: 6 additions & 0 deletions nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface ConfigParams {
* (default: true)
*/
enablePrintQuotePackages: boolean
/** Set the path of the log files (Default: `no logs`) */
logPath?: string
}
/** An request to create a watchlist group */
export interface CreateWatchlistGroup {
Expand Down Expand Up @@ -2048,6 +2050,10 @@ export class PushQuote {
get tradeStatus(): TradeStatus
/** Trade session */
get tradeSession(): TradeSession
/** Increase volume between pushes */
get currentVolume(): number
/** Increase turnover between pushes */
get currentTurnover(): Decimal
}
/** Push real-time depth */
export class PushDepth {
Expand Down
6 changes: 6 additions & 0 deletions nodejs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct ConfigParams {
/// Enable printing the opened quote packages when connected to the server
/// (default: true)
pub enable_print_quote_packages: bool,
/// Set the path of the log files (Default: `no logs`)
pub log_path: Option<String>,
}

/// Configuration for LongPort sdk
Expand Down Expand Up @@ -75,6 +77,10 @@ impl Config {
config = config.dont_print_quote_packages();
}

if let Some(log_path) = params.log_path {
config = config.log_path(log_path);
}

Self(config)
}

Expand Down
2 changes: 1 addition & 1 deletion python/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ args = ["install", "maturin==1.7.4"]
command = "pip"
args = [
"install",
"target/wheels/longport-2.1.6-cp311-cp311-win_amd64.whl",
"target/wheels/longport-2.1.7-cp311-cp311-win_amd64.whl",
"-I",
]
dependencies = ["python"]
Loading

0 comments on commit 01f310a

Please sign in to comment.