github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/log/event.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package log
     4  
     5  import (
     6  	"runtime/debug"
     7  
     8  	"../protocol"
     9  	"../time/unix"
    10  )
    11  
    12  func NewEvent(level protocol.LogType, domian, message string) (event *Event) {
    13  	return &Event{
    14  		level:   level,
    15  		time:    unix.Now(),
    16  		domain:  domian,
    17  		message: message,
    18  		stack:   nil,
    19  	}
    20  }
    21  
    22  func TraceEvent(level protocol.LogType, domian, message string) (event *Event) {
    23  	return &Event{
    24  		level:   level,
    25  		time:    unix.Now(),
    26  		domain:  domian,
    27  		message: message,
    28  		stack:   debug.Stack(),
    29  	}
    30  }
    31  
    32  func InfoEvent(domian, message string) (event *Event) {
    33  	return &Event{
    34  		level:   protocol.LogEvent_Information,
    35  		time:    unix.Now(),
    36  		domain:  domian,
    37  		message: message,
    38  		stack:   nil,
    39  	}
    40  }
    41  
    42  func NoticeEvent(domian, message string) (event *Event) {
    43  	return &Event{
    44  		level:   protocol.LogEvent_Notice,
    45  		time:    unix.Now(),
    46  		domain:  domian,
    47  		message: message,
    48  		stack:   nil,
    49  	}
    50  }
    51  
    52  func DebugEvent(domian, message string) (event *Event) {
    53  	return &Event{
    54  		level:   protocol.LogEvent_Debug,
    55  		time:    unix.Now(),
    56  		domain:  domian,
    57  		message: message,
    58  		stack:   nil,
    59  	}
    60  }
    61  
    62  func DeepDebugEvent(domian, message string) (event *Event) {
    63  	return &Event{
    64  		level:   protocol.LogEvent_DeepDebug,
    65  		time:    unix.Now(),
    66  		domain:  domian,
    67  		message: message,
    68  		stack:   nil,
    69  	}
    70  }
    71  
    72  func WarnEvent(domian, message string) (event *Event) {
    73  	return &Event{
    74  		level:   protocol.LogEvent_Warning,
    75  		time:    unix.Now(),
    76  		domain:  domian,
    77  		message: message,
    78  		stack:   nil,
    79  	}
    80  }
    81  
    82  // FatalEvent return new event with panic level and added stack trace.
    83  func PanicEvent(domian, message string) (event *Event) {
    84  	return &Event{
    85  		level:   protocol.LogEvent_Panic,
    86  		time:    unix.Now(),
    87  		domain:  domian,
    88  		message: message,
    89  		stack:   debug.Stack(),
    90  	}
    91  }
    92  
    93  // FatalEvent return new event with fatal level and added stack trace.
    94  func FatalEvent(domian, message string) (event *Event) {
    95  	return &Event{
    96  		level:   protocol.LogEvent_Fatal,
    97  		time:    unix.Now(),
    98  		domain:  domian,
    99  		message: message,
   100  		stack:   debug.Stack(),
   101  	}
   102  }
   103  
   104  // ConfEvent return new event with "Confidential" level
   105  func ConfEvent(domian, message string) (event *Event) {
   106  	return &Event{
   107  		level:   protocol.LogEvent_Confidential,
   108  		time:    unix.Now(),
   109  		domain:  domian,
   110  		message: message,
   111  		stack:   nil,
   112  	}
   113  }
   114  
   115  // Event implement protocol.LogEvent
   116  type Event struct {
   117  	level   protocol.LogType
   118  	time    unix.Time
   119  	domain  string
   120  	message string
   121  	stack   []byte
   122  }
   123  
   124  func (e *Event) MainType() protocol.EventMainType { return protocol.EventMainType_Log }
   125  func (e *Event) SubType() protocol.EventSubType   { return protocol.EventSubType(e.level) }
   126  func (e *Event) Cancelable() bool                 { return false }
   127  func (e *Event) DefaultPrevented() bool           { return false }
   128  func (e *Event) Bubbles() bool                    { return false }
   129  func (e *Event) PreventDefault()                  {}
   130  
   131  func (e *Event) Level() protocol.LogType { return e.level }
   132  func (e *Event) Time() protocol.Time     { return &e.time }
   133  func (e *Event) Domain() string          { return e.domain }
   134  func (e *Event) Message() string         { return e.message }
   135  func (e *Event) Stack() []byte           { return e.stack }
   136  
   137  /*
   138  	-- protocol.Syllab interface Encoder & Decoder --
   139  */
   140  func (e *Event) CheckSyllab(payload []byte) (err protocol.Error) {
   141  	if len(payload) < int(e.LenOfSyllabStack()) {
   142  		// err = syllab.ErrShortArrayDecode
   143  	}
   144  	return
   145  }
   146  func (e *Event) FromSyllab(payload []byte, stackIndex uint32) {
   147  }
   148  func (e *Event) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   149  	return
   150  }
   151  func (e *Event) LenAsSyllab() uint64      { return uint64(e.LenOfSyllabStack() + e.LenOfSyllabHeap()) }
   152  func (e *Event) LenOfSyllabStack() uint32 { return 33 }
   153  func (e *Event) LenOfSyllabHeap() (ln uint32) {
   154  	return uint32(len(e.domain) + len(e.stack) + len(e.message))
   155  }