]> Cypherpunks.ru repositories - goredo.git/blob - log.go
Various refactoring
[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         "flag"
24         "fmt"
25         "os"
26         "strings"
27 )
28
29 const (
30         EnvLevel   = "REDO_LEVEL"
31         EnvDebug   = "REDO_DEBUG"
32         EnvLogWait = "REDO_LOG_WAIT"
33         EnvLogLock = "REDO_LOG_LOCK"
34         EnvLogPid  = "REDO_LOG_PID"
35         EnvLogJS   = "REDO_LOG_JS"
36         EnvNoColor = "NO_COLOR"
37
38         CReset   = "\033[0m"
39         CBold    = "\033[1m"
40         CBlack   = "\033[30;1m"
41         CRed     = "\033[31;1m"
42         CGreen   = "\033[32;1m"
43         CYellow  = "\033[33;1m"
44         CBlue    = "\033[34;1m"
45         CMagenta = "\033[35;1m"
46         CCyan    = "\033[36;1m"
47         CWhite   = "\033[37;1m"
48
49         CDebug = CYellow
50         CRedo  = CGreen
51         CWait  = CBlue
52         CLock  = CCyan
53         CErr   = CRed
54         CWarn  = CMagenta
55         CJS    = CWhite
56         CNone  = "NONE"
57 )
58
59 var (
60         Level   = 0
61         NoColor bool
62         Debug   bool
63         LogWait bool
64         LogLock bool
65         LogJS   bool
66         MyPid   int
67
68         flagDebug   = flag.Bool("debug", false, "enable debug logging (REDO_DEBUG=1)")
69         flagLogWait = flag.Bool("log-wait", false, "enable wait messages logging (REDO_LOG_WAIT=1)")
70         flagLogLock = flag.Bool("log-lock", false, "enable lock messages logging (REDO_LOG_LOCK=1)")
71         flagLogPid  = flag.Bool("log-pid", false, "append PIDs (REDO_LOG_PID=1)")
72         flagLogJS   = flag.Bool("log-js", false, "enable jobserver messages logging (REDO_LOG_JS=1)")
73 )
74
75 func trace(level, format string, args ...interface{}) {
76         var p string
77         if MyPid != 0 {
78                 p = fmt.Sprintf("[%d] ", MyPid)
79         }
80         switch level {
81         case CNone:
82                 os.Stderr.WriteString(StderrPrefix + p + fmt.Sprintf(format+"\n", args...))
83                 return
84         case CDebug:
85                 if !Debug {
86                         return
87                 }
88                 p += "dbg  "
89         case CWait:
90                 if !(LogWait || Debug) {
91                         return
92                 }
93                 p += "wait "
94         case CRedo:
95                 p += "redo "
96         case CLock:
97                 if !(LogLock || Debug) {
98                         return
99                 }
100                 p += "lock "
101         case CJS:
102                 if !(LogJS || Debug) {
103                         return
104                 }
105                 p += "js   "
106         case CErr:
107                 p += "err  "
108         case CWarn:
109                 p += "warn "
110         }
111         msg := fmt.Sprintf(format, args...)
112         msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg)
113         os.Stderr.WriteString(msg + "\n")
114 }
115
116 func colourize(colour, s string) string {
117         if NoColor {
118                 return s
119         }
120         return colour + s + CReset
121 }