Skip to content

Commit 818dbb8

Browse files
committed
enable cDebugFile in verbose mode
1 parent d3de1cf commit 818dbb8

File tree

6 files changed

+207
-196
lines changed

6 files changed

+207
-196
lines changed

bridge/cgo_bridge.go

Lines changed: 121 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import (
3030
"go.uber.org/zap"
3131
)
3232

33-
const DEBUG_FILE = "sqlite_rsync_debug.log"
34-
3533
var (
3634
handleCounter int64
3735
handleMap = make(map[int64]cgo.Handle)
3836
handleMutex sync.RWMutex
3937
)
4038

39+
const DEBUG_FILE = "sqlrsync-debug"
40+
4141
// GetDatabaseInfo wraps the C function to get database info
4242
func GetDatabaseInfo(dbPath string) (*DatabaseInfo, error) {
4343
return cgoGetDatabaseInfo(dbPath)
@@ -67,12 +67,12 @@ func cgoGetDatabaseInfo(dbPath string) (*DatabaseInfo, error) {
6767
}
6868

6969
// RunOriginSync wraps the C function to run origin synchronization
70-
func RunOriginSync(dbPath string, dryRun bool, client *Client) error {
70+
func RunOriginSync(dbPath string, dryRun bool, client *BridgeClient) error {
7171
return cgoRunOriginSync(dbPath, dryRun, client)
7272
}
7373

7474
// cgoRunOriginSync wraps the C function to run origin synchronization
75-
func cgoRunOriginSync(dbPath string, dryRun bool, client *Client) error {
75+
func cgoRunOriginSync(dbPath string, dryRun bool, client *BridgeClient) error {
7676
// Prepare C configuration
7777
cDbPath := C.CString(dbPath)
7878
defer C.free(unsafe.Pointer(cDbPath))
@@ -86,9 +86,9 @@ func cgoRunOriginSync(dbPath string, dryRun bool, client *Client) error {
8686
wal_only: 0,
8787
error_file: nil,
8888
}
89-
// Set debug_file if DEBUG_FILE is defined (non-empty)
90-
if DEBUG_FILE != "" {
91-
cDebugFile := C.CString(DEBUG_FILE + ".origin.log")
89+
// Set debugFile if debugFile is defined (non-empty)
90+
if client.Config.EnableSQLiteRsyncLogging {
91+
cDebugFile := C.CString(DEBUG_FILE + "-push.log")
9292
defer C.free(unsafe.Pointer(cDebugFile))
9393
config.error_file = cDebugFile
9494
}
@@ -100,11 +100,11 @@ func cgoRunOriginSync(dbPath string, dryRun bool, client *Client) error {
100100
// Create cgo handle for the client and store it in map
101101
handle := cgo.NewHandle(client)
102102
handleID := atomic.AddInt64(&handleCounter, 1)
103-
103+
104104
handleMutex.Lock()
105105
handleMap[handleID] = handle
106106
handleMutex.Unlock()
107-
107+
108108
defer func() {
109109
handleMutex.Lock()
110110
delete(handleMap, handleID)
@@ -117,7 +117,7 @@ func cgoRunOriginSync(dbPath string, dryRun bool, client *Client) error {
117117
cHandleID := C.malloc(C.sizeof_long)
118118
defer C.free(cHandleID)
119119
*(*C.long)(cHandleID) = C.long(handleID)
120-
120+
121121
ioCtx := C.sqlite_rsync_io_context_t{
122122
user_data: cHandleID,
123123
read_func: C.read_callback_t(C.go_local_read_callback),
@@ -150,16 +150,16 @@ func cgoCleanup() {
150150
func go_local_read_callback(userData unsafe.Pointer, buffer *C.uint8_t, size C.int) C.int {
151151

152152
handleID := int64(*(*C.long)(userData))
153-
153+
154154
handleMutex.RLock()
155155
handle, ok := handleMap[handleID]
156156
handleMutex.RUnlock()
157-
157+
158158
if !ok {
159159
return -1
160160
}
161-
162-
client := handle.Value().(*Client)
161+
162+
client := handle.Value().(*BridgeClient)
163163

164164
if client.ReadFunc == nil {
165165
client.Logger.Error("Read function not set")
@@ -186,16 +186,16 @@ func go_local_read_callback(userData unsafe.Pointer, buffer *C.uint8_t, size C.i
186186
//export go_local_write_callback
187187
func go_local_write_callback(userData unsafe.Pointer, buffer *C.uint8_t, size C.int) C.int {
188188
handleID := int64(*(*C.long)(userData))
189-
189+
190190
handleMutex.RLock()
191191
handle, ok := handleMap[handleID]
192192
handleMutex.RUnlock()
193-
193+
194194
if !ok {
195195
return -1
196196
}
197-
198-
client := handle.Value().(*Client)
197+
198+
client := handle.Value().(*BridgeClient)
199199

200200
if client.WriteFunc == nil {
201201
client.Logger.Error("Write function not set")
@@ -216,117 +216,117 @@ func go_local_write_callback(userData unsafe.Pointer, buffer *C.uint8_t, size C.
216216
}
217217

218218
// RunReplicaSync wraps the C function to run replica synchronization
219-
func RunReplicaSync(originDbPath, replicaDbPath string, client *Client) error {
220-
return cgoRunReplicaSync(originDbPath, replicaDbPath, client)
219+
func RunReplicaSync(originDbPath, replicaDbPath string, client *BridgeClient) error {
220+
return cgoRunReplicaSync(originDbPath, replicaDbPath, client)
221221
}
222222

223223
// RunDirectSync wraps the C function to run direct local synchronization
224224
func RunDirectSync(originDbPath, replicaDbPath string, dryRun bool, verbose int) error {
225-
return cgoRunDirectSync(originDbPath, replicaDbPath, dryRun, verbose)
225+
return cgoRunDirectSync(originDbPath, replicaDbPath, dryRun, verbose)
226226
}
227227

228228
// cgoRunReplicaSync wraps the C function to run replica synchronization
229-
func cgoRunReplicaSync(originDbPath, replicaDbPath string, client *Client) error {
230-
// Prepare C configuration
231-
cOriginPath := C.CString(originDbPath)
232-
defer C.free(unsafe.Pointer(cOriginPath))
233-
234-
cReplicaPath := C.CString(replicaDbPath)
235-
defer C.free(unsafe.Pointer(cReplicaPath))
236-
237-
config := C.sqlite_rsync_config_t{
238-
origin_path: cOriginPath,
239-
replica_path: cReplicaPath,
240-
protocol_version: C.PROTOCOL_VERSION,
241-
verbose_level: 0,
242-
dry_run: 0,
243-
wal_only: 0,
244-
error_file: nil,
245-
}
246-
247-
// Set debug_file if DEBUG_FILE is defined
248-
if DEBUG_FILE != "" {
249-
cDebugFile := C.CString(DEBUG_FILE + "replica.log")
250-
defer C.free(unsafe.Pointer(cDebugFile))
251-
config.error_file = cDebugFile
252-
}
253-
254-
// Create cgo handle for the client
255-
handle := cgo.NewHandle(client)
256-
handleID := atomic.AddInt64(&handleCounter, 1)
257-
258-
handleMutex.Lock()
259-
handleMap[handleID] = handle
260-
handleMutex.Unlock()
261-
262-
defer func() {
263-
handleMutex.Lock()
264-
delete(handleMap, handleID)
265-
handleMutex.Unlock()
266-
handle.Delete()
267-
}()
268-
269-
// Setup I/O context with Go callbacks
270-
cHandleID := C.malloc(C.sizeof_long)
271-
defer C.free(cHandleID)
272-
*(*C.long)(cHandleID) = C.long(handleID)
273-
274-
ioCtx := C.sqlite_rsync_io_context_t{
275-
user_data: cHandleID,
276-
read_func: C.read_callback_t(C.go_local_read_callback),
277-
write_func: C.write_callback_t(C.go_local_write_callback),
278-
}
279-
280-
// Run the replica synchronization
281-
result := C.sqlite_rsync_run_replica(&config, &ioCtx)
282-
if result != 0 {
283-
return &SQLiteRsyncError{
284-
Code: int(result),
285-
Message: "replica sync failed",
286-
}
287-
}
288-
289-
return nil
229+
func cgoRunReplicaSync(originDbPath, replicaDbPath string, client *BridgeClient) error {
230+
// Prepare C configuration
231+
cOriginPath := C.CString(originDbPath)
232+
defer C.free(unsafe.Pointer(cOriginPath))
233+
234+
cReplicaPath := C.CString(replicaDbPath)
235+
defer C.free(unsafe.Pointer(cReplicaPath))
236+
237+
config := C.sqlite_rsync_config_t{
238+
origin_path: cOriginPath,
239+
replica_path: cReplicaPath,
240+
protocol_version: C.PROTOCOL_VERSION,
241+
verbose_level: 0,
242+
dry_run: 0,
243+
wal_only: 0,
244+
error_file: nil,
245+
}
246+
247+
// Set debugFile if debugFile is defined
248+
if client.Config.EnableSQLiteRsyncLogging {
249+
cDebugFile := C.CString(DEBUG_FILE + "-push.log")
250+
defer C.free(unsafe.Pointer(cDebugFile))
251+
config.error_file = cDebugFile
252+
}
253+
254+
// Create cgo handle for the client
255+
handle := cgo.NewHandle(client)
256+
handleID := atomic.AddInt64(&handleCounter, 1)
257+
258+
handleMutex.Lock()
259+
handleMap[handleID] = handle
260+
handleMutex.Unlock()
261+
262+
defer func() {
263+
handleMutex.Lock()
264+
delete(handleMap, handleID)
265+
handleMutex.Unlock()
266+
handle.Delete()
267+
}()
268+
269+
// Setup I/O context with Go callbacks
270+
cHandleID := C.malloc(C.sizeof_long)
271+
defer C.free(cHandleID)
272+
*(*C.long)(cHandleID) = C.long(handleID)
273+
274+
ioCtx := C.sqlite_rsync_io_context_t{
275+
user_data: cHandleID,
276+
read_func: C.read_callback_t(C.go_local_read_callback),
277+
write_func: C.write_callback_t(C.go_local_write_callback),
278+
}
279+
280+
// Run the replica synchronization
281+
result := C.sqlite_rsync_run_replica(&config, &ioCtx)
282+
if result != 0 {
283+
return &SQLiteRsyncError{
284+
Code: int(result),
285+
Message: "replica sync failed",
286+
}
287+
}
288+
289+
return nil
290290
}
291291

292292
// cgoRunDirectSync wraps the C function to run direct local synchronization
293293
func cgoRunDirectSync(originDbPath, replicaDbPath string, dryRun bool, verbose int) error {
294-
// Prepare C configuration
295-
cOriginPath := C.CString(originDbPath)
296-
defer C.free(unsafe.Pointer(cOriginPath))
297-
298-
cReplicaPath := C.CString(replicaDbPath)
299-
defer C.free(unsafe.Pointer(cReplicaPath))
300-
301-
config := C.sqlite_rsync_config_t{
302-
origin_path: cOriginPath,
303-
replica_path: cReplicaPath,
304-
protocol_version: C.PROTOCOL_VERSION,
305-
verbose_level: C.int(verbose),
306-
dry_run: 0,
307-
wal_only: 0,
308-
error_file: nil,
309-
}
310-
311-
// Set debug_file if DEBUG_FILE is defined
312-
if DEBUG_FILE != "" {
313-
cDebugFile := C.CString(DEBUG_FILE + ".direct.log")
314-
defer C.free(unsafe.Pointer(cDebugFile))
315-
config.error_file = cDebugFile
316-
}
317-
318-
if dryRun {
319-
config.dry_run = 1
320-
}
321-
322-
// Run direct sync without WebSocket I/O - use the original C implementation
323-
result := C.sqlite_rsync_run_direct(&config)
324-
if result != 0 {
325-
return &SQLiteRsyncError{
326-
Code: int(result),
327-
Message: "direct sync failed",
328-
}
329-
}
330-
331-
return nil
332-
}
294+
// Prepare C configuration
295+
cOriginPath := C.CString(originDbPath)
296+
defer C.free(unsafe.Pointer(cOriginPath))
297+
298+
cReplicaPath := C.CString(replicaDbPath)
299+
defer C.free(unsafe.Pointer(cReplicaPath))
300+
301+
config := C.sqlite_rsync_config_t{
302+
origin_path: cOriginPath,
303+
replica_path: cReplicaPath,
304+
protocol_version: C.PROTOCOL_VERSION,
305+
verbose_level: C.int(verbose),
306+
dry_run: 0,
307+
wal_only: 0,
308+
error_file: nil,
309+
}
310+
311+
// Set debugFile if debugFile is defined
312+
if verbose > 0 {
313+
cDebugFile := C.CString(DEBUG_FILE + "-push.log")
314+
defer C.free(unsafe.Pointer(cDebugFile))
315+
config.error_file = cDebugFile
316+
}
317+
318+
if dryRun {
319+
config.dry_run = 1
320+
}
321+
322+
// Run direct sync without WebSocket I/O - use the original C implementation
323+
result := C.sqlite_rsync_run_direct(&config)
324+
if result != 0 {
325+
return &SQLiteRsyncError{
326+
Code: int(result),
327+
Message: "direct sync failed",
328+
}
329+
}
330+
331+
return nil
332+
}

0 commit comments

Comments
 (0)