OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / log.go
1 // Copyright (c) 2013-2017 The btcsuite developers
2 // Copyright (c) 2017 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5
6 package main
7
8 import (
9         "fmt"
10         "os"
11         "path/filepath"
12
13         "github.com/btcsuite/btcd/addrmgr"
14         "github.com/btcsuite/btcd/blockchain"
15         "github.com/btcsuite/btcd/blockchain/indexers"
16         "github.com/btcsuite/btcd/connmgr"
17         "github.com/btcsuite/btcd/database"
18         "github.com/btcsuite/btcd/mempool"
19         "github.com/btcsuite/btcd/mining"
20         "github.com/btcsuite/btcd/mining/cpuminer"
21         "github.com/btcsuite/btcd/netsync"
22         "github.com/btcsuite/btcd/peer"
23         "github.com/btcsuite/btcd/txscript"
24         "github.com/btcsuite/btclog"
25         "github.com/jrick/logrotate/rotator"
26 )
27
28 // logWriter implements an io.Writer that outputs to both standard output and
29 // the write-end pipe of an initialized log rotator.
30 type logWriter struct{}
31
32 func (logWriter) Write(p []byte) (n int, err error) {
33         os.Stdout.Write(p)
34         logRotator.Write(p)
35         return len(p), nil
36 }
37
38 // Loggers per subsystem.  A single backend logger is created and all subsytem
39 // loggers created from it will write to the backend.  When adding new
40 // subsystems, add the subsystem logger variable here and to the
41 // subsystemLoggers map.
42 //
43 // Loggers can not be used before the log rotator has been initialized with a
44 // log file.  This must be performed early during application startup by calling
45 // initLogRotator.
46 var (
47         // backendLog is the logging backend used to create all subsystem loggers.
48         // The backend must not be used before the log rotator has been initialized,
49         // or data races and/or nil pointer dereferences will occur.
50         backendLog = btclog.NewBackend(logWriter{})
51
52         // logRotator is one of the logging outputs.  It should be closed on
53         // application shutdown.
54         logRotator *rotator.Rotator
55
56         adxrLog = backendLog.Logger("ADXR")
57         amgrLog = backendLog.Logger("AMGR")
58         cmgrLog = backendLog.Logger("CMGR")
59         bcdbLog = backendLog.Logger("BCDB")
60         btcdLog = backendLog.Logger("BTCD")
61         chanLog = backendLog.Logger("CHAN")
62         discLog = backendLog.Logger("DISC")
63         indxLog = backendLog.Logger("INDX")
64         minrLog = backendLog.Logger("MINR")
65         peerLog = backendLog.Logger("PEER")
66         rpcsLog = backendLog.Logger("RPCS")
67         scrpLog = backendLog.Logger("SCRP")
68         srvrLog = backendLog.Logger("SRVR")
69         syncLog = backendLog.Logger("SYNC")
70         txmpLog = backendLog.Logger("TXMP")
71 )
72
73 // Initialize package-global logger variables.
74 func init() {
75         addrmgr.UseLogger(amgrLog)
76         connmgr.UseLogger(cmgrLog)
77         database.UseLogger(bcdbLog)
78         blockchain.UseLogger(chanLog)
79         indexers.UseLogger(indxLog)
80         mining.UseLogger(minrLog)
81         cpuminer.UseLogger(minrLog)
82         peer.UseLogger(peerLog)
83         txscript.UseLogger(scrpLog)
84         netsync.UseLogger(syncLog)
85         mempool.UseLogger(txmpLog)
86 }
87
88 // subsystemLoggers maps each subsystem identifier to its associated logger.
89 var subsystemLoggers = map[string]btclog.Logger{
90         "ADXR": adxrLog,
91         "AMGR": amgrLog,
92         "CMGR": cmgrLog,
93         "BCDB": bcdbLog,
94         "BTCD": btcdLog,
95         "CHAN": chanLog,
96         "DISC": discLog,
97         "INDX": indxLog,
98         "MINR": minrLog,
99         "PEER": peerLog,
100         "RPCS": rpcsLog,
101         "SCRP": scrpLog,
102         "SRVR": srvrLog,
103         "SYNC": syncLog,
104         "TXMP": txmpLog,
105 }
106
107 // initLogRotator initializes the logging rotater to write logs to logFile and
108 // create roll files in the same directory.  It must be called before the
109 // package-global log rotater variables are used.
110 func initLogRotator(logFile string) {
111         logDir, _ := filepath.Split(logFile)
112         err := os.MkdirAll(logDir, 0700)
113         if err != nil {
114                 fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
115                 os.Exit(1)
116         }
117         r, err := rotator.New(logFile, 10*1024, false, 3)
118         if err != nil {
119                 fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
120                 os.Exit(1)
121         }
122
123         logRotator = r
124 }
125
126 // setLogLevel sets the logging level for provided subsystem.  Invalid
127 // subsystems are ignored.  Uninitialized subsystems are dynamically created as
128 // needed.
129 func setLogLevel(subsystemID string, logLevel string) {
130         // Ignore invalid subsystems.
131         logger, ok := subsystemLoggers[subsystemID]
132         if !ok {
133                 return
134         }
135
136         // Defaults to info if the log level is invalid.
137         level, _ := btclog.LevelFromString(logLevel)
138         logger.SetLevel(level)
139 }
140
141 // setLogLevels sets the log level for all subsystem loggers to the passed
142 // level.  It also dynamically creates the subsystem loggers as needed, so it
143 // can be used to initialize the logging system.
144 func setLogLevels(logLevel string) {
145         // Configure all sub-systems with the new logging level.  Dynamically
146         // create loggers as needed.
147         for subsystemID := range subsystemLoggers {
148                 setLogLevel(subsystemID, logLevel)
149         }
150 }
151
152 // directionString is a helper function that returns a string that represents
153 // the direction of a connection (inbound or outbound).
154 func directionString(inbound bool) string {
155         if inbound {
156                 return "inbound"
157         }
158         return "outbound"
159 }
160
161 // pickNoun returns the singular or plural form of a noun depending
162 // on the count n.
163 func pickNoun(n uint64, singular, plural string) string {
164         if n == 1 {
165                 return singular
166         }
167         return plural
168 }