Add UpdateLogLevel method

This commit is contained in:
Arnaud 2025-10-07 08:34:44 +02:00
parent c5b302f5cf
commit da4b5bef8d
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
7 changed files with 108 additions and 15 deletions

View File

@ -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

View File

@ -5,7 +5,6 @@ package codex
#cgo LDFLAGS: -L../vendor/nim-codex/ -Wl,-rpath,../vendor/nim-codex/build
#include <stdlib.h>
#include <stdbool.h>
#include "../vendor/nim-codex/library/libcodex.h"
typedef struct {

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdbool.h>
#include "../vendor/nim-codex/library/libcodex.h"
extern void callback(int ret, char* msg, size_t len, void* resp);

View File

@ -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")

View File

@ -2,13 +2,21 @@ package codex
/*
#include "bridge.h"
#include <stdlib.h>
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
}

View File

@ -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'")
}
}

2
vendor/nim-codex vendored

@ -1 +1 @@
Subproject commit 40cc0195dc99eecd4ea3a4396bb9f1dbd107fd86
Subproject commit c028073053dfecf918ed4c0cff4fc953f7f20a56