diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-17 11:57:52 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-17 11:57:52 +0100 |
| commit | 48a9df043df64887cb99e03d7613379c947d11d8 (patch) | |
| tree | 897d94bcc55c481a82878c5d2de5ec3369df33ed /src | |
| parent | a54421dd976fd8081e96c11c2621076876c9986b (diff) | |
| download | BobinkCOpcUa-48a9df043df64887cb99e03d7613379c947d11d8.tar.gz BobinkCOpcUa-48a9df043df64887cb99e03d7613379c947d11d8.zip | |
Add configurable log level as optional CLI argument
All three programs now accept an optional second argument [log-level]
(trace, debug, info, warning, error, fatal) defaulting to info. The
level is applied by setting the logger context pointer directly,
avoiding a memory leak that would occur from overwriting the
heap-allocated logger struct. Also documents the ASan leak-check
workflow in CLAUDE.md.
Diffstat (limited to 'src')
| -rw-r--r-- | src/client_find_servers.c | 17 | ||||
| -rw-r--r-- | src/common.c | 20 | ||||
| -rw-r--r-- | src/common.h | 11 | ||||
| -rw-r--r-- | src/server_lds.c | 21 | ||||
| -rw-r--r-- | src/server_register.c | 19 |
5 files changed, 82 insertions, 6 deletions
diff --git a/src/client_find_servers.c b/src/client_find_servers.c index e50623f..a85b63f 100644 --- a/src/client_find_servers.c +++ b/src/client_find_servers.c @@ -211,10 +211,21 @@ readServerTime (UA_Client *client, int main (int argc, char **argv) { - if (argc != 2) + if (argc < 2 || argc > 3) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Usage: %s <config-file>", argv[0]); + "Usage: %s <config-file> [log-level]", argv[0]); + return EXIT_FAILURE; + } + + const char *logLevelStr = (argc == 3) ? argv[2] : "info"; + int logLevel = parseLogLevel (logLevelStr); + if (logLevel < 0) + { + UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, + "Unknown log level: %s " + "(expected trace, debug, info, warning, error, fatal)", + logLevelStr); return EXIT_FAILURE; } @@ -302,6 +313,8 @@ main (int argc, char **argv) configFree (&cfg); return EXIT_FAILURE; } + UA_ClientConfig *clientConfig = UA_Client_getConfig (client); + clientConfig->logging->context = (void *)(uintptr_t)logLevel; UA_ApplicationDescription *applicationDescriptionArray = NULL; size_t applicationDescriptionArraySize = 0; diff --git a/src/common.c b/src/common.c index d102868..7d378f1 100644 --- a/src/common.c +++ b/src/common.c @@ -59,6 +59,26 @@ loadFile (const char *const path) * Security Helpers * ======================================================================== */ +int +parseLogLevel (const char *name) +{ + static const struct + { + const char *name; + UA_LogLevel level; + } levels[] = { + { "trace", UA_LOGLEVEL_TRACE }, { "debug", UA_LOGLEVEL_DEBUG }, + { "info", UA_LOGLEVEL_INFO }, { "warning", UA_LOGLEVEL_WARNING }, + { "error", UA_LOGLEVEL_ERROR }, { "fatal", UA_LOGLEVEL_FATAL }, + }; + for (size_t i = 0; i < sizeof (levels) / sizeof (levels[0]); i++) + { + if (strcmp (name, levels[i].name) == 0) + return (int)levels[i].level; + } + return -1; +} + UA_MessageSecurityMode parseSecurityMode (const char *name) { diff --git a/src/common.h b/src/common.h index e3d2f4c..e8c0c78 100644 --- a/src/common.h +++ b/src/common.h @@ -44,6 +44,17 @@ UA_Server *createSecureServer (UA_UInt16 port, const char *applicationUri, UA_StatusCode *retval); /** + * @brief Parses a log-level name into the corresponding UA_LogLevel value. + * + * Accepted names (case-sensitive): "trace", "debug", "info", "warning", + * "error", "fatal". + * + * @param name Log-level name string. + * @return The matching UA_LogLevel, or -1 if the name is not recognized. + */ +int parseLogLevel (const char *name); + +/** * @brief Parses a security mode name into the corresponding enum value. * * Accepted names: "None", "Sign", "SignAndEncrypt". diff --git a/src/server_lds.c b/src/server_lds.c index c6960d5..2fe508f 100644 --- a/src/server_lds.c +++ b/src/server_lds.c @@ -35,10 +35,21 @@ main (int argc, char *argv[]) signal (SIGINT, stopHandler); signal (SIGTERM, stopHandler); - if (argc != 2) + if (argc < 2 || argc > 3) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Usage: %s <config-file>", argv[0]); + "Usage: %s <config-file> [log-level]", argv[0]); + return EXIT_FAILURE; + } + + const char *logLevelStr = (argc == 3) ? argv[2] : "info"; + int logLevel = parseLogLevel (logLevelStr); + if (logLevel < 0) + { + UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, + "Unknown log level: %s " + "(expected trace, debug, info, warning, error, fatal)", + logLevelStr); return EXIT_FAILURE; } @@ -116,6 +127,12 @@ main (int argc, char *argv[]) } UA_ServerConfig *serverConfig = UA_Server_getConfig (server); + serverConfig->logging->context = (void *)(uintptr_t)logLevel; + + /* Some OPC UA stacks omit the timestamp in the request header. The + default behaviour rejects these requests with BadInvalidTimestamp. + Downgrade to a warning so third-party servers can still register. */ + serverConfig->verifyRequestTimestamp = UA_RULEHANDLING_WARN; /* Configure access control after server creation because UA_ServerConfig_setDefaultWithSecurityPolicies (called by diff --git a/src/server_register.c b/src/server_register.c index ae8e959..5bdba8e 100644 --- a/src/server_register.c +++ b/src/server_register.c @@ -42,10 +42,21 @@ main (int argc, char **argv) signal (SIGINT, stopHandler); signal (SIGTERM, stopHandler); - if (argc != 2) + if (argc < 2 || argc > 3) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Usage: %s <config-file>", argv[0]); + "Usage: %s <config-file> [log-level]", argv[0]); + return EXIT_FAILURE; + } + + const char *logLevelStr = (argc == 3) ? argv[2] : "info"; + int logLevel = parseLogLevel (logLevelStr); + if (logLevel < 0) + { + UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, + "Unknown log level: %s " + "(expected trace, debug, info, warning, error, fatal)", + logLevelStr); return EXIT_FAILURE; } @@ -182,6 +193,7 @@ main (int argc, char **argv) } UA_ServerConfig *serverConfig = UA_Server_getConfig (server); + serverConfig->logging->context = (void *)(uintptr_t)logLevel; /* Configure access control after server creation because UA_ServerConfig_setDefaultWithSecurityPolicies (called by @@ -224,6 +236,7 @@ main (int argc, char **argv) configFree (&cfg); return EXIT_FAILURE; } + clientConfig.logging->context = (void *)(uintptr_t)logLevel; if (clientUsername) UA_ClientConfig_setAuthenticationUsername (&clientConfig, clientUsername, clientPassword); @@ -254,6 +267,7 @@ main (int argc, char **argv) trustPaths, trustSize, securityMode, securityPolicyUri); if (retval == UA_STATUSCODE_GOOD) { + clientConfig.logging->context = (void *)(uintptr_t)logLevel; if (clientUsername) UA_ClientConfig_setAuthenticationUsername ( &clientConfig, clientUsername, clientPassword); @@ -278,6 +292,7 @@ main (int argc, char **argv) trustSize, securityMode, securityPolicyUri); if (retval == UA_STATUSCODE_GOOD) { + clientConfig.logging->context = (void *)(uintptr_t)logLevel; if (clientUsername) UA_ClientConfig_setAuthenticationUsername ( &clientConfig, clientUsername, clientPassword); |
