github.com/osrg/gobgp/v3@v3.30.0/cmd/gobgpd/util.go (about) 1 // Copyright (C) 2017 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //go:build !windows 17 // +build !windows 18 19 package main 20 21 import ( 22 "log/syslog" 23 "os" 24 "os/signal" 25 "runtime" 26 "runtime/debug" 27 "strings" 28 "syscall" 29 30 "github.com/osrg/gobgp/v3/pkg/log" 31 "github.com/sirupsen/logrus" 32 lSyslog "github.com/sirupsen/logrus/hooks/syslog" 33 ) 34 35 func init() { 36 go func() { 37 sigCh := make(chan os.Signal, 1) 38 signal.Notify(sigCh, syscall.SIGUSR1) 39 for range sigCh { 40 runtime.GC() 41 debug.FreeOSMemory() 42 } 43 }() 44 } 45 46 func addSyslogHook(host, facility string) error { 47 dst := strings.SplitN(host, ":", 2) 48 network := "" 49 addr := "" 50 if len(dst) == 2 { 51 network = dst[0] 52 addr = dst[1] 53 } 54 55 priority := syslog.Priority(0) 56 switch facility { 57 case "kern": 58 priority = syslog.LOG_KERN 59 case "user": 60 priority = syslog.LOG_USER 61 case "mail": 62 priority = syslog.LOG_MAIL 63 case "daemon": 64 priority = syslog.LOG_DAEMON 65 case "auth": 66 priority = syslog.LOG_AUTH 67 case "syslog": 68 priority = syslog.LOG_SYSLOG 69 case "lpr": 70 priority = syslog.LOG_LPR 71 case "news": 72 priority = syslog.LOG_NEWS 73 case "uucp": 74 priority = syslog.LOG_UUCP 75 case "cron": 76 priority = syslog.LOG_CRON 77 case "authpriv": 78 priority = syslog.LOG_AUTHPRIV 79 case "ftp": 80 priority = syslog.LOG_FTP 81 case "local0": 82 priority = syslog.LOG_LOCAL0 83 case "local1": 84 priority = syslog.LOG_LOCAL1 85 case "local2": 86 priority = syslog.LOG_LOCAL2 87 case "local3": 88 priority = syslog.LOG_LOCAL3 89 case "local4": 90 priority = syslog.LOG_LOCAL4 91 case "local5": 92 priority = syslog.LOG_LOCAL5 93 case "local6": 94 priority = syslog.LOG_LOCAL6 95 case "local7": 96 priority = syslog.LOG_LOCAL7 97 } 98 99 hook, err := lSyslog.NewSyslogHook(network, addr, syslog.LOG_INFO|priority, "bgpd") 100 if err != nil { 101 return err 102 } 103 logger.AddHook(hook) 104 return nil 105 } 106 107 type builtinLogger struct { 108 logger *logrus.Logger 109 } 110 111 func (l *builtinLogger) Panic(msg string, fields log.Fields) { 112 l.logger.WithFields(logrus.Fields(fields)).Panic(msg) 113 } 114 115 func (l *builtinLogger) Fatal(msg string, fields log.Fields) { 116 l.logger.WithFields(logrus.Fields(fields)).Fatal(msg) 117 } 118 119 func (l *builtinLogger) Error(msg string, fields log.Fields) { 120 l.logger.WithFields(logrus.Fields(fields)).Error(msg) 121 } 122 123 func (l *builtinLogger) Warn(msg string, fields log.Fields) { 124 l.logger.WithFields(logrus.Fields(fields)).Warn(msg) 125 } 126 127 func (l *builtinLogger) Info(msg string, fields log.Fields) { 128 l.logger.WithFields(logrus.Fields(fields)).Info(msg) 129 } 130 131 func (l *builtinLogger) Debug(msg string, fields log.Fields) { 132 l.logger.WithFields(logrus.Fields(fields)).Debug(msg) 133 } 134 135 func (l *builtinLogger) SetLevel(level log.LogLevel) { 136 l.logger.SetLevel(logrus.Level(level)) 137 } 138 139 func (l *builtinLogger) GetLevel() log.LogLevel { 140 return log.LogLevel(l.logger.GetLevel()) 141 }