github.com/osrg/gobgp@v2.0.0+incompatible/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  // +build !windows
    17  
    18  package main
    19  
    20  import (
    21  	"log/syslog"
    22  	"os"
    23  	"os/signal"
    24  	"runtime"
    25  	"runtime/debug"
    26  	"strings"
    27  	"syscall"
    28  
    29  	log "github.com/sirupsen/logrus"
    30  	lSyslog "github.com/sirupsen/logrus/hooks/syslog"
    31  )
    32  
    33  func init() {
    34  	go func() {
    35  		sigCh := make(chan os.Signal, 1)
    36  		signal.Notify(sigCh, syscall.SIGUSR1)
    37  		for range sigCh {
    38  			runtime.GC()
    39  			debug.FreeOSMemory()
    40  		}
    41  	}()
    42  }
    43  
    44  func addSyslogHook(host, facility string) error {
    45  	dst := strings.SplitN(host, ":", 2)
    46  	network := ""
    47  	addr := ""
    48  	if len(dst) == 2 {
    49  		network = dst[0]
    50  		addr = dst[1]
    51  	}
    52  
    53  	priority := syslog.Priority(0)
    54  	switch facility {
    55  	case "kern":
    56  		priority = syslog.LOG_KERN
    57  	case "user":
    58  		priority = syslog.LOG_USER
    59  	case "mail":
    60  		priority = syslog.LOG_MAIL
    61  	case "daemon":
    62  		priority = syslog.LOG_DAEMON
    63  	case "auth":
    64  		priority = syslog.LOG_AUTH
    65  	case "syslog":
    66  		priority = syslog.LOG_SYSLOG
    67  	case "lpr":
    68  		priority = syslog.LOG_LPR
    69  	case "news":
    70  		priority = syslog.LOG_NEWS
    71  	case "uucp":
    72  		priority = syslog.LOG_UUCP
    73  	case "cron":
    74  		priority = syslog.LOG_CRON
    75  	case "authpriv":
    76  		priority = syslog.LOG_AUTHPRIV
    77  	case "ftp":
    78  		priority = syslog.LOG_FTP
    79  	case "local0":
    80  		priority = syslog.LOG_LOCAL0
    81  	case "local1":
    82  		priority = syslog.LOG_LOCAL1
    83  	case "local2":
    84  		priority = syslog.LOG_LOCAL2
    85  	case "local3":
    86  		priority = syslog.LOG_LOCAL3
    87  	case "local4":
    88  		priority = syslog.LOG_LOCAL4
    89  	case "local5":
    90  		priority = syslog.LOG_LOCAL5
    91  	case "local6":
    92  		priority = syslog.LOG_LOCAL6
    93  	case "local7":
    94  		priority = syslog.LOG_LOCAL7
    95  	}
    96  
    97  	hook, err := lSyslog.NewSyslogHook(network, addr, syslog.LOG_INFO|priority, "bgpd")
    98  	if err != nil {
    99  		return err
   100  	}
   101  	log.AddHook(hook)
   102  	return nil
   103  }