From da4b5bef8df481cc3c65b18d125c39468adf3506 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Tue, 7 Oct 2025 08:34:44 +0200 Subject: [PATCH] Add UpdateLogLevel method --- .github/workflows/ci.yml | 8 +++++ codex/bridge.go | 1 - codex/bridge.h | 1 + codex/codex.go | 14 ++------- codex/debug.go | 31 ++++++++++++++++++- codex/debug_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ vendor/nim-codex | 2 +- 7 files changed, 108 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0be4523..69932b5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,14 @@ jobs: with: go-version-file: go.mod + - name: Build libcodex + if: steps.cache-libcodex.outputs.cache-hit != 'true' + run: | + sudo apt-get update + sudo apt-get install -y build-essential cmake curl git rustc cargo + make update + make libcodex + - name: Record submodule commit run: git -C vendor/nim-codex rev-parse HEAD > vendor/nim-codex/.codex-commit diff --git a/codex/bridge.go b/codex/bridge.go index 9403472..23d897f 100644 --- a/codex/bridge.go +++ b/codex/bridge.go @@ -5,7 +5,6 @@ package codex #cgo LDFLAGS: -L../vendor/nim-codex/ -Wl,-rpath,../vendor/nim-codex/build #include - #include #include "../vendor/nim-codex/library/libcodex.h" typedef struct { diff --git a/codex/bridge.h b/codex/bridge.h index c2809fe..3224ad1 100644 --- a/codex/bridge.h +++ b/codex/bridge.h @@ -1,5 +1,6 @@ #pragma once +#include #include "../vendor/nim-codex/library/libcodex.h" extern void callback(int ret, char* msg, size_t len, void* resp); \ No newline at end of file diff --git a/codex/codex.go b/codex/codex.go index ead436e..d2e43a2 100644 --- a/codex/codex.go +++ b/codex/codex.go @@ -56,18 +56,6 @@ import ( "unsafe" ) -type LogLevel string - -const ( - Trace LogLevel = "TRACE" - Debug LogLevel = "DEBUG" - Info LogLevel = "INFO" - Notice LogLevel = "NOTICE" - Warn LogLevel = "WARN" - Error LogLevel = "ERROR" - Fatal LogLevel = "FATAL" -) - type LogFormat string const ( @@ -165,6 +153,7 @@ func (node CodexNode) StartAsync(onDone func(error)) { // Stop stops the Codex node. func (node CodexNode) Stop() error { bridge := newBridgeCtx() + defer bridge.free() if C.cGoCodexStop(node.ctx, bridge.resp) != C.RET_OK { return bridge.callError("cGoCodexStop") @@ -178,6 +167,7 @@ func (node CodexNode) Stop() error { // The node must be stopped before calling this method. func (node CodexNode) Destroy() error { bridge := newBridgeCtx() + defer bridge.free() if C.cGoCodexDestroy(node.ctx, bridge.resp) != C.RET_OK { return bridge.callError("cGoCodexDestroy") diff --git a/codex/debug.go b/codex/debug.go index 4c96241..e550c6f 100644 --- a/codex/debug.go +++ b/codex/debug.go @@ -2,13 +2,21 @@ package codex /* #include "bridge.h" + #include static int cGoCodexDebug(void* codexCtx, void* resp) { return codex_debug(codexCtx, (CodexCallback) callback, resp); } + + static int cGoCodexLogLevel(void* codexCtx, char* logLevel, void* resp) { + return codex_log_level(codexCtx, logLevel, (CodexCallback) callback, resp); + } */ import "C" -import "encoding/json" +import ( + "encoding/json" + "unsafe" +) type Node struct { NodeId string `json:"nodeId"` @@ -50,3 +58,24 @@ func (node CodexNode) Debug() (DebugInfo, error) { err = json.Unmarshal([]byte(value), &info) return info, err } + +// UpdateLogLevel updates Chronicles’ runtime logging configuration. +// You can pass a plain level: TRACE, DEBUG, INFO, NOTICE, WARN, ERROR, FATAL. +// The default level is TRACE. +// You can also use Chronicles topic directives. So for example if you want +// to update the general level to INFO but want to see TRACE logs for the codexlib +// topic, you can pass "INFO,codexlib:TRACE". +func (node CodexNode) UpdateLogLevel(logLevel string) error { + bridge := newBridgeCtx() + defer bridge.free() + + var cLogLevel = C.CString(string(logLevel)) + defer C.free(unsafe.Pointer(cLogLevel)) + + if C.cGoCodexLogLevel(node.ctx, cLogLevel, bridge.resp) != C.RET_OK { + return bridge.callError("cGoCodexLogLevel") + } + + _, err := bridge.wait() + return err +} diff --git a/codex/debug_test.go b/codex/debug_test.go index c5dd0b6..b5cafcc 100644 --- a/codex/debug_test.go +++ b/codex/debug_test.go @@ -1,6 +1,8 @@ package codex import ( + "os" + "strings" "testing" ) @@ -21,3 +23,67 @@ func TestDebug(t *testing.T) { t.Error("Debug info AnnounceAddresses is empty") } } + +func TestUpdateLogLevel(t *testing.T) { + tmpFile, err := os.CreateTemp("", "codex-log-*.log") + if err != nil { + t.Fatalf("Failed to create temp log file: %v", err) + } + defer os.Remove(tmpFile.Name()) + + node, err := CodexNew(CodexConfig{ + LogFile: tmpFile.Name(), + }) + if err != nil { + t.Fatalf("Failed to create Codex node: %v", err) + } + + t.Cleanup(func() { + if err := node.Stop(); err != nil { + t.Logf("cleanup codex: %v", err) + } + + if err := node.Destroy(); err != nil { + t.Logf("cleanup codex: %v", err) + } + }) + + if err := node.Start(); err != nil { + t.Fatalf("Failed to start Codex node: %v", err) + } + + content, err := os.ReadFile(tmpFile.Name()) + if err != nil { + t.Fatalf("Failed to read log file: %v", err) + } + if !strings.Contains(string(content), "Started codex node") { + t.Errorf("Log file does not contain 'Started codex node' %s", string(content)) + } + + if err := node.Stop(); err != nil { + t.Fatalf("Failed to stop Codex node: %v", err) + } + + err = node.UpdateLogLevel("ERROR") + if err != nil { + t.Fatalf("UpdateLogLevel call failed: %v", err) + } + + if err := os.WriteFile(tmpFile.Name(), []byte{}, 0644); err != nil { + t.Fatalf("Failed to clear log file: %v", err) + } + + err = node.Start() + if err != nil { + t.Fatalf("Failed to start Codex node: %v", err) + } + + content, err = os.ReadFile(tmpFile.Name()) + if err != nil { + t.Fatalf("Failed to read log file: %v", err) + } + + if strings.Contains(string(content), "Starting discovery node") { + t.Errorf("Log file contains 'Starting discovery node'") + } +} diff --git a/vendor/nim-codex b/vendor/nim-codex index 40cc019..c028073 160000 --- a/vendor/nim-codex +++ b/vendor/nim-codex @@ -1 +1 @@ -Subproject commit 40cc0195dc99eecd4ea3a4396bb9f1dbd107fd86 +Subproject commit c028073053dfecf918ed4c0cff4fc953f7f20a56