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