diff options
Diffstat (limited to 'matterbridge.go')
-rw-r--r-- | matterbridge.go | 73 |
1 files changed, 48 insertions, 25 deletions
diff --git a/matterbridge.go b/matterbridge.go index 0dac8af5..6c6b11fe 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -17,46 +17,69 @@ import ( var ( version = "1.14.0-dev" githash string + + flagConfig = flag.String("conf", "matterbridge.toml", "config file") + flagDebug = flag.Bool("debug", false, "enable debug") + flagVersion = flag.Bool("version", false, "show version") + flagGops = flag.Bool("gops", false, "enable gops agent") ) func main() { - logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: true}) - flog := logrus.WithFields(logrus.Fields{"prefix": "main"}) - flagConfig := flag.String("conf", "matterbridge.toml", "config file") - flagDebug := flag.Bool("debug", false, "enable debug") - flagVersion := flag.Bool("version", false, "show version") - flagGops := flag.Bool("gops", false, "enable gops agent") flag.Parse() + if *flagVersion { + fmt.Printf("version: %s %s\n", version, githash) + return + } + + rootLogger := setupLogger() + logger := rootLogger.WithFields(logrus.Fields{"prefix": "main"}) + if *flagGops { if err := agent.Listen(agent.Options{}); err != nil { - flog.Errorf("failed to start gops agent: %#v", err) + logger.Errorf("Failed to start gops agent: %#v", err) } else { defer agent.Close() } } - if *flagVersion { - fmt.Printf("version: %s %s\n", version, githash) - return - } - if *flagDebug || os.Getenv("DEBUG") == "1" { - logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true}) - flog.Info("Enabling debug") - logrus.SetLevel(logrus.DebugLevel) - } - flog.Printf("Running version %s %s", version, githash) + + logger.Printf("Running version %s %s", version, githash) if strings.Contains(version, "-dev") { - flog.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.") + logger.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.") } - cfg := config.NewConfig(*flagConfig) + + cfg := config.NewConfig(rootLogger, *flagConfig) cfg.BridgeValues().General.Debug = *flagDebug - r, err := gateway.NewRouter(cfg, bridgemap.FullMap) + + r, err := gateway.NewRouter(rootLogger, cfg, bridgemap.FullMap) if err != nil { - flog.Fatalf("Starting gateway failed: %s", err) + logger.Fatalf("Starting gateway failed: %s", err) } - err = r.Start() - if err != nil { - flog.Fatalf("Starting gateway failed: %s", err) + if err = r.Start(); err != nil { + logger.Fatalf("Starting gateway failed: %s", err) } - flog.Printf("Gateway(s) started succesfully. Now relaying messages") + logger.Printf("Gateway(s) started succesfully. Now relaying messages") select {} } + +func setupLogger() *logrus.Logger { + logger := &logrus.Logger{ + Out: os.Stdout, + Formatter: &prefixed.TextFormatter{ + PrefixPadding: 13, + DisableColors: true, + FullTimestamp: true, + }, + Level: logrus.InfoLevel, + } + if *flagDebug || os.Getenv("DEBUG") == "1" { + logger.Formatter = &prefixed.TextFormatter{ + PrefixPadding: 13, + DisableColors: true, + FullTimestamp: false, + ForceFormatting: true, + } + logger.Level = logrus.DebugLevel + logger.WithFields(logrus.Fields{"prefix": "main"}).Info("Enabling debug logging.") + } + return logger +} |