Adding support for logging & verbose logging. Consolidation of error handling. Integration of jWalterWeatherman library. Fixed #137

This commit is contained in:
spf13
2014-03-31 13:23:34 -04:00
parent 2fa3761ec9
commit e50b9d8ac1
15 changed files with 140 additions and 111 deletions

View File

@@ -16,21 +16,23 @@ package hugolib
import (
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/spf13/hugo/helpers"
"io/ioutil"
"launchpad.net/goyaml"
"os"
"path"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman"
"launchpad.net/goyaml"
)
// config file items
type Config struct {
ContentDir, PublishDir, BaseUrl, StaticDir string
Path, CacheDir, LayoutDir, DefaultLayout string
ConfigFile string
ConfigFile, LogFile string
Title string
Indexes map[string]string // singular, plural
ProcessFilters map[string][]string
@@ -50,8 +52,8 @@ func SetupConfig(cfgfile *string, path *string) *Config {
c.ConfigFile = cfg
if err != nil {
fmt.Printf("%v", err)
fmt.Println(" using defaults instead")
jww.ERROR.Printf("%v", err)
jww.FEEDBACK.Println("using defaults instead")
}
// set defaults
@@ -92,19 +94,19 @@ func (c *Config) readInConfig() {
switch path.Ext(c.ConfigFile) {
case ".yaml":
if err := goyaml.Unmarshal(file, &c); err != nil {
fmt.Printf("Error parsing config: %s", err)
jww.ERROR.Printf("Error parsing config: %s", err)
os.Exit(1)
}
case ".json":
if err := json.Unmarshal(file, &c); err != nil {
fmt.Printf("Error parsing config: %s", err)
jww.ERROR.Printf("Error parsing config: %s", err)
os.Exit(1)
}
case ".toml":
if _, err := toml.Decode(string(file), &c); err != nil {
fmt.Printf("Error parsing config: %s", err)
jww.ERROR.Printf("Error parsing config: %s", err)
os.Exit(1)
}
}
@@ -115,13 +117,13 @@ func (c *Config) setPath(p string) {
if p == "" {
path, err := findPath()
if err != nil {
fmt.Printf("Error finding path: %s", err)
jww.ERROR.Printf("Error finding path: %s", err)
}
c.Path = path
} else {
path, err := filepath.Abs(p)
if err != nil {
fmt.Printf("Error finding path: %s", err)
jww.ERROR.Printf("Error finding path: %s", err)
}
c.Path = path
}

View File

@@ -3,9 +3,10 @@ package hugolib
import (
"errors"
"fmt"
"os"
"strconv"
"time"
jww "github.com/spf13/jwalterweatherman"
)
func interfaceToTime(i interface{}) time.Time {
@@ -17,9 +18,9 @@ func interfaceToTime(i interface{}) time.Time {
if e == nil {
return d
}
errorln("Could not parse Date/Time format:", e)
jww.ERROR.Println("Could not parse Date/Time format:", e)
default:
errorln("Only Time is supported for this key")
jww.ERROR.Println("Only Time is supported for this key")
}
return *new(time.Time)
@@ -53,11 +54,6 @@ func stringToDate(s string) (time.Time, error) {
})
}
// TODO remove this and return a proper error.
func errorln(str string, a ...interface{}) {
fmt.Fprintln(os.Stderr, str, a)
}
func parseDateWith(s string, dates []string) (d time.Time, e error) {
for _, dateType := range dates {
if d, e = time.Parse(dateType, s); e == nil {
@@ -77,7 +73,7 @@ func interfaceToBool(i interface{}) bool {
}
return false
default:
errorln("Only Boolean values are supported for this YAML key")
jww.ERROR.Println("Only Boolean values are supported for this YAML key")
}
return false
@@ -109,11 +105,11 @@ func interfaceToFloat64(i interface{}) float64 {
if err == nil {
return float64(v)
} else {
errorln("Only Floats are supported for this key\nErr:", err)
jww.ERROR.Println("Only Floats are supported for this key\nErr:", err)
}
default:
errorln("Only Floats are supported for this key")
jww.ERROR.Println("Only Floats are supported for this key")
}
return 0.0
@@ -136,10 +132,10 @@ func interfaceToInt(i interface{}) int {
if err == nil {
return int(v)
} else {
errorln("Only Ints are supported for this key\nErr:", err)
jww.ERROR.Println("Only Ints are supported for this key\nErr:", err)
}
default:
errorln("Only Ints are supported for this key")
jww.ERROR.Println("Only Ints are supported for this key")
}
return 0
@@ -154,7 +150,7 @@ func interfaceToString(i interface{}) string {
case int:
return strconv.FormatInt(int64(i.(int)), 10)
default:
errorln(fmt.Sprintf("Only Strings are supported for this key (got type '%T'): %s", s, s))
jww.ERROR.Println(fmt.Sprintf("Only Strings are supported for this key (got type '%T'): %s", s, s))
}
return ""

View File

@@ -17,19 +17,21 @@ import (
"bytes"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser"
"github.com/spf13/hugo/template/bundle"
"github.com/theplant/blackfriday"
"html/template"
"io"
"launchpad.net/goyaml"
json "launchpad.net/rjson"
"net/url"
"path"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser"
"github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman"
"github.com/theplant/blackfriday"
"launchpad.net/goyaml"
json "launchpad.net/rjson"
)
type Page struct {
@@ -142,6 +144,8 @@ func newPage(filename string) *Page {
File: File{FileName: filename, Extension: "html"},
Node: Node{Keywords: make([]string, 10, 30)},
Params: make(map[string]interface{})}
jww.DEBUG.Println("Reading from", page.File.FileName)
page.Date, _ = time.Parse("20060102", "20080101")
page.guessSection()
return &page
@@ -212,6 +216,7 @@ func ReadFrom(buf io.Reader, name string) (page *Page, err error) {
// Parse for metadata & body
if err = p.parse(buf); err != nil {
jww.ERROR.Print(err)
return
}

View File

@@ -15,15 +15,14 @@ package hugolib
import (
"bytes"
"fmt"
"github.com/spf13/hugo/template/bundle"
"html/template"
"reflect"
"strings"
"unicode"
)
var _ = fmt.Println
"github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman"
)
type ShortcodeFunc func([]string) string
@@ -296,8 +295,8 @@ func ShortcodeRender(tmpl *template.Template, data *ShortcodeWithPage) string {
buffer := new(bytes.Buffer)
err := tmpl.Execute(buffer, data)
if err != nil {
fmt.Println("error processing shortcode", tmpl.Name(), "\n ERR:", err)
fmt.Println(data)
jww.ERROR.Println("error processing shortcode", tmpl.Name(), "\n ERR:", err)
jww.WARN.Println(data)
}
return buffer.String()
}

View File

@@ -14,21 +14,23 @@
package hugolib
import (
"bitbucket.org/pkg/inflect"
"bytes"
"fmt"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/template/bundle"
"github.com/spf13/hugo/transform"
"github.com/spf13/nitro"
"html/template"
"io"
"os"
"strings"
"sync"
"time"
"bitbucket.org/pkg/inflect"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/template/bundle"
"github.com/spf13/hugo/transform"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
)
var _ = transform.AbsURL
@@ -104,9 +106,9 @@ func (s *Site) Build() (err error) {
return
}
if err = s.Render(); err != nil {
fmt.Printf("Error rendering site: %s\nAvailable templates:\n", err)
jww.ERROR.Printf("Error rendering site: %s\nAvailable templates:\n", err)
for _, template := range s.Tmpl.Templates() {
fmt.Printf("\t%s\n", template.Name())
jww.ERROR.Printf("\t%s\n", template.Name())
}
return
}
@@ -190,7 +192,7 @@ func (s *Site) Render() (err error) {
func (s *Site) checkDescriptions() {
for _, p := range s.Pages {
if len(p.Description) < 60 {
fmt.Println(p.FileName + " ")
jww.FEEDBACK.Println(p.FileName + " ")
}
}
}
@@ -309,9 +311,7 @@ func (s *Site) BuildSiteMeta() (err error) {
s.Indexes[plural].Add(idx, x)
}
} else {
if s.Config.Verbose {
fmt.Fprintf(os.Stderr, "Invalid %s in %s\n", plural, p.File.FileName)
}
jww.ERROR.Printf("Invalid %s in %s\n", plural, p.File.FileName)
}
}
}
@@ -533,9 +533,9 @@ func (s *Site) RenderHomePage() error {
}
func (s *Site) Stats() {
fmt.Printf("%d pages created \n", len(s.Pages))
jww.FEEDBACK.Printf("%d pages created \n", len(s.Pages))
for _, pl := range s.Config.Indexes {
fmt.Printf("%d %s index created\n", len(s.Indexes[pl]), pl)
jww.FEEDBACK.Printf("%d %s index created\n", len(s.Indexes[pl]), pl)
}
}
@@ -572,9 +572,7 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
layout := s.findFirstLayout(layouts...)
if layout == "" {
if s.Config.Verbose {
fmt.Printf("Unable to locate layout: %s\n", layouts)
}
jww.WARN.Printf("Unable to locate layout: %s\n", layouts)
return
}
@@ -601,7 +599,7 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
err = s.renderThing(d, layout, renderBuffer)
if err != nil {
// Behavior here should be dependent on if running in server or watch mode.
fmt.Println(fmt.Errorf("Rendering error: %v", err))
jww.ERROR.Println(fmt.Errorf("Rendering error: %v", err))
if !s.Running() {
os.Exit(-1)
}
@@ -631,7 +629,6 @@ func (s *Site) renderThing(d interface{}, layout string, w io.Writer) error {
if s.Tmpl.Lookup(layout) == nil {
return fmt.Errorf("Layout not found: %s", layout)
}
//defer w.Close()
return s.Tmpl.ExecuteTemplate(w, layout, d)
}
@@ -652,9 +649,7 @@ func (s *Site) initTarget() {
func (s *Site) WritePublic(path string, reader io.Reader) (err error) {
s.initTarget()
if s.Config.Verbose {
fmt.Println(path)
}
jww.DEBUG.Println("writing to", path)
return s.Target.Publish(path, reader)
}
@@ -666,9 +661,7 @@ func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
}
}
if s.Config.Verbose {
fmt.Println(path)
}
jww.DEBUG.Println("alias created at", path)
return s.Alias.Publish(path, permalink)
}

View File

@@ -2,9 +2,10 @@ package hugolib
import (
"bytes"
"fmt"
"os/exec"
"strings"
jww "github.com/spf13/jwalterweatherman"
)
var summaryLength = 70
@@ -62,7 +63,7 @@ func getRstContent(content []byte) string {
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
fmt.Println(err)
jww.ERROR.Println(err)
}
rstLines := strings.Split(out.String(), "\n")