Skip to content

Commit 1c274c5

Browse files
committed
Add ability to debug to file
1 parent b0c5fa9 commit 1c274c5

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Ok Response
199199
{"type":"pong"}
200200
```
201201

202-
## How to develop
202+
## How to develop / Debug
203203

204204
```sh
205205
# After a change update the binary in your path using:
@@ -212,12 +212,21 @@ go run .
212212

213213
Debug data send by scraper to this program
214214
```sh
215+
# Logs the IO to scraper_client_input.log
215216
export LOG_SCRAPER_CLIENT_INPUT=true
217+
218+
# Logs the debug messages to scraper_client.log
219+
export ENABLE_SCRAPER_CLIENT_LOG=true
220+
216221
# Now run your scraper
217222
go run .
218223

219224
# Now all messages written to this program are also written to scraper_client_input.log
220-
# You can follow the input by tailing the file:
225+
# You can follow the input by tailing the file in a new terminal:
226+
tail -f scraper_client_input.log
227+
228+
# All debug messages are now visible in scraper_client.log
229+
# You can follow the input by tailing the file in a new terminal:
221230
tail -f scraper_client_input.log
222231

223232
# After collecting the input data you can also use rtcv_scraper_client to replay sending the data
@@ -230,6 +239,7 @@ rtcv_scraper_client \
230239
-replaySkipCommands has_cached_reference,set_cached_reference,ping,get_users_secret
231240
```
232241

242+
233243
## How to ship?
234244

235245
Currently we don't have pre build binaries so you'll need to compile the binary yourself

logger.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
const logfileName = "scraper_client.log"
11+
12+
func openLogFile() (*os.File, error) {
13+
return os.OpenFile(logfileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
14+
}
15+
16+
var truthyStringValues = map[string]bool{
17+
"1": true,
18+
"t": true,
19+
"true": true,
20+
}
21+
22+
var logFileEnabled = truthyStringValues[strings.ToLower(os.Getenv("ENABLE_SCRAPER_CLIENT_LOG"))]
23+
24+
// DebugToLogfile appends a new debug line to the logfile
25+
func DebugToLogfile(a ...interface{}) {
26+
if !logFileEnabled {
27+
return
28+
}
29+
30+
f, err := openLogFile()
31+
if err != nil {
32+
if !errors.Is(err, os.ErrNotExist) {
33+
return
34+
}
35+
_, err = os.Create(logfileName)
36+
if err != nil {
37+
return
38+
}
39+
f, err = openLogFile()
40+
if err != nil {
41+
return
42+
}
43+
}
44+
45+
f.WriteString(fmt.Sprintln(a...))
46+
f.Close()
47+
}

main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ func main() {
8989

9090
api := NewAPI()
9191

92-
logInput := map[string]bool{
93-
"1": true,
94-
"t": true,
95-
"true": true,
96-
}[strings.ToLower(os.Getenv("LOG_SCRAPER_CLIENT_INPUT"))]
92+
logInput := truthyStringValues[strings.ToLower(os.Getenv("LOG_SCRAPER_CLIENT_INPUT"))]
9793

9894
var logInputFile *os.File
9995
var err error
@@ -226,6 +222,8 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
226222
return returnErr(err)
227223
}
228224

225+
DebugToLogfile(referenceNrs)
226+
229227
now := time.Now()
230228
for _, nr := range referenceNrs {
231229
api.Cache[nr] = now
@@ -335,7 +333,10 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
335333
return MessageTypeOk, nil
336334
}
337335

338-
return MessageTypeOk, api.CacheEntryExists(referenceNr)
336+
hasCachedReference := api.CacheEntryExists(referenceNr)
337+
DebugToLogfile("has_cached_reference", referenceNr, ">", hasCachedReference)
338+
339+
return MessageTypeOk, hasCachedReference
339340
case "ping":
340341
return MessageTypePong, nil
341342
default:

0 commit comments

Comments
 (0)