parent
9d44c05a17
commit
a70f1ac424
@ -5,11 +5,11 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,19 +31,19 @@ type Response struct {
|
|||||||
func SendAndRetrieve(data Request) Response {
|
func SendAndRetrieve(data Request) Response {
|
||||||
conn, err := net.Dial("unix", *SocketPath)
|
conn, err := net.Dial("unix", *SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Error opening connection top daemon:", err)
|
logger.Fatalln("Error opening connection top daemon:", err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
err = gob.NewEncoder(conn).Encode(data)
|
err = gob.NewEncoder(conn).Encode(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Can't send message:", err)
|
logger.Fatalln("Can't send message:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response Response
|
var response Response
|
||||||
err = gob.NewDecoder(conn).Decode(&response)
|
err = gob.NewDecoder(conn).Decode(&response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Invalid answer from daemon:", err)
|
logger.Fatalln("Invalid answer from daemon:", err)
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -81,13 +81,13 @@ func (csf ClientStatusFlush) MarshalJSON() ([]byte, error) {
|
|||||||
func usage(err string) {
|
func usage(err string) {
|
||||||
fmt.Println("Usage: reactionc")
|
fmt.Println("Usage: reactionc")
|
||||||
fmt.Println("Usage: reactionc flush <PATTERN>")
|
fmt.Println("Usage: reactionc flush <PATTERN>")
|
||||||
log.Fatalln(err)
|
logger.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClientShow(streamfilter, format string) {
|
func ClientShow(streamfilter, format string) {
|
||||||
response := SendAndRetrieve(Request{Show, streamfilter})
|
response := SendAndRetrieve(Request{Show, streamfilter})
|
||||||
if response.Err != nil {
|
if response.Err != nil {
|
||||||
log.Fatalln("Received error from daemon:", response.Err)
|
logger.Fatalln("Received error from daemon:", response.Err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
var text []byte
|
var text []byte
|
||||||
@ -98,7 +98,7 @@ func ClientShow(streamfilter, format string) {
|
|||||||
text, err = yaml.Marshal(response.ClientStatus)
|
text, err = yaml.Marshal(response.ClientStatus)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to convert daemon binary response to text format:", err)
|
logger.Fatalln("Failed to convert daemon binary response to text format:", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(text))
|
fmt.Println(string(text))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
@ -107,7 +107,7 @@ func ClientShow(streamfilter, format string) {
|
|||||||
func ClientFlush(pattern, streamfilter, format string) {
|
func ClientFlush(pattern, streamfilter, format string) {
|
||||||
response := SendAndRetrieve(Request{Flush, pattern})
|
response := SendAndRetrieve(Request{Flush, pattern})
|
||||||
if response.Err != nil {
|
if response.Err != nil {
|
||||||
log.Fatalln("Received error from daemon:", response.Err)
|
logger.Fatalln("Received error from daemon:", response.Err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
var text []byte
|
var text []byte
|
||||||
@ -118,7 +118,7 @@ func ClientFlush(pattern, streamfilter, format string) {
|
|||||||
text, err = yaml.Marshal(ClientStatusFlush(response.ClientStatus))
|
text, err = yaml.Marshal(ClientStatusFlush(response.ClientStatus))
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to convert daemon binary response to text format:", err)
|
logger.Fatalln("Failed to convert daemon binary response to text format:", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(text))
|
fmt.Println(string(text))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -2,15 +2,15 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Executes a command and channel-send its stdout
|
// Executes a command and channel-send its stdout
|
||||||
@ -21,17 +21,29 @@ func cmdStdout(commandline []string) chan *string {
|
|||||||
cmd := exec.Command(commandline[0], commandline[1:]...)
|
cmd := exec.Command(commandline[0], commandline[1:]...)
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL couldn't open stdout on command:", err)
|
logger.Fatalln("couldn't open stdout on command:", err)
|
||||||
|
}
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalln("couldn't open stderr on command:", err)
|
||||||
}
|
}
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
log.Fatalln("FATAL couldn't start command:", err)
|
logger.Fatalln("couldn't start command:", err)
|
||||||
}
|
}
|
||||||
defer stdout.Close()
|
defer stdout.Close()
|
||||||
|
defer stderr.Close()
|
||||||
|
func() {
|
||||||
|
errscan := bufio.NewScanner(stderr)
|
||||||
|
for errscan.Scan() {
|
||||||
|
line := errscan.Text()
|
||||||
|
logger.Println(logger.WARN, "stderr:", line)
|
||||||
|
}
|
||||||
|
}()
|
||||||
scanner := bufio.NewScanner(stdout)
|
scanner := bufio.NewScanner(stdout)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
lines <- &line
|
lines <- &line
|
||||||
|
logger.Println(logger.DEBUG, "stdout:", line)
|
||||||
}
|
}
|
||||||
close(lines)
|
close(lines)
|
||||||
}()
|
}()
|
||||||
@ -57,7 +69,7 @@ func (f *Filter) match(line *string) string {
|
|||||||
match := matches[regex.SubexpIndex(f.pattern.name)]
|
match := matches[regex.SubexpIndex(f.pattern.name)]
|
||||||
|
|
||||||
if f.pattern.notAnIgnore(&match) {
|
if f.pattern.notAnIgnore(&match) {
|
||||||
log.Printf("INFO %s.%s: match [%v]\n", f.stream.name, f.name, match)
|
logger.Printf(logger.INFO, "%s.%s: match [%v]\n", f.stream.name, f.name, match)
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,12 +93,12 @@ func (a *Action) exec(match string) {
|
|||||||
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match))
|
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("INFO %s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
|
logger.Printf(logger.INFO, "%s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
|
||||||
|
|
||||||
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
|
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
|
||||||
|
|
||||||
if ret := cmd.Run(); ret != nil {
|
if ret := cmd.Run(); ret != nil {
|
||||||
log.Printf("ERROR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
|
logger.Printf(logger.ERROR, "%s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -241,7 +253,7 @@ func matchesManagerHandleMatch(pft PFT) bool {
|
|||||||
|
|
||||||
func StreamManager(s *Stream, endedSignal chan *Stream) {
|
func StreamManager(s *Stream, endedSignal chan *Stream) {
|
||||||
defer wgStreams.Done()
|
defer wgStreams.Done()
|
||||||
log.Printf("INFO %s: start %s\n", s.name, s.Cmd)
|
logger.Printf(logger.INFO, "%s: start %s\n", s.name, s.Cmd)
|
||||||
|
|
||||||
lines := cmdStdout(s.Cmd)
|
lines := cmdStdout(s.Cmd)
|
||||||
for {
|
for {
|
||||||
@ -345,13 +357,13 @@ func Daemon(confFilename string) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case finishedStream := <-endSignals:
|
case finishedStream := <-endSignals:
|
||||||
log.Printf("ERROR %s stream finished", finishedStream.name)
|
logger.Printf(logger.ERROR, "%s stream finished", finishedStream.name)
|
||||||
nbStreamsInExecution--
|
nbStreamsInExecution--
|
||||||
if nbStreamsInExecution == 0 {
|
if nbStreamsInExecution == 0 {
|
||||||
quit()
|
quit()
|
||||||
}
|
}
|
||||||
case <-sigs:
|
case <-sigs:
|
||||||
log.Printf("INFO Received SIGINT/SIGTERM, exiting")
|
logger.Printf(logger.INFO, "Received SIGINT/SIGTERM, exiting")
|
||||||
quit()
|
quit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -360,19 +372,19 @@ func Daemon(confFilename string) {
|
|||||||
func quit() {
|
func quit() {
|
||||||
// send stop to StreamManager·s
|
// send stop to StreamManager·s
|
||||||
close(stopStreams)
|
close(stopStreams)
|
||||||
log.Println("INFO Waiting for Streams to finish...")
|
logger.Println(logger.INFO, "Waiting for Streams to finish...")
|
||||||
wgStreams.Wait()
|
wgStreams.Wait()
|
||||||
// ActionsManager calls wgActions.Done() when it has launched all pending actions
|
// ActionsManager calls wgActions.Done() when it has launched all pending actions
|
||||||
wgActions.Add(1)
|
wgActions.Add(1)
|
||||||
// send stop to ActionsManager
|
// send stop to ActionsManager
|
||||||
close(stopActions)
|
close(stopActions)
|
||||||
// stop all actions
|
// stop all actions
|
||||||
log.Println("INFO Waiting for Actions to finish...")
|
logger.Println(logger.INFO, "Waiting for Actions to finish...")
|
||||||
wgActions.Wait()
|
wgActions.Wait()
|
||||||
// delete pipe
|
// delete pipe
|
||||||
err := os.Remove(*SocketPath)
|
err := os.Remove(*SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to remove socket:", err)
|
logger.Println(logger.ERROR, "Failed to remove socket:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
|
@ -4,9 +4,10 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -19,10 +20,10 @@ func openDB(path string) (bool, *ReadDB) {
|
|||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
log.Printf("WARN No DB found at %s. It's ok if this is the first time reaction is running.\n", path)
|
logger.Printf(logger.WARN, "No DB found at %s. It's ok if this is the first time reaction is running.\n", path)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
log.Fatalln("FATAL Failed to open DB:", err)
|
logger.Fatalln("Failed to open DB:", err)
|
||||||
}
|
}
|
||||||
return false, &ReadDB{file, gob.NewDecoder(file)}
|
return false, &ReadDB{file, gob.NewDecoder(file)}
|
||||||
}
|
}
|
||||||
@ -30,7 +31,7 @@ func openDB(path string) (bool, *ReadDB) {
|
|||||||
func createDB(path string) *WriteDB {
|
func createDB(path string) *WriteDB {
|
||||||
file, err := os.Create(path)
|
file, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to create DB:", err)
|
logger.Fatalln("Failed to create DB:", err)
|
||||||
}
|
}
|
||||||
return &WriteDB{file, gob.NewEncoder(file)}
|
return &WriteDB{file, gob.NewEncoder(file)}
|
||||||
}
|
}
|
||||||
@ -53,11 +54,11 @@ func (c *Conf) manageLogs(logDB *WriteDB, flushDB *WriteDB) {
|
|||||||
// let's say 100 000 entries ~ 10 MB
|
// let's say 100 000 entries ~ 10 MB
|
||||||
if cpt == 100_000 {
|
if cpt == 100_000 {
|
||||||
cpt = 0
|
cpt = 0
|
||||||
log.Printf("INFO Rotating database...")
|
logger.Printf(logger.INFO, "Rotating database...")
|
||||||
logDB.file.Close()
|
logDB.file.Close()
|
||||||
flushDB.file.Close()
|
flushDB.file.Close()
|
||||||
logDB, flushDB = c.RotateDB(false)
|
logDB, flushDB = c.RotateDB(false)
|
||||||
log.Printf("INFO Rotated database")
|
logger.Printf(logger.INFO, "Rotated database")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,10 +79,10 @@ func (c *Conf) RotateDB(startup bool) (*WriteDB, *WriteDB) {
|
|||||||
}
|
}
|
||||||
doesntExist, flushReadDB = openDB(flushDBName)
|
doesntExist, flushReadDB = openDB(flushDBName)
|
||||||
if doesntExist {
|
if doesntExist {
|
||||||
log.Println("WARN Strange! No flushes db, opening /dev/null instead")
|
logger.Println(logger.WARN, "Strange! No flushes db, opening /dev/null instead")
|
||||||
doesntExist, flushReadDB = openDB("/dev/null")
|
doesntExist, flushReadDB = openDB("/dev/null")
|
||||||
if doesntExist {
|
if doesntExist {
|
||||||
log.Fatalln("Opening dummy /dev/null failed")
|
logger.Fatalln("Opening dummy /dev/null failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,18 +92,18 @@ func (c *Conf) RotateDB(startup bool) (*WriteDB, *WriteDB) {
|
|||||||
|
|
||||||
err = logReadDB.file.Close()
|
err = logReadDB.file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to close old DB:", err)
|
logger.Fatalln("Failed to close old DB:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// It should be ok to rename an open file
|
// It should be ok to rename an open file
|
||||||
err = os.Rename(logDBNewName, logDBName)
|
err = os.Rename(logDBNewName, logDBName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to replace old DB with new one:", err)
|
logger.Fatalln("Failed to replace old DB with new one:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Remove(flushDBName)
|
err = os.Remove(flushDBName)
|
||||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
log.Fatalln("FATAL Failed to delete old DB:", err)
|
logger.Fatalln("Failed to delete old DB:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
flushWriteDB = createDB(flushDBName)
|
flushWriteDB = createDB(flushDBName)
|
||||||
@ -116,11 +117,11 @@ func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.E
|
|||||||
defer func() {
|
defer func() {
|
||||||
for sf, t := range discardedEntries {
|
for sf, t := range discardedEntries {
|
||||||
if t > 0 {
|
if t > 0 {
|
||||||
log.Printf("WARN info discarded %v times from the DBs: stream/filter not found: %s.%s\n", t, sf.s, sf.f)
|
logger.Printf(logger.WARN, "info discarded %v times from the DBs: stream/filter not found: %s.%s\n", t, sf.s, sf.f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if malformedEntries > 0 {
|
if malformedEntries > 0 {
|
||||||
log.Printf("WARN %v malformed entries discarded from the DBs\n", malformedEntries)
|
logger.Printf(logger.WARN, "%v malformed entries discarded from the DBs\n", malformedEntries)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -210,6 +211,6 @@ func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.E
|
|||||||
func encodeOrFatal(enc *gob.Encoder, entry LogEntry) {
|
func encodeOrFatal(enc *gob.Encoder, entry LogEntry) {
|
||||||
err := enc.Encode(entry)
|
err := enc.Encode(entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to write to new DB:", err)
|
logger.Fatalln("Failed to write to new DB:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
app/pipe.go
19
app/pipe.go
@ -2,12 +2,13 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_actionsLock, local_matchesLock *sync.Mutex) ClientStatus {
|
func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_actionsLock, local_matchesLock *sync.Mutex) ClientStatus {
|
||||||
@ -56,19 +57,19 @@ func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_a
|
|||||||
func createOpenSocket() net.Listener {
|
func createOpenSocket() net.Listener {
|
||||||
err := os.MkdirAll(path.Dir(*SocketPath), 0755)
|
err := os.MkdirAll(path.Dir(*SocketPath), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to create socket directory")
|
logger.Fatalln("Failed to create socket directory")
|
||||||
}
|
}
|
||||||
_, err = os.Stat(*SocketPath)
|
_, err = os.Stat(*SocketPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("WARN socket", SocketPath, "already exists: Is the daemon already running? Deleting.")
|
logger.Println(logger.WARN, "socket", SocketPath, "already exists: Is the daemon already running? Deleting.")
|
||||||
err = os.Remove(*SocketPath)
|
err = os.Remove(*SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to remove socket:", err)
|
logger.Fatalln("Failed to remove socket:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ln, err := net.Listen("unix", *SocketPath)
|
ln, err := net.Listen("unix", *SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to create socket:", err)
|
logger.Fatalln("Failed to create socket:", err)
|
||||||
}
|
}
|
||||||
return ln
|
return ln
|
||||||
}
|
}
|
||||||
@ -80,7 +81,7 @@ func SocketManager(streams map[string]*Stream) {
|
|||||||
for {
|
for {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR Failed to open connection from cli:", err)
|
logger.Println(logger.ERROR, "Failed to open connection from cli:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go func(conn net.Conn) {
|
go func(conn net.Conn) {
|
||||||
@ -90,7 +91,7 @@ func SocketManager(streams map[string]*Stream) {
|
|||||||
|
|
||||||
err := gob.NewDecoder(conn).Decode(&request)
|
err := gob.NewDecoder(conn).Decode(&request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR Invalid Message from cli:", err)
|
logger.Println(logger.ERROR, "Invalid Message from cli:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,13 +109,13 @@ func SocketManager(streams map[string]*Stream) {
|
|||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
response.ClientStatus = genClientStatus(<-actionsC.ret, <-matchesC.ret, &lock, &lock)
|
response.ClientStatus = genClientStatus(<-actionsC.ret, <-matchesC.ret, &lock, &lock)
|
||||||
default:
|
default:
|
||||||
log.Println("ERROR Invalid Message from cli: unrecognised Request type")
|
logger.Println(logger.ERROR, "Invalid Message from cli: unrecognised Request type")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = gob.NewEncoder(conn).Encode(response)
|
err = gob.NewEncoder(conn).Encode(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR Can't respond to cli:", err)
|
logger.Println(logger.ERROR, "Can't respond to cli:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}(conn)
|
}(conn)
|
||||||
|
@ -3,12 +3,13 @@ package app
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
|
|
||||||
"github.com/google/go-jsonnet"
|
"github.com/google/go-jsonnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,23 +21,23 @@ func (c *Conf) setup() {
|
|||||||
pattern.nameWithBraces = fmt.Sprintf("<%s>", pattern.name)
|
pattern.nameWithBraces = fmt.Sprintf("<%s>", pattern.name)
|
||||||
|
|
||||||
if pattern.Regex == "" {
|
if pattern.Regex == "" {
|
||||||
log.Fatalf("FATAL Bad configuration: pattern's regex %v is empty!", patternName)
|
logger.Fatalf("Bad configuration: pattern's regex %v is empty!", patternName)
|
||||||
}
|
}
|
||||||
|
|
||||||
compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex))
|
compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("FATAL Bad configuration: pattern %v doesn't compile!", patternName)
|
logger.Fatalf("Bad configuration: pattern %v doesn't compile!", patternName)
|
||||||
}
|
}
|
||||||
c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex)
|
c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex)
|
||||||
for _, ignore := range pattern.Ignore {
|
for _, ignore := range pattern.Ignore {
|
||||||
if !compiled.MatchString(ignore) {
|
if !compiled.MatchString(ignore) {
|
||||||
log.Fatalf("FATAL Bad configuration: pattern ignore '%v' doesn't match pattern %v! It should be fixed or removed.", ignore, pattern.nameWithBraces)
|
logger.Fatalf("Bad configuration: pattern ignore '%v' doesn't match pattern %v! It should be fixed or removed.", ignore, pattern.nameWithBraces)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.Streams) == 0 {
|
if len(c.Streams) == 0 {
|
||||||
log.Fatalln("FATAL Bad configuration: no streams configured!")
|
logger.Fatalln("Bad configuration: no streams configured!")
|
||||||
}
|
}
|
||||||
for streamName := range c.Streams {
|
for streamName := range c.Streams {
|
||||||
|
|
||||||
@ -44,11 +45,11 @@ func (c *Conf) setup() {
|
|||||||
stream.name = streamName
|
stream.name = streamName
|
||||||
|
|
||||||
if strings.Contains(stream.name, ".") {
|
if strings.Contains(stream.name, ".") {
|
||||||
log.Fatalln("FATAL Bad configuration: character '.' is not allowed in stream names", stream.name)
|
logger.Fatalln("Bad configuration: character '.' is not allowed in stream names", stream.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(stream.Filters) == 0 {
|
if len(stream.Filters) == 0 {
|
||||||
log.Fatalln("FATAL Bad configuration: no filters configured in", stream.name)
|
logger.Fatalln("Bad configuration: no filters configured in", stream.name)
|
||||||
}
|
}
|
||||||
for filterName := range stream.Filters {
|
for filterName := range stream.Filters {
|
||||||
|
|
||||||
@ -57,23 +58,23 @@ func (c *Conf) setup() {
|
|||||||
filter.name = filterName
|
filter.name = filterName
|
||||||
|
|
||||||
if strings.Contains(filter.name, ".") {
|
if strings.Contains(filter.name, ".") {
|
||||||
log.Fatalln("FATAL Bad configuration: character '.' is not allowed in filter names", filter.name)
|
logger.Fatalln("Bad configuration: character '.' is not allowed in filter names", filter.name)
|
||||||
}
|
}
|
||||||
// Parse Duration
|
// Parse Duration
|
||||||
if filter.RetryPeriod == "" {
|
if filter.RetryPeriod == "" {
|
||||||
if filter.Retry > 1 {
|
if filter.Retry > 1 {
|
||||||
log.Fatalln("FATAL Bad configuration: retry but no retry-duration in", stream.name, ".", filter.name)
|
logger.Fatalln("Bad configuration: retry but no retry-duration in", stream.name, ".", filter.name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
|
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Bad configuration: Failed to parse retry time in", stream.name, ".", filter.name, ":", err)
|
logger.Fatalln("Bad configuration: Failed to parse retry time in", stream.name, ".", filter.name, ":", err)
|
||||||
}
|
}
|
||||||
filter.retryDuration = retryDuration
|
filter.retryDuration = retryDuration
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filter.Regex) == 0 {
|
if len(filter.Regex) == 0 {
|
||||||
log.Fatalln("FATAL Bad configuration: no regexes configured in", stream.name, ".", filter.name)
|
logger.Fatalln("Bad configuration: no regexes configured in", stream.name, ".", filter.name)
|
||||||
}
|
}
|
||||||
// Compute Regexes
|
// Compute Regexes
|
||||||
// Look for Patterns inside Regexes
|
// Look for Patterns inside Regexes
|
||||||
@ -86,7 +87,7 @@ func (c *Conf) setup() {
|
|||||||
} else if filter.pattern == pattern {
|
} else if filter.pattern == pattern {
|
||||||
// no op
|
// no op
|
||||||
} else {
|
} else {
|
||||||
log.Fatalf(
|
logger.Fatalf(
|
||||||
"Bad configuration: Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
|
"Bad configuration: Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
|
||||||
filter.pattern.name, patternName, streamName, filterName,
|
filter.pattern.name, patternName, streamName, filterName,
|
||||||
)
|
)
|
||||||
@ -101,7 +102,7 @@ func (c *Conf) setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(filter.Actions) == 0 {
|
if len(filter.Actions) == 0 {
|
||||||
log.Fatalln("FATAL Bad configuration: no actions configured in", stream.name, ".", filter.name)
|
logger.Fatalln("Bad configuration: no actions configured in", stream.name, ".", filter.name)
|
||||||
}
|
}
|
||||||
for actionName := range filter.Actions {
|
for actionName := range filter.Actions {
|
||||||
|
|
||||||
@ -110,17 +111,17 @@ func (c *Conf) setup() {
|
|||||||
action.name = actionName
|
action.name = actionName
|
||||||
|
|
||||||
if strings.Contains(action.name, ".") {
|
if strings.Contains(action.name, ".") {
|
||||||
log.Fatalln("FATAL Bad configuration: character '.' is not allowed in action names", action.name)
|
logger.Fatalln("Bad configuration: character '.' is not allowed in action names", action.name)
|
||||||
}
|
}
|
||||||
// Parse Duration
|
// Parse Duration
|
||||||
if action.After != "" {
|
if action.After != "" {
|
||||||
afterDuration, err := time.ParseDuration(action.After)
|
afterDuration, err := time.ParseDuration(action.After)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Bad configuration: Failed to parse after time in ", stream.name, ".", filter.name, ".", action.name, ":", err)
|
logger.Fatalln("Bad configuration: Failed to parse after time in ", stream.name, ".", filter.name, ".", action.name, ":", err)
|
||||||
}
|
}
|
||||||
action.afterDuration = afterDuration
|
action.afterDuration = afterDuration
|
||||||
} else if action.OnExit {
|
} else if action.OnExit {
|
||||||
log.Fatalln("FATAL Bad configuration: Cannot have `onexit: true` without an `after` directive in", stream.name, ".", filter.name, ".", action.name)
|
logger.Fatalln("Bad configuration: Cannot have `onexit: true` without an `after` directive in", stream.name, ".", filter.name, ".", action.name)
|
||||||
}
|
}
|
||||||
if filter.longuestActionDuration == nil || filter.longuestActionDuration.Milliseconds() < action.afterDuration.Milliseconds() {
|
if filter.longuestActionDuration == nil || filter.longuestActionDuration.Milliseconds() < action.afterDuration.Milliseconds() {
|
||||||
filter.longuestActionDuration = &action.afterDuration
|
filter.longuestActionDuration = &action.afterDuration
|
||||||
@ -134,7 +135,7 @@ func parseConf(filename string) *Conf {
|
|||||||
|
|
||||||
data, err := os.Open(filename)
|
data, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to read configuration file:", err)
|
logger.Fatalln("Failed to read configuration file:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var conf Conf
|
var conf Conf
|
||||||
@ -148,7 +149,7 @@ func parseConf(filename string) *Conf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("FATAL Failed to parse configuration file:", err)
|
logger.Fatalln("Failed to parse configuration file:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.setup()
|
conf.setup()
|
||||||
|
58
logger/log.go
Normal file
58
logger/log.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
type Level int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DEBUG = Level(1)
|
||||||
|
INFO = Level(2)
|
||||||
|
WARN = Level(3)
|
||||||
|
ERROR = Level(4)
|
||||||
|
FATAL = Level(5)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (l Level) String() string {
|
||||||
|
switch l {
|
||||||
|
case DEBUG:
|
||||||
|
return "DEBUG "
|
||||||
|
case INFO:
|
||||||
|
return "INFO "
|
||||||
|
case WARN:
|
||||||
|
return "WARN "
|
||||||
|
case ERROR:
|
||||||
|
return "ERROR "
|
||||||
|
case FATAL:
|
||||||
|
return "FATAL "
|
||||||
|
default:
|
||||||
|
return "????? "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var LogLevel Level = 2
|
||||||
|
|
||||||
|
func SetLogLevel(level Level) {
|
||||||
|
LogLevel = level
|
||||||
|
}
|
||||||
|
|
||||||
|
func Println(level Level, args ...any) {
|
||||||
|
if level >= LogLevel {
|
||||||
|
log.Println(level, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Printf(level Level, format string, args ...any) {
|
||||||
|
if level >= LogLevel {
|
||||||
|
log.Printf(level.String()+format, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatalln(args ...any) {
|
||||||
|
level := FATAL
|
||||||
|
log.Fatalln(level.String(), args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatalf(format string, args ...any) {
|
||||||
|
level := FATAL
|
||||||
|
log.Fatalf(level.String()+format, args)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user