From 3c8a47cface47ce13d0f04dbd59c0e3d9294423f Mon Sep 17 00:00:00 2001 From: Rachel Powers Date: Wed, 15 Jul 2020 18:26:31 -0600 Subject: [PATCH] split logging into info and debug level fix file extension validaiton to work for uppercase --- cmd/dungeondraft-pack/dungeondraft-pack.go | 12 ++++++++++-- cmd/dungeondraft-unpack/dungeondraft-unpack.go | 14 +++++++++++--- internal/structures/structures.go | 10 +++++----- pkg/pack/pack.go | 14 +++++++++----- pkg/unpack/unpack.go | 13 ++++++++----- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/cmd/dungeondraft-pack/dungeondraft-pack.go b/cmd/dungeondraft-pack/dungeondraft-pack.go index effb67b..039ca2a 100644 --- a/cmd/dungeondraft-pack/dungeondraft-pack.go +++ b/cmd/dungeondraft-pack/dungeondraft-pack.go @@ -23,8 +23,11 @@ func main() { flag.Usage = usage // args go here - debugPtr := flag.Bool("debug", false, "output debug info level log messages?") - flag.BoolVar(debugPtr, "v", false, "alias of -debug") + debugPtr := flag.Bool("debug", false, "output Debug level log messages?") + flag.BoolVar(debugPtr, "vv", false, "alias of -debug") + + infoPtr := flag.Bool("info", false, "output Info level log messages?") + flag.BoolVar(infoPtr, "v", false, "alias of -info") overwritePtr := flag.Bool("overwrite", false, "overwrite output files at dest") flag.BoolVar(overwritePtr, "O", false, "alias of -overwrite") @@ -32,6 +35,7 @@ func main() { flag.Parse() debug := *debugPtr + info := *infoPtr overwrite := *overwritePtr var inDir, outDir string @@ -73,7 +77,11 @@ func main() { // Only log the warning severity or above. log.SetLevel(log.WarnLevel) if debug { + log.SetLevel(log.DebugLevel) + } else if info { log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(log.WarnLevel) } l := log.WithFields(log.Fields{ diff --git a/cmd/dungeondraft-unpack/dungeondraft-unpack.go b/cmd/dungeondraft-unpack/dungeondraft-unpack.go index c18642f..3f24e25 100644 --- a/cmd/dungeondraft-unpack/dungeondraft-unpack.go +++ b/cmd/dungeondraft-unpack/dungeondraft-unpack.go @@ -22,10 +22,13 @@ func main() { flag.Usage = usage // args go here - debugPtr := flag.Bool("debug", false, "output debug info level log messages?") - flag.BoolVar(debugPtr, "v", false, "alias of -debug") + debugPtr := flag.Bool("debug", false, "output Debug level log messages?") + flag.BoolVar(debugPtr, "vv", false, "alias of -debug") - overwritePtr := flag.Bool("overwrite", false, "overwrite outputfiles at dest") + infoPtr := flag.Bool("info", false, "output Info level log messages?") + flag.BoolVar(infoPtr, "v", false, "alias of -info") + + overwritePtr := flag.Bool("overwrite", false, "overwrite output files at dest") flag.BoolVar(overwritePtr, "O", false, "alias of -overwrite") ripPtr := flag.Bool("riptex", false, "convert .tex files int he package to normal image formats (probably never needed)") @@ -34,6 +37,7 @@ func main() { flag.Parse() debug := *debugPtr + info := *infoPtr overwrite := *overwritePtr ripTex := *ripPtr @@ -61,7 +65,11 @@ func main() { // Only log the warning severity or above. log.SetLevel(log.WarnLevel) if debug { + log.SetLevel(log.DebugLevel) + } else if info { log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(log.WarnLevel) } outDirPath, err := filepath.Abs(flag.Arg(1)) diff --git a/internal/structures/structures.go b/internal/structures/structures.go index 03c4f79..ba6c013 100644 --- a/internal/structures/structures.go +++ b/internal/structures/structures.go @@ -141,10 +141,10 @@ func (fil *FileInfoList) UpdateOffsets(offset int64) { // Write out headers and file contents to io func (fil *FileInfoList) Write(log logrus.FieldLogger, out io.Writer, offset int64) (err error) { - log.Info("updating offsets...") + log.Debug("updating offsets...") fil.UpdateOffsets(fil.Size + offset) - log.Info("writing files...") + log.Debug("writing files...") err = fil.WriteHeaders(log, out) if err != nil { return @@ -158,7 +158,7 @@ func (fil *FileInfoList) Write(log logrus.FieldLogger, out io.Writer, offset int // WriteHeaders write out the headers to io func (fil *FileInfoList) WriteHeaders(log logrus.FieldLogger, out io.Writer) (err error) { - log.Info("writing file headers") + log.Debug("writing file headers") for _, pair := range fil.FileList { // write path length @@ -188,7 +188,7 @@ func (fil *FileInfoList) WriteHeaders(log logrus.FieldLogger, out io.Writer) (er // this function does NOT handle padding inbetween filedata. this may be a problem func (fil *FileInfoList) WriteFiles(log logrus.FieldLogger, out io.Writer) (err error) { - log.Info("writing file data") + log.Debug("writing file data") for _, pair := range fil.FileList { err = fil.writeFile(log.WithField("file", pair.Info.Path), out, pair.Info) if err != nil { @@ -201,7 +201,7 @@ func (fil *FileInfoList) WriteFiles(log logrus.FieldLogger, out io.Writer) (err func (fil *FileInfoList) writeFile(log logrus.FieldLogger, out io.Writer, info FileInfo) (err error) { - log.Info("writing") + log.Debug("writing") data, err := ioutil.ReadFile(info.Path) if err != nil { diff --git a/pkg/pack/pack.go b/pkg/pack/pack.go index 63be467..a142c20 100644 --- a/pkg/pack/pack.go +++ b/pkg/pack/pack.go @@ -140,7 +140,7 @@ func (p *Packer) PackPackage(outDir string) (err error) { } } - l.Info("writing package") + l.Debug("writing package") var out *os.File out, err = os.Create(outPackagePath) if err != nil { @@ -158,12 +158,14 @@ func (p *Packer) PackPackage(outDir string) (err error) { return } + l.Info("packing complete") + return } // BuildFileList builds a list of files at the target directory for inclusion in a .dungeondraft_pack file func (p *Packer) BuildFileList() (err error) { - p.log.Info("beginning directory traversal to collect file list") + p.log.Debug("beginning directory traversal to collect file list") err = filepath.Walk(p.path, p.fileListWalkFunc) if err != nil { @@ -230,9 +232,9 @@ func (p *Packer) fileListWalkFunc(path string, info os.FileInfo, err error) erro } if info.IsDir() { - l.Info("is directory, decending into...") + l.Debug("is directory, decending into...") } else { - ext := filepath.Ext(path) + ext := strings.ToLower(filepath.Ext(path)) if utils.StringInSlice(ext, p.ValidExts) { if info.Mode().IsRegular() { @@ -249,6 +251,8 @@ func (p *Packer) fileListWalkFunc(path string, info os.FileInfo, err error) erro l.Info("including") p.FileList = append(p.FileList, fInfo) } + } else { + l.WithField("ext", ext).WithField("validExts", p.ValidExts).Debug("Invalid file ext, not including.") } } @@ -262,7 +266,7 @@ func (p *Packer) write(l logrus.FieldLogger, out io.WriteSeeker) (err error) { fileInfoList := structures.NewFileInfoList(p.FileList) - l.Info("writing package headers...") + l.Debug("writing package headers...") // write file header err = headers.Write(out) if !utils.CheckErrorWrite(l, err) { diff --git a/pkg/unpack/unpack.go b/pkg/unpack/unpack.go index 9b8f99b..c4e3a0e 100644 --- a/pkg/unpack/unpack.go +++ b/pkg/unpack/unpack.go @@ -43,6 +43,7 @@ func (u *Unpacker) ExtractPackage(r io.ReadSeeker, outDir string) (err error) { } err = u.ExtractFilelist(r, fileList, outDir) + return } @@ -175,6 +176,8 @@ func (u *Unpacker) ExtractFilelist(r io.ReadSeeker, fileList []structures.FileIn } + u.log.Info("unpacking complete") + return } @@ -191,7 +194,7 @@ func (u *Unpacker) ReadPackageFilelist(r io.ReadSeeker) (fileList []structures.F fileList, err = u.getFileList(r) - u.log.WithField("fileList", fileList).Info("file list") + u.log.WithField("fileList", fileList).Debug("file list") return } @@ -212,7 +215,7 @@ func (u *Unpacker) isValidPackage(r io.ReadSeeker) (bool, error) { } if bytes.Equal(magicBuf, magic) { - u.log.Infof("looks like a pck archive") + u.log.Debugf("looks like a pck archive") _, err = r.Seek(0, io.SeekStart) // back to start if !utils.CheckErrorSeek(u.log, err) { return false, err @@ -220,7 +223,7 @@ func (u *Unpacker) isValidPackage(r io.ReadSeeker) (bool, error) { } else { u.log. WithField("magic", magicBuf). - WithField("expectedMagic", magic).Info("Failed to read GDPC pck Magic") + WithField("expectedMagic", magic).Debug("Failed to read GDPC pck Magic") _, err = r.Seek(-4, io.SeekEnd) // 4 bytes from end if !utils.CheckErrorSeek(u.log, err) { @@ -233,7 +236,7 @@ func (u *Unpacker) isValidPackage(r io.ReadSeeker) (bool, error) { } if !bytes.Equal(magicBuf, magic) { - u.log.Info("looks like a self-contained exe", u.name) + u.log.Debug("looks like a self-contained exe", u.name) _, err = r.Seek(-12, io.SeekEnd) // 12 bytes from end if !utils.CheckErrorSeek(u.log, err) { @@ -277,7 +280,7 @@ func (u *Unpacker) getFileList(r io.ReadSeeker) (fileList []structures.FileInfo, return } - u.log.WithField("headers", headers).Info("info") + u.log.WithField("headers", headers).Debug("info") fileCount := headers.FileCount