diff --git a/cmd/magneticod/main.go b/cmd/magneticod/main.go index 86e9037..188679b 100644 --- a/cmd/magneticod/main.go +++ b/cmd/magneticod/main.go @@ -1,7 +1,8 @@ package main import ( - "fmt" + "github.com/pkg/errors" + "math/rand" "net" "os" "os/signal" @@ -40,6 +41,8 @@ type opFlags struct { Profile string } +var compiledOn string + func main() { loggerLevel := zap.NewAtomicLevel() // Logging levels: ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal"). @@ -58,9 +61,10 @@ func main() { return } - zap.L().Info("magneticod v0.7.0-beta1 has been started.") - zap.L().Info("Copyright (C) 2017 Mert Bora ALPER .") + zap.L().Info("magneticod v0.7.0-beta2 has been started.") + zap.L().Info("Copyright (C) 2018 Mert Bora ALPER .") zap.L().Info("Dedicated to Cemile Binay, in whose hands I thrived.") + zap.S().Infof("Compiled on %s", compiledOn) switch opFlags.Verbosity { case 0: @@ -91,18 +95,23 @@ func main() { zap.L().Panic("NOT IMPLEMENTED") } + // Initialise the random number generator + rand.Seed(time.Now().UnixNano()) + // Handle Ctrl-C gracefully. interruptChan := make(chan os.Signal) signal.Notify(interruptChan, os.Interrupt) database, err := persistence.MakeDatabase(opFlags.DatabaseURL, logger) if err != nil { - logger.Sugar().Fatalf("Could not open the database at `%s`: %s", opFlags.DatabaseURL, err.Error()) + logger.Sugar().Fatalf("Could not open the database at `%s`", opFlags.DatabaseURL, zap.Error(err)) } trawlingManager := dht.NewTrawlingManager(opFlags.TrawlerMlAddrs) metadataSink := metadata.NewSink(2 * time.Minute) + zap.L().Debug("Peer ID", zap.ByteString("peerID", metadataSink.PeerID)) + // The Event Loop for stopped := false; !stopped; { select { @@ -117,8 +126,8 @@ func main() { case md := <-metadataSink.Drain(): if err := database.AddNewTorrent(md.InfoHash, md.Name, md.Files); err != nil { - logger.Sugar().Fatalf("Could not add new torrent %x to the database: %s", - md.InfoHash, err.Error()) + logger.Sugar().Fatalf("Could not add new torrent %x to the database", + md.InfoHash, zap.Error(err)) } zap.L().Info("Fetched!", zap.String("name", md.Name), util.HexField("infoHash", md.InfoHash)) @@ -147,13 +156,16 @@ func parseFlags() (*opFlags, error) { "sqlite3://" + appdirs.UserDataDir("magneticod", "", "", false) + "/database.sqlite3" + - "?_journal_mode=WAL" // https://github.com/mattn/go-sqlite3#connection-string + "?_journal_mode=WAL" + // https://github.com/mattn/go-sqlite3#connection-string + "&_busy_timeout=3000" + // in milliseconds + "&_foreign_keys=true" + } else { opF.DatabaseURL = cmdF.DatabaseURL } if err = checkAddrs(cmdF.TrawlerMlAddrs); err != nil { - zap.S().Fatalf("Of argument (list) `trawler-ml-addr` %s", err.Error()) + zap.S().Fatalf("Of argument (list) `trawler-ml-addr`", zap.Error(err)) } else { opF.TrawlerMlAddrs = cmdF.TrawlerMlAddrs } @@ -178,7 +190,7 @@ func checkAddrs(addrs []string) error { // well. _, err := net.ResolveUDPAddr("udp", addr) if err != nil { - return fmt.Errorf("with %d(th) address `%s`: %s", i+1, addr, err.Error()) + return errors.Wrapf(err, "%d(th) address (%s) error", i+1, addr) } } return nil diff --git a/cmd/magneticow/main.go b/cmd/magneticow/main.go index 1f6803a..1785c94 100644 --- a/cmd/magneticow/main.go +++ b/cmd/magneticow/main.go @@ -31,6 +31,8 @@ import ( const N_TORRENTS = 20 +var compiledOn string + // Set a Decoder instance as a package global, because it caches // meta-data about structs, and an instance can be shared safely. var decoder = schema.NewDecoder() @@ -44,6 +46,7 @@ var opts struct { Credentials map[string][]byte // TODO: encapsulate credentials and mutex for safety CredentialsRWMutex sync.RWMutex CredentialsPath string + Verbosity int } func main() { @@ -58,14 +61,27 @@ func main() { zap.ReplaceGlobals(logger) zap.L().Info("magneticow v0.7.0-beta1 has been started.") - zap.L().Info("Copyright (C) 2017 Mert Bora ALPER .") + zap.L().Info("Copyright (C) 2018 Mert Bora ALPER .") zap.L().Info("Dedicated to Cemile Binay, in whose hands I thrived.") + zap.S().Infof("Compiled on %s", compiledOn) if err := parseFlags(); err != nil { zap.L().Error("Error while initializing", zap.Error(err)) return } + switch opts.Verbosity { + case 0: + loggerLevel.SetLevel(zap.WarnLevel) + case 1: + loggerLevel.SetLevel(zap.InfoLevel) + default: // Default: i.e. in case of 2 or more. + // TODO: print the caller (function)'s name and line number! + loggerLevel.SetLevel(zap.DebugLevel) + } + + zap.ReplaceGlobals(logger) + // Reload credentials when you receive SIGHUP sighupChan := make(chan os.Signal, 1) signal.Notify(sighupChan, syscall.SIGHUP) @@ -190,6 +206,8 @@ func parseFlags() error { Database string `short:"d" long:"database" description:"URL of the (magneticod) database"` Cred string `short:"c" long:"credentials" description:"Path to the credentials file"` NoAuth bool ` long:"no-auth" description:"Disables authorisation"` + + Verbose []bool `short:"v" long:"verbose" description:"Increases verbosity."` } if _, err := flags.Parse(&cmdFlags); err != nil { @@ -221,8 +239,6 @@ func parseFlags() error { opts.CredentialsPath = cmdFlags.Cred } - fmt.Printf("%v credpath %s\n", cmdFlags.NoAuth, opts.CredentialsPath) - if opts.CredentialsPath != "" { opts.Credentials = make(map[string][]byte) if err := loadCred(opts.CredentialsPath); err != nil { @@ -232,6 +248,8 @@ func parseFlags() error { opts.Credentials = nil } + opts.Verbosity = len(cmdFlags.Verbose) + return nil }