github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/gopkg.in/mgo.v2/log.go (about) 1 // mgo - MongoDB driver for Go 2 // 3 // Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net> 4 // 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions are met: 9 // 10 // 1. Redistributions of source code must retain the above copyright notice, this 11 // list of conditions and the following disclaimer. 12 // 2. Redistributions in binary form must reproduce the above copyright notice, 13 // this list of conditions and the following disclaimer in the documentation 14 // and/or other materials provided with the distribution. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 package mgo 28 29 import ( 30 "fmt" 31 "sync" 32 ) 33 34 // --------------------------------------------------------------------------- 35 // Logging integration. 36 37 // Avoid importing the log type information unnecessarily. There's a small cost 38 // associated with using an interface rather than the type. Depending on how 39 // often the logger is plugged in, it would be worth using the type instead. 40 type log_Logger interface { 41 Output(calldepth int, s string) error 42 } 43 44 var ( 45 globalLogger log_Logger 46 globalDebug bool 47 globalMutex sync.Mutex 48 ) 49 50 // RACE WARNING: There are known data races when logging, which are manually 51 // silenced when the race detector is in use. These data races won't be 52 // observed in typical use, because logging is supposed to be set up once when 53 // the application starts. Having raceDetector as a constant, the compiler 54 // should elide the locks altogether in actual use. 55 56 // Specify the *log.Logger object where log messages should be sent to. 57 func SetLogger(logger log_Logger) { 58 if raceDetector { 59 globalMutex.Lock() 60 defer globalMutex.Unlock() 61 } 62 globalLogger = logger 63 } 64 65 // Enable the delivery of debug messages to the logger. Only meaningful 66 // if a logger is also set. 67 func SetDebug(debug bool) { 68 if raceDetector { 69 globalMutex.Lock() 70 defer globalMutex.Unlock() 71 } 72 globalDebug = debug 73 } 74 75 func log(v ...interface{}) { 76 if raceDetector { 77 globalMutex.Lock() 78 defer globalMutex.Unlock() 79 } 80 if globalLogger != nil { 81 globalLogger.Output(2, fmt.Sprint(v...)) 82 } 83 } 84 85 func logln(v ...interface{}) { 86 if raceDetector { 87 globalMutex.Lock() 88 defer globalMutex.Unlock() 89 } 90 if globalLogger != nil { 91 globalLogger.Output(2, fmt.Sprintln(v...)) 92 } 93 } 94 95 func logf(format string, v ...interface{}) { 96 if raceDetector { 97 globalMutex.Lock() 98 defer globalMutex.Unlock() 99 } 100 if globalLogger != nil { 101 globalLogger.Output(2, fmt.Sprintf(format, v...)) 102 } 103 } 104 105 func debug(v ...interface{}) { 106 if raceDetector { 107 globalMutex.Lock() 108 defer globalMutex.Unlock() 109 } 110 if globalDebug && globalLogger != nil { 111 globalLogger.Output(2, fmt.Sprint(v...)) 112 } 113 } 114 115 func debugln(v ...interface{}) { 116 if raceDetector { 117 globalMutex.Lock() 118 defer globalMutex.Unlock() 119 } 120 if globalDebug && globalLogger != nil { 121 globalLogger.Output(2, fmt.Sprintln(v...)) 122 } 123 } 124 125 func debugf(format string, v ...interface{}) { 126 if raceDetector { 127 globalMutex.Lock() 128 defer globalMutex.Unlock() 129 } 130 if globalDebug && globalLogger != nil { 131 globalLogger.Output(2, fmt.Sprintf(format, v...)) 132 } 133 }