Skip to content

Commit

Permalink
Add initial build tag based debug logging (#209)
Browse files Browse the repository at this point in the history
* Add initial build tag based debug logging

* rm unrelated change
  • Loading branch information
suyashkumar authored Jul 16, 2021
1 parent 8033032 commit a5e9b1a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ build:
build-fast:
go build -o build/${BINARY} ./cmd/dicomutil

.PHONY: build-debug
build-debug:
go build -tags debug -o build/${BINARY} ./cmd/dicomutil

.PHONY: test
test:
go test ./... -v
Expand Down
3 changes: 3 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os"

"github.com/suyashkumar/dicom/pkg/charset"
"github.com/suyashkumar/dicom/pkg/debug"
"github.com/suyashkumar/dicom/pkg/dicomio"
"github.com/suyashkumar/dicom/pkg/frame"
"github.com/suyashkumar/dicom/pkg/tag"
Expand Down Expand Up @@ -119,10 +120,12 @@ func NewParser(in io.Reader, bytesToRead int64, frameChannel chan *frame.Frame)
frameChannel: frameChannel,
}

debug.Log("NewParser: readHeader")
elems, err := p.readHeader()
if err != nil {
return nil, err
}
debug.Log("NewParser: readHeader complete")

p.dataset = Dataset{Elements: elems}
// TODO(suyashkumar): avoid storing the metadata pointers twice (though not that expensive)
Expand Down
15 changes: 15 additions & 0 deletions pkg/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build debug

package debug

import "log"

// Log only logs when the debug build flag is present.
func Log(data string) {
log.Println(data)
}

// Logf only logs with a formatted string when the debug build flag is present.
func Logf(format string, args ...interface{}) {
log.Printf(format, args...)
}
9 changes: 9 additions & 0 deletions pkg/debug/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !debug

package debug

// Log only logs when the debug build flag is present.
func Log(data string) {}

// Logf only logs with a formatted string when the debug build flag is present.
func Logf(format string, args ...interface{}) {}
10 changes: 7 additions & 3 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"unicode"

"github.com/suyashkumar/dicom/pkg/debug"
"github.com/suyashkumar/dicom/pkg/vrraw"

"github.com/suyashkumar/dicom/pkg/dicomio"
Expand Down Expand Up @@ -218,6 +219,8 @@ func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Fr

pixelsPerFrame := MustGetInts(rows.Value)[0] * MustGetInts(cols.Value)[0]

debug.Logf("readNativeFrames:\nRows: %d\nCols:%d\nFrames::%d\nBitsAlloc:%d\nSamplesPerPixel:%d", rows, cols, nFrames, bitsAllocated, samplesPerPixel)

// Parse the pixels:
image.Frames = make([]frame.Frame, nFrames)
bo := d.ByteOrder()
Expand Down Expand Up @@ -368,7 +371,7 @@ func readBytes(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error)
if vl%2 != 0 {
return nil, ErrorOWRequiresEvenVL
}

buf := bytes.NewBuffer(make([]byte, 0, vl))
numWords := int(vl / 2)
for i := 0; i < numWords; i++ {
Expand Down Expand Up @@ -510,6 +513,7 @@ func readElement(r dicomio.Reader, d *Dataset, fc chan<- *frame.Frame) (*Element
if err != nil {
return nil, err
}
debug.Logf("readElement: tag: %s", t.String())

readImplicit := r.IsImplicit()
if *t == tag.Item {
Expand All @@ -521,13 +525,13 @@ func readElement(r dicomio.Reader, d *Dataset, fc chan<- *frame.Frame) (*Element
if err != nil {
return nil, err
}
debug.Logf("readElement: vr: %s", vr)

vl, err := readVL(r, readImplicit, *t, vr)
if err != nil {
return nil, err
}

// log.Println("readElement: vr, vl", vr, vl)
debug.Logf("readElement: vl: %d", vl)

val, err := readValue(r, *t, vr, vl, readImplicit, d, fc)
if err != nil {
Expand Down

0 comments on commit a5e9b1a

Please sign in to comment.