Set default logging level and allow event bus configuration

This commit is contained in:
Sarah Jamie Lewis 2021-03-17 12:03:25 -07:00
parent eb06bdd19a
commit 64aeaad23a
4 changed files with 43 additions and 7 deletions

22
lib.go
View File

@ -1,4 +1,5 @@
//package cwtch
package main
import "C"
@ -37,8 +38,8 @@ func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) {
}
func StartCwtch(appDir string, torPath string) {
log.SetLevel(log.LevelDebug)
//log.ExcludeFromPattern("service.go")
log.SetLevel(log.LevelInfo)
log.ExcludeFromPattern("service.go")
utils.InitGlobalSettingsFile(appDir, "be gay do crime")
@ -68,6 +69,7 @@ func StartCwtch(appDir string, torPath string) {
acnQueue = event.NewQueue()
newApp.GetPrimaryBus().Subscribe(event.ACNStatus, acnQueue)
newApp.GetPrimaryBus().Subscribe(utils.UpdateGlobalSettings, acnQueue)
newApp.GetPrimaryBus().Subscribe(utils.SetLoggingLevel, acnQueue)
eventHandler = utils.NewEventHandler(newApp)
@ -134,6 +136,22 @@ func SendAppEvent(eventJson string) {
}
log.Debugf("New Settings %v", globalSettings)
utils.WriteGlobalSettings(globalSettings)
case utils.SetLoggingLevel:
_, warn := new_event.Data[utils.Warn]
_, error := new_event.Data[utils.Error]
_, debug := new_event.Data[utils.Debug]
_, info := new_event.Data[utils.Info]
// Assign logging level in priority order. The highest logging level wins in the
// event of multiple fields.
if info {
log.SetLevel(log.LevelInfo)
} else if warn {
log.SetLevel(log.LevelWarn)
} else if error {
log.SetLevel(log.LevelError)
} else if debug {
log.SetLevel(log.LevelDebug)
}
default: // do nothing
}
}

View File

@ -1,8 +1,8 @@
package utils
type Contact struct {
Name string `json:"name"`
Onion string `json:"onion"`
Status string `json:"status"`
Picture string `json:"picture"`
Name string `json:"name"`
Onion string `json:"onion"`
Status string `json:"status"`
Picture string `json:"picture"`
}

View File

@ -106,7 +106,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
}
cpicPath := GetPicturePath(cpic)
contactInfo := profile.GetContact(contact)
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State, Picture: cpicPath,})
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State, Picture: cpicPath})
}
bytes, _ := json.Marshal(contacts)

18
utils/logging.go Normal file
View File

@ -0,0 +1,18 @@
package utils
import "cwtch.im/cwtch/event"
// An event to set the logging level dynamically from the UI
const (
SetLoggingLevel = event.Type("SetLoggingLevel")
)
// Logging Levels as Event Fields. Note: Unlike most event we don't cae about
// the *value* of the field, only the presence. If more than one of these fields is
// present in a single SetLoggingLevel event then the highest logging level is used. INFO < WARN < ERROR < DEBUG
const (
Warn = event.Field("Warn")
Error = event.Field("Error")
Debug = event.Field("Debug")
Info = event.Field("Info")
)