• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokath / trice / 20314806283

17 Dec 2025 07:12PM UTC coverage: 46.561% (-9.6%) from 56.117%
20314806283

push

github

rokath
minor corrections

2532 of 5438 relevant lines covered (46.56%)

868.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.13
/pkg/msg/msg.go
1
// Copyright 2020 Thomas.Hoehenleitner [at] seerose.net
2
// Use of this source code is governed by a license that can be found in the LICENSE file.
3

4
// Package msg provides common message functions.
5
package msg
6

7
import (
8
        "errors"
9
        "fmt"
10
        "io"
11
        "log"
12
        "os"
13
        "path/filepath"
14
        "runtime"
15
        "sync"
16
)
17

18
var Verbose bool
19

20
// Info prints info with location info.
21
func Info(info string) {
1✔
22
        pc, fn, line, ok := runtime.Caller(1)
1✔
23
        fmtMessage(pc, fn, line, ok, errors.New(info))
1✔
24
}
1✔
25

26
// OnErrF prints info and a common error message with location info when err is not nil.
27
func OnErrF(w io.Writer, err error) {
×
28
        if nil == err {
×
29
                return
×
30
        }
×
31
        pc, fn, line, ok := runtime.Caller(1)
×
32
        fmtFMessage(w, pc, fn, line, ok, err)
×
33
}
34

35
// OnErrFv prints info and a common error message with location info when err is not nil.
36
func OnErrFv(w io.Writer, err error) error {
×
37
        if nil == err {
×
38
                return nil
×
39
        }
×
40
        pc, fn, line, ok := runtime.Caller(1)
×
41
        fmtFMessage(w, pc, fn, line, ok, err)
×
42
        return err
×
43
}
44

45
// OnErr prints info and a common error message with location info when err is not nil.
46
func OnErr(err error) {
2✔
47
        if nil == err {
3✔
48
                return
1✔
49
        }
1✔
50
        pc, fn, line, ok := runtime.Caller(1)
1✔
51
        fmtMessage(pc, fn, line, ok, err)
1✔
52
}
53

54
// FatalOnErr ends in osExit(1) if err not nil.
55
func FatalOnErr(err error) {
3✔
56
        if nil == err {
5✔
57
                return
2✔
58
        }
2✔
59
        pc, fn, line, ok := runtime.Caller(1)
1✔
60
        logMessage(pc, fn, line, ok, err)
1✔
61
}
62

63
// InfoOnErr prints info and a common error message with location info when err is not nil.
64
func InfoOnErr(err error, info string) {
2✔
65
        if nil == err {
3✔
66
                return
1✔
67
        }
1✔
68
        fmt.Println(info)
1✔
69
        pc, fn, line, ok := runtime.Caller(1)
1✔
70
        fmtMessage(pc, fn, line, ok, err)
1✔
71
}
72

73
// FatalInfoOnErr ends in osExit(1) if err not nil.
74
func FatalInfoOnErr(err error, info string) {
3✔
75
        if nil == err {
5✔
76
                return
2✔
77
        }
2✔
78
        log.Println(info)
1✔
79
        pc, fn, line, ok := runtime.Caller(1)
1✔
80
        logMessage(pc, fn, line, ok, err)
1✔
81
}
82

83
// OnTrue
84

85
// OnTrue prints info and a common error message with location info when flag is true.
86
func OnTrue(flag bool) {
2✔
87
        if !flag {
3✔
88
                return
1✔
89
        }
1✔
90
        pc, fn, line, ok := runtime.Caller(1)
1✔
91
        fmtMessage(pc, fn, line, ok, nil)
1✔
92
}
93

94
// FatalOnTrue ends in osExit(1) if flag is true.
95
func FatalOnTrue(flag bool) {
3✔
96
        if !flag {
5✔
97
                return
2✔
98
        }
2✔
99
        pc, fn, line, ok := runtime.Caller(1)
1✔
100
        logMessage(pc, fn, line, ok, nil)
1✔
101
}
102

103
// InfoOnTrue
104

105
// InfoOnTrue prints info and a common error message with location info when flag is true.
106
func InfoOnTrue(flag bool, info string) {
2✔
107
        if !flag {
3✔
108
                return
1✔
109
        }
1✔
110
        pc, fn, line, ok := runtime.Caller(1)
1✔
111
        fmtMessage(pc, fn, line, ok, errors.New(info))
1✔
112
}
113

114
// FatalInfoOnTrue prints info and a common error message with location info when err is not nil.
115
func FatalInfoOnTrue(flag bool, info string) {
3✔
116
        if !flag {
5✔
117
                return
2✔
118
        }
2✔
119
        fmt.Println(info)
1✔
120
        pc, fn, line, ok := runtime.Caller(1)
1✔
121
        logMessage(pc, fn, line, ok, errors.New(info))
1✔
122
}
123

124
// OnFalse
125

126
// OnFalse prints info and a common error message with location info when flag is false.
127
func OnFalse(flag bool) {
2✔
128
        if flag {
3✔
129
                return
1✔
130
        }
1✔
131
        pc, fn, line, ok := runtime.Caller(1)
1✔
132
        fmtMessage(pc, fn, line, ok, nil)
1✔
133
}
134

135
// FatalOnFalse ends in osExit(1) if flag is false.
136
func FatalOnFalse(flag bool) {
3✔
137
        if flag {
5✔
138
                return
2✔
139
        }
2✔
140
        pc, fn, line, ok := runtime.Caller(1)
1✔
141
        logMessage(pc, fn, line, ok, nil)
1✔
142
}
143

144
// InfoOnFalse prints info and a common error message with location info when flag is false.
145
func InfoOnFalse(flag bool, info string) {
2✔
146
        if flag {
3✔
147
                return
1✔
148
        }
1✔
149
        pc, fn, line, ok := runtime.Caller(1)
1✔
150
        fmtMessage(pc, fn, line, ok, errors.New(info))
1✔
151
}
152

153
// FatalInfoOnFalse prints info and a common error message with location info when err is not nil.
154
func FatalInfoOnFalse(flag bool, info string) {
3✔
155
        if flag {
5✔
156
                return
2✔
157
        }
2✔
158
        fmt.Println(info)
1✔
159
        pc, fn, line, ok := runtime.Caller(1)
1✔
160
        logMessage(pc, fn, line, ok, errors.New(info))
1✔
161
}
162

163
const (
164
        formatString = "Error in %s:%d: func '%s' -> %v"
165
        seriousError = "Error: Could not recover caller information."
166
)
167

168
var (
169
        logFatalf = log.Fatalf // https://stackoverflow.com/questions/30688554/how-to-test-go-function-containing-log-fatal/45380105
170
)
171

172
func fmtFMessage(w io.Writer, pc uintptr, fn string, line int, ok bool, err error) {
7✔
173
        funcName := runtime.FuncForPC(pc).Name()
7✔
174
        fileName := filepath.Base(fn)
7✔
175
        if ok {
14✔
176
                fmt.Fprintf(w, formatString+"\n", fileName, line, funcName, err)
7✔
177
        } else {
7✔
178
                fmt.Fprintln(w, seriousError)
×
179
        }
×
180
}
181

182
func fmtMessage(pc uintptr, fn string, line int, ok bool, err error) {
7✔
183
        fmtFMessage(os.Stdout, pc, fn, line, ok, err)
7✔
184
}
7✔
185

186
func logMessage(pc uintptr, fn string, line int, ok bool, err error) {
6✔
187
        funcName := runtime.FuncForPC(pc).Name()
6✔
188
        fileName := filepath.Base(fn)
6✔
189
        if ok {
12✔
190
                logFatalf(formatString, fileName, line, funcName, err)
6✔
191
        } else {
6✔
192
                logFatalf("%s", seriousError)
×
193
        }
×
194
}
195

196
// -----------------------------------------------
197

198
type OrigLogFatalf func(format string, v ...interface{})
199

200
var m *sync.RWMutex
201

202
func init() {
1✔
203
        m = new(sync.RWMutex)
1✔
204
}
1✔
205

206
// OsExitDisallow replace the original fatal function
207
func OsExitDisallow() (o OrigLogFatalf) {
×
208
        m.Lock()
×
209
        o = logFatalf
×
210

×
211
        logFatalf = func(format string, args ...interface{}) {
×
212
                if len(args) > 0 {
×
213
                        fmt.Printf(format, args)
×
214
                } else {
×
215
                        fmt.Print(format)
×
216
                }
×
217
        }
218
        return
×
219
}
220

221
// OsExitAllow place the original fatal function back
222
func OsExitAllow(o OrigLogFatalf) {
×
223
        logFatalf = o
×
224
        m.Unlock()
×
225
}
×
226

227
func Tell(w io.Writer, info string) {
×
228
        if Verbose {
×
229
                fmt.Fprintln(w, info)
×
230
        }
×
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc