aboutsummaryrefslogtreecommitdiffhomepage
path: root/system
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-09-06 15:48:57 +0900
committerOphestra <cat@gensokyo.uk>2025-09-06 15:50:29 +0900
commitac81cfbedc438593f3b504d53728914ac6a3d359 (patch)
tree68818f8cfb8a8a88e64f609bdb7968cddcc6352f /system
parent05db06c87b743b2e1eb0e8158476ba2a24962967 (diff)
system/dbus: print incomplete string in buffer
Not sure if this will ever be reached, but nice to have nonetheless. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'system')
-rw-r--r--system/dbus.go15
-rw-r--r--system/dbus_test.go45
2 files changed, 34 insertions, 26 deletions
diff --git a/system/dbus.go b/system/dbus.go
index 819f2495..3348bf63 100644
--- a/system/dbus.go
+++ b/system/dbus.go
@@ -7,7 +7,6 @@ import (
"fmt"
"log"
"reflect"
- "strconv"
"strings"
"sync"
"syscall"
@@ -170,9 +169,11 @@ func (s *linePrefixWriter) write(p []byte, a int) (int, error) {
return a + n, nil
} else {
n, _ := s.buf.Write(p[:i])
+ s.n += n + 1
v := s.buf.String()
if strings.HasPrefix(v, "init: ") {
+ s.n -= len(v) + 1
// pass through container init messages
s.println(s.prefix + v)
} else {
@@ -180,22 +181,20 @@ func (s *linePrefixWriter) write(p []byte, a int) (int, error) {
}
s.buf.Reset()
- s.n += n + 1
return s.write(p[i+1:], a+n+1)
}
}
func (s *linePrefixWriter) Dump() {
s.mu.RLock()
- // the final write might go past the threshold,
- // and the buffer might still contain data
- var n int
for _, m := range s.msg {
- n += len(m)
s.println(s.prefix + m)
}
- if s.n > lpwSizeThreshold {
- s.println(s.prefix + "dropped " + strconv.Itoa(s.n-n) + " bytes of output")
+ if s.buf != nil && s.buf.Len() != 0 {
+ s.println("*" + s.prefix + s.buf.String())
+ }
+ if s.n >= lpwSizeThreshold {
+ s.println("+" + s.prefix + "write threshold reached, output may be incomplete")
}
s.mu.RUnlock()
}
diff --git a/system/dbus_test.go b/system/dbus_test.go
index e3ace3d5..7ad7bb22 100644
--- a/system/dbus_test.go
+++ b/system/dbus_test.go
@@ -392,10 +392,11 @@ func TestLinePrefixWriter(t *testing.T) {
}{
{"nop", "(nop) ", func(func(string)) {}, nil, nil, nil, nil, ""},
- {"partial", "(break) ", func(w func(string)) {
+ {"partial", "(partial) ", func(w func(string)) {
w("C-65533: -> ")
- }, nil, nil, nil, nil,
- "C-65533: -> "},
+ }, nil, nil, nil, []string{
+ "*(partial) C-65533: -> ",
+ }, "C-65533: -> "},
{"break", "(break) ", func(w func(string)) {
w("C-65533: -> ")
@@ -414,8 +415,10 @@ func TestLinePrefixWriter(t *testing.T) {
{"threshold", "(threshold) ", func(w func(s string)) {
w(string(make([]byte, lpwSizeThreshold)))
w("\n")
- }, []error{nil, syscall.ENOMEM}, nil, nil, nil,
- string(make([]byte, lpwSizeThreshold))},
+ }, []error{nil, syscall.ENOMEM}, nil, nil, []string{
+ "*(threshold) " + string(make([]byte, lpwSizeThreshold)),
+ "+(threshold) write threshold reached, output may be incomplete",
+ }, string(make([]byte, lpwSizeThreshold))},
{"threshold multi", "(threshold multi) ", func(w func(s string)) {
w(":3\n")
@@ -423,15 +426,20 @@ func TestLinePrefixWriter(t *testing.T) {
w("\n")
}, []error{nil, nil, syscall.ENOMEM}, nil, []string{
":3",
- }, nil, string(make([]byte, lpwSizeThreshold-3))},
+ }, []string{
+ "*(threshold multi) " + string(make([]byte, lpwSizeThreshold-3)),
+ "+(threshold multi) write threshold reached, output may be incomplete",
+ }, string(make([]byte, lpwSizeThreshold-3))},
{"threshold multi partial", "(threshold multi partial) ", func(w func(s string)) {
w(":3\n")
w(string(make([]byte, lpwSizeThreshold-2)))
- }, []error{nil, syscall.ENOMEM}, nil, []string{
+ w("dropped\n")
+ }, []error{nil, nil, syscall.ENOMEM}, nil, []string{
":3",
}, []string{
- "dropped 16777215 bytes of output",
+ "*(threshold multi partial) " + string(make([]byte, lpwSizeThreshold-2)),
+ "+(threshold multi partial) write threshold reached, output may be incomplete",
}, string(make([]byte, lpwSizeThreshold-2))},
{"threshold exact", "(threshold exact) ", func(w func(s string)) {
@@ -439,7 +447,9 @@ func TestLinePrefixWriter(t *testing.T) {
w("\n")
}, nil, nil, []string{
string(make([]byte, lpwSizeThreshold-1)),
- }, nil, ""},
+ }, []string{
+ "+(threshold exact) write threshold reached, output may be incomplete",
+ }, ""},
{"sample", "(dbus) ", func(w func(s string)) {
w("init: received setup parameters\n")
@@ -595,15 +605,14 @@ func TestLinePrefixWriter(t *testing.T) {
var pos int
tc.f(func(s string) {
- if _, err := out.Write([]byte(s)); err != nil {
- if tc.wantErr != nil {
- if !reflect.DeepEqual(err, tc.wantErr[pos]) {
- t.Fatalf("Write: error = %v, want %v", err, tc.wantErr[pos])
- }
- } else {
- t.Fatalf("Write: unexpected error: %v", err)
- return
+ _, err := out.Write([]byte(s))
+ if tc.wantErr != nil {
+ if !reflect.DeepEqual(err, tc.wantErr[pos]) {
+ t.Fatalf("Write: error = %v, want %v", err, tc.wantErr[pos])
}
+ } else if err != nil {
+ t.Fatalf("Write: unexpected error: %v", err)
+ return
}
pos++
})
@@ -629,7 +638,7 @@ func TestLinePrefixWriter(t *testing.T) {
wantDump[i] = tc.prefix + m
}
for i, m := range tc.wantExt {
- wantDump[len(tc.want)+i] = tc.prefix + m
+ wantDump[len(tc.want)+i] = m
}
t.Run("dump", func(t *testing.T) {
got := make([]string, 0, len(wantDump))