diagnosticism

package module
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: BSD-3-Clause Imports: 15 Imported by: 1

README

Diagnosticism.Go

License GitHub release Last Commit Go Go Report Card Go Reference

Basic diagnostic facilities, for Go

Table of Contents

Introduction

Diagnosticism provides low-level diagnostics facilities to support library programming. The first Diagnosticism library was a C library with a C++ wrapper. There have been several implementations in other languages. Diagnosticism.Go is the Go version.

Installation

Install:

go get "github.com/synesissoftware/Diagnosticism.Go"

Use:

import diagnosticism "github.com/synesissoftware/Diagnosticism.Go"

Components

Diagnosticism.Go provides components in the following categories:

  • Contingent Reporting
  • Diagnostic Logging
  • Tracing

NOTE: for the moment, the Diagnostic Logging facilities emit to the standard error stream, via the Contingent Reporting API. In the near future this will be changed to work with more sophisticated logging libraries, including the standard logging facilities and the (as yet to be released) Pantheios.Go.

Constants

No public constants are defined at this time.

Functions

The following functions are defined:

Contingent Reporting
// Issues the given message and then end the process.
//
// If [IsMirroringToLog] is `true`, then the message will be emitted to the
// log before terminating.
func Abort(message string)

// Issues a formatted message and then end the process.
//
// If [IsMirroringToLog] is `true`, then the message will be emitted to the
// log before terminating.
func Abortf(format string, args ...any)

// Issues the given message to the standard error stream.
//
// If [IsMirroringToLog] is `true`, then the message will also be emitted to
// the log.
func ConRep(message string)

// Issues a formatted message to the standard error stream.
//
// If [IsMirroringToLog] is `true`, then the message will also be emitted to
// the log.
func ConRepf(format string, args ...any)

// Sets whether should mirror contingent reports (via [ConRep], [ConRepf],
// [Abort], [Abortf]) to the log.
func MirrorToLog(enable bool) bool

// Indicates whether mirroring contingent reports (via [ConRep], [ConRepf],
// [Abort], [Abortf]) to the log.
func IsMirroringToLog() bool
Debug
// Obtains the file information for the calling function.
func File() string

// Obtains the file and line information for the calling function.
func FileLine() string

// Obtains the file, line, and function information for the calling
// function.
func FileLineFunction() string

// Obtains the line information for the calling function.
func Line() int

// Obtains the line and function information for the calling function.
func LineFunction() string
Logging/Tracing
func SetBackEnd(be *BackEnd)

// Obtains the current backend handler function.
func GetBackEndHandlerFunc() *BackEnd

// Sets whether logging is enabled.
func EnableLogging(enable bool)

// Indicates whether logging is enabled.
func IsLoggingEnabled() bool

// Logs the arguments at the given severity.
func Log(severity severity.Severity, args ...any)

// Logs the formatted arguments at the given severity.
func Logf(severity severity.Severity, format string, args ...any)
func EnableTracing(enable bool) bool

func IsTracingEnabled() bool

// Creates an argument descriptor that will trace the argument name, type,
// and value.
func Trarg(name string, value any) TraceArgument

// Creates an argument descriptor that will trace the argument name, but not
// type and value.
func TrargNameOnly(name string, value any) TraceArgument

// Creates an argument descriptor that will trace the argument name and
// type, but not value.
func TrargNameTypeOnly(name string, value any) TraceArgument

func TrargTrunc(name string, value any) TraceArgument

// Provides named-argument tracing of a function/method, as in:
//
//	 import d "github.com/synesissoftware/Diagnosticism.Go"
//
//		func SomeFunction(x, y int, order string) {
//
//			d.Trace(d.FileLineFunction(),
//				d.Trarg("x", x),
//				d.Trarg("y", y),
//				d.TrargNameTypeOnly("order", order),
//			)
//
//			. . . impl. of SomeFunc()
//		}
//
// The first parameter `function_name` is a string, and the remaining
// parameters are a variable length list of TraceArgument instances, which
// may be created using the `Trarg()` and `TrargNameOnly()` functions
func Trace(function_name string, args ...TraceArgument)
// Middleware adapter that causes a request to be logged, according to the
// given flags and options
//
// Parameters:
//   - +flags+ (LogRequestFlags) A combination of flags that moderate the behaviour
//   - +options+ Optional arguments (see below)
//
// Options:
//   - * (severity.Severity) The first option of this type is used for before and/or after logging; if none specified, before and/or after logging is done using severity.Informational
func LogRequest(flags LogRequestFlags, options ...any) func(http.Handler) http.Handler
// Obtains the stock string form of a severity.
func TranslateStockSeverity(severity Severity) string
Interfaces

No public interface are defined at this time.

Structures
Logging/Tracing
// Type describing an entry to be processed by the logging back-end.
type BackEndEntry struct {
	// The severity of the log statement.
	Severity severity.Severity
	// The time at which the log statement was consumed.
	Time time.Time
	// The statement message.
	Message string
}

// Backend log handler.
type BackEnd struct {

	// Flags that control the back-end behaviour/features
	Flags BackEndFlag
	// The back-end handler function. May not be nil
	HandlerFunc BackEndHandlerFunc
	// The string to be used as a separator. If the empty string, then the
	// default separator - " : " - is used. If no separator is desired, the
	// NoPrefixSeparator flag must be specified
	PrefixSeparator string
}
Timing
// Decimal Order-Of-Magnitude frequency histoGRAM
//
// # Note:
// This is a Go port of the equivalent `stlsoft::doomgram` class from the
// **STLSoft** libraries (https://github.com/synesissoftware/STLSoft-1.11).
type DOOMGram struct {
}

Examples

Examples are provided in the examples directory, along with a markdown description for each. A detailed list TOC of them is provided in EXAMPLES.md.

Project Information

Where to get help

GitHub Page

Contribution guidelines

Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/Diagnosticism.Go.

Dependencies
Development Dependencies
Dependent projects
License

Diagnosticism.Go is released under the 3-clause BSD license. See LICENSE for details.

Documentation

Index

Constants

View Source
const (
	AfterPrefix  = "AFTER "
	BeforePrefix = "BEFORE "
)
View Source
const (
	VersionMajor uint16 = 0
	VersionMinor uint16 = 12
	VersionPatch uint16 = 0
	VersionAB    uint16 = 0xFFFF
	Version      uint64 = (uint64(VersionMajor) << 48) + (uint64(VersionMinor) << 32) + (uint64(VersionPatch) << 16) + (uint64(VersionAB) << 0)
)

Variables

This section is empty.

Functions

func Abort added in v0.3.0

func Abort(message string)

Issues the given message and then end the process.

If IsMirroringToLog is `true`, then the message will be emitted to the log before terminating.

func Abortf added in v0.3.2

func Abortf(format string, args ...any)

Issues a formatted message and then end the process.

If IsMirroringToLog is `true`, then the message will be emitted to the log before terminating.

func ConRep added in v0.3.0

func ConRep(message string)

Issues the given message to the standard error stream.

If IsMirroringToLog is `true`, then the message will also be emitted to the log.

func ConRepf added in v0.3.2

func ConRepf(format string, args ...any)

Issues a formatted message to the standard error stream.

If IsMirroringToLog is `true`, then the message will also be emitted to the log.

func DOOMScope added in v0.11.0

func DOOMScope(
	dg *DOOMGram,
	rwmu *sync.RWMutex,
	f func() error,
) error

Invokes a function in a timed manner and updates the DOOMGram instance, with access protected by the sync.RWMutex if provided.

Parameters:

  • dg The DOOMGram instance to be modified;
  • rwmu Optional sync.RWMutex with which to protect access to dg;
  • fn Function to be timed;

func EnableLogging added in v0.2.0

func EnableLogging(enable bool)

Sets whether logging is enabled.

func EnableTracing

func EnableTracing(enable bool) bool

Enables tracing globally (for all threads/goroutines).

func File added in v0.10.0

func File() string

Obtains the file information for the calling function.

func FileLine added in v0.7.0

func FileLine() string

Obtains the file and line information for the calling function.

func FileLineFunction added in v0.7.0

func FileLineFunction() string

Obtains the file, line, and function information for the calling function.

func Function added in v0.12.0

func Function() string

Obtains the function information for the calling function.

func IsLoggingEnabled added in v0.2.0

func IsLoggingEnabled() bool

Indicates whether logging is enabled.

func IsMirroringToLog added in v0.3.0

func IsMirroringToLog() bool

Indicates whether mirroring contingent reports (via ConRep, ConRepf, Abort, Abortf) to the log.

If IsMirroringToLog is `true`, then the message will also be emitted to the log before terminating.

func IsTracingEnabled

func IsTracingEnabled() bool

Indicates whether tracing is enabled globally (for all threads/goroutines).

func Line added in v0.12.0

func Line() int

Obtains the line information for the calling function.

func LineFunction added in v0.12.0

func LineFunction() string

Obtains the line and function information for the calling function.

func Log added in v0.2.0

func Log(severity severity.Severity, args ...any)

Logs the arguments at the given severity.

func LogRequest added in v0.4.0

func LogRequest(flags LogRequestFlags, options ...any) func(http.Handler) http.Handler

Middleware adapter that causes a request to be logged, according to the given flags and options

Parameters:

  • +flags+ (LogRequestFlags) A combination of flags that moderate the behaviour
  • +options+ Optional arguments (see below)

Options:

  • * (severity.Severity) The first option of this type is used for before and/or after logging; if none specified, before and/or after logging is done using severity.Informational

func Logf added in v0.3.2

func Logf(severity severity.Severity, format string, args ...any)

Logs the formatted arguments at the given severity.

func MirrorToLog added in v0.3.0

func MirrorToLog(enable bool) bool

Sets whether should mirror contingent reports (via ConRep, ConRepf, Abort, Abortf) to the log.

If IsMirroringToLog is `true`, then the message will also be emitted to the log before terminating.

func Trace

func Trace(function_name string, args ...TraceArgument)

Provides named-argument tracing of a function/method, as in:

 import d "github.com/synesissoftware/Diagnosticism.Go"

	func SomeFunction(x, y int, order string) {

		d.Trace(d.FileLineFunction(),
			d.Trarg("x", x),
			d.Trarg("y", y),
			d.TrargNameTypeOnly("order", order),
		)

		. . . impl. of SomeFunc()
	}

The first parameter `function_name` is a string, and the remaining parameters are a variable length list of TraceArgument instances, which may be created using the `Trarg()` and `TrargNameOnly()` functions

func VersionString added in v0.5.0

func VersionString() string

Types

type BackEnd added in v0.6.0

type BackEnd struct {

	// Flags that control the back-end behaviour/features
	Flags BackEndFlag
	// The back-end handler function. May not be nil
	HandlerFunc BackEndHandlerFunc
	// The string to be used as a separator. If the empty string, then the
	// default separator - " : " - is used. If no separator is desired, the
	// NoPrefixSeparator flag must be specified
	PrefixSeparator string
}

Backend log handler.

func GetBackEndHandlerFunc added in v0.6.0

func GetBackEndHandlerFunc() *BackEnd

Obtains the current backend handler function.

func SetBackEnd added in v0.6.0

func SetBackEnd(be *BackEnd) (r *BackEnd)

type BackEndEntry added in v0.6.0

type BackEndEntry struct {
	// The severity of the log statement.
	Severity severity.Severity
	// The time at which the log statement was consumed.
	Time time.Time
	// The statement message.
	Message string
}

Type describing an entry to be processed by the logging back-end.

type BackEndFlag added in v0.6.0

type BackEndFlag int

Flags for controlling back-end behaviour/features.

const (
	NoPrefix          BackEndFlag = 1
	NoPrefixSeparator BackEndFlag = 2
	NoTime            BackEndFlag = 4
)

type BackEndHandlerFunc added in v0.6.0

type BackEndHandlerFunc func(be *BackEnd, bee *BackEndEntry)

The BackEndHandlerFunc is called when a log statement is to be emitted.

type DOOMGram added in v0.8.0

type DOOMGram struct {
	// contains filtered or unexported fields
}

Decimal Order-Of-Magnitude frequency histoGRAM

# Note: This is a Go port of the equivalent `stlsoft::doomgram` class from the **STLSoft** libraries (https://github.com/synesissoftware/STLSoft-1.11).

func (DOOMGram) EventCount added in v0.8.0

func (d DOOMGram) EventCount() uint64

Number of events counted.

func (DOOMGram) EventTimeTotal added in v0.8.0

func (d DOOMGram) EventTimeTotal() (bool, uint64)

Attempts to obtain the total event time (in nanoseconds).

func (DOOMGram) EventTimeTotalRaw added in v0.8.0

func (d DOOMGram) EventTimeTotalRaw() uint64

Obtains the total event time (in nanoseconds), regardless of whether overflow has occurred.

func (DOOMGram) MaxEventTime added in v0.8.0

func (d DOOMGram) MaxEventTime() (bool, uint64)

Attempts to obtain the maximum event time.

func (DOOMGram) MinEventTime added in v0.8.0

func (d DOOMGram) MinEventTime() (bool, uint64)

Attempts to obtain the minimum event time.

func (DOOMGram) NumEventsIe100s added in v0.8.0

func (d DOOMGram) NumEventsIe100s() uint64

Number of events counted in the interval [100s, ∞).

func (DOOMGram) NumEventsIn100ms added in v0.8.0

func (d DOOMGram) NumEventsIn100ms() uint64

Number of events counted in the interval [100ms, 1s).

func (DOOMGram) NumEventsIn100ns added in v0.8.0

func (d DOOMGram) NumEventsIn100ns() uint64

Number of events counted in the interval [100ns, 1µs).

func (DOOMGram) NumEventsIn100us added in v0.8.0

func (d DOOMGram) NumEventsIn100us() uint64

Number of events counted in the interval [100µs, 1ms).

func (DOOMGram) NumEventsIn10ms added in v0.8.0

func (d DOOMGram) NumEventsIn10ms() uint64

Number of events counted in the interval [10ms, 100ms).

func (DOOMGram) NumEventsIn10ns added in v0.8.0

func (d DOOMGram) NumEventsIn10ns() uint64

Number of events counted in the interval [10ns, 100ns).

func (DOOMGram) NumEventsIn10s added in v0.8.0

func (d DOOMGram) NumEventsIn10s() uint64

Number of events counted in the interval [10s, 100s).

func (DOOMGram) NumEventsIn10us added in v0.8.0

func (d DOOMGram) NumEventsIn10us() uint64

Number of events counted in the interval [10µs, 100µs).

func (DOOMGram) NumEventsIn1ms added in v0.8.0

func (d DOOMGram) NumEventsIn1ms() uint64

Number of events counted in the interval [1ms, 10ms).

func (DOOMGram) NumEventsIn1ns added in v0.8.0

func (d DOOMGram) NumEventsIn1ns() uint64

Number of events counted in the interval [1ns, 10ns).

func (DOOMGram) NumEventsIn1s added in v0.8.0

func (d DOOMGram) NumEventsIn1s() uint64

Number of events counted in the interval [1s, 10s).

func (DOOMGram) NumEventsIn1us added in v0.8.0

func (d DOOMGram) NumEventsIn1us() uint64

Number of events counted in the interval [1µs, 10µs).

func (DOOMGram) Overflowed added in v0.8.0

func (d DOOMGram) Overflowed() bool

Indicates whether overflow has occurred.

func (*DOOMGram) PushEventDuration added in v0.8.0

func (d *DOOMGram) PushEventDuration(duration time.Duration) bool

func (*DOOMGram) PushEventTimeMs added in v0.8.0

func (d *DOOMGram) PushEventTimeMs(time_in_ms uint64) bool

func (*DOOMGram) PushEventTimeNs added in v0.8.0

func (d *DOOMGram) PushEventTimeNs(time_in_ns uint64) bool

func (*DOOMGram) PushEventTimeS added in v0.8.0

func (d *DOOMGram) PushEventTimeS(time_in_s uint64) bool

func (*DOOMGram) PushEventTimeUs added in v0.8.0

func (d *DOOMGram) PushEventTimeUs(time_in_us uint64) bool

func (DOOMGram) ToStrip added in v0.8.0

func (d DOOMGram) ToStrip() string

Converts a DOOMGram instance into a timing strip string representation.

type LogRequestFlags added in v0.4.0

type LogRequestFlags int
const (

	// Do not log before the request
	LogRequest_NotBefore LogRequestFlags = 1 << iota
	// Do not log after the request
	LogRequest_NotAfter LogRequestFlags = 1 << iota
	// Do not include a BEFORE/AFTER prefix
	LogRequest_NoWhenLabel LogRequestFlags = 1 << iota

	// Do not include the method
	LogRequest_NotMethod LogRequestFlags = 1 << iota
	// Do not include the URL
	LogRequest_NotURL LogRequestFlags = 1 << iota
	// Include the protocol
	LogRequest_Protocol LogRequestFlags = 1 << iota
)

type TraceArgument

type TraceArgument struct {
	Name  string
	Value any
	Flags TraceArgumentFlags
}

Structure defining trace argument, generated by Trarg and TrargNameOnly.

func Trarg

func Trarg(name string, value any) TraceArgument

Creates an argument descriptor that will trace the argument name, type, and value.

func TrargNameOnly

func TrargNameOnly(name string, value any) TraceArgument

Creates an argument descriptor that will trace the argument name, but not type and value.

func TrargNameTypeOnly added in v0.9.0

func TrargNameTypeOnly(name string, value any) TraceArgument

Creates an argument descriptor that will trace the argument name and type, but not value.

func TrargTrunc added in v0.9.0

func TrargTrunc(name string, value any) TraceArgument

func (TraceArgument) String added in v0.2.0

func (arg TraceArgument) String() string

type TraceArgumentFlags added in v0.9.0

type TraceArgumentFlags int64

Flags type for [TraceArgument.Flags].

const (
	None          TraceArgumentFlags = 0                     // No flags specified
	NameOnly      TraceArgumentFlags = 0x0000_0000_0000_0001 // Does not trace the value of a function parameter.
	NameTypeOnly  TraceArgumentFlags = 0x0000_0000_0000_0002 // Does not trace the type or the value of a function parameter.
	TruncateValue TraceArgumentFlags = 0x0000_0000_0000_0004 // Causes the value to be truncated to 20 runes.
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL