Logging
Runtime log messages from the DLL are pushed to the application through a callback.
Log categories
log_category_t (defined in ethercat_types.h) is the set of values for the int category argument that a log_callback_t callback receives, one-to-one with the log categories of the library:
typedef enum {
LOG_CATEGORY_ERROR = 0, /* system errors and exceptions */
LOG_CATEGORY_WARNING = 1, /* potential problems */
LOG_CATEGORY_MESSAGE = 2, /* general information (state changes and so on) */
LOG_CATEGORY_MAILBOX = 3, /* CoE/SoE/FoE/EoE mailbox communication (off by default) */
LOG_CATEGORY_PDO = 4, /* PDO data-change log (off by default) */
LOG_CATEGORY_DEBUG = 5 /* detailed debug information (off by default) */
} log_category_t;
The categories the DLL pushes through log_callback_t stop at LOG_CATEGORY_DEBUG (5). The structured log entry log_entry_t in logging/logging.h also supports LOG_LOCAL = 6, which is for client-side local logging only and does not pass through the DLL.
The PDO log is written only when PDO data changes; it does not produce an entry every cycle. It is useful for debugging PDO mapping problems.
Log callback
SetLogCallback()
void SetLogCallback(log_callback_t callback);
Registers the log callback. All log messages from inside the DLL are delivered through this callback.
Callback type:
typedef void (*log_callback_t)(int category, const char* message);
Callback parameters:
category(int) — log category (log_category_t)message(const char*) — log message text
Crash notification
SetCrashCallback()
void SetCrashCallback(crash_callback_t callback);
Registers the crash notification callback for exceptions raised inside the runtime library. The runtime library also writes a crash log.
Callback type:
typedef void (*crash_callback_t)(int exception_code, const char* message);
Callback parameters:
exception_code(int) — exception codemessage(const char*) — exception message
Log switches
SetPDOLogging()
void SetPDOLogging(BOOL enable);
Enables or disables the PDO data exchange log (LOG_CATEGORY_PDO). Disabled by default.
SetMailboxLogging()
void SetMailboxLogging(BOOL enable);
Enables or disables the mailbox communication log (LOG_CATEGORY_MAILBOX). Disabled by default.
SetDebugLogging()
void SetDebugLogging(BOOL enable);
Enables or disables the debug log (LOG_CATEGORY_DEBUG). Disabled by default.
The PDO log produces a large amount of output at high PDO cycle rates; enable it for debugging only. Keep the defaults in production (ERROR/WARNING/MESSAGE only).
Debug log archiving
CloseDebugLog()
void CloseDebugLog(void);
Closes and flushes the debug log file; use it to archive logs before the program exits.
Full example
#include "ethercat.h"
#include <stdio.h>
/* log callback */
static void on_log(int category, const char* message)
{
const char* tags[] = {"错误", "警告", "消息", "邮箱", "PDO", "调试"}; /* Error / Warning / Message / Mailbox / PDO / Debug */
const char* tag = (category >= 0 && category <= 5) ? tags[category] : "未知"; /* unknown */
printf("[%s] %s\n", tag, message);
}
/* crash callback */
static void on_crash(int code, const char* message)
{
printf("[崩溃] 代码=%d: %s\n", code, message); /* [Crash] code=%d: %s */
}
int main(void)
{
/* register callbacks */
SetLogCallback(on_log);
SetCrashCallback(on_crash);
/* enable detailed logging (for debugging) */
SetDebugLogging(TRUE);
SetMailboxLogging(TRUE);
/* SetPDOLogging(TRUE); -- enable only for debugging */
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
getchar();
Stop(master);
Dispose(master);
return 0;
}
Log callbacks fire on a non-UI thread; to show messages in a GUI application, post them to the UI thread.