Skip to content

Commit

Permalink
fix: long message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 10, 2021
1 parent dea4240 commit 5d0a433
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion exporter/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ func TextExporter(s *store.Store) http.HandlerFunc {

fmt.Fprint(w, vert, strings.Repeat(" ", 78), vert, "\n")

fmt.Fprintf(w, "%s%-78s%s\n", vert, i.Message, vert)
for offset := 0; offset < len(i.Message); offset += 78 {
end := offset + 78
if end >= len(i.Message) {
end = len(i.Message) - 1
}
fmt.Fprintf(w, "%s%-78s%s\n", vert, i.Message[offset:end], vert)
}

if bold {
fmt.Fprintln(w, "┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻")
Expand Down
12 changes: 11 additions & 1 deletion store/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ func ParseRecord(s string) (Record, error) {
return r, nil
}

func (r Record) Sanitize() Record {
return Record{
CheckedAt: r.CheckedAt,
Target: r.Target,
Status: r.Status,
Message: strings.ReplaceAll(strings.ReplaceAll(r.Message, "\t", " "), "\n", " "),
Latency: r.Latency,
}
}

func (r Record) String() string {
return strings.Join([]string{
r.CheckedAt.Format(time.RFC3339),
r.Status.String(),
fmt.Sprintf("%.3f", float64(r.Latency.Microseconds())/1000),
r.Target.String(),
strings.ReplaceAll(strings.ReplaceAll(r.Message, "\t", " "), "\n", " "),
r.Message,
}, "\t")
}
2 changes: 2 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func (s *Store) Append(r Record) {
s.Lock()
defer s.Unlock()

r = r.Sanitize()

f, err := os.OpenFile(s.Path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open log file: %s", err)
Expand Down

0 comments on commit 5d0a433

Please sign in to comment.