Skip to content

Commit a58c911

Browse files
committed
Add SetLoggingOptions() to configure options globally
Also adapt `NewLoggingConnection()` and its usages
1 parent 8502bc8 commit a58c911

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

kernel/logging_connection.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,12 @@ func go_log_callback_bridge(user_data unsafe.Pointer, message *C.char, message_l
5151
//
5252
// Parameters:
5353
// - callback: Function that will receive log messages
54-
// - options: Formatting options for log messages (timestamps, thread names, etc.)
5554
//
5655
// Returns an error if the logging connection cannot be created.
57-
func NewLoggingConnection(callback LogCallback, options LoggingOptions) (*LoggingConnection, error) {
58-
cOptions := C.btck_LoggingOptions{
59-
log_timestamps: boolToInt(options.LogTimestamps),
60-
log_time_micros: boolToInt(options.LogTimeMicros),
61-
log_threadnames: boolToInt(options.LogThreadNames),
62-
log_sourcelocations: boolToInt(options.LogSourceLocations),
63-
always_print_category_levels: boolToInt(options.AlwaysPrintCategoryLevel),
64-
}
65-
56+
func NewLoggingConnection(callback LogCallback) (*LoggingConnection, error) {
6657
callbackHandle := cgo.NewHandle(callback)
6758
ptr := C.btck_logging_connection_create((C.btck_LogCallback)(C.go_log_callback_bridge),
68-
unsafe.Pointer(callbackHandle), C.btck_DestroyCallback(C.go_delete_handle), cOptions)
59+
unsafe.Pointer(callbackHandle), C.btck_DestroyCallback(C.go_delete_handle))
6960
if ptr == nil {
7061
callbackHandle.Delete()
7162
return nil, &InternalError{"Failed to create logging connection"}
@@ -74,6 +65,23 @@ func NewLoggingConnection(callback LogCallback, options LoggingOptions) (*Loggin
7465
return &LoggingConnection{uniqueHandle: h}, nil
7566
}
7667

68+
// SetLoggingOptions configures the formatting options for the global internal logger.
69+
//
70+
// This changes global settings and affects all existing LoggingConnection instances.
71+
//
72+
// Parameters:
73+
// - options: Formatting options for log messages (timestamps, thread names, etc.)
74+
func SetLoggingOptions(options LoggingOptions) {
75+
cOptions := C.btck_LoggingOptions{
76+
log_timestamps: boolToInt(options.LogTimestamps),
77+
log_time_micros: boolToInt(options.LogTimeMicros),
78+
log_threadnames: boolToInt(options.LogThreadNames),
79+
log_sourcelocations: boolToInt(options.LogSourceLocations),
80+
always_print_category_levels: boolToInt(options.AlwaysPrintCategoryLevel),
81+
}
82+
C.btck_logging_set_options(cOptions)
83+
}
84+
7785
// DisableLogging permanently disables the global internal logger.
7886
//
7987
// No log messages will be buffered internally after this is called, and the buffer

kernel/logging_connection_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func TestLoggingOptions(t *testing.T) {
4444

4545
for _, tc := range testCases {
4646
t.Run(tc.name, func(t *testing.T) {
47-
conn, err := NewLoggingConnection(func(_ string) {}, tc.options)
47+
SetLoggingOptions(tc.options)
48+
conn, err := NewLoggingConnection(func(_ string) {})
4849
if err != nil {
4950
t.Fatalf("NewLoggingConnection() error = %v", err)
5051
}

utils/logger.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func NewLogger(handler slog.Handler, options kernel.LoggingOptions) (*Logger, er
2727
parser: parser,
2828
}
2929

30+
// Set global logging options
31+
kernel.SetLoggingOptions(options)
32+
3033
// Create logging connection with our message handler
31-
conn, err := kernel.NewLoggingConnection(viewer.handleMessage, options)
34+
conn, err := kernel.NewLoggingConnection(viewer.handleMessage)
3235
if err != nil {
3336
return nil, fmt.Errorf("failed to create logging connection: %w", err)
3437
}

0 commit comments

Comments
 (0)