Skip to Content
go-dicom(3)
Packages

config

Library-wide settings: where the library logs, and how strictly values are validated when read and written, either for the whole program or for one operation through a context.Context.

import "github.com/amrshadid/go-dicom/config" · pkg.go.dev

When to use it

  • Send the library's log to your own slog.Logger, or silence it: config.SetLogger.
  • Give one operation a logger of its own, so two reads or two servers in one process log apart: config.WithLogger.
  • Make one operation strict, or lenient: config.WithReadingValidationMode, StrictReading, DisableValueValidation.

Per-read and per-server loggers are also options of their own: filereader.ReadOptions.Logger, network.SCPConfig.Logger, network.SCUConfig.Logger. The network layer has its own verbosity (network.SetDefaultLogLevel).

Key types and functions

NameWhat it is for
SetLogger, LoggerThe program-wide logger; by default warnings and errors to stderr.
SetDebugDebug level on or off, to a writer.
WithLogger, LoggerFromContextA logger carried by a context, for one operation.
ValidationModeIGNORE, WARN or RAISE.
WithReadingValidationMode, WithWritingValidationModeA validation mode for one operation.
StrictReading, StrictValidation, DisableValueValidationShorthands for the above.
Get, SettingsThe global settings, with getters and setters.
ResetBack to the defaults.

Example

// Route the library's log to a logger of one's own, and make one call strict.
package main

import (
	"context"
	"fmt"
	"log/slog"
	"os"

	"github.com/amrshadid/go-dicom/charset"
	"github.com/amrshadid/go-dicom/config"
)

func main() {
	// Everything the library logs, for the whole program.
	config.SetLogger(slog.New(slog.NewTextHandler(os.Stdout, nil)))

	declared := []string{"ISO_IR 999"} // not a defined term

	// The default reading mode is WARN: logged, and the default repertoire used.
	enc, err := charset.ConvertEncodingsWithContext(context.Background(), declared)
	fmt.Println("default:", enc, err)

	// RAISE for this call alone; the global setting is untouched.
	strict := config.WithReadingValidationMode(context.Background(), config.RAISE)
	enc, err = charset.ConvertEncodingsWithContext(strict, declared)
	fmt.Println("strict:", enc, err)

	// A logger for this call alone.
	quiet := config.WithLogger(context.Background(), slog.New(slog.DiscardHandler))
	enc, err = charset.ConvertEncodingsWithContext(quiet, declared)
	fmt.Println("quiet:", enc, err)
}

It prints the warning for the unknown term, then default: [ISO-8859-1] <nil>, strict: [] DicomUnicodeDecodeError (ISO_IR 999): Unknown encoding 'ISO_IR 999' and quiet: [ISO-8859-1] <nil> with nothing logged.

Notes

  • The settings are guarded by a lock. They are still globals: a test or a library that changes them changes them for every goroutine. Prefer the context forms.
  • The context's reading validation mode is consulted by charset. Element validation in dataelem reads the global mode, and a retired attribute is a warning in any mode.
  • Several Settings fields (UseDSNumpy, AllowDSFloat, ConvertWrongLengthToUN among them) carry names from pydicom's configuration and are not read by the library.
  • What can be configured on the network side is in CONFORMANCE.md §5.

Note

RegisterPixelDataHandler and the PixelDataHandler interface here are not how decoders are replaced. To substitute a decoder, use compress.GetExternalRegistry().RegisterExternalDecoder; see compress.

See also

charset · filereader · network · errors · Configuration and errors

On this page