-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput.go
105 lines (91 loc) · 2.76 KB
/
output.go
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
package dynamo
//----------------------------------------------------------------------
// This file is part of Dynamo.
// Copyright (C) 2011-2020 Bernd Fix
//
// Dynamo is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Dynamo is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------
import (
"fmt"
"log"
"os"
)
//======================================================================
// Normal program messages
//======================================================================
// Msg (plain message)
func Msg(msg string) {
log.Println(msg)
}
// Msgf (formatted message)
func Msgf(format string, args ...interface{}) {
log.Printf(format, args...)
}
// Fatal terminates the application with plain message
func Fatal(msg string) {
log.Fatal(msg)
}
// Fatalf terminates the application with formatted message
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
//======================================================================
// DEBUG messages
//======================================================================
// Shared debugger instance
var Dbg *Debugger
// Debugger writes debug messages to a file (if defined)
type Debugger struct {
file *os.File // reference to debug file (or nil if not defined)
console bool
}
// SetDebugger instantiates a new Debugger
func SetDebugger(file string) {
Dbg = new(Debugger)
if len(file) == 0 {
Dbg.file = nil
} else {
if file == "-" {
Dbg.console = true
Dbg.file = os.Stdout
} else {
var err error
if Dbg.file, err = os.Create(file); err != nil {
Fatal(err.Error())
}
}
}
}
// Close debugger file
func (dbg *Debugger) Close() *Result {
if dbg != nil && dbg.file != nil && !dbg.console {
dbg.file.Close()
}
return Success()
}
// Msg to write a plain message into the debugger file
func (dbg *Debugger) Msg(msg string) {
if dbg != nil && dbg.file != nil {
dbg.file.WriteString(msg + "\n")
}
}
// Msgf to write a formatted message into the debugger file
func (dbg *Debugger) Msgf(format string, args ...interface{}) {
if dbg != nil && dbg.file != nil {
msg := fmt.Sprintf(format, args...)
dbg.file.WriteString(msg)
}
}