Skip to content

Commit

Permalink
implement part of tx
Browse files Browse the repository at this point in the history
  • Loading branch information
powerpook committed Dec 10, 2021
1 parent a5d382c commit fcbef35
Show file tree
Hide file tree
Showing 115 changed files with 9,276 additions and 9,364 deletions.
126 changes: 66 additions & 60 deletions packages/log/filename_hook.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,66 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package log

import (
"path"
"runtime"
"strings"
"time"

"github.com/IBAX-io/go-ibax/packages/conf"

"github.com/sirupsen/logrus"
)

// ContextHook storing nothing but behavior
type ContextHook struct{}

// Levels returns all log levels
func (hook ContextHook) Levels() []logrus.Level {
return logrus.AllLevels
}

// Fire the log entry
func (hook ContextHook) Fire(entry *logrus.Entry) error {
var pc []uintptr
if _, skip := entry.Data["nocontext"]; skip {
delete(entry.Data, "nocontext")
return nil
}
if conf.Config.Log.LogLevel == "DEBUG" {
pc = make([]uintptr, 15, 15)
} else {
pc = make([]uintptr, 4, 4)
}
cnt := runtime.Callers(6, pc)

count := 0
for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !strings.Contains(name, "github.com/sirupsen/logrus") {
file, line := fu.FileLine(pc[i] - 1)
if count == 0 {
entry.Data["file"] = path.Base(file)
entry.Data["func"] = path.Base(name)
entry.Data["line"] = line
entry.Data["time"] = time.Now().Format(time.RFC3339)
if conf.Config.Log.LogLevel != "DEBUG" {
break
entry.Data["from"] = append(entry.Data["from"].([]string), path.Base(name))
}
count += 1
}
}
return nil
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package log

import (
"path"
"runtime"
"strings"
"time"

"github.com/IBAX-io/go-ibax/packages/conf"

"github.com/sirupsen/logrus"
)

// ContextHook storing nothing but behavior
type ContextHook struct{}

// Levels returns all log levels
func (hook ContextHook) Levels() []logrus.Level {
return logrus.AllLevels
}

// Fire the log entry
func (hook ContextHook) Fire(entry *logrus.Entry) error {
var pc []uintptr
if _, skip := entry.Data["nocontext"]; skip {
delete(entry.Data, "nocontext")
return nil
}
if conf.Config.Log.LogLevel == "DEBUG" {
pc = make([]uintptr, 15, 15)
} else {
pc = make([]uintptr, 4, 4)
}
cnt := runtime.Callers(6, pc)

count := 0
for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !strings.Contains(name, "github.com/sirupsen/logrus") {
file, line := fu.FileLine(pc[i] - 1)
if count == 0 {
entry.Data["file"] = path.Base(file)
entry.Data["func"] = path.Base(name)
entry.Data["line"] = line
entry.Data["time"] = time.Now().Format(time.RFC3339)
if conf.Config.Log.LogLevel != "DEBUG" {
break
}
}
if count >= 1 {
if count == 1 {
entry.Data["from"] = []string{}
}
entry.Data["from"] = append(entry.Data["from"].([]string), path.Base(name))
}
count += 1
}
}
return nil
}
51 changes: 29 additions & 22 deletions packages/log/hex_hook.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package log


// Levels returns all log levels
func (hook HexHook) Levels() []logrus.Level {
return logrus.AllLevels
}

// Fire the log entry
func (hook HexHook) Fire(entry *logrus.Entry) error {
for i := range entry.Data {
if b, ok := entry.Data[i].([]byte); ok {
entry.Data[i] = hex.EncodeToString(b)
}
}
return nil
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package log

import (
"encoding/hex"

"github.com/sirupsen/logrus"
)

type HexHook struct{}

// Levels returns all log levels
func (hook HexHook) Levels() []logrus.Level {
return logrus.AllLevels
}

// Fire the log entry
func (hook HexHook) Fire(entry *logrus.Entry) error {
for i := range entry.Data {
if b, ok := entry.Data[i].([]byte); ok {
entry.Data[i] = hex.EncodeToString(b)
}
}
return nil
}
Loading

0 comments on commit fcbef35

Please sign in to comment.