isolate debugging messages behind new -Verbose flag (argument, not in config file)

This commit is contained in:
mappu 2018-06-03 18:51:59 +12:00
parent 57724ad67c
commit 246d2f12c8
2 changed files with 16 additions and 6 deletions

View File

@ -27,6 +27,7 @@ type NTFServer struct {
configFile string
config NTFConfig
conns map[string]*libnmdc.HubConnection // hubnick -> hubconn
verbose bool
// Except the coalesce buffer, that requires a background worker.
coalesceBufferMut sync.Mutex
@ -43,12 +44,13 @@ type upstreamMessage struct {
evt libnmdc.HubEvent
}
func NewNTFServer(configFile string) (*NTFServer, error) {
func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) {
ret := NTFServer{
configFile: configFile,
hubMessages: make(chan upstreamMessage, 0),
conns: make(map[string]*libnmdc.HubConnection),
coalesceBuffer: make(map[string]time.Time),
verbose: verbose,
}
// Config
@ -76,7 +78,9 @@ func NewNTFServer(configFile string) (*NTFServer, error) {
ret.bot = bot
if ret.verbose {
bot.Debug = true
}
log.Printf("Connected to telegram as '%s'", bot.Self.UserName)
@ -263,6 +267,10 @@ func (this *NTFServer) Coalesce(hubNick, message string) bool {
}
func (this *NTFServer) HandleHubMessage(msg upstreamMessage) {
if this.verbose {
log.Printf("Hub: %#v", msg)
}
switch msg.evt.EventType {
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_CONNECTION_STATE_CHANGED:
@ -298,7 +306,6 @@ func (this *NTFServer) HandleHubMessage(msg upstreamMessage) {
log.Printf("Hub(%s): Unhandled(%d): %s", msg.hubNick, msg.evt.EventType, msg.evt.Message)
}
log.Printf("Hub: %#v", msg)
}
func (this *NTFServer) HandleMessage(update telegram.Update) {
@ -325,9 +332,11 @@ func (this *NTFServer) HandleMessage(update telegram.Update) {
log.Printf("Message from unknown chat %d (not our supergroup, not a PM, ...)", update.Message.Chat.ID)
}
if this.verbose {
fmt.Printf("%#v\n", update.Message)
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
}
}
// kickAndDrop deregisters an account and kicks them from the group chat

View File

@ -8,9 +8,10 @@ import (
func main() {
configFile := flag.String("ConfigFile", "config.json", "")
verbose := flag.Bool("Verbose", false, "")
flag.Parse()
svr, err := NewNTFServer(*configFile)
svr, err := NewNTFServer(*configFile, *verbose)
if err != nil {
log.Fatal(err.Error())
}