Skip to main content

Logging

Access the log view through master.logs(), or control the log switches through the static methods of com.darra.ethercat.statics.StaticOther.​

Runtime logs reach master.logs() (a read-only list) through the global log callback. Call StaticOther.initializeLogging() once before use to register the SDK's log callback (idempotent); you can also take over this global slot with master.setLogCallback(...), but then logs no longer enter master.logs() automatically and you have to forward them yourself.

The output location of logs is the in-memory view master.logs(); the SDK does not write log files and does not print to the console. To get them into a file or the console, forward them yourself in a listener. Every log entry carries a timestamp, a category and a message, and toString() outputs [yyyy-MM-dd HH:mm:ss.SSS] [类别] 消息.

Quick start​

// Register the SDK log callback (idempotent, once is enough)
StaticOther.initializeLogging();

// Bind console output
master.logs().addUpdatedListener(() -> {
LogManager.LogView logs = master.logs();
if (logs.getCount() > 0) {
System.out.println(logs.get(logs.getCount() - 1));
}
});

// Show errors and warnings only
master.logs().setFilter(LogManager.LogCategory.ERROR, LogManager.LogCategory.WARNING);

// Enable debug logging (off by default)
StaticOther.enableDebugLogging(true);

Log categories​

public enum LogCategory {
ERROR(0), // system errors and exceptions
WARNING(1), // potential problems
MESSAGE(2), // general information (state changes and so on)
MAILBOX(3), // CoE/SoE/FoE/EoE mailbox communication (off by default)
PDO(4), // PDO data-change log (off by default)
DEBUG(5), // detailed debug information (off by default)
LOCAL(6); // local log
}

Related enums:

MemberValueMeaningIn view by default
ERROR0System errors and exceptionsYes
WARNING1Potential problemsYes
MESSAGE2General information (state changes and so on)Yes
MAILBOX3CoE/SoE/FoE/EoE mailbox communicationNo; add it to the filter explicitly and turn the switch on
PDO4PDO data-change logNo; add it to the filter explicitly and turn the switch on
DEBUG5Detailed debug informationNo; start with -Ddarra.sdk.debug=true
LOCAL6Local logYes

LogCategory exposes the public field value (the enum's numeric value) and the static method fromValue(int) (an unknown value falls back to MESSAGE).

Two levels of filtering
  1. Write side: DEBUG is discarded by default and is kept only when the JVM is started with -Ddarra.sdk.debug=true; PDO / MAILBOX are discarded by default and must be let through with setPdoTraceEnabled(true) / setMailboxTraceEnabled(true) respectively.
  2. View side: setFilter(...) decides which categories the view shows; the default is ERROR / WARNING / MESSAGE.

Both have to be open before high-frequency protocol logs appear in master.logs().

PDO log

The PDO log is output only when PDO data changes; it does not produce a log entry every cycle. It is useful for debugging PDO mapping problems.

Method overview​

The live log view (a read-only list) is maintained automatically by the logging system:

CategoryMethodTypeR/WDescription
Log viewmaster.logs()LogManager.LogViewRead-onlyLive log view
getCount()intRead-onlyNumber of entries after filtering
get(int index)LogEntryRead-onlyGets a log entry by index (index after filtering)
getAll()List<LogEntry>Read-onlyAll entries (ignoring the filter)
getFilteredSnapshot()List<LogEntry>Read-onlySnapshot after filtering
setFilter(LogCategory...)voidWrite-onlySets the category filter
addUpdatedListener(Runnable) / removeUpdatedListener(Runnable)voidWrite-onlyUpdate listener
Log singletonLogManager.getInstance()LogManagerRead-onlyLog manager singleton
getDefaultView()LogViewRead-onlyDefault log view (the one master.logs() returns)
addLog(LogCategory, String) / addLog(int, String)voidWrite-onlyWrites one entry to the view (overload takes the category value)
clear()voidWrite-onlyClears all entries
Write switchesLogManager.getInstance().setPdoTraceEnabled(boolean) / isPdoTraceEnabled()void / booleanRead-writeWhether PDO logs are let into the view (off by default)
LogManager.getInstance().setMailboxTraceEnabled(boolean) / isMailboxTraceEnabled()void / booleanRead-writeWhether mailbox logs are let into the view (off by default)
Log switchesStaticOther.enablePDOLogging(boolean)static voidWrite-onlyRuntime-side PDO log switch (off by default)
StaticOther.enableMailboxLogging(boolean)static voidWrite-onlyRuntime-side mailbox log switch (off by default)
StaticOther.enableDebugLogging(boolean)static voidWrite-onlyRuntime-side debug log switch (off by default)
StaticOther.initializeLogging()static voidWrite-onlyRegisters the SDK log callback (idempotent)
StaticOther.isLoggingInitialized()static booleanRead-onlyWhether initialization has happened
StaticOther.setSdkLogCallbackEnabled(boolean)static voidWrite-onlyWhether the SDK may take the callback slot (must be called before initialization)
StaticOther.releaseLogging()static booleanWrite-onlyReturns the callback slot
Callbacksmaster.setLogCallback(...)voidWrite-onlyTakes over the underlying log slot
master.setCrashCallback(...)voidWrite-onlyCrash notification callback

Related structures:

public class LogEntry {
public final Date timestamp; // log timestamp
public final LogCategory category; // log category
public final String message; // log message text

// toString() output: [yyyy-MM-dd HH:mm:ss.SSS] [category] message
}

LogEntry has a public constructor LogEntry(LogCategory, String), which you can use to output the application's own log entries in the same format.

API details​

master.logs()​

public LogManager.LogView logs()

Returns the live log view (a read-only list), maintained automatically by the logging system. The view holds at most 10000 entries; when the limit is exceeded the oldest batch is removed automatically. getAll() returns a snapshot copy of the current contents, so entries written afterwards are not reflected in a list you have already taken.

Return value:

  • LogManager.LogView — the default view (equivalent to LogManager.getInstance().getDefaultView())

setFilter(LogCategory... categories)​

public void setFilter(LogCategory... categories)

Sets the filter so that only the given categories are shown. Replaces the previous filter settings.

Parameters:

  • categories (LogCategory...) — log categories to show; pass nothing and the view is empty

Example:

// Show errors and warnings only
master.logs().setFilter(LogManager.LogCategory.ERROR, LogManager.LogCategory.WARNING);

// Switch to mailbox communication only (replaces the filter)
master.logs().setFilter(LogManager.LogCategory.MAILBOX);

getCount()​

public int getCount()

Return value:

  • int — the current number of entries after filtering (not the total number of entries)

get(int index)​

public LogEntry get(int index)

Gets a filtered log entry by index.

Parameters:

  • index (int) — index after filtering (0 .. getCount() - 1)

Return value:

  • LogEntry — the log entry

Exceptions:

  • IndexOutOfBoundsException — the index is out of range

getAll()​

public List<LogEntry> getAll()

Gets all log entries (ignoring the filter) and returns a snapshot copy. Useful for export or custom filtering.

Return value:

  • List<LogEntry> — a copy of all entries

getFilteredSnapshot()​

public List<LogEntry> getFilteredSnapshot()

Gets a snapshot copy of the filtered log entries (the batch form of getCount() + get(int), so you can take it once and then traverse it at your own pace).

Return value:

  • List<LogEntry> — a copy of the filtered entries

addUpdatedListener(Runnable) / removeUpdatedListener(Runnable)​

public void addUpdatedListener(Runnable listener)
public void removeUpdatedListener(Runnable listener)

Adds / removes a log change listener: it fires when a new entry is written or the filter changes. An add call with null is ignored.

Exceptions:

  • An exception thrown inside the listener is swallowed and does not interrupt the main logging path; do not rely on throwing exceptions inside a listener to control flow

Log switches​

The mailbox, PDO and debug logs are off by default and are enabled manually when needed. The switches are process-level static methods defined on com.darra.ethercat.statics.StaticOther.

import com.darra.ethercat.statics.StaticOther;

enablePDOLogging(boolean enable)​

public static void enablePDOLogging(boolean enable)

Enables the PDO log. It outputs only when PDO data changes; it affects real-time performance, so use it with care.

Parameters:

  • enable (boolean) — true = on

Notes: This switch only controls whether logs are produced. For entries to appear in master.logs() you also need LogManager.getInstance().setPdoTraceEnabled(true) and LogCategory.PDO added to the filter.

enableMailboxLogging(boolean enable)​

public static void enableMailboxLogging(boolean enable)

Enables mailbox logging (CoE/SoE/FoE/EoE mailbox communication logs).

Parameters:

  • enable (boolean) — true = on

Notes: As above; on the view side you need setMailboxTraceEnabled(true) and the filter must let MAILBOX through.

enableDebugLogging(boolean enable)​

public static void enableDebugLogging(boolean enable)

Enables debug logging (detailed debug information).

Parameters:

  • enable (boolean) — true = on

Notes: On the view side, DEBUG entries are controlled by the startup parameter -Ddarra.sdk.debug=true (decided when the JVM starts; it cannot be changed at run time). Debug logs appear in master.logs() only when both conditions are met.

initializeLogging / isLoggingInitialized / setSdkLogCallbackEnabled / releaseLogging​

public static void initializeLogging()
public static boolean isLoggingInitialized()
public static void setSdkLogCallbackEnabled(boolean enabled)
public static boolean releaseLogging()

Initialization, state query, slot authorization and release for the logging system.

Semantics:

  • initializeLogging() is idempotent: repeated calls return immediately and do not overwrite a callback you registered afterwards. It must be called once explicitly before runtime logs are forwarded to master.logs(); the DC event stream, which depends on the log stream, also requires initialization.
  • The log / crash callback is a single process-wide slot. If the application manages the callback entirely on its own, call setSdkLogCallbackEnabled(false) first and then initializeLogging(), and the SDK does not occupy the slot. Calling that method after initialization has no effect (the slot is already settled).
  • releaseLogging() returns the slot: it unregisters the callback the SDK registered, after which the application can register its own. Returns true = unregistered (or nothing was registered); false = unregistering failed.

Callbacks​

master.setLogCallback(...)​

public void setLogCallback(DarraCore.LogCallback callback)

public interface LogCallback {
void invoke(int category, Pointer message); // message = UTF-8 string pointer
}

Sets the log callback, which receives all runtime log messages.

Parameters:

  • callback — the callback; category is the log category value (matching LogCategory.value) and message is a UTF-8 string pointer

Example:

master.setLogCallback((category, message) -> {
String text = (message != null) ? message.getString(0, "UTF-8") : "";
System.out.println("[" + LogManager.LogCategory.fromValue(category) + "] " + text);
});

Semantics:

  • The log callback is a single process-wide slot. This method occupies it and makes a later StaticOther.initializeLogging() leave your callback untouched; after this call, logs no longer enter master.logs() automatically and you have to forward them yourself
  • To hand the slot back to the SDK, use StaticOther.releaseLogging()

master.setCrashCallback(...)​

public void setCrashCallback(DarraCore.CrashNotifyCallback callback)

public interface CrashNotifyCallback {
void invoke(int errorCode, Pointer details); // details = detail string pointer
}

Sets the crash notification callback, which fires when a fatal error occurs.

Parameters:

  • callback — the callback; errorCode is the error code and details is the detail (a UTF-8 string pointer)

Example:

master.setCrashCallback((errorCode, details) -> {
String text = (details != null) ? details.getString(0, "UTF-8") : "";
System.out.println("崩溃: code=" + errorCode + ", " + text); // Crash: code={errorCode}, {text}
});

Semantics: The crash slot is separate from the log slot: once registered, the SDK's crash notifications are still forwarded to each master instance through the master registry, so the two do not compete.

Performance constraints​

  • What is off by default is the high-frequency categories: PDO / MAILBOX / DEBUG are discarded by default. Before enabling them, remember they are for debugging; do not leave them on in production.
  • The view has a limit: at most 10000 entries are kept, and when the limit is exceeded the oldest batch is removed in one go. For long-term retention, write the entries to disk yourself in a listener.
  • The listener runs synchronously on the thread that writes the log: the implementation must return quickly and must not do heavy work; to update the UI, switch back to the UI thread (see the Swing / JavaFX example below).
  • Do not write logs from inside a listener: it re-enters.
  • An exception thrown by the callback itself is turned into an ERROR log entry on the spot and does not propagate into the runtime.

Full example​

// 0. Register the SDK log callback (idempotent)
StaticOther.initializeLogging();

// 1. Bind console output (show all logs live)
master.logs().addUpdatedListener(() -> {
LogManager.LogView logs = master.logs();
if (logs.getCount() > 0) {
System.out.println(logs.get(logs.getCount() - 1));
}
});

// 2. Enable debug logging (produced on the runtime side + let through on the view side)
StaticOther.enableDebugLogging(true);
StaticOther.enableMailboxLogging(true);
LogManager.getInstance().setMailboxTraceEnabled(true); // let mailbox logs through on the view side

// 3. Filter logs (errors and debug only)
master.logs().setFilter(
LogManager.LogCategory.ERROR,
LogManager.LogCategory.DEBUG);

// 4. Export all logs (ignoring the filter)
List<LogManager.LogEntry> allLogs = master.logs().getAll();
for (LogManager.LogEntry entry : allLogs) {
System.out.println(entry);
}
Swing / JavaFX binding

addUpdatedListener fires on a non-UI thread; when updating the UI you need to synchronize:

// Swing
master.logs().addUpdatedListener(() ->
SwingUtilities.invokeLater(() ->
listModel.addElement(master.logs().get(master.logs().getCount() - 1))));

// JavaFX
master.logs().addUpdatedListener(() ->
Platform.runLater(() ->
logItems.add(master.logs().get(master.logs().getCount() - 1))));

Failures and handling​

SymptomMeaningAction
No logs visible in master.logs()StaticOther.initializeLogging() was not called, or the slot was taken over by setLogCallbackInitialize first; if you took over the callback yourself, forward the entries into the view yourself
enablePDOLogging is on but no PDO logs appearThe view side has not let them throughCall setPdoTraceEnabled(true) and add PDO to setFilter(...)
enableMailboxLogging is on but no mailbox logs appearAs aboveCall setMailboxTraceEnabled(true) and let MAILBOX through
enableDebugLogging is on but no debug logs appearThe view side is decided by the startup parameterAdd -Ddarra.sdk.debug=true when starting the JVM
Only a few categories are wanted but nothing appearssetFilter(...) replaces the whole filterPass all the categories you want to show, not just the newly added one
get(index) throws IndexOutOfBoundsExceptionThe index is counted after filteringCall getCount() first, or use getFilteredSnapshot()
Older entries keep disappearingThe view is capped at 10000 entriesFor long-term retention, write them to disk yourself in a listener
The UI update reports a thread errorThe listener fires on the thread that writes the logSwitch back to the UI thread with SwingUtilities.invokeLater / Platform.runLater
initializeLogging() throwsThe runtime is not loadedLoad the runtime first, then initialize logging