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

@ -15,18 +15,20 @@ package commands
import (
"fmt"
"github.com/mostafah/fsync"
"github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher"
"github.com/spf13/nitro"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/mostafah/fsync"
"github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
)
var Config *hugolib.Config
@ -44,8 +46,8 @@ Complete documentation is available at http://hugo.spf13.com`,
}
var hugoCmdV *cobra.Command
var BuildWatch, Draft, UglyUrls, Verbose bool
var Source, Destination, BaseUrl, CfgFile string
var BuildWatch, Draft, UglyUrls, Verbose, Logging, VerboseLog bool
var Source, Destination, BaseUrl, CfgFile, LogFile string
func Execute() {
AddCommands()
@ -67,6 +69,9 @@ func init() {
HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyurls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().BoolVar(&Logging, "log", false, "Enable Logging")
HugoCmd.PersistentFlags().StringVar(&LogFile, "logfile", "", "Log File path (if set, logging enabled automatically)")
HugoCmd.PersistentFlags().BoolVar(&VerboseLog, "verboselog", false, "verbose logging")
HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
hugoCmdV = HugoCmd
@ -86,12 +91,35 @@ func InitializeConfig() {
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
Config.Verbose = Verbose
}
if hugoCmdV.PersistentFlags().Lookup("logfile").Changed {
Config.LogFile = LogFile
}
if BaseUrl != "" {
Config.BaseUrl = BaseUrl
}
if Destination != "" {
Config.PublishDir = Destination
}
if VerboseLog || Logging || Config.LogFile != "" {
if Config.LogFile != "" {
jww.SetLogFile(Config.LogFile)
} else {
jww.UseTempLogFile("hugo")
}
} else {
jww.DiscardLogging()
}
if Config.Verbose {
jww.SetStdoutThreshold(jww.LevelDebug)
}
if VerboseLog {
jww.SetLogThreshold(jww.LevelDebug)
}
}
func build(watches ...bool) {
@ -103,8 +131,8 @@ func build(watches ...bool) {
utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
fmt.Println("Press ctrl+c to stop")
jww.FEEDBACK.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
jww.FEEDBACK.Println("Press ctrl+c to stop")
utils.CheckErr(NewWatcher(0))
}
}
@ -123,7 +151,7 @@ func getDirList() []string {
var a []string
walker := func(path string, fi os.FileInfo, err error) error {
if err != nil {
fmt.Println("Walker: ", err)
jww.ERROR.Println("Walker: ", err)
return nil
}
@ -151,7 +179,7 @@ func buildSite(watching ...bool) (err error) {
return
}
site.Stats()
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
return nil
}
@ -182,9 +210,7 @@ func NewWatcher(port int) error {
for {
select {
case evs := <-watcher.Event:
if Verbose {
fmt.Println(evs)
}
jww.INFO.Println(evs)
static_changed := false
dynamic_changed := false
@ -214,7 +240,7 @@ func NewWatcher(port int) error {
if static_changed {
fmt.Print("Static file changed, syncing\n\n")
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
utils.StopOnErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
}
if dynamic_changed {

View file

@ -15,9 +15,10 @@
package commands
import (
"fmt"
"github.com/spf13/cobra"
"syscall"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
)
func init() {
@ -33,22 +34,22 @@ var limit = &cobra.Command{
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
jww.ERROR.Println("Error Getting Rlimit ", err)
}
fmt.Println("Current rLimit:", rLimit)
jww.FEEDBACK.Println("Current rLimit:", rLimit)
fmt.Println("Attempting to increase limit")
jww.FEEDBACK.Println("Attempting to increase limit")
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting rLimit ", err)
jww.ERROR.Println("Error Setting rLimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting rLimit ", err)
jww.ERROR.Println("Error Getting rLimit ", err)
}
fmt.Println("rLimit after change:", rLimit)
jww.FEEDBACK.Println("rLimit after change:", rLimit)
},
}
@ -56,14 +57,14 @@ func tweakLimit() {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Unable to obtain rLimit", err)
jww.ERROR.Println("Unable to obtain rLimit", err)
}
if rLimit.Cur < rLimit.Max {
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Unable to increase number of open files limit", err)
jww.ERROR.Println("Unable to increase number of open files limit", err)
}
}
}

View file

@ -15,11 +15,13 @@ package commands
import (
"fmt"
"github.com/spf13/cobra"
"net/http"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
)
var serverPort int
@ -62,7 +64,7 @@ func server(cmd *cobra.Command, args []string) {
// Watch runs its own server as part of the routine
if serverWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
jww.FEEDBACK.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
err := NewWatcher(serverPort)
if err != nil {
fmt.Println(err)
@ -73,21 +75,19 @@ func server(cmd *cobra.Command, args []string) {
}
func serve(port int) {
if Verbose {
fmt.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))
}
jww.FEEDBACK.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))
if BaseUrl == "" {
fmt.Printf("Web Server is available at %s\n", Config.BaseUrl)
jww.FEEDBACK.Printf("Web Server is available at %s\n", Config.BaseUrl)
} else {
fmt.Printf("Web Server is available at http://localhost:%v\n", port)
jww.FEEDBACK.Printf("Web Server is available at http://localhost:%v\n", port)
}
fmt.Println("Press ctrl+c to stop")
err := http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir))))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
jww.ERROR.Printf("Error: %s\n", err.Error())
os.Exit(1)
}
}

View file

@ -15,6 +15,7 @@ package commands
import (
"fmt"
"github.com/spf13/cobra"
)