Skip to content

Commit

Permalink
Fix logging, add credit to acarl005/stripansi
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed May 5, 2024
1 parent 10c6d96 commit f188825
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 3 additions & 1 deletion quickgo/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var levelMap = map[LogLevel]string{
}

const (
OutputAll LogLevel = -1

// DebugLevel is the lowest log level.
DebugLevel LogLevel = iota
Expand All @@ -35,6 +34,9 @@ const (

// ErrorLevel is used for errors.
ErrorLevel

// OutputAll is used to output all log levels in the SetOutput function.
OutputAll LogLevel = -1
)

type LogWriter struct {
Expand Down
7 changes: 2 additions & 5 deletions quickgo/quickgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ type ansiStrippedWriter struct {
io.Writer
}

// https://github.com/acarl005/stripansi
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

var re = regexp.MustCompile(ansi)

func StripANSI(str string) string {
return re.ReplaceAllString(str, "")
}

func (w *ansiStrippedWriter) Write(p []byte) (n int, err error) {
return w.Writer.Write([]byte(StripANSI(string(p))))
return w.Writer.Write([]byte(re.ReplaceAllString(string(p), "")))
}

// Output the log file to the given writer.
Expand Down
12 changes: 11 additions & 1 deletion quickgo/quickgo_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ func (a *App) HttpHandler() http.Handler {
mux.Handle("/static/", &LogHandler{
Handler: http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))),
Where: "static files",
Level: logger.DebugLevel,
})
mux.Handle("/favicon.ico", &LogHandler{
Handler: http.HandlerFunc(a.serveFavicon),
Level: logger.DebugLevel,
Where: "favicon",
})
return a.middleware(mux)
Expand Down Expand Up @@ -131,14 +133,18 @@ func (a *App) serveProjects(w http.ResponseWriter, r *http.Request) {
return
}

logger.Debugf("Reading project '%s'", pathParts[0])
proj, closeFiles, err := a.ReadProjectConfig(pathParts[0])
if err != nil {
logger.Errorf("Failed to read project '%s': %v", pathParts[0], err)
http.Error(w, "Invalid project", http.StatusBadRequest)
return
}

defer closeFiles()
defer func() {
logger.Debugf("Closing project '%s'", proj.Name)
closeFiles()
}()

parent, fileLike, err = proj.Root.FindWithParent(pathParts[1:])
if err != nil {
Expand All @@ -164,6 +170,8 @@ func (a *App) serveProjects(w http.ResponseWriter, r *http.Request) {
context.Dir = dir
context.ObjectList = FileObjects

logger.Debugf("Serving directory '%s' in project '%s'", dir.Name, proj.Name)

if err = a.executeServeTemplate(w, "dir.tmpl", &context); err != nil {
logger.Errorf("Failed to render directory object in project '%s': %v", proj.Name, err)
http.Error(w, "Failed to render directory in project", http.StatusInternalServerError)
Expand Down Expand Up @@ -195,6 +203,8 @@ func (a *App) serveProjects(w http.ResponseWriter, r *http.Request) {
context.File = file
context.Content = content

logger.Debugf("Serving file '%s' in project '%s'", file.Name, proj.Name)

if err = a.executeServeTemplate(w, "file.tmpl", &context); err != nil {
logger.Errorf("Failed to render file object in project '%s': %v", proj.Name, err)
http.Error(w, "Failed to render file in project", http.StatusInternalServerError)
Expand Down

0 comments on commit f188825

Please sign in to comment.