-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_logs.go
More file actions
135 lines (112 loc) · 3.85 KB
/
console_logs.go
File metadata and controls
135 lines (112 loc) · 3.85 KB
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
package devbrowser
import (
"errors"
"fmt"
"github.com/tinywasm/devbrowser/cdproto/audits"
"github.com/tinywasm/devbrowser/cdproto/log"
"github.com/tinywasm/devbrowser/cdproto/runtime"
"github.com/tinywasm/devbrowser/chromedp"
)
// initializeConsoleCapture sets up the console log capturing system using Chrome DevTools Protocol.
// This captures ALL console messages including those from page load, using runtime events.
func (b *DevBrowser) initializeConsoleCapture() error {
if b.Ctx == nil {
return errors.New("browser context not initialized")
}
// Initialize the console logs slice
b.ConsoleLogs = []string{}
// Listen for console API called events and console cleared events
chromedp.ListenTarget(b.Ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *runtime.EventConsoleAPICalled:
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
// Format the arguments without prefix
var message string
for i, arg := range ev.Args {
if i > 0 {
message += " "
}
// Get the string value from the RemoteObject
if arg.Value != nil {
// Extract the raw value without JSON encoding
val := string(arg.Value)
// Remove surrounding quotes if it's a string value
if len(val) > 2 && val[0] == '"' && val[len(val)-1] == '"' {
val = val[1 : len(val)-1]
}
message += val
} else if arg.Description != "" {
message += arg.Description
}
}
// Add to logs without type prefix to save tokens
b.ConsoleLogs = append(b.ConsoleLogs, message)
case *runtime.EventExceptionThrown:
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
// Capture uncaught exceptions
msg := fmt.Sprintf("[Exception] %s", ev.ExceptionDetails.Text)
if ev.ExceptionDetails.Exception != nil && ev.ExceptionDetails.Exception.Description != "" {
msg += ": " + ev.ExceptionDetails.Exception.Description
}
b.ConsoleLogs = append(b.ConsoleLogs, msg)
case *log.EventEntryAdded:
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
// Capture browser logs (network errors, security warnings, etc.)
// Format: [Level] Source: Text
msg := fmt.Sprintf("[%s] %s: %s", ev.Entry.Level, ev.Entry.Source, ev.Entry.Text)
if ev.Entry.URL != "" {
msg += fmt.Sprintf(" (%s)", ev.Entry.URL)
}
b.ConsoleLogs = append(b.ConsoleLogs, msg)
case *audits.EventIssueAdded:
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
// Capture Audit Issues (Cookie warnings, Mixed Content, etc.)
msg := fmt.Sprintf("[Issue] %s", ev.Issue.Code)
// Details are often complex structs, so we stick to the code mostly.
// Ideally we could parse Issue.Details but it is a complex union type.
b.ConsoleLogs = append(b.ConsoleLogs, msg)
case *runtime.EventExecutionContextsCleared:
// Clear logs when execution contexts are cleared (page reload/navigation)
b.LogsMutex.Lock()
b.ConsoleLogs = []string{}
b.LogsMutex.Unlock()
}
})
// Enable console, log, and audits domains to start receiving events
err := chromedp.Run(b.Ctx,
runtime.Enable(),
log.Enable(),
audits.Enable(),
)
if err != nil {
return errors.New("initializeConsoleCapture: " + err.Error())
}
return nil
}
// GetConsoleLogs returns captured console logs from the browser.
// Returns an error if the browser context is not initialized.
func (b *DevBrowser) GetConsoleLogs() ([]string, error) {
if b.Ctx == nil {
return nil, errors.New("browser context not initialized")
}
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
// Return a copy of the logs to avoid race conditions
logsCopy := make([]string, len(b.ConsoleLogs))
copy(logsCopy, b.ConsoleLogs)
return logsCopy, nil
}
// ClearConsoleLogs clears the captured console logs.
func (b *DevBrowser) ClearConsoleLogs() error {
if b.Ctx == nil {
return errors.New("browser context not initialized")
}
b.LogsMutex.Lock()
defer b.LogsMutex.Unlock()
b.ConsoleLogs = []string{}
return nil
}