Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fallsimply committed Mar 13, 2020
1 parent bfe7418 commit f5960e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 10 additions & 0 deletions bilet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bilit

import (
"log"
"os"
"regexp"
)

Expand All @@ -11,6 +13,7 @@ var (
DefaultPattern = ".+"
//Debug enables printing of the map passed to a template
Debug = false
out = log.New(os.Stdout, "", log.Lshortfile)
)

//Data is a data map for bilit
Expand All @@ -29,6 +32,13 @@ func (t Tmpl) RegEx() *regexp.Regexp {
return t.regex
}

func init() {
out.SetPrefix("[bilit] ")
if Debug {
out.SetPrefix("[bilit(DEBUG)] ")
}
}

//Template makes a template
func Template(str string, pattern ...string) Tmpl {
if len(pattern) == 0 {
Expand Down
11 changes: 2 additions & 9 deletions pull.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package bilit

import (
"fmt"
)

func makePullRegex(str string, pattern string) string {
var substitution = "(?P<${groupName}>" + pattern + ")"
return GroupRegexp.ReplaceAllString(str, substitution)
Expand All @@ -12,17 +8,14 @@ func makePullRegex(str string, pattern string) string {
//Pull extracts data from a populated string using a template
func (t Tmpl) Pull(str string) map[string]string {
if Debug {
fmt.Println(t.regex)
out.Println(t.RegEx())
}
matches := map[string]string{}
for _, line := range t.RegEx().FindAllStringSubmatch(str, -1) {
if Debug {
println(line)
}
for n := 1; n < len(line); n++ {
name := t.RegEx().SubexpNames()[n]
if Debug {
println(name + ": " + line[n])
out.Println(name + ": " + line[n])
}
matches[name] = string(line[n])
}
Expand Down

0 comments on commit f5960e6

Please sign in to comment.