| 1 |
package lhscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/ |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytesscip-go gomod github.com/golang/go/src go1.21 bytes/" |
| •5 |
"encoding/jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/" |
| 6 |
"fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 7 |
"ioscip-go gomod github.com/golang/go/src go1.21 io/" |
| 8 |
"osscip-go gomod github.com/golang/go/src go1.21 os/" |
| 9 |
"stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 10 |
"syncscip-go gomod github.com/golang/go/src go1.21 sync/" |
| 11 |
"timescip-go gomod github.com/golang/go/src go1.21 time/" |
| 12 |
|
| 13 |
"github.com/olekukonko/ll/lxscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/" |
| 14 |
) |
| 15 |
|
| 16 |
var jsonBufPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonBufPool. = syncscip-go gomod github.com/golang/go/src go1.21 sync/.Poolscip-go gomod github.com/golang/go/src go1.21 sync/Pool#{ |
| 17 |
Newscip-go gomod github.com/golang/go/src go1.21 sync/Pool#New.: func() any { |
| 18 |
return new(bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#) |
| 19 |
}, |
| 20 |
} |
| 21 |
|
| 22 |
// fieldsMapPool pools map[string]interface{} to reduce allocations |
| 23 |
var fieldsMapPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/fieldsMapPool. = syncscip-go gomod github.com/golang/go/src go1.21 sync/.Poolscip-go gomod github.com/golang/go/src go1.21 sync/Pool#{ |
| 24 |
Newscip-go gomod github.com/golang/go/src go1.21 sync/Pool#New.: func() any { |
| 25 |
return make(map[string]interface{}, 8) |
| 26 |
}, |
| 27 |
} |
| 28 |
|
| 29 |
// jsonOutputPool pools JsonOutput structs to reduce allocations |
| 30 |
var jsonOutputPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonOutputPool. = syncscip-go gomod github.com/golang/go/src go1.21 sync/.Poolscip-go gomod github.com/golang/go/src go1.21 sync/Pool#{ |
| 31 |
Newscip-go gomod github.com/golang/go/src go1.21 sync/Pool#New.: func() any { |
| 32 |
return &JsonOutputscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#{ |
| 33 |
Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields.: make(map[string]interface{}, 8), |
| 34 |
} |
| 35 |
}, |
| 36 |
} |
| 37 |
|
| 38 |
// JSONHandler is a handler that outputs log entries as JSON objects. |
| 39 |
// It formats log entries with timestamp, level, message, namespace, fields, and optional |
| 40 |
// stack traces or dump segments, writing the result to the provided writer. |
| 41 |
// Thread-safe with a mutex to protect concurrent writes. |
| 42 |
type JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler# struct { |
| 43 |
writerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#writer. ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer# // Destination for JSON output |
| 44 |
timeFmtscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#timeFmt. string // Format for timestamp (default: RFC3339Nano) |
| 45 |
prettyscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#pretty. bool // Enable pretty printing with indentation if true |
| 46 |
muscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#mu. syncscip-go gomod github.com/golang/go/src go1.21 sync/.Mutexscip-go gomod github.com/golang/go/src go1.21 sync/Mutex# // Protects concurrent access to writer |
| 47 |
} |
| 48 |
|
| 49 |
// JsonOutput represents the JSON structure for a log entry. |
| 50 |
// It includes all relevant log data, such as timestamp, level, message, and optional |
| 51 |
// stack trace or dump segments, serialized as a JSON object. |
| 52 |
type JsonOutputscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput# struct { |
| 53 |
Timescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Time. string `json:"ts"` // Timestamp in specified format |
| 54 |
Levelscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Level. string `json:"lvl"` // Log level (e.g., "INFO") |
| 55 |
Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Class. string `json:"class"` // Entry class (e.g., "Text", "Dump") |
| 56 |
Msgscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Msg. string `json:"msg"` // Log message |
| 57 |
Namespacescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Namespace. string `json:"ns"` // Namespace path |
| 58 |
Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Stack. []byte `json:"stack"` // Stack trace (if present) |
| 59 |
Dumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Dump. []dumpSegmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment# `json:"dump"` // Hex/ASCII dump segments (for ClassDump) |
| 60 |
Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. map[string]interface{} `json:"fields"` // Custom fields |
| 61 |
} |
| 62 |
|
| 63 |
// dumpSegment represents a single segment of a hex/ASCII dump. |
| 64 |
// Used for ClassDump entries to structure position, hex values, and ASCII representation. |
| 65 |
type dumpSegmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment# struct { |
| 66 |
Offsetscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#Offset. int `json:"offset"` // Starting byte offset of the segment |
| 67 |
Hexscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#Hex. []string `json:"hex"` // Hexadecimal values of bytes |
| 68 |
ASCIIscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#ASCII. string `json:"ascii"` // ASCII representation of bytes |
| 69 |
} |
| 70 |
|
| 71 |
// NewJSONHandler creates a new JSONHandler writing to the specified writer. |
| 72 |
// It initializes the handler with a default timestamp format (RFC3339Nano) and optional |
| 73 |
// configuration functions to customize settings like pretty printing. |
| 74 |
// Example: |
| 75 |
// |
| 76 |
// handler := NewJSONHandler(os.Stdout) |
| 77 |
// logger := ll.New("app").Enable().Handler(handler) |
| 78 |
// logger.Info("Test") // Output: {"ts":"...","lvl":"INFO","class":"Text","msg":"Test","ns":"app","stack":null,"dump":null,"fields":null} |
| 79 |
func NewJSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/NewJSONHandler().(w ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, opts ...func(*JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#)) *JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler# { |
| 80 |
h := &JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#{ |
| 81 |
writerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#writer.: w, // Set output writer |
| 82 |
timeFmtscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#timeFmt.: timescip-go gomod github.com/golang/go/src go1.21 time/.RFC3339Nanoscip-go gomod github.com/golang/go/src go1.21 time/RFC3339Nano., // Default timestamp format |
| 83 |
} |
| 84 |
// Apply configuration options |
| 85 |
for _, opt := range opts { |
| 86 |
opt(h) |
| 87 |
} |
| 88 |
return h |
| 89 |
} |
| 90 |
|
| 91 |
// Handle processes a log entry and writes it as JSON. |
| 92 |
// It delegates to specialized methods based on the entry's class (Dump or regular), |
| 93 |
// ensuring thread-safety with a mutex. |
| 94 |
// Returns an error if JSON encoding or writing fails. |
| 95 |
// Example: |
| 96 |
// |
| 97 |
// handler.Handle(&lx.Entry{Message: "test", Level: lx.LevelInfo}) // Writes JSON object |
| 98 |
func (h *JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#) Handlescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#Handle().(e *lxscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/.Entryscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#) error { |
| 99 |
h.muscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#mu..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 100 |
defer h.muscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#mu..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 101 |
// Handle dump entries separately |
| 102 |
if e.Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Class. == lxscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/.ClassDumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/ClassDump. { |
| 103 |
return h.handleDumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#handleDump().(e) |
| 104 |
} |
| 105 |
// Handle standard log entries |
| 106 |
return h.handleRegularscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#handleRegular().(e) |
| 107 |
} |
| 108 |
|
| 109 |
// Output sets the Writer destination for JSONHandler's output, ensuring thread safety with a mutex lock. |
| 110 |
func (h *JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#) Outputscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#Output().(w ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#) { |
| 111 |
h.muscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#mu..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 112 |
defer h.muscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#mu..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 113 |
h.writerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#writer. = w |
| 114 |
} |
| 115 |
|
| 116 |
// handleRegular handles standard log entries (non-dump). |
| 117 |
// It converts the entry to a JsonOutput struct and encodes it as JSON, |
| 118 |
// applying pretty printing if enabled. Logs encoding errors to stderr for debugging. |
| 119 |
// Returns an error if encoding or writing fails. |
| 120 |
// Example (internal usage): |
| 121 |
// |
| 122 |
// h.handleRegular(&lx.Entry{Message: "test", Level: lx.LevelInfo}) // Writes JSON object |
| 123 |
func (h *JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#) handleRegularscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#handleRegular().(e *lxscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/.Entryscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#) error { |
| 124 |
// Get fieldsMap from pool to avoid allocation |
| 125 |
fieldsMap := fieldsMapPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/fieldsMapPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(map[string]interface{}) |
| 126 |
// Clear any existing keys from previous use |
| 127 |
for k := range fieldsMap { |
| 128 |
delete(fieldsMap, k) |
| 129 |
} |
| 130 |
// Convert ordered fields to map for JSON output |
| 131 |
for _, pair := range e.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Fields. { |
| 132 |
fieldsMap[pair.Keyscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Field#Key.] = pair.Valuescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Field#Value. |
| 133 |
} |
| 134 |
|
| 135 |
// Get JsonOutput from pool |
| 136 |
entry := jsonOutputPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonOutputPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(*JsonOutputscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#) |
| 137 |
entry.Timescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Time. = e.Timestampscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Timestamp..Formatscip-go gomod github.com/golang/go/src go1.21 time/Time#Format().(h.timeFmtscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#timeFmt.) |
| 138 |
entry.Levelscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Level. = e.Levelscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Level..Stringscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/LevelType#String().() |
| 139 |
entry.Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Class. = e.Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Class..Stringscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/ClassType#String().() |
| 140 |
entry.Msgscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Msg. = e.Messagescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Message. |
| 141 |
entry.Namespacescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Namespace. = e.Namespacescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Namespace. |
| 142 |
entry.Dumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Dump. = nil |
| 143 |
entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. = fieldsMap |
| 144 |
entry.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Stack. = e.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Stack. |
| 145 |
|
| 146 |
// Acquire buffer from pool to avoid allocation and reduce syscalls |
| 147 |
buf := jsonBufPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonBufPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(*bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#) |
| 148 |
buf.Resetscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Reset().() |
| 149 |
defer func() { |
| 150 |
// Return all pooled objects |
| 151 |
jsonBufPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonBufPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(buf) |
| 152 |
// Reset and return fieldsMap to pool |
| 153 |
for k := range entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. { |
| 154 |
delete(entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields., k) |
| 155 |
} |
| 156 |
fieldsMapPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/fieldsMapPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields.) |
| 157 |
// Reset and return JsonOutput to pool |
| 158 |
entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. = nil |
| 159 |
entry.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Stack. = nil |
| 160 |
entry.Dumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Dump. = nil |
| 161 |
jsonOutputPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonOutputPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(entry) |
| 162 |
}() |
| 163 |
|
| 164 |
// Create JSON encoder writing to buffer (uses go-json for 2-5x speedup) |
| 165 |
enc := jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.NewEncoderscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/NewEncoder().(buf) |
| 166 |
if h.prettyscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#pretty. { |
| 167 |
// Enable indentation for pretty printing |
| 168 |
enc.SetIndentscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Encoder#SetIndent().("", " ") |
| 169 |
} |
| 170 |
// Encode JSON to buffer |
| 171 |
err := enc.Encodescip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Encoder#Encode().(entry) |
| 172 |
if err != nil { |
| 173 |
// Log encoding error for debugging |
| 174 |
fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.21 os/.Stderrscip-go gomod github.com/golang/go/src go1.21 os/Stderr., "JSON encode error: %v\n", err) |
| 175 |
return err |
| 176 |
} |
| 177 |
// Write buffer to underlying writer in one go |
| 178 |
_, err = h.writerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#writer..Writescip-go gomod github.com/golang/go/src go1.21 io/Writer#Write().(buf.Bytesscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Bytes().()) |
| 179 |
return err |
| 180 |
} |
| 181 |
|
| 182 |
// handleDump processes ClassDump entries, converting hex dump output to JSON segments. |
| 183 |
// It parses the dump message into structured segments with offset, hex, and ASCII data, |
| 184 |
// encoding them as a JsonOutput struct. |
| 185 |
// Returns an error if parsing or encoding fails. |
| 186 |
// Example (internal usage): |
| 187 |
// |
| 188 |
// h.handleDump(&lx.Entry{Class: lx.ClassDump, Message: "pos 00 hex: 61 62 'ab'"}) // Writes JSON with dump segments |
| 189 |
func (h *JSONHandlerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#) handleDumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#handleDump().(e *lxscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/.Entryscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#) error { |
| 190 |
var segments []dumpSegmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment# |
| 191 |
lines := stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Splitscip-go gomod github.com/golang/go/src go1.21 strings/Split().(e.Messagescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Message., "\n") |
| 192 |
// Parse each line of the dump message |
| 193 |
for _, line := range lines { |
| 194 |
if !stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(line, "pos") { |
| 195 |
continue // Skip non-dump lines |
| 196 |
} |
| 197 |
parts := stringsscip-go gomod github.com/golang/go/src go1.21 strings/.SplitNscip-go gomod github.com/golang/go/src go1.21 strings/SplitN().(line, "hex:", 2) |
| 198 |
if len(parts) != 2 { |
| 199 |
continue // Skip invalid lines |
| 200 |
} |
| 201 |
// Parse position |
| 202 |
var offset int |
| 203 |
fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sscanfscip-go gomod github.com/golang/go/src go1.21 fmt/Sscanf().(parts[0], "pos %d", &offset) |
| 204 |
// Parse hex and ASCII |
| 205 |
hexAscii := stringsscip-go gomod github.com/golang/go/src go1.21 strings/.SplitNscip-go gomod github.com/golang/go/src go1.21 strings/SplitN().(parts[1], "'", 2) |
| 206 |
hexStr := stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.21 strings/Fields().(stringsscip-go gomod github.com/golang/go/src go1.21 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.21 strings/TrimSpace().(hexAscii[0])) |
| 207 |
// Create dump segment |
| 208 |
segments = append(segments, dumpSegmentscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#{ |
| 209 |
Offsetscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#Offset.: offset, // Set byte offset |
| 210 |
Hexscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#Hex.: hexStr, // Set hex values |
| 211 |
ASCIIscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/dumpSegment#ASCII.: stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Trimscip-go gomod github.com/golang/go/src go1.21 strings/Trim().(hexAscii[1], "'"), // Set ASCII representation |
| 212 |
}) |
| 213 |
} |
| 214 |
|
| 215 |
// Get fieldsMap from pool |
| 216 |
fieldsMap := fieldsMapPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/fieldsMapPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(map[string]interface{}) |
| 217 |
for k := range fieldsMap { |
| 218 |
delete(fieldsMap, k) |
| 219 |
} |
| 220 |
for _, pair := range e.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Fields. { |
| 221 |
fieldsMap[pair.Keyscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Field#Key.] = pair.Valuescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Field#Value. |
| 222 |
} |
| 223 |
|
| 224 |
// Get JsonOutput from pool |
| 225 |
entry := jsonOutputPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonOutputPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(*JsonOutputscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#) |
| 226 |
entry.Timescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Time. = e.Timestampscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Timestamp..Formatscip-go gomod github.com/golang/go/src go1.21 time/Time#Format().(h.timeFmtscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#timeFmt.) |
| 227 |
entry.Levelscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Level. = e.Levelscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Level..Stringscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/LevelType#String().() |
| 228 |
entry.Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Class. = e.Classscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Class..Stringscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/ClassType#String().() |
| 229 |
entry.Msgscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Msg. = "dumping segments" |
| 230 |
entry.Namespacescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Namespace. = e.Namespacescip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Namespace. |
| 231 |
entry.Dumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Dump. = segments |
| 232 |
entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. = fieldsMap |
| 233 |
entry.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Stack. = e.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lx`/Entry#Stack. |
| 234 |
|
| 235 |
// Acquire buffer from pool |
| 236 |
buf := jsonBufPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonBufPool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(*bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#) |
| 237 |
buf.Resetscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Reset().() |
| 238 |
defer func() { |
| 239 |
jsonBufPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonBufPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(buf) |
| 240 |
for k := range entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. { |
| 241 |
delete(entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields., k) |
| 242 |
} |
| 243 |
fieldsMapPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/fieldsMapPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields.) |
| 244 |
entry.Fieldsscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Fields. = nil |
| 245 |
entry.Stackscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Stack. = nil |
| 246 |
entry.Dumpscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JsonOutput#Dump. = nil |
| 247 |
jsonOutputPoolscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/jsonOutputPool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(entry) |
| 248 |
}() |
| 249 |
|
| 250 |
// Encode JSON output with dump segments to buffer |
| 251 |
enc := jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.NewEncoderscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/NewEncoder().(buf) |
| 252 |
if h.prettyscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#pretty. { |
| 253 |
enc.SetIndentscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Encoder#SetIndent().("", " ") |
| 254 |
} |
| 255 |
err := enc.Encodescip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Encoder#Encode().(entry) |
| 256 |
if err != nil { |
| 257 |
fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.21 os/.Stderrscip-go gomod github.com/golang/go/src go1.21 os/Stderr., "JSON dump encode error: %v\n", err) |
| 258 |
return err |
| 259 |
} |
| 260 |
// Write buffer to underlying writer |
| 261 |
_, err = h.writerscip-go gomod github.com/olekukonko/ll . `github.com/olekukonko/ll/lh`/JSONHandler#writer..Writescip-go gomod github.com/golang/go/src go1.21 io/Writer#Write().(buf.Bytesscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Bytes().()) |
| 262 |
return err |
| 263 |
} |