mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-03 11:53:43 +02:00
src/main.go: rewrite openlog() to a more specific setLogFile() function.
This commit is contained in:
parent
184302b9e4
commit
c4e404f873
1 changed files with 19 additions and 6 deletions
25
src/main.go
25
src/main.go
|
|
@ -14,6 +14,12 @@ import (
|
||||||
var logFile string
|
var logFile string
|
||||||
var pidFile string
|
var pidFile string
|
||||||
|
|
||||||
|
// Global variables
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
// File descriptor to the current log file.
|
||||||
|
var logfd *os.File
|
||||||
|
|
||||||
// argv_listen_addr
|
// argv_listen_addr
|
||||||
// Parse listen address from command line.
|
// Parse listen address from command line.
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
@ -38,29 +44,36 @@ func argv_listen_addr() string {
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func openlog(file string) *os.File {
|
func setLogFile() {
|
||||||
|
|
||||||
|
// Open file
|
||||||
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
return fd
|
|
||||||
|
// Close old one (if open)
|
||||||
|
if logfd.Fd() < 0 {
|
||||||
|
logfd.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update variable and set log writer.
|
||||||
|
logfd = fd
|
||||||
|
log.SetWriter(logfd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// main
|
// main
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var logfd *os.File
|
|
||||||
|
|
||||||
// Command line parsing
|
// Command line parsing
|
||||||
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
||||||
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
||||||
getopt.Parse()
|
getopt.Parse()
|
||||||
|
|
||||||
|
// Open logfile.
|
||||||
if len(logFile) > 0 {
|
if len(logFile) > 0 {
|
||||||
logfd = openlog(logFile)
|
setLogFile()
|
||||||
log.SetWriter(logfd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Process is starting with PID: %d", pid.Get())
|
log.Info("Process is starting with PID: %d", pid.Get())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue