verbose

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 9 Imported by: 2

README

Verbose

Allows you to easily add in statements to your code that allow you to trace what your code is doing or what a variable, is equal to at a certain point. This also prints the line number so you can quickly find what file / line we're on. Good for when you've not looked at the code in a year or more.

For example, lets say in your code you're doing a database query, no error but it gives you unexpected results. Use verbose to print the query, maybe the query isn't right in this instance.

The way it is intended is you can leave these in the code. Use it in conjunction with flag or pflag so that you can just invoke it. Normally the user will not see the verbose statements, but a -v or --verbose when running, and they show up.

This will also print to a file, or to stderr if you want it to. This allows you to have your end user run it in verbose mode, log it to a file that they can send you in an email allowing you to evaluate what's going on with your code.

Example Code:

func main() {
	i := 0
	var testme string
	var help bool
	// Notice the formatting is the same as if you used the Linux date command. 
	verb := verbose.New("%A %B %Y, %I:%M:%S %P %Z")
	var flagset pflag.FlagSet
	flagset.BoolVarP(&verb.V, "verbose", "v", false, "Verbose Mode")
	flagset.BoolVarP(&help, "help", "h", false, "Help")
	flagset.StringVarP(&testme, "string", "s", "", "String to print out.")
	flagset.MarkHidden("verbose")
	flagset.Parse(os.Args[1:])
	
	verb.Printf("%s started\n",os.Args[0])
	verb.Printf("Remaining Args passed: %v\n",flagset.Args())
	... lots of code ...
	// verb.
	verb.Println("Query Database for user status:")
	verb.Println("Query:",query)
	... lots of code ...
	verb.Println("Rest Call to www.somewhere.com")

When the user runs a program like this if the -v or --verbose is not passed as an option, the verb.Print statements will not print.

The default output is to os.Stdout - but this can be changed. If in the above example we wanted to write the output to a file. or to stderr we can do that two ways.

  1. set verb.Out:=*os.Writer
// set message to file
w:=os.Create("verbose.txt")
verb.Out=w
verb.Println("This line goes to a file")
  1. Use verb.Fprintf(os.Writer, "Your message goes here.")
// Set message to stderr:
verb.Fprintf(os.Stderr, "My Message Goes Here")

Documentation

Overview

Package verbose provides a simple package to allow for printing out messages that help you show the flow of how your program works. And you don't have to remove them. Just implement it as a 'flag': verb:=verbose.New() flag.BoolVar(&verb.V, "v", false, "Verbose mode"

Then in your code you can put in your code: func main() { ...code... verb.Println("Database Query:",query) ... code ... verb.Println("Calculating world peace -- this could take some time.", worldPeace) You can leave this in your code, hide that there is a -v option if you want to. But the result is that when things go wrong and inevitably things go wrong, you can see the flow of your program. For information about UTF-8 strings in Go, see https://blog.golang.org/strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MyTest added in v0.5.0

func MyTest() string

func TimeFormatStr

func TimeFormatStr(tformat string) (fmtStr string)

Converts Unix/Linux date stamp to Go Date Format. date +"%A %d %B %Y, %I:%M:%S %P %Z" Returns: Thursday December 12 2022, 06:03:08 P EST verbose.TimeFormatStr("%A %B %d %Y, %I:%M:%S %P %Z") Returns: "Monday January 02 2006, 03:05:05 PM MST". When put into time.Now().Format("Monday January 02 2006, 03:05:05 PM MST") Would give you: "Thursday December 12 2022, 06:03:08 PM EST"

Types

type Verb

type Verb struct {
	// V when set to true enables the verbose printing.
	V bool
	// set the date format using standard Go Formatting 2006/01/02 15:04:05
	Dformat string
	// Set the delimiter between date, line number and print string.
	Delimeter string
	// If set to false, date will not be printed
	PrintDate bool
	// If set to false, line number will not be printed
	PrintLine bool
	// Set where to write the print statements. By default it's stderr, but you can change it to stdout, or to a file.
	Out io.Writer `default0:os.Stderr`
}

func New

func New(w io.Writer, a ...any) (v Verb)

Returns a type Verb and sets some defaults. If nothing passed verbose.New(), no date is used if verbose.New("default") use a default date string. Date string can be customized by either setting Dformat using go date format string or by passing a linux date compatible string to verbose.New.

func (*Verb) Err added in v1.1.0

func (v *Verb) Err(err error, str string, e ...bool) bool

func (*Verb) ErrOut added in v1.1.0

func (v *Verb) ErrOut(err error, str string, e ...bool) bool

err out will always print if an error is present. to only print when verb is true use Err. Prints to whatever verbose.Out is set to.

func (*Verb) Fprint

func (verb *Verb) Fprint(w io.Writer, a ...any)

Just like fmt.Fprint -- only prints when verbose.V is true. Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Fprintf

func (verb *Verb) Fprintf(w io.Writer, format string, a ...any)

Just like fmt.Fprintf, but only prints if verb.V is true. Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Fprintln

func (verb *Verb) Fprintln(w io.Writer, a ...any)

Just like fmt.Fprintln -- only prints when verbose.V is true. Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Print

func (v *Verb) Print(a ...any)

Just like fmt.Print -- only prints when verbose.V is true. Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Printf

func (v *Verb) Printf(format string, a ...any)

Just like fmt.Printf, but only prints if verb.V is true Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Printj added in v1.2.0

func (v *Verb) Printj(data interface{})

Prints a interface (struct) in indented JSON. Only prints if verb.V is true Line numbers are not printed.

func (*Verb) Println

func (v *Verb) Println(a ...any)

Just like fmt.Println -- only prints when verbose.V is true, Only prints the date and line number if PrintDate and PrintLine are true

func (*Verb) Spin added in v1.1.0

func (v *Verb) Spin(quit chan bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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