refactor: logger now only enables for indexer crates + takes in a loglevel option

This commit is contained in:
erhant 2026-06-19 13:41:36 +03:00
parent 956dc6278a
commit 4a3fa1d4be
4 changed files with 38 additions and 28 deletions

View File

@ -8,7 +8,6 @@
use std::time::Duration; use std::time::Duration;
use anyhow::{Context as _, Result}; use anyhow::{Context as _, Result};
use indexer_ffi::Runtime;
use indexer_service_protocol::Account; use indexer_service_protocol::Account;
use integration_tests::{ use integration_tests::{
L2_TO_L1_TIMEOUT, TIME_TO_WAIT_FOR_BLOCK_SECONDS, private_mention, public_mention, L2_TO_L1_TIMEOUT, TIME_TO_WAIT_FOR_BLOCK_SECONDS, private_mention, public_mention,
@ -102,11 +101,8 @@ fn indexer_ffi_state_consistency() -> Result<()> {
info!("Waiting for indexer to parse blocks"); info!("Waiting for indexer to parse blocks");
std::thread::sleep(L2_TO_L1_TIMEOUT); std::thread::sleep(L2_TO_L1_TIMEOUT);
// Safety: ctx runtime is valid for the lifetime of the returned Runtime
let runtime = unsafe { Runtime::from_borrowed(ctx.runtime()) };
let acc1_ind_state_ffi = unsafe { let acc1_ind_state_ffi = unsafe {
indexer_ffi_helpers::query_account( indexer_ffi_helpers::query_account(
&raw const runtime,
&raw const indexer_ffi, &raw const indexer_ffi,
(&ctx.ctx().existing_public_accounts()[0]).into(), (&ctx.ctx().existing_public_accounts()[0]).into(),
) )
@ -119,7 +115,6 @@ fn indexer_ffi_state_consistency() -> Result<()> {
let acc2_ind_state_ffi = unsafe { let acc2_ind_state_ffi = unsafe {
indexer_ffi_helpers::query_account( indexer_ffi_helpers::query_account(
&raw const runtime,
&raw const indexer_ffi, &raw const indexer_ffi,
(&ctx.ctx().existing_public_accounts()[1]).into(), (&ctx.ctx().existing_public_accounts()[1]).into(),
) )

View File

@ -8,7 +8,6 @@
use std::time::Duration; use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use indexer_ffi::Runtime;
use indexer_service_protocol::Account; use indexer_service_protocol::Account;
use integration_tests::{L2_TO_L1_TIMEOUT, TIME_TO_WAIT_FOR_BLOCK_SECONDS, public_mention}; use integration_tests::{L2_TO_L1_TIMEOUT, TIME_TO_WAIT_FOR_BLOCK_SECONDS, public_mention};
use log::info; use log::info;
@ -75,11 +74,8 @@ fn indexer_ffi_state_consistency_with_labels() -> Result<()> {
info!("Waiting for indexer to parse blocks"); info!("Waiting for indexer to parse blocks");
std::thread::sleep(L2_TO_L1_TIMEOUT); std::thread::sleep(L2_TO_L1_TIMEOUT);
// Safety: ctx runtime is valid for the lifetime of the returned Runtime
let runtime = unsafe { Runtime::from_borrowed(ctx.runtime()) };
let acc1_ind_state_ffi = unsafe { let acc1_ind_state_ffi = unsafe {
indexer_ffi_helpers::query_account( indexer_ffi_helpers::query_account(
&raw const runtime,
&raw const indexer_ffi, &raw const indexer_ffi,
(&ctx.ctx().existing_public_accounts()[0]).into(), (&ctx.ctx().existing_public_accounts()[0]).into(),
) )

View File

@ -464,18 +464,18 @@ struct PointerResult_Runtime__OperationStatus new_runtime(void);
enum OperationStatus stop_indexer(struct IndexerServiceFFI *indexer); enum OperationStatus stop_indexer(struct IndexerServiceFFI *indexer);
/** /**
* Initializes the FFI's logger. * Initializes logging for the indexer at `level`.
* *
* Wires up `env_logger`, so the library's `log::*` output is controlled by the * - `level` is a null-terminated string (`off`/`error`/`warn`/`info`/`debug`/ `trace`,
* `RUST_LOG` environment variable (e.g. `RUST_LOG=info`). Without this, the * case-insensitive); null or unparseable falls back to `info`.
* FFI's log calls go nowhere and since failures are otherwise reported only
* as numeric [`OperationStatus`](crate::errors::OperationStatus) codes, there
* is no other way to see *why* a call failed.
* *
* Safe to call multiple times and from any consumer: if a global logger is * Only the `indexer_ffi` and `indexer_core` targets are enabled!
* already set, the call is a no-op. *
* # Safety
* - `level` must be a valid null-terminated C string, or null.
* - First call to this function wins; subsequent calls are no-ops.
*/ */
void init_logger(void); void init_logger(const char *level);
/** /**
* # Safety * # Safety

View File

@ -1,14 +1,33 @@
/// Initializes the FFI's logger. use std::ffi::{CStr, c_char};
use log::LevelFilter;
/// Initializes logging for the indexer at `level`.
/// ///
/// Wires up `env_logger`, so the library's `log::*` output is controlled by the /// - `level` is a null-terminated string (`off`/`error`/`warn`/`info`/`debug`/ `trace`,
/// `RUST_LOG` environment variable (e.g. `RUST_LOG=info`). Without this, the /// case-insensitive); null or unparseable falls back to `info`.
/// FFI's log calls go nowhere — and since failures are otherwise reported only
/// as numeric [`OperationStatus`](crate::errors::OperationStatus) codes, there
/// is no other way to see *why* a call failed.
/// ///
/// Safe to call multiple times and from any consumer: if a global logger is /// Only the `indexer_ffi` and `indexer_core` targets are enabled!
/// already set, the call is a no-op. ///
/// # Safety
/// - `level` must be a valid null-terminated C string, or null.
/// - First call to this function wins; subsequent calls are no-ops.
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn init_logger() { pub unsafe extern "C" fn init_logger(level: *const c_char) {
let _ignore_me = env_logger::try_init(); let level = if level.is_null() {
LevelFilter::Info
} else {
unsafe { CStr::from_ptr(level) }
.to_str()
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(LevelFilter::Info)
};
env_logger::Builder::new()
.filter_level(LevelFilter::Off)
.filter_module("indexer_ffi", level)
.filter_module("indexer_core", level)
.try_init()
.ok();
} }