dl

package module
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 19 Imported by: 0

README

dl(Debug x Log) - The instant logger package for debug

Go Reference .github/workflows/ci.yml Go Report Card codecov MIT License

delog.gif

Description

Who doesn't write wrong codes? No one.
Then, programs don't work well, and developers write logs for debug to understand what happens.

However, some developers forget to delete their logs after resolving the problem and push their codes. In the worse case, the logs might be released.

dl is developed to resolve their problems.

Features

  • Logging package for debug in Go
  • Command for Sweeping all functions of this package
  • Command for installing git hooks and .gitignore

Installation

Go1.18
$ go install github.com/task4233/dl/cmd/dl@latest

Use Case

Debug

Playground

package main

import (
	"os"

	"github.com/task4233/dl"
)

type U[T any] []T

func (t U[T]) append(v T) {
	t = append(t, v)
	dl.Info(t)
}

func (t U[T]) change(v T) {
	t[0] = v
	dl.FInfo(os.Stdout, t)
}

func main() {
	t := U[int]([]int{1, 3})
	t.append(5)
	t.change(5)
}


// Output:
// [DeLog] info: main.U[int]{1, 3, 5} (main.U[int]) prog.go:13
// [DeLog] info: main.U[int]{5, 3} (main.U[int]) prog.go:18
Adds dl into Git hooks
  1. Please run commands below to install dl in your Git repository.
$ dl init .
  1. Just commit
  • delog is used in the file.
$ cat main.go 
package main

import (
	"fmt"
	
	"github.com/task4233/dl"
)

func SayHi[T any](v T) {
	dl.Printf("Type: %T, v: %v\n", v, v) // This statement can be removed by `$ dl clean main.go`
	fmt.Println("Hi, ", v)
}

func main() {
    SayHi("hoge")
}
  • invoke $ git commit
$ git add main.go
$ git commit -m "feat: add main.go"
remove dl from main.go # automatically removed
[master 975ecf9] feat: add main.go
 1 file changed, 12 insertions(+), 21 deletions(-)
 rewrite main.go (91%)
  • delog is removed automatically
$ git diff HEAD^
diff --git a/main.go b/main.go
index 90a78bd..0e28e8a 100644
--- a/main.go
+++ b/main.go
@@ -1,21 +1,12 @@
 package main

+import (
+       "fmt"
+)
 
+func SayHi[T any](v T) {
+       fmt.Println("Hi, ", v)
+}

 func main() {
+       SayHi("hoge")
 }
  • removed delog codes are restored(not commited)
$ cat main.go 
package main

import (
	"fmt"
	
	"github.com/task4233/dl"
)

func SayHi[T any](v T) {
	dl.Printf("Type: %T, v: %v\n", v, v) // This statement can be removed by `$ dl clean main.go`
	fmt.Println("Hi, ", v)
}

func main() {
    SayHi("hoge")
}
Remove dl from GitHooks
$ dl remove .

Contribution

Please feel free to make issues and pull requests.

Author

task4233

Documentation

Overview

Example
package main

import (
	"fmt"
	"os"

	"github.com/go-logr/logr"
	"github.com/go-logr/zapr"
	"github.com/task4233/dl"
	"go.uber.org/zap"
)

func main() {
	num := 123
	name := "dl"
	type MyInt int
	var myNum MyInt = 123

	dl.Fprintln(os.Stdout, "num: ", num)
	dl.Println("num: ", num)
	dl.Fprintf(os.Stdout, "name: %s\n", name)
	dl.Printf("name: %s", name)
	dl.FInfo(os.Stdout, myNum)
	dl.Info(myNum)

	zapLog, err := zap.NewDevelopment()
	if err != nil {
		panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
	}
	var log logr.Logger = zapr.NewLogger(zapLog)
	dlr := dl.NewLogger(&log)
	dlr.Info("Info: ", "num", num)

}
Output:

num:  123
name: dl
[DeLog] info: 123 (dl_test.MyInt) log_example_test.go:23

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FInfo

func FInfo[T any](w io.Writer, v T) (int, error)

FInfo gives a val, a type, a file name, a line number and writes to w..

func Fprintf

func Fprintf(w io.Writer, format string, v ...any) (int, error)

Fprintf formats according to a format specifier and writes to w. Arguments are handled in the manner of fmt.FPrintf.

func Fprintln

func Fprintln(w io.Writer, v ...any) (int, error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. Arguments are handled in the manner of fmt.FPrintln.

func Info

func Info[T any](v T) (int, error)

Info gives a val, a type, a file name, a line number to print to the standard logger.

func Printf

func Printf(format string, v ...any) (int, error)

Printf calls Fprintf to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...any) (int, error)

Println calls Fprintln to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

Types

type Clean added in v0.5.5

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

func NewClean added in v0.5.5

func NewClean() *Clean

func (*Clean) Evacuate added in v0.5.5

func (c *Clean) Evacuate(ctx context.Context, baseDirPath string, srcFilePath string) error

Evacuate copies ".go" files to under ".dl" directory. This method requires ".dl" directory to exist. This method doesn't allow to invoke with a file included in `excludeFiles`.

func (*Clean) Run added in v0.5.5

func (c *Clean) Run(ctx context.Context, baseDir string) error

Run deletes all methods related to dl in ".go" files under the given directory path

func (*Clean) Sweep added in v0.5.5

func (c *Clean) Sweep(ctx context.Context, targetFilePath string) error

Sweep deletes all methods related to dl in a ".go" file. This method requires ".dl" directory to exist.

type Cmd added in v0.5.5

type Cmd interface {
	Run(ctx context.Context, baseDir string) error
}

type DeLog

type DeLog struct {
}

DeLog structs.

func New

func New() *DeLog

New for running dl package with CLI.

func (*DeLog) Run

func (d *DeLog) Run(ctx context.Context, version string, args []string) error

Run executes each method for dl package.

type Init added in v0.5.5

type Init struct{}

Init structs for $ dl init.

func NewInit added in v0.5.5

func NewInit() *Init

NewInit for running $ dl init.

func (*Init) Run added in v0.5.5

func (i *Init) Run(ctx context.Context, baseDir string) error

Run prepares an environment for dl commands.

type IntHeap added in v0.5.6

type IntHeap []int

An IntHeap is a min-heap of ints.

func (IntHeap) Len added in v0.5.6

func (h IntHeap) Len() int

func (IntHeap) Less added in v0.5.6

func (h IntHeap) Less(i, j int) bool

greater order

func (*IntHeap) Pop added in v0.5.6

func (h *IntHeap) Pop() any

func (*IntHeap) Push added in v0.5.6

func (h *IntHeap) Push(x any)

func (IntHeap) Swap added in v0.5.6

func (h IntHeap) Swap(i, j int)

type Logger added in v0.5.8

type Logger struct {
	*logr.Logger
}

Logger is a struct for preserving *logr.Logger.

func NewLogger added in v0.5.8

func NewLogger(l *logr.Logger) *Logger

NewLogger wraps logr.Logger.

type Remove added in v0.5.5

type Remove struct{}

func NewRemove added in v0.5.5

func NewRemove() *Remove

func (*Remove) Run added in v0.5.5

func (r *Remove) Run(ctx context.Context, baseDir string) error

Remove removes environment settings for dl commands.

type Restore added in v0.5.5

type Restore struct{}

func NewRestore added in v0.5.5

func NewRestore() *Restore

func (*Restore) Run added in v0.5.5

func (r *Restore) Run(ctx context.Context, baseDir string) error

Restore restores raw files from .dl directory

Directories

Path Synopsis
cmd
dl command

Jump to

Keyboard shortcuts

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