logging_log.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package fairwind
  2. import (
  3. "sync"
  4. "syscall/js"
  5. )
  6. type Log struct {
  7. mutex sync.Mutex
  8. formatter LogFormatter
  9. lines []LogLine
  10. }
  11. func NewLog(formatter LogFormatter) *Log {
  12. return &Log{
  13. formatter: formatter,
  14. lines: []LogLine{},
  15. }
  16. }
  17. func (this *Log) Debug(message string, parameters ...LoggingParameter) {
  18. this.log(SEVERITY_DEBUG, message, parameters...)
  19. }
  20. func (this *Log) Information(message string, parameters ...LoggingParameter) {
  21. this.log(SEVERITY_INFORMATION, message, parameters...)
  22. }
  23. func (this *Log) Warning(message string, parameters ...LoggingParameter) {
  24. this.log(SEVERITY_WARNING, message, parameters...)
  25. }
  26. func (this *Log) Error(message string, parameters ...LoggingParameter) {
  27. this.log(SEVERITY_ERROR, message, parameters...)
  28. }
  29. func (this *Log) Critical(message string, parameters ...LoggingParameter) {
  30. this.log(SEVERITY_CRITICAL, message, parameters...)
  31. }
  32. func (this *Log) log(severity int, message string, parameters ...LoggingParameter) {
  33. this.mutex.Lock()
  34. defer this.mutex.Unlock()
  35. // TODO: detect OS
  36. console := js.Global().Get("console")
  37. line, err := this.formatter.Format(
  38. *NewLogLine(severity, message, parameters),
  39. )
  40. if err != nil {
  41. // TODO: log error OS-specific
  42. return
  43. }
  44. console.Call("log", line)
  45. }