mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
This change is largely experimental and it's entirely possible it could wipe out your existing configuration. Be warned. Old config path was: ~/.wtf/ New config path is: ~/.config/wtf/ If an existing config directory already exists, this change attempts to copy it to the new location. Note that if your config file contains paths to files in the old config directory, they won't work. You'll need to change them by hand.
131 lines
2.3 KiB
Go
131 lines
2.3 KiB
Go
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
//"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/senorprogrammer/wtf/wtf"
|
|
)
|
|
|
|
const maxBufferSize int64 = 1024
|
|
|
|
type Widget struct {
|
|
wtf.TextWidget
|
|
|
|
filePath string
|
|
}
|
|
|
|
func NewWidget() *Widget {
|
|
widget := Widget{
|
|
TextWidget: wtf.NewTextWidget(" Logs ", "logger", true),
|
|
|
|
filePath: logFilePath(),
|
|
}
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func Log(msg string) {
|
|
if logFileMissing() {
|
|
return
|
|
}
|
|
|
|
f, err := os.OpenFile(logFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
log.Fatalf("error opening file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
log.Println(msg)
|
|
}
|
|
|
|
func (widget *Widget) Refresh() {
|
|
if logFileMissing() {
|
|
return
|
|
}
|
|
|
|
widget.UpdateRefreshedAt()
|
|
widget.View.SetTitle(fmt.Sprintf("%s", widget.Name))
|
|
|
|
logLines := widget.tailFile()
|
|
widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(logLines)))
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
func (widget *Widget) contentFrom(logLines []string) string {
|
|
str := ""
|
|
|
|
for _, line := range logLines {
|
|
chunks := strings.Split(line, " ")
|
|
|
|
if len(chunks) >= 4 {
|
|
str = str + fmt.Sprintf(
|
|
"[green]%s[white] [yellow]%s[white] %s\n",
|
|
chunks[0],
|
|
chunks[1],
|
|
strings.Join(chunks[3:], " "),
|
|
)
|
|
}
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func logFileMissing() bool {
|
|
return logFilePath() == ""
|
|
}
|
|
|
|
func logFilePath() string {
|
|
dir, err := wtf.Home()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return filepath.Join(dir, ".config", "wtf", "log.txt")
|
|
}
|
|
|
|
func (widget *Widget) tailFile() []string {
|
|
file, err := os.Open(widget.filePath)
|
|
if err != nil {
|
|
return []string{}
|
|
}
|
|
defer file.Close()
|
|
|
|
stat, err := file.Stat()
|
|
if err != nil {
|
|
return []string{}
|
|
}
|
|
|
|
bufferSize := maxBufferSize
|
|
if maxBufferSize > stat.Size() {
|
|
bufferSize = stat.Size()
|
|
}
|
|
|
|
startPos := stat.Size() - bufferSize
|
|
|
|
buff := make([]byte, bufferSize)
|
|
_, err = file.ReadAt(buff, startPos)
|
|
if err != nil {
|
|
return []string{}
|
|
}
|
|
|
|
logLines := strings.Split(string(buff), "\n")
|
|
|
|
// Reverse the array of lines
|
|
// Offset by two to account for the blank line at the end
|
|
last := len(logLines) - 2
|
|
for i := 0; i < len(logLines)/2; i++ {
|
|
logLines[i], logLines[last-i] = logLines[last-i], logLines[i]
|
|
}
|
|
|
|
return logLines
|
|
}
|