2022-02-06 06:42:13 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2022 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"log/syslog"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-02-06 19:30:53 +01:00
|
|
|
// log.Default() not added until go 1.16
|
|
|
|
func logdefault() *log.Logger {
|
2022-02-09 22:11:35 +01:00
|
|
|
return log.New(os.Stderr, "", log.LstdFlags)
|
2022-02-06 19:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var elog = logdefault()
|
|
|
|
var ilog = logdefault()
|
|
|
|
var dlog = logdefault()
|
2022-02-06 06:42:13 +01:00
|
|
|
|
|
|
|
var elogname, ilogname, dlogname, alllogname string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&elogname, "errorlog", "stderr", "error log file (or stderr, null, syslog)")
|
|
|
|
flag.StringVar(&ilogname, "infolog", "stderr", "info log file (or stderr, null, syslog)")
|
|
|
|
flag.StringVar(&dlogname, "debuglog", "stderr", "debug log file (or stderr, null, syslog)")
|
|
|
|
flag.StringVar(&alllogname, "log", "stderr", "combined log file (or stderr, null, syslog)")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:30:58 +01:00
|
|
|
func loggingArgs() []string {
|
|
|
|
return []string{"-errorlog", elogname, "-infolog", ilogname, "-debuglog", dlogname}
|
|
|
|
}
|
|
|
|
|
2022-02-06 06:42:13 +01:00
|
|
|
func initLogging(elogname, ilogname, dlogname string) {
|
|
|
|
elog = openlog(elogname, syslog.LOG_ERR)
|
|
|
|
ilog = openlog(ilogname, syslog.LOG_INFO)
|
|
|
|
dlog = openlog(dlogname, syslog.LOG_DEBUG)
|
|
|
|
}
|
|
|
|
|
|
|
|
func openlog(name string, prio syslog.Priority) *log.Logger {
|
|
|
|
if name == "stderr" {
|
2022-02-09 22:11:35 +01:00
|
|
|
return log.New(os.Stderr, "", log.LstdFlags)
|
2022-02-06 06:42:13 +01:00
|
|
|
}
|
2022-02-06 06:45:54 +01:00
|
|
|
if name == "stdout" {
|
2022-02-09 22:11:35 +01:00
|
|
|
return log.New(os.Stdout, "", log.LstdFlags)
|
2022-02-06 06:45:54 +01:00
|
|
|
}
|
|
|
|
if name == "null" {
|
2022-02-09 22:11:35 +01:00
|
|
|
return log.New(ioutil.Discard, "", log.LstdFlags)
|
2022-02-06 06:45:54 +01:00
|
|
|
}
|
2022-02-06 06:42:13 +01:00
|
|
|
if name == "syslog" {
|
2022-02-09 22:11:35 +01:00
|
|
|
w, err := syslog.New(syslog.LOG_UUCP|prio, "honk")
|
2022-02-06 06:42:13 +01:00
|
|
|
if err != nil {
|
|
|
|
elog.Printf("can't create syslog: %s", err)
|
2022-02-06 19:30:53 +01:00
|
|
|
return logdefault()
|
2022-02-06 06:42:13 +01:00
|
|
|
}
|
2022-02-09 22:11:35 +01:00
|
|
|
return log.New(w, "", log.LstdFlags)
|
2022-02-06 06:42:13 +01:00
|
|
|
}
|
|
|
|
fd, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
elog.Printf("can't open log file %s: %s", name, err)
|
2022-02-06 19:30:53 +01:00
|
|
|
return logdefault()
|
2022-02-06 06:42:13 +01:00
|
|
|
}
|
2022-02-09 22:11:35 +01:00
|
|
|
logger := log.New(fd, "", log.LstdFlags)
|
2022-02-06 06:42:13 +01:00
|
|
|
logger.Printf("new log started")
|
|
|
|
return logger
|
|
|
|
}
|