Update logging
This commit is contained in:
parent
d35d6d9ea4
commit
5106303181
57
main.go
57
main.go
@ -13,7 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
daemon = false
|
|
||||||
serviceFile = "/adm/services"
|
serviceFile = "/adm/services"
|
||||||
services = make(map[string]Service)
|
services = make(map[string]Service)
|
||||||
controlSocket = "/adm/headless9/ctl/headless9.ctl"
|
controlSocket = "/adm/headless9/ctl/headless9.ctl"
|
||||||
@ -23,12 +22,16 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if flag.NArg() == 0 {
|
if flag.NArg() == 0 {
|
||||||
|
logFile, err := os.Create(fmt.Sprintf("%+v/%+v.log", logPath, "headless9"))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.SetOutput(logFile)
|
||||||
log.Println(DAEMON_START)
|
log.Println(DAEMON_START)
|
||||||
daemon = true
|
|
||||||
runDaemon()
|
runDaemon()
|
||||||
}
|
}
|
||||||
if flag.NArg() != 2 {
|
if flag.NArg() != 2 {
|
||||||
fmt.Println(CMD_SYNTAX)
|
log.Println(CMD_SYNTAX)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
verb := flag.Args()[0]
|
verb := flag.Args()[0]
|
||||||
@ -37,41 +40,33 @@ func main() {
|
|||||||
f := getCtl()
|
f := getCtl()
|
||||||
_, err := fmt.Fprintf(f, "%+v %+v", verb, service)
|
_, err := fmt.Fprintf(f, "%+v %+v", verb, service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(CTL_UNABLE_WRITE, controlSocket)
|
log.Fatalf(CTL_UNABLE_WRITE, controlSocket)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
err = watchFile(controlSocket)
|
err = watchFile(controlSocket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(strings.Join(sysTail(10, controlSocket), "\n"))
|
fmt.Println(sysTail(10, controlSocket))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCtl() *os.File {
|
func getCtl() *os.File {
|
||||||
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0660)
|
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if daemon {
|
log.Fatalf(CTL_NOT_OPEN, controlSocket)
|
||||||
log.Printf(CTL_NOT_OPEN, controlSocket)
|
|
||||||
} else {
|
|
||||||
fmt.Printf(CTL_NOT_OPEN, controlSocket)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err = f.Truncate(0)
|
err = f.Truncate(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if daemon {
|
log.Fatalf(CTL_NOT_CLEAR, controlSocket)
|
||||||
log.Printf(CTL_NOT_CLEAR, controlSocket)
|
|
||||||
} else {
|
|
||||||
fmt.Printf(CTL_NOT_CLEAR, controlSocket)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_, err = f.Seek(0, 0)
|
_, err = f.Seek(0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if daemon {
|
log.Fatalf(CTL_NOT_REWOUND, controlSocket)
|
||||||
log.Printf(CTL_NOT_REWOUND, controlSocket)
|
}
|
||||||
} else {
|
f, err = os.OpenFile(controlSocket, os.O_RDWR, 0660)
|
||||||
fmt.Printf(CTL_NOT_REWOUND, controlSocket)
|
if err != nil {
|
||||||
}
|
log.Fatalf(CTL_NOT_OPEN, controlSocket)
|
||||||
}
|
}
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
@ -142,18 +137,23 @@ func processCommand(cmd string) error {
|
|||||||
verb := parts[0]
|
verb := parts[0]
|
||||||
svc := parts[1]
|
svc := parts[1]
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
messages := 0
|
||||||
|
|
||||||
if verb == "restart" || verb == "stop" {
|
if verb == "restart" || verb == "stop" {
|
||||||
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/ctl", services[svc].ProcessHandle.Pid), os.O_WRONLY, 0660)
|
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/ctl", services[svc].ProcessHandle.Pid), os.O_WRONLY, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("")
|
log.Printf(DAEMON_CTL_FAILED, err, time.Since(start))
|
||||||
|
messages++
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
_, err = f.WriteString("kill")
|
_, err = f.WriteString("kill")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(DAEMON_CTL_FAILED, err, time.Since(start))
|
log.Printf(DAEMON_CTL_FAILED, err, time.Since(start))
|
||||||
|
messages++
|
||||||
} else {
|
} else {
|
||||||
delete(services, svc)
|
delete(services, svc)
|
||||||
log.Printf(DAEMON_CTL_PROCESSED, svc, time.Since(start))
|
log.Printf(DAEMON_CTL_PROCESSED, svc, time.Since(start))
|
||||||
|
messages++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if verb == "restart" || verb == "start" {
|
if verb == "restart" || verb == "start" {
|
||||||
@ -168,10 +168,22 @@ func processCommand(cmd string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf(DAEMON_SVC_MISSING, svcArgs[0])
|
log.Printf(DAEMON_SVC_MISSING, svcArgs[0])
|
||||||
|
messages++
|
||||||
go execCommand(svcArgs[0], svcArgs[1:]...)
|
go execCommand(svcArgs[0], svcArgs[1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ctlOut := ""
|
||||||
|
if verb == "status" {
|
||||||
|
ctlOut = strings.Join(sysTail(10, fmt.Sprintf("%+v/%+v.log", logPath, svc)), "\n")
|
||||||
|
} else {
|
||||||
|
ctlOut = strings.Join(sysTail(messages, fmt.Sprintf("%+v/%+v.log", logPath, svc)), "\n")
|
||||||
|
}
|
||||||
|
f := getCtl()
|
||||||
|
_, err := fmt.Fprintf(f, "%+v", ctlOut)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf(CTL_UNABLE_WRITE, controlSocket)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +276,6 @@ func PanicSafe(a ...interface{}) {
|
|||||||
func sysTail(count int, path string) []string {
|
func sysTail(count int, path string) []string {
|
||||||
c := exec.Command("tail", fmt.Sprintf("-%d", count+1), path)
|
c := exec.Command("tail", fmt.Sprintf("-%d", count+1), path)
|
||||||
output, _ := c.Output()
|
output, _ := c.Output()
|
||||||
//log.Printf("SysTail call output: %+v\nEND", string(output))
|
|
||||||
lines := strings.Split(string(output), "\n")
|
lines := strings.Split(string(output), "\n")
|
||||||
|
|
||||||
return lines[:len(lines)-1]
|
return lines[:len(lines)-1]
|
||||||
|
Loading…
Reference in New Issue
Block a user