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