]> Cypherpunks.ru repositories - goredo.git/blob - log.go
335ca53c3d0a146079286a4c4b04caed91c6f450
[goredo.git] / log.go
1 /*
2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Logging facilities
19
20 package main
21
22 import (
23         "fmt"
24         "os"
25         "strings"
26 )
27
28 const (
29         CReset   = "\033[0m"
30         CBold    = "\033[1m"
31         CBlack   = "\033[30;1m"
32         CRed     = "\033[31;1m"
33         CGreen   = "\033[32;1m"
34         CYellow  = "\033[33;1m"
35         CBlue    = "\033[34;1m"
36         CMagenta = "\033[35;1m"
37         CCyan    = "\033[36;1m"
38         CWhite   = "\033[37;1m"
39
40         CDebug = CYellow
41         CRedo  = CGreen
42         CWait  = CBlue
43         CLock  = CCyan
44         CErr   = CRed
45         CWarn  = CMagenta
46         CJS    = CWhite
47         CNone  = "NONE"
48 )
49
50 var (
51         NoColor bool
52         Debug   bool
53         LogWait bool
54         LogLock bool
55         LogJS   bool
56         MyPid   int
57 )
58
59 func trace(level, format string, args ...interface{}) {
60         var p string
61         if MyPid != 0 {
62                 p = fmt.Sprintf("[%d] ", MyPid)
63         }
64         switch level {
65         case CNone:
66                 os.Stderr.WriteString(StderrPrefix + p + fmt.Sprintf(format+"\n", args...))
67                 return
68         case CDebug:
69                 if !Debug {
70                         return
71                 }
72                 p += "dbg  "
73         case CWait:
74                 if !(LogWait || Debug) {
75                         return
76                 }
77                 p += "wait "
78         case CRedo:
79                 p += "redo "
80         case CLock:
81                 if !(LogLock || Debug) {
82                         return
83                 }
84                 p += "lock "
85         case CJS:
86                 if !(LogJS || Debug) {
87                         return
88                 }
89                 p += "js   "
90         case CErr:
91                 p += "err  "
92         case CWarn:
93                 p += "warn "
94         }
95         msg := fmt.Sprintf(format, args...)
96         msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg)
97         os.Stderr.WriteString(msg + "\n")
98 }
99
100 func colourize(colour, s string) string {
101         if NoColor {
102                 return s
103         }
104         return colour + s + CReset
105 }