diff --git a/Makefile b/Makefile index 4016346..2accf61 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ all: # simple sortcut to push all git changes push: - # git pull + git pull git add --all -git commit -a -s git push diff --git a/chomp.go b/chomp.go new file mode 100644 index 0000000..acedd47 --- /dev/null +++ b/chomp.go @@ -0,0 +1,89 @@ +package log + +/* + perl 'chomp' + + send it anything, always get back a string +*/ + +import "fmt" +import "reflect" +import "strings" +import "bytes" + +// import "github.com/davecgh/go-spew/spew" + +func chompBytesBuffer(buf *bytes.Buffer) string { + var bytesSplice []byte + bytesSplice = buf.Bytes() + + return Chomp(string(bytesSplice)) +} + +// +// TODO: obviously this is stupidly wrong +// TODO: fix this to trim fucking everything +// really world? 8 fucking years of this language +// and I'm fucking writing this? jesus. how the +// hell is everyone else doing this? Why isn't +// this already in the strings package? +// +func perlChomp(s string) string { + // lots of stuff in go moves around the whole block of whatever it is so lots of things are padded with NULL values + s = strings.Trim(s, "\x00") // removes NULL (needed!) + + // TODO: christ. make some fucking regex that takes out every NULL, \t, ' ", \n, and \r + s = strings.Trim(s, "\n") + s = strings.Trim(s, "\n") + s = strings.TrimSuffix(s, "\r") + s = strings.TrimSuffix(s, "\n") + + s = strings.TrimSpace(s) // this is like 'chomp' in perl + s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl + return s +} + +// TODO: fix this to chomp \n \r NULL \t and ' ' +func Chomp(a interface{}) string { + // switch reflect.TypeOf(a) { + switch t := a.(type) { + case string: + var s string + s = a.(string) + return perlChomp(s) + case []uint8: + // log.Printf("shell.Chomp() FOUND []uint8") + var tmp []uint8 + tmp = a.([]uint8) + + s := string(tmp) + return perlChomp(s) + case uint64: + // log.Printf("shell.Chomp() FOUND []uint64") + s := fmt.Sprintf("%d", a.(uint64)) + return perlChomp(s) + case int64: + // log.Printf("shell.Chomp() FOUND []int64") + s := fmt.Sprintf("%d", a.(int64)) + return perlChomp(s) + case *bytes.Buffer: + // log.Printf("shell.Chomp() FOUND *bytes.Buffer") + var tmp *bytes.Buffer + tmp = a.(*bytes.Buffer) + if (tmp == nil) { + return "" + } + + var bytesSplice []byte + bytesSplice = tmp.Bytes() + return Chomp(string(bytesSplice)) + default: + // tmp := fmt.Sprint("shell.Chomp() NO HANDLER FOR TYPE: %T", a) + // handleError(fmt.Errorf(tmp), -1) + Println("log.Chomp() NEED TO MAKE CONVERTER FOR type =", reflect.TypeOf(t)) + } + Println("log.Chomp() THIS SHOULD NEVER HAPPEN") + // tmp := "shell.Chomp() THIS SHOULD NEVER HAPPEN" + // handleError(fmt.Errorf(tmp), -1) + return "" +} diff --git a/linux.go b/linux.go new file mode 100644 index 0000000..d923a43 --- /dev/null +++ b/linux.go @@ -0,0 +1,40 @@ +// +build linux,go1.7 + +// put stuff in here that you only want compiled under linux + +package log + +import origlog "log" +import "os" +import "os/signal" +import "syscall" + +// import "runtime" +// import "time" +// import "reflect" + +// import "git.wit.com/wit/shell" +// import "github.com/davecgh/go-spew/spew" + +import "github.com/sirupsen/logrus" +import "github.com/wercker/journalhook" + +var sigChan chan os.Signal + +func init() { + origlog.Println("LINUX LOG() INIT()") + systemdlog = logrus.New() + journalhook.Enable() + systemdlog.Println("LINUX LOG() INIT()") + systemdlog.Println("LINUX LOG() INIT()") +} + +func handleSignal(err interface{}, ret int) { + origlog.Println("handleSignal() only should be compiled on linux") + sigChan = make(chan os.Signal, 3) + signal.Notify(sigChan, syscall.SIGUSR1) +} + +func UseJournalctl() { + journalhook.Enable() +} diff --git a/log.go b/log.go index 850ae06..c8e2f6b 100644 --- a/log.go +++ b/log.go @@ -1,18 +1,32 @@ package log /* - send it anything, always get back a string + override the log */ import "fmt" +import "strings" import origlog "log" import "github.com/sirupsen/logrus" -import "git.wit.com/wit/shell" + +var newlog = logrus.New() +var systemdlog *logrus.Logger var debug bool = false var warn bool = false var err bool = false +type Log struct { + Debug bool + Warn bool + Err bool +} + +func New() *Log { + var tmp Log + return &tmp +} + func SetDebug(a bool) { debug = a } @@ -25,32 +39,43 @@ func SetError(a bool) { err = a } +func (a *Log) Printer(b string) { + origlog.Println(b) +} + func Print(a ...interface{}) { origlog.Println(a) } func Println(format string, a ...interface{}) { - if (debug) { - logrus.Println(a) - } +// if (debug) { +// newlog.Println(a) +// } var tmp string if (a == nil) { tmp = fmt.Sprintf(format) } else { tmp = fmt.Sprintf(format, a) } - origlog.Println(shell.Chomp(tmp)) + + // log each line + lines := strings.Split(Chomp(tmp), "\n") + for _, line := range lines { + origlog.Println(line) + } } -func Debug(a ...interface{}) { +func Debug(format string, a ...interface{}) { if (debug) { - origlog.Println(a) + newlog.Debug(format, a) + systemdlog.Println("SYSTEMLOG:", format, a) } } func Debugln(a ...interface{}) { if (debug) { - origlog.Println(a) + newlog.Debug(a) + systemdlog.Println("SYSTEMLOG:", a) } }