Skip to content

Commit

Permalink
split logging into info and debug level
Browse files Browse the repository at this point in the history
fix file extension validaiton to work for uppercase
  • Loading branch information
Ryex committed Jul 16, 2020
1 parent bc70c3b commit 3c8a47c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
12 changes: 10 additions & 2 deletions cmd/dungeondraft-pack/dungeondraft-pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ 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")

flag.Parse()

debug := *debugPtr
info := *infoPtr
overwrite := *overwritePtr

var inDir, outDir string
Expand Down Expand Up @@ -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{
Expand Down
14 changes: 11 additions & 3 deletions cmd/dungeondraft-unpack/dungeondraft-unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -34,6 +37,7 @@ func main() {
flag.Parse()

debug := *debugPtr
info := *infoPtr
overwrite := *overwritePtr
ripTex := *ripPtr

Expand Down Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions internal/structures/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions pkg/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {

Expand All @@ -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.")
}
}

Expand All @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/unpack/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (u *Unpacker) ExtractPackage(r io.ReadSeeker, outDir string) (err error) {
}

err = u.ExtractFilelist(r, fileList, outDir)

return
}

Expand Down Expand Up @@ -175,6 +176,8 @@ func (u *Unpacker) ExtractFilelist(r io.ReadSeeker, fileList []structures.FileIn

}

u.log.Info("unpacking complete")

return
}

Expand All @@ -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
}
Expand All @@ -212,15 +215,15 @@ 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
}
} 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 3c8a47c

Please sign in to comment.