Skip to content

Commit

Permalink
A more proper logger
Browse files Browse the repository at this point in the history
  • Loading branch information
xeome committed Jul 24, 2023
1 parent dd9fe75 commit fc90841
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e
github.com/gin-gonic/gin v1.9.1
github.com/kardianos/service v1.2.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.16.0
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand Down Expand Up @@ -363,6 +365,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
Expand Down
24 changes: 12 additions & 12 deletions hyperv/vhd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package hyperv
import (
"encoding/gob"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
Expand All @@ -14,6 +12,8 @@ import (
"github.com/gin-gonic/gin"
)

var log = utilities.Log

type VHDPath []VHDPathElement

type VHDPathElement struct {
Expand Down Expand Up @@ -108,32 +108,32 @@ func Refresh() {
pathListLock.Lock()
err = Load(executablePath+"\\vhdpath.gob", &VHDPathList)
if err != nil {
fmt.Println("Unable to load VHDPathList from file. Loading from PowerShell.")
log.Info("Unable to load VHDPathList from file. Loading from PowerShell.")
} else {
fmt.Println("Finished loading VHDPathList from disk.")
log.Info("Finished loading VHDPathList from disk.")
}
pathListLock.Unlock()

fmt.Println("Reinitializing VHDPathList.")
log.Info("Reinitializing VHDPathList.")
output, err := utilities.CommandLine(`Get-VM | Get-VMHardDiskDrive | Select-Object -Property Path, VMId | ConvertTo-Json`)
if err != nil {
fmt.Println(err.Error())
log.Error(err.Error())
return
}

fmt.Println("Waiting for VHDPathList lock.")
log.Info("Waiting for VHDPathList lock.")
pathListLock.Lock()
VHDPathList, err = UnmarshalVHDPath(output)
pathListLock.Unlock()
if err != nil {
fmt.Println(err.Error())
log.Error(err.Error())
return
}
fmt.Println("Finished reinitializing VHDPathList.")
log.Info("Finished reinitializing VHDPathList.")

err = Save(executablePath+"\\vhdpath.gob", VHDPathList)
if err != nil {
fmt.Println(err.Error())
log.Error(err.Error())
return
}
}
Expand All @@ -148,9 +148,9 @@ func Init() {
pathListLock.Lock()
err = Load(executablePath+"\\vhdpath.gob", &VHDPathList)
if err != nil {
fmt.Println("Unable to load VHDPathList from file. Loading from PowerShell.")
log.Info("Unable to load VHDPathList from file. Loading from PowerShell.")
} else {
fmt.Println("Finished loading VHDPathList from disk.")
log.Info("Finished loading VHDPathList from disk.")
}
pathListLock.Unlock()
}
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,22 @@ func main() {
flag.Parse()

utilities.Init()
utilities.SetupLogger()
hyperv.Init()

go func() {
for {
time.Sleep(660 * time.Second)
hyperv.Refresh()
logger.Info("Hyper-V module reinitialized.")
time.Sleep(660 * time.Second)
}
}()

go func() {
for {
time.Sleep(2700 * time.Second)
utilities.RefreshShellQueue()
logger.Info("Shell queue reinitialized.")
time.Sleep(2700 * time.Second)
}
}()

Expand Down
4 changes: 2 additions & 2 deletions utilities/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func addSession() {
taskQueue <- struct{}{}
return
}
fmt.Println("No task queue or task queue is full")
log.Warn("No task queue or task queue is full")
}
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func CommandLine(command string) ([]byte, error) {

for {
if shellQueue.Len() == 0 {
fmt.Println("No session available, waiting in the queue...")
log.Warn("No session available, waiting in the queue...")
break
}

Expand Down
1 change: 0 additions & 1 deletion utilities/configparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utilities

import (
"flag"
"log"
"os"
"path/filepath"

Expand Down
31 changes: 31 additions & 0 deletions utilities/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utilities

import (
"runtime"
"strconv"
"strings"

walog "github.com/sirupsen/logrus"
)

var Log = walog.New()

var log = Log

func SetupLogger() {

log.SetFormatter(&walog.TextFormatter{
ForceColors: true, // Enable colors in the console output
FullTimestamp: true, // Show full timestamp with date and time
TimestampFormat: "2006-01-02 15:04:05",
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
// Get the filename and the function name from the file path
slash := strings.LastIndex(f.File, "/")
filename := f.File[slash+1:]
return "", "[" + filename + ":" + strconv.Itoa(f.Line) + "]"
},
})

log.SetReportCaller(true)
log.SetLevel(walog.TraceLevel)
}

0 comments on commit fc90841

Please sign in to comment.