From b7d07d56c03d88f566e4a2228f52288d4c9808e5 Mon Sep 17 00:00:00 2001 From: shash256 <111925100+shash256@users.noreply.github.com> Date: Mon, 5 Jan 2026 19:06:18 +0700 Subject: [PATCH] feat: reduce log file --- sds/Makefile | 2 +- sds/sds.go | 27 ++++++++ sds/sds_test.go | 168 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 1 deletion(-) diff --git a/sds/Makefile b/sds/Makefile index b51a569..210a20e 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -20,7 +20,7 @@ prepare: cd $(THIRD_PARTY_DIR) && \ git clone $(NIM_SDS_REPO) && \ cd $(NIM_SDS_DIR) && \ - git checkout master; \ + git checkout feat-reduce-logs; \ make update; \ else \ echo "nim-sds repository already exists."; \ diff --git a/sds/sds.go b/sds/sds.go index cd8168e..ef3cc85 100644 --- a/sds/sds.go +++ b/sds/sds.go @@ -128,6 +128,10 @@ package sds SdsStartPeriodicTasks(rmCtx, (SdsCallBack) SdsGoCallback, resp); } + static int cGoSdsSetLogFile(const char* logFilePath) { + return SdsSetLogFile(logFilePath); + } + */ import "C" import ( @@ -138,6 +142,7 @@ import ( "sync" "time" "unsafe" + "os" ) const requestTimeout = 30 * time.Second @@ -560,3 +565,25 @@ func (rm *ReliabilityManager) StartPeriodicTasks() error { return errors.New(errMsg) } + +// SetLogFile configures the log file path for nim-sds logging +// This allows status-go to specify where nim-sds should write its logs +func SetLogFile(logFilePath string) error { + if logFilePath == "" { + return errors.New("log file path cannot be empty") + } + + cLogFilePath := C.CString(logFilePath) + defer C.free(unsafe.Pointer(cLogFilePath)) + + result := C.cGoSdsSetLogFile(cLogFilePath) + if result != C.RET_OK { + return errors.New("failed to set log file path") + } + + // Set environment variable for nim-sds to use + os.Setenv("SDS_LOG_FILE", logFilePath) + + Debug("Successfully set nim-sds log file path to: %s", logFilePath) + return nil +} \ No newline at end of file diff --git a/sds/sds_test.go b/sds/sds_test.go index 5e80cf7..a695d91 100644 --- a/sds/sds_test.go +++ b/sds/sds_test.go @@ -1,6 +1,9 @@ package sds import ( + "fmt" + "os" + "path/filepath" "sync" "testing" "time" @@ -677,3 +680,168 @@ func TestMultiChannelCallbacks(t *testing.T) { require.Equal(t, channel2, readyMessages[ackID2_ch2], "OnMessageReady for ack2 has incorrect channel") require.Len(t, readyMessages, 2, "Expected exactly 2 ready messages") } + +// Test logging configuration functionality +func TestSetLogFile(t *testing.T) { + // Create a temporary directory for test logs + tempDir, err := os.MkdirTemp("", "nim-sds-test-logs") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + // Test 1: Set a valid log file path + testLogFile := filepath.Join(tempDir, "nim-sds-test.log") + + err = SetLogFile(testLogFile) + require.NoError(t, err) + + // Test 2: Verify the environment variable was set correctly by creating a ReliabilityManager + // and checking that it doesn't return errors related to logging + rm, err := NewReliabilityManager() + require.NoError(t, err) + defer rm.Cleanup() + + // Test 3: Try to generate some logs (this will trigger error logs internally) + testMessage := []byte("Test log message") + testMessageID := MessageID("test-log-msg") + testChannelID := "test-log-channel" + + // This should work and potentially generate some logs + _, err = rm.WrapOutgoingMessage(testMessage, testMessageID, testChannelID) + if err != nil { + t.Logf("WrapOutgoingMessage failed (expected in some environments): %v", err) + } + + // Give some time for potential async logging + time.Sleep(100 * time.Millisecond) + + // Test 4: Check that log directory was created (if any logs were written) + if _, err := os.Stat(tempDir); os.IsNotExist(err) { + t.Log("Log directory was not created (no logs generated, which is fine)") + } else { + t.Log("Log directory exists (logs may have been generated)") + + // Check if log file was created + if _, err := os.Stat(testLogFile); err == nil { + t.Log("Log file was created successfully") + + // Read log file content to verify it's not empty + content, err := os.ReadFile(testLogFile) + if err != nil { + t.Logf("Could not read log file: %v", err) + } else { + t.Logf("Log file content length: %d bytes", len(content)) + if len(content) > 0 { + t.Logf("Log file contains data: %s", string(content)[:min(100, len(content))]) + } + } + } else { + t.Log("Log file was not created (no error logs generated)") + } + } + + // Test 5: Test error case - empty log file path + err = SetLogFile("") + require.Error(t, err, "Expected error when setting empty log file path") + + // Test 6: Test error case - nil path should work (it just sets empty env var) + err = SetLogFile("/tmp/test-path.log") + require.NoError(t, err, "Setting valid path should work") +} + +// Test hybrid logging behavior by checking environment variable setting +func TestLogFileEnvironmentVariable(t *testing.T) { + // Save original environment + originalLogFile := os.Getenv("STATUS_SDS_LOG_FILE") + defer func() { + if originalLogFile != "" { + os.Setenv("STATUS_SDS_LOG_FILE", originalLogFile) + } else { + os.Unsetenv("STATUS_SDS_LOG_FILE") + } + }() + + // Test that SetLogFile sets the environment variable correctly + testPath := "/test/path/nim-sds.log" + err := SetLogFile(testPath) + require.NoError(t, err) + + // Check that environment variable was set + envValue := os.Getenv("STATUS_SDS_LOG_FILE") + require.Equal(t, testPath, envValue, "Environment variable STATUS_SDS_LOG_FILE should match set path") + + // Test that we can create ReliabilityManager with custom log file + rm, err := NewReliabilityManager() + require.NoError(t, err) + defer rm.Cleanup() + + t.Log("Successfully created ReliabilityManager with custom log file path") +} + +// Test logging configuration with real message operations +func TestLoggingWithMessageOperations(t *testing.T) { + // Create temp directory + tempDir, err := os.MkdirTemp("", "nim-sds-msg-logs") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + // Set log file + logFile := filepath.Join(tempDir, "message-ops.log") + err = SetLogFile(logFile) + require.NoError(t, err) + + // Create RMs + senderRM, err := NewReliabilityManager() + require.NoError(t, err) + defer senderRM.Cleanup() + + receiverRM, err := NewReliabilityManager() + require.NoError(t, err) + defer receiverRM.Cleanup() + + // Perform operations that might generate logs + channelID := "logging-test-channel" + + // Send messages + for i := 0; i < 5; i++ { + msgID := MessageID(fmt.Sprintf("log-test-msg-%d", i)) + payload := []byte(fmt.Sprintf("Test message %d for logging", i)) + + wrappedMsg, err := senderRM.WrapOutgoingMessage(payload, msgID, channelID) + if err != nil { + t.Logf("WrapOutgoingMessage %d failed: %v", i, err) + continue + } + + // Unwrap on receiver + _, err = receiverRM.UnwrapReceivedMessage(wrappedMsg) + if err != nil { + t.Logf("UnwrapReceivedMessage %d failed: %v", i, err) + } + } + + // Wait for potential async logging + time.Sleep(200 * time.Millisecond) + + // Check if logs were written + if _, err := os.Stat(logFile); err == nil { + content, err := os.ReadFile(logFile) + if err == nil && len(content) > 0 { + t.Logf("Log file created with %d bytes of content", len(content)) + // Don't log full content as it might be large + } else { + t.Log("Log file exists but has no content") + } + } else { + t.Log("No log file was created (no errors generated)") + } + + t.Log("Message operations completed successfully with logging configured") +} + +// Helper function for min +func min(a, b int) int { + if a < b { + return a + } + return b +}