From a5e9b1a5e0498637efcb448360bac998e08bf0fb Mon Sep 17 00:00:00 2001 From: Suyash Kumar Date: Thu, 15 Jul 2021 20:48:42 -0400 Subject: [PATCH] Add initial build tag based debug logging (#209) * Add initial build tag based debug logging * rm unrelated change --- Makefile | 4 ++++ parse.go | 3 +++ pkg/debug/debug.go | 15 +++++++++++++++ pkg/debug/default.go | 9 +++++++++ read.go | 10 +++++++--- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 pkg/debug/debug.go create mode 100644 pkg/debug/default.go diff --git a/Makefile b/Makefile index 389c91a1..277a6904 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/parse.go b/parse.go index 8684418e..9cceb3d1 100644 --- a/parse.go +++ b/parse.go @@ -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" @@ -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) diff --git a/pkg/debug/debug.go b/pkg/debug/debug.go new file mode 100644 index 00000000..e9c1297f --- /dev/null +++ b/pkg/debug/debug.go @@ -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...) +} diff --git a/pkg/debug/default.go b/pkg/debug/default.go new file mode 100644 index 00000000..5b13f7bb --- /dev/null +++ b/pkg/debug/default.go @@ -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{}) {} diff --git a/read.go b/read.go index a5d5b2a0..f9953355 100644 --- a/read.go +++ b/read.go @@ -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" @@ -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() @@ -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++ { @@ -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 { @@ -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 {