logging.go (1651B)
1 // Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org> 2 // 3 // This file is part of tordam 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package tordam 19 20 import ( 21 "log" 22 "os" 23 "path" 24 "runtime" 25 ) 26 27 var ( 28 inte *log.Logger 29 warn *log.Logger 30 info *log.Logger 31 ) 32 33 // LogInit is the initializer for the internal tordam logging functions. 34 // It should be called from programs using the library, with something like: 35 // tordam.LogInit(os.Stdout) 36 func LogInit(f *os.File) { 37 inte = log.New(f, "INTERNAL ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) 38 warn = log.New(f, "WARNING: ", log.Ldate|log.Ltime) 39 info = log.New(f, "INFO: ", log.Ldate|log.Ltime) 40 } 41 42 func fname() string { 43 pc, _, _, _ := runtime.Caller(2) 44 if fn := runtime.FuncForPC(pc); fn != nil { 45 return path.Base(fn.Name()) + "()" 46 } else { 47 return "?()" 48 } 49 } 50 51 func rpcWarn(msg string) { 52 warn.Printf("%s: %s", fname(), msg) 53 } 54 55 func rpcInfo(msg string) { 56 info.Printf("%s: %s", fname(), msg) 57 } 58 59 func rpcInternalErr(msg string) { 60 inte.Printf("%s: %s", fname(), msg) 61 }