From 069d32d4afa321f424db61ce390255e6d7d303b7 Mon Sep 17 00:00:00 2001 From: restitux Date: Thu, 14 Apr 2022 00:55:04 -0600 Subject: [PATCH] Converted logging to go-logging --- go.mod | 6 ++++++ go.sum | 8 +++++-- main.go | 66 ++++++++++++++++++++++++++++++++++----------------------- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index eed49c0..1e05b19 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,11 @@ go 1.18 require ( github.com/mattn/go-sqlite3 v1.14.12 + github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/pelletier/go-toml/v2 v2.0.0-beta.8 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/go.sum b/go.sum index e27fe31..09a33b9 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,10 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/pelletier/go-toml/v2 v2.0.0-beta.8 h1:dy81yyLYJDwMTifq24Oi/IslOslRrDSb3jwDggjz3Z0= github.com/pelletier/go-toml/v2 v2.0.0-beta.8/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -10,5 +13,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 3428ea7..63e0d33 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" _ "github.com/mattn/go-sqlite3" + "github.com/op/go-logging" toml "github.com/pelletier/go-toml/v2" ) @@ -52,7 +53,7 @@ func get_config(config_dir string) (*Config, error) { config_path := filepath.Join(config_dir, "server.toml") - fmt.Printf("Config File Dir: %v\n", config_path) + log.Debugf("Config File Dir: %v\n", config_path) // open file, creating it if empty config_file, err := os.OpenFile(config_path, os.O_RDWR|os.O_CREATE, 0755) @@ -153,6 +154,8 @@ type TVShow struct { } func scan_tvshow_root(root string, db *sql.DB) error { + log.Noticef("Scanning directory root %v", root) + files, err := ioutil.ReadDir(root) if err != nil { return fmt.Errorf("Could not get files in directory \"%v\": %v", root, err) @@ -190,7 +193,7 @@ func insert_tvshow_nfo(show_dir string, stmt *sql.Stmt) bool { tvshow_nfo_file, err := os.OpenFile(tvshow_nfo_path, os.O_RDONLY, 0755) if err != nil { - fmt.Fprintf(os.Stderr, "Could not open tvshow.nfo file in \"%v\"\n", show_dir) + log.Debugf("Could not open tvshow.nfo file in \"%v\"\n", show_dir) return false } defer tvshow_nfo_file.Close() @@ -198,25 +201,25 @@ func insert_tvshow_nfo(show_dir string, stmt *sql.Stmt) bool { // try to parse nfo file data, err := ioutil.ReadAll(tvshow_nfo_file) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not read data from tvshow.nfo file: %v\n", err) + log.Debugf("Could not read data from tvshow.nfo file: %v\n", err) return false } var tvshow TVShow err = xml.Unmarshal(data, &tvshow) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not parse tvshow.nfo file contents: %v\n", err) + log.Debugf("Could not parse tvshow.nfo file contents: %v\n", err) return false } // TODO: Replace with print formatting logic - fmt.Printf("%v (%v):\n", tvshow.Title, tvshow.Year) - fmt.Printf(" Original Title: %v\n", tvshow.OriginalTile) - fmt.Printf(" Show Title: %v\n", tvshow.ShowTitle) + log.Debugf("%v (%v):\n", tvshow.Title, tvshow.Year) + log.Debugf(" Original Title: %v\n", tvshow.OriginalTile) + log.Debugf(" Show Title: %v\n", tvshow.ShowTitle) _, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not insert tvshow into database: %v\n", err) + log.Debugf("Could not insert tvshow \"%v\" into database: %v\n", tvshow.Title, err) return false } return true @@ -232,7 +235,7 @@ type Episode struct { func scan_tvshow_episodes(show_dir string) error { - fmt.Printf("Scanning episodes for show %v\n", show_dir) + log.Infof("Scanning episodes for show %v\n", show_dir) files, err := ioutil.ReadDir(show_dir) if err != nil { @@ -248,17 +251,17 @@ func scan_tvshow_episodes(show_dir string) error { if file.Name() == "tvshow.nfo" { continue } else if ext == ".nfo" { - fmt.Printf("Found nfo file: \"%v\"\n", file.Name()) + log.Debugf("Found nfo file: \"%v\"\n", file.Name()) } else { - fmt.Printf("Skipping file \"%v\", not an nfo file\n", file.Name()) + log.Debugf("Skipping file \"%v\", not an nfo file\n", file.Name()) continue } - fmt.Printf("%v\n", file.Name()) + log.Debugf("%v\n", file.Name()) nfo_file, err := os.OpenFile(nfo_path, os.O_RDONLY, 0755) if err != nil { - fmt.Fprintf(os.Stderr, "Could not open nfo file \"%v\": %v", nfo_path, err) + log.Debugf("Could not open nfo file \"%v\": %v", nfo_path, err) continue } defer nfo_file.Close() @@ -266,32 +269,45 @@ func scan_tvshow_episodes(show_dir string) error { // try to parse nfo file data, err := ioutil.ReadAll(nfo_file) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not read data from nfo file \"%v\": %v\n", nfo_path, err) + log.Debugf("Could not read data from nfo file \"%v\": %v\n", nfo_path, err) continue } var episode Episode err = xml.Unmarshal(data, &episode) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not parse contents of nfo file \"%v\": %v\n", nfo_path, err) + log.Debugf("Could not parse contents of nfo file \"%v\": %v\n", nfo_path, err) continue } - fmt.Printf(" Title: %v\n", episode.Title) - fmt.Printf(" Season %v\n", episode.Season) - fmt.Printf(" Episode %v\n", episode.Episode) - fmt.Printf(" Show: %v\n", episode.ShowTitle) + log.Debugf(" Title: %v\n", episode.Title) + log.Debugf(" Season %v\n", episode.Season) + log.Debugf(" Episode %v\n", episode.Episode) + log.Debugf(" Show: %v\n", episode.ShowTitle) num_episodes++ } - fmt.Printf("Found %v episodes\n", num_episodes) + log.Infof("Found %v episodes\n", num_episodes) return nil } + +var log = logging.MustGetLogger("ikinuki-server") + func main() { + var format = logging.MustStringFormatter( + `%{color}%{time:15:04:05.000} %{level:.4s}:%{color:reset} %{message}`, + ) + + backend := logging.NewLogBackend(os.Stderr, "", 0) + backendFormatter := logging.NewBackendFormatter(backend, format) + backendLeveled := logging.AddModuleLevel(backendFormatter) + backendLeveled.SetLevel(logging.DEBUG, "") + + logging.SetBackend(backendLeveled) config_dir := get_config_dir() @@ -300,24 +316,22 @@ func main() { panic(err) } if config == nil { - fmt.Printf("Created config file at %v, please edit and launch again\n", config_dir) + log.Warningf("Created config file at %v, please edit and launch again\n", config_dir) os.Exit(0) } - fmt.Printf("Config: %v\n", config) + log.Debugf("Config: %v\n", config) data_dir := get_data_dir() db, err := open_database(data_dir) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not open database: %v", err) - os.Exit(1) + log.Fatalf("Could not open database: %v", err) } defer db.Close() err = scan_tvshow_root(config.ScanDirectory, db) if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: Could not scan directory \"%v\": %v\n", config.ScanDirectory, err) - os.Exit(1) + log.Fatalf("Could not scan directory \"%v\": %v\n", config.ScanDirectory, err) } }