mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Add a widget to display the log file in reverse order
This commit is contained in:
		
							parent
							
								
									582eba4c7f
								
							
						
					
					
						commit
						09a4139922
					
				
							
								
								
									
										110
									
								
								logging/log.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								logging/log.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,110 @@ | ||||
| package logging | ||||
| 
 | ||||
| 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 ", "logging", 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)) | ||||
| 
 | ||||
| 	widget.View.SetText(fmt.Sprintf("%s", widget.tailFile())) | ||||
| } | ||||
| 
 | ||||
| /* -------------------- Unexported Functions -------------------- */ | ||||
| 
 | ||||
| func logFileMissing() bool { | ||||
| 	return logFilePath() == "" | ||||
| } | ||||
| 
 | ||||
| func logFilePath() string { | ||||
| 	dir, err := wtf.Home() | ||||
| 	if err != nil { | ||||
| 		return "" | ||||
| 	} | ||||
| 
 | ||||
| 	return filepath.Join(dir, ".wtf", "log.txt") | ||||
| } | ||||
| 
 | ||||
| func (widget *Widget) tailFile() string { | ||||
| 	file, err := os.Open(widget.filePath) | ||||
| 	if err != nil { | ||||
| 		return "" | ||||
| 	} | ||||
| 	defer file.Close() | ||||
| 
 | ||||
| 	stat, err := file.Stat() | ||||
| 	if err != nil { | ||||
| 		return "" | ||||
| 	} | ||||
| 
 | ||||
| 	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 "" | ||||
| 	} | ||||
| 
 | ||||
| 	dataArr := strings.Split(string(buff), "\n") | ||||
| 
 | ||||
| 	// Reverse the array of lines | ||||
| 	// Offset by two to account for the blank line at the end | ||||
| 	last := len(dataArr) - 2 | ||||
| 	for i := 0; i < len(dataArr)/2; i++ { | ||||
| 		dataArr[i], dataArr[last-i] = dataArr[last-i], dataArr[i] | ||||
| 	} | ||||
| 
 | ||||
| 	return fmt.Sprintf("%s\n", strings.Join(dataArr, "\n")) | ||||
| } | ||||
							
								
								
									
										7
									
								
								wtf.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								wtf.go
									
									
									
									
									
								
							| @ -29,6 +29,7 @@ import ( | ||||
| 	"github.com/senorprogrammer/wtf/ipaddresses/ipinfo" | ||||
| 	"github.com/senorprogrammer/wtf/jenkins" | ||||
| 	"github.com/senorprogrammer/wtf/jira" | ||||
| 	"github.com/senorprogrammer/wtf/logging" | ||||
| 	"github.com/senorprogrammer/wtf/newrelic" | ||||
| 	"github.com/senorprogrammer/wtf/opsgenie" | ||||
| 	"github.com/senorprogrammer/wtf/power" | ||||
| @ -221,6 +222,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { | ||||
| 		Widgets = append(Widgets, jenkins.NewWidget()) | ||||
| 	case "jira": | ||||
| 		Widgets = append(Widgets, jira.NewWidget()) | ||||
| 	case "logging": | ||||
| 		Widgets = append(Widgets, logging.NewWidget()) | ||||
| 	case "newrelic": | ||||
| 		Widgets = append(Widgets, newrelic.NewWidget()) | ||||
| 	case "opsgenie": | ||||
| @ -285,10 +288,10 @@ func main() { | ||||
| 	go redrawApp(app) | ||||
| 	go watchForConfigChanges(app, flags.Config, display.Grid, pages) | ||||
| 
 | ||||
| 	logging.Log("Running!") | ||||
| 
 | ||||
| 	if err := app.SetRoot(pages, true).Run(); err != nil { | ||||
| 		fmt.Printf("Error: %v\n", err) | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| 
 | ||||
| 	wtf.Log("Running!") | ||||
| } | ||||
|  | ||||
							
								
								
									
										26
									
								
								wtf/log.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								wtf/log.go
									
									
									
									
									
								
							| @ -1,26 +0,0 @@ | ||||
| package wtf | ||||
| 
 | ||||
| import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| ) | ||||
| 
 | ||||
| //Log basic message logging, defaults to ~/.wtf/log.txt | ||||
| func Log(message string) { | ||||
| 
 | ||||
| 	dir, err := Home() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	logfile := filepath.Join(dir, ".wtf", "log.txt") | ||||
| 	f, err := os.OpenFile(logfile, 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(message) | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user