Skip to main content

Logging

Access the global log manager through LogManager::instance().​

Master runtime logs are pushed to LogManager, which is registered automatically at initialization.

Quick start​

use darra_ethercat::logging::{LogManager, LogCategory};

let log_mgr = LogManager::instance();

// bind to console output
log_mgr.default_view().set_on_updated(|entry| {
println!("{}", entry);
});

// show errors and warnings only
log_mgr.default_view().set_filter(&[LogCategory::Error, LogCategory::Warning]);

// add a custom log entry
log_mgr.add_log(LogCategory::Message, "主站已初始化"); // Master initialized

Log categories​

#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LogCategory {
Error = 0, // system errors and exceptions
Warning = 1, // potential problems
Message = 2, // general information
Mailbox = 3, // mailbox communication (off by default)
PDO = 4, // PDO data changes (off by default)
Debug = 5, // detailed debugging (off by default)
Local = 6, // console output capture
}

Log manager​

LogManager::instance()​

pub fn instance() -> &'static LogManager

Gets the global log manager singleton.

default_view()​

pub fn default_view(&self) -> &LogView

Gets the default log view.

add_log()​

pub fn add_log(&self, category: LogCategory, message: &str)

Adds a log entry.

clear()​

pub fn clear(&self)

Clears all log entries.

Log entries​

pub struct LogEntry {
pub timestamp: SystemTime, // log timestamp
pub category: LogCategory, // log category
pub message: String, // log message text
}

Log view​

set_on_updated()​

pub fn set_on_updated<F>(&self, callback: F)
where F: Fn(&LogEntry) + Send + Sync + 'static

Fires when the log data changes. The callback receives the newly added log entry.

set_filter()​

pub fn set_filter(&self, categories: &[LogCategory])

Sets the filter so that only the specified categories are shown.

reset_filter()​

pub fn reset_filter(&self)

Resets the filter (all categories are shown).

get_all()​

pub fn get_all(&self) -> Vec<LogEntry>

Gets all log entries (the filter is ignored).

get_filtered()​

pub fn get_filtered(&self) -> Vec<LogEntry>

Gets the filtered log entries.

count()​

pub fn count(&self) -> usize

Current number of filtered log entries.

set_retain_count() / retain_count()​

pub fn set_retain_count(&self, count: usize)
pub fn retain_count(&self) -> usize

Sets or gets the maximum number of retained entries (0 = use the default limit of 10000).

Capacity and retention policy

The log list has an upper limit; when it is exceeded, the oldest entries are removed automatically.

Automatic retention mode: after entering the SafeOp/OP state the log switches to retention mode automatically — runtime events (slave offline, PDO frame loss, DC synchronization loss and so on) are not cleared by later operations. Leaving SafeOp/OP restores normal mode.

Log switches​

Control runtime log output through EtherCATMaster static methods:

/// enable PDO logging
pub fn set_pdo_logging(enable: bool)

/// enable mailbox logging
pub fn set_mailbox_logging(enable: bool)

/// enable debug logging
pub fn set_debug_logging(enable: bool)

The same switches also have module-level entry points under darra_ethercat::master::other, plus a global callback registration:

pub fn initialize_logging(
log_callback: Option<LogCallback>,
crash_callback: Option<CrashNotifyCallback>,
)
pub fn enable_pdo_logging(enable: bool)
pub fn enable_mailbox_logging(enable: bool)
pub fn enable_debug_logging(enable: bool)

initialize_logging() registers the global log callback and the crash-notification callback, and turns off the PDO, mailbox and debug logs (all off by default). Call it once before creating the master.

Full example​

use darra_ethercat::logging::{LogManager, LogCategory};
use darra_ethercat::EtherCATMaster;

let log_mgr = LogManager::instance();
let view = log_mgr.default_view();

// 1. Bind console output
view.set_on_updated(|entry| {
println!("{}", entry);
});

// 2. Enable debug logging
EtherCATMaster::set_debug_logging(true);
EtherCATMaster::set_mailbox_logging(true);

// 3. Filter the log
view.set_filter(&[LogCategory::Error, LogCategory::Debug]);

// 4. Export all log entries
let all_logs = view.get_all();
let content: String = all_logs.iter()
.map(|e| format!("{}\n", e))
.collect();
std::fs::write("debug.log", content)?;