File size: 5,167 Bytes
5d65746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package proxy

import (
	"bytes"
	"encoding/json"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"sync/atomic"
)

var debugLoggingEnabled atomic.Bool
var apiInputLoggingEnabled atomic.Bool
var apiOutputLoggingEnabled atomic.Bool
var notionRequestLoggingEnabled atomic.Bool
var notionResponseLoggingEnabled atomic.Bool
var globalLogWriter = newDebugFilterWriter()

var debugLogTags = []string{
	"[debug]",
	"[bridge]",
	"[session]",
	"[thinking]",
	"[req]",
	"[search]",
	"[upload-debug]",
}

type debugFilterWriter struct {
	mu   sync.RWMutex
	out  io.Writer
	file *os.File
}

func newDebugFilterWriter() *debugFilterWriter {
	return &debugFilterWriter{
		out: os.Stderr,
	}
}

func init() {
	debugLoggingEnabled.Store(true)
	log.SetOutput(globalLogWriter)
}

func (w *debugFilterWriter) Write(p []byte) (int, error) {
	if !DebugLoggingEnabled() && isDebugLogLine(p) {
		return len(p), nil
	}
	w.mu.RLock()
	out := w.out
	w.mu.RUnlock()
	if out == nil {
		out = os.Stderr
	}
	return out.Write(p)
}

func (w *debugFilterWriter) SetOutputPath(path string) error {
	path = strings.TrimSpace(path)

	target := io.Writer(os.Stderr)
	var file *os.File

	if path != "" {
		dir := filepath.Dir(path)
		if dir != "." && dir != "" {
			if err := os.MkdirAll(dir, 0755); err != nil {
				return err
			}
		}

		f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
		if err != nil {
			return err
		}
		target = f
		file = f
	}

	w.mu.Lock()
	oldFile := w.file
	w.out = target
	w.file = file
	w.mu.Unlock()

	if oldFile != nil && oldFile != file {
		_ = oldFile.Close()
	}
	return nil
}

func isDebugLogLine(p []byte) bool {
	line := string(p)
	for _, tag := range debugLogTags {
		if strings.Contains(line, tag) {
			return true
		}
	}
	return false
}

func SetDebugLoggingEnabled(enabled bool) {
	debugLoggingEnabled.Store(enabled)
}

func DebugLoggingEnabled() bool {
	return debugLoggingEnabled.Load()
}

func ConfigureLogOutput(path string) error {
	return globalLogWriter.SetOutputPath(path)
}

func SetAPILogInputEnabled(enabled bool) {
	apiInputLoggingEnabled.Store(enabled)
}

func APILogInputEnabled() bool {
	return apiInputLoggingEnabled.Load()
}

func SetAPILogOutputEnabled(enabled bool) {
	apiOutputLoggingEnabled.Store(enabled)
}

func APILogOutputEnabled() bool {
	return apiOutputLoggingEnabled.Load()
}

func SetNotionRequestLoggingEnabled(enabled bool) {
	notionRequestLoggingEnabled.Store(enabled)
}

func NotionRequestLoggingEnabled() bool {
	return notionRequestLoggingEnabled.Load()
}

func SetNotionResponseLoggingEnabled(enabled bool) {
	notionResponseLoggingEnabled.Store(enabled)
}

func NotionResponseLoggingEnabled() bool {
	return notionResponseLoggingEnabled.Load()
}

func LogAPIInputJSON(requestID, label string, v interface{}) {
	if !APILogInputEnabled() {
		return
	}
	logJSONPayload("[api-in]", requestID, label, v)
}

func LogAPIInputJSONBytes(requestID, label string, raw []byte) {
	if !APILogInputEnabled() {
		return
	}
	logJSONBytesPayload("[api-in]", requestID, label, raw)
}

func LogAPIInputText(requestID, label, text string) {
	if !APILogInputEnabled() {
		return
	}
	logTextPayload("[api-in]", requestID, label, text)
}

func LogAPIOutputJSON(requestID, label string, v interface{}) {
	if !APILogOutputEnabled() {
		return
	}
	logJSONPayload("[api-out]", requestID, label, v)
}

func LogAPIOutputText(requestID, label, text string) {
	if !APILogOutputEnabled() {
		return
	}
	logTextPayload("[api-out]", requestID, label, text)
}

func LogNotionRequestJSON(requestID, label string, v interface{}) {
	if !NotionRequestLoggingEnabled() {
		return
	}
	logJSONPayload("[notion-req]", requestID, label, v)
}

func LogNotionResponseJSON(requestID, label string, v interface{}) {
	if !NotionResponseLoggingEnabled() {
		return
	}
	logJSONPayload("[notion-resp]", requestID, label, v)
}

func LogNotionResponseJSONBytes(requestID, label string, raw []byte) {
	if !NotionResponseLoggingEnabled() {
		return
	}
	logJSONBytesPayload("[notion-resp]", requestID, label, raw)
}

func LogNotionResponseText(requestID, label, text string) {
	if !NotionResponseLoggingEnabled() {
		return
	}
	logTextPayload("[notion-resp]", requestID, label, text)
}

func logJSONPayload(tag, requestID, label string, v interface{}) {
	raw, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		log.Printf("%s %s %s marshal error: %v", tag, requestID, label, err)
		return
	}
	logTextPayload(tag, requestID, label, string(raw))
}

func logJSONBytesPayload(tag, requestID, label string, raw []byte) {
	var buf bytes.Buffer
	if err := json.Indent(&buf, raw, "", "  "); err == nil {
		logTextPayload(tag, requestID, label, buf.String())
		return
	}
	logTextPayload(tag, requestID, label, string(raw))
}

func logTextPayload(tag, requestID, label, text string) {
	label = strings.TrimSpace(label)
	requestID = strings.TrimSpace(requestID)
	if requestID != "" && label != "" {
		log.Printf("%s %s %s\n%s", tag, requestID, label, text)
		return
	}
	if requestID != "" {
		log.Printf("%s %s\n%s", tag, requestID, text)
		return
	}
	if label != "" {
		log.Printf("%s %s\n%s", tag, label, text)
		return
	}
	log.Printf("%s\n%s", tag, text)
}