Converted logging to go-logging
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user