Skip to content

Commit

Permalink
Refactor API
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrash committed Apr 4, 2021
1 parent e7249f5 commit 1185204
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 171 deletions.
6 changes: 4 additions & 2 deletions ftl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env sh
set -x -e -u

ssh $1 mkdir -p /root/go/src/github.com/ftlops/ftl
ssh $1 rm -rf /root/go/src/github.com/ftlops/ftl/
ssh $1 mkdir -p /root/go/src/github.com/ftlops/ftl/{log,ops}
scp *.go $1:/root/go/src/github.com/ftlops/ftl/

scp log/*.go $1:/root/go/src/github.com/ftlops/ftl/log
scp ops/*.go $1:/root/go/src/github.com/ftlops/ftl/ops

date=$(date +%s)
dst=/var/log/ftl/$date/plans
Expand Down
35 changes: 33 additions & 2 deletions ftl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
package ftl

var DefaultOps = Ops{&LogLog{}}
import (
"fmt"

"github.com/ftlops/ftl/log"
"github.com/ftlops/ftl/ops"
)

func init() {
ops.Logger = &log.DefaultLogger
}

type State int

const (
StateUnchanged State = iota
StateChanged
)

func (s State) String() string {
switch s {
case StateUnchanged:
return "unchanged"
case StateChanged:
return "changed"
default:
return fmt.Sprintf("unknown (%d)", s)
}
}

type StepFunc func() State

func Step(name string, f StepFunc) {
DefaultOps.Step(name, f)
log.BeginStep(name)
state := f()
log.EndStep(state.String())
}
72 changes: 0 additions & 72 deletions log.go

This file was deleted.

92 changes: 92 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package log

import (
"fmt"
"log"
"os"
"runtime/debug"
"strings"
)

var DefaultLogger = Logger{}

func BeginStep(name string) { DefaultLogger.BeginStep(name) }
func EndStep(result string) { DefaultLogger.EndStep(result) }
func Fatal(v ...interface{}) { DefaultLogger.Fatal(v...) }
func Error(v ...interface{}) { DefaultLogger.Error(v...) }
func Info(v ...interface{}) { DefaultLogger.Info(v...) }
func Debug(v ...interface{}) { DefaultLogger.Debug(v...) }
func Trace(v ...interface{}) { DefaultLogger.Trace(v...) }

type Logger struct {
stack []string
lastFul string
}

func (l *Logger) BeginStep(name string) {
l.stack = append(l.stack, name)

l.printf("(((", []interface{}{})
}

func (l *Logger) EndStep(result string) {
l.printf(")))", []interface{}{"->", result})
l.stack = l.stack[:len(l.stack)-1]
}

func (l *Logger) Fatal(v ...interface{}) {
l.printf("FTL", v)
debug.PrintStack()
os.Exit(1)
}

func (l *Logger) Error(v ...interface{}) {
l.printf("ERR", v)
}

func (l *Logger) Info(v ...interface{}) {
l.printf("INF", v)
}

func (l *Logger) Debug(v ...interface{}) {
l.printf("DBG", v)
}

func (l *Logger) Trace(v ...interface{}) {
l.printf("TRC", v)
}

func (l *Logger) printf(lvl string, v []interface{}) {
full := l.fmtStack()
if l.lastFul != full {
log.Println(l.fmt(lvl, full, v)...)
l.lastFul = full
} else {
shortStack := "["
prefix := "... > "
basename := l.stack[len(l.stack)-1]
padLeft := len(full) - len(basename) - len(prefix) - 2
for i := 0; i < padLeft; i++ {
shortStack += " "
}
if padLeft > 0 {
shortStack += prefix
}
shortStack += basename + "]"
log.Println(l.fmt(lvl, shortStack, v)...)
}
}

func (l *Logger) fmt(lvl, stack string, v []interface{}) []interface{} {
var vs []interface{}
vs = append(vs, lvl)
vs = append(vs, stack)
for _, x := range v {
vs = append(vs, x)
}
return vs
}

func (l *Logger) fmtStack() string {
return fmt.Sprintf("[%s]", strings.Join(l.stack, " > "))
}
Loading

0 comments on commit 1185204

Please sign in to comment.