añadido la capacidad de visualizarlos con colores

master
DEV 3 years ago
parent 08d8cb8eb2
commit cf8a09df72

@ -26,8 +26,10 @@ package errs
import (
"fmt"
"reflect"
"strconv"
"sync"
"time"
"typex"
)
@ -38,7 +40,11 @@ type (
FmtText struct {
FmtTextAtt
dateLayout string
fixName int
fieldSep string
indent string
fmtField string
traceFmt func(s *FmtTextState, t StackTrace)
providers sync.Map
}
@ -50,11 +56,22 @@ type (
func NewFmtText(options ...FmtTextFnc) *FmtText {
return applyFmtTextOptions(&FmtText{
fm := applyFmtTextOptions(&FmtText{
FmtTextAtt: FmtTextAttIndent | FmtTextAttVerbose | FmtTextAttTrace | FmtTextAttColors,
dateLayout: time.RFC822Z,
indent : " ",
fixName : 12,
fieldSep : " : ",
traceFmt : formatStackTrace ,
}, options...) }
}, options...)
if fm.fixName > 0 {
fm.fmtField = "%-" + strconv.Itoa(fm.fixName) + "s"
} else {
fm.fmtField = "%s"
}
return fm
}
@ -93,13 +110,17 @@ func (f *FmtText) Format (err error) []byte {
func (f *FmtText) formatAll (s *FmtTextState, err error) {
s.WriteFieldString("error",err.Error())
s.WriteFieldString("error-type",reflect.TypeOf(err).String())
ls := UnwrapAll(err)
for _,e := range ls {
f.format(s,e)
}
if c := UnwrapCause(err); c != nil {
s.WriteString("\n")
s.writeString("-Previous-----------------------------------------\n",[]FmtTextColor{FmtTextColorCyan})
s.IncLevel()
if f.Verbose() {
f.formatAll(s, c.Cause())
@ -108,14 +129,15 @@ func (f *FmtText) formatAll (s *FmtTextState, err error) {
}
s.DecLevel()
}
}
func (f *FmtText) format (s *FmtTextState, err error) {
rt := reflect.TypeOf(err)
//s.WriteFieldString("error",err.Error())
//s.WriteFieldString("error-type",rt.String())
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
err = reflect.ValueOf(err).Elem().Interface().(error)
@ -180,19 +202,19 @@ func (f *FmtText) buildFmtTextProvider (rtype reflect.Type) FmtTextProvider {
func formatErrCode (s *FmtTextState, e error) {
s.WriteFieldString("error-code",e.(ErrCode).Code())
//s.WriteFieldString("error-code",e.(ErrCode).Code())
}
func formatErrPanic (s *FmtTextState, e error ){
s.WriteFieldString("error-panic",e.(ErrPanic).Error())
//s.WriteFieldString("error-panic",e.(ErrPanic).Error())
}
func formatErrTrace (s *FmtTextState, e error) {
s.WriteFieldString("error-Trace",e.(ErrTrace).Error())
//s.WriteFieldString("error-Trace",e.(ErrTrace).Error())
}
func formatErrCause (s *FmtTextState, e error) {
s.WriteFieldString("error-cause",e.(ErrCause).Error())
//s.WriteFieldString("error-cause",e.(ErrCause).Error())
}
func formatErrInfo (s *FmtTextState, e error) {
@ -200,7 +222,9 @@ func formatErrInfo (s *FmtTextState, e error) {
s.WriteFieldString("message" ,ei.Message())
s.WriteFieldString("info" ,ei.Info ())
if s.Level() == 0 {
s.WriteFieldString( "date-time", ei.Date().Format(time.RFC822Z))
s.WriteFieldTime("date-time",ei.Date(),FmtTextColorRed,FmtTextColorMagenta)
s.WriteFieldTime("date-time",ei.Date(),FmtTextColorGreen)
s.WriteFieldTime("date-time",ei.Date())
ei.MapMetas(func(n string, v any) bool {
s.WriteFieldString(n,fmt.Sprintf("%v",v))
return true
@ -209,25 +233,28 @@ func formatErrInfo (s *FmtTextState, e error) {
}
func formatErrAny (s *FmtTextState, err error) {
s.WriteFieldString("error",err.Error())
//s.WriteFieldString("error",err.Error())
}
func formatStackTrace(s *FmtTextState, t StackTrace) {
for _,f := range t.Frames() {
s.Buffer.WriteString(f.Package)
s.Buffer.WriteByte(':')
s.Buffer.WriteString(f.FuncName)
s.Buffer.WriteByte('\n')
s.Buffer.WriteByte('\t')
s.Buffer.WriteString(f.File)
s.Buffer.WriteByte(':')
s.Buffer.WriteString(fmt.Sprintf("%d\n",f.LineNumber))
s.WriteString(f.Package+"."+f.FuncName,FmtTextColorYellow)
s.WriteString(fmt.Sprintf(" %.90s:%d",f.File,f.LineNumber),FmtTextColorBlue)
if sc,er := f.SourceLine(); er == nil {
s.Buffer.WriteString(ColorMagenta.Sprint(sc))
//s.Buffer.WriteString(fmt.Sprintf("\t\x1b[%dm%s\x1b[0m\b\n", uint8(Green), sc))
//s.Buffer.WriteByte('\t')
//s.Buffer.WriteString(sc)
//s.Buffer.WriteByte('\n')
if len(sc) > 80 {
ls := typex.SplitString(sc,80)
for _,l := range ls {
FmtTextColorMagenta.BeginString()
s.Buffer.WriteString(l)
s.Buffer.WriteByte('\n')
FmtTextColorMagenta.EndString()
}
continue
}
s.Buffer.WriteString(" ")
s.Buffer.WriteString(FmtTextColorMagenta.Sprintf(sc))
s.Buffer.WriteByte('\n')
}
}

@ -0,0 +1,60 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name color.go
// Created by DEV
// Modified 27/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package errs
import "fmt"
const (
FmtTextColorBlack FmtTextColor = iota + 30
FmtTextColorRed
FmtTextColorGreen
FmtTextColorYellow
FmtTextColorBlue
FmtTextColorMagenta
FmtTextColorCyan
FmtTextColorWhite
)
type (
FmtTextColor uint8
)
func (c FmtTextColor) BeginString () string {
return fmt.Sprintf("\u001B[%dm",uint8(c))
}
func (_ FmtTextColor) EndString () string {
return "\x1b[0m" }
func (c FmtTextColor) Sprintf (fm string, params ...any) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) }
func (c FmtTextColor) IsValid() bool {
return c >= FmtTextColorBlack && c <= FmtTextColorWhite }

@ -33,3 +33,6 @@ func applyFmtTextOptions (fmt *FmtText, options ...FmtTextFnc) *FmtText {
}
func FmtTextWithDateFormat (layout string) FmtTextFnc {
return func(f *FmtText) {
f.dateLayout = layout } }

@ -26,7 +26,9 @@ package errs
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
)
type (
@ -67,15 +69,72 @@ func (s *FmtTextState) DecLevel() {
s.level-- }
func (s *FmtTextState) WriteString (str string) {
s.Buffer.WriteString(fmt.Sprintf("%s%s\n",s.buildIndent(),str))
}
func (s *FmtTextState) WriteFieldString (name, value string) {
func (s *FmtTextState) WriteString (str string, color ...FmtTextColor) {
s.writeString(fmt.Sprintf("%s%s\n",s.buildIndent(),str),color) }
func (s *FmtTextState) WriteFieldString (name, value string, color ...FmtTextColor) {
if s.fmt.IgnoreEmpties() && len(value) == 0 {
return
}
s.Buffer.WriteString(fmt.Sprintf("%s%-10s: %s\n",s.buildIndent(),name,value))
s.writeField(name,value,color)
}
func (s *FmtTextState) WriteFieldInt (name string, num int, color ...FmtTextColor) {
s.writeField(name,strconv.Itoa(num),color) }
func (s *FmtTextState) WriteFieldFloat(name string, num float64, dec int, color ...FmtTextColor) {
f := "%-10s: %f\n"
if dec < 0 {
f = "%-10s: %." + strconv.Itoa(dec) + "f\n"
}
s.writeField(name,fmt.Sprintf(f,num),color)
}
func (s *FmtTextState) WriteFieldTime (name string, date time.Time, color ...FmtTextColor) {
s.writeField(name,date.Format(s.fmt.dateLayout),color) }
func (s *FmtTextState) writeField (name, value string, color []FmtTextColor) {
s.Buffer.WriteString(s.buildIndent())
if len(color) == 0 || !color[0].IsValid() {
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField, name))
s.Buffer.WriteString(s.fmt.fieldSep)
s.Buffer.WriteString(value)
s.Buffer.WriteString("\n")
return
}
c1 := color[0]
s.Buffer.WriteString(c1.BeginString())
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField,name))
if len(color) == 1 || !color[1].IsValid() {
s.Buffer.WriteString(s.fmt.fieldSep)
s.Buffer.WriteString(value)
s.Buffer.WriteString(c1.EndString())
} else {
s.Buffer.WriteString(c1.EndString())
s.Buffer.WriteString(s.fmt.fieldSep)
s.Buffer.WriteString(color[1].BeginString())
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField,value))
s.Buffer.WriteString(color[1].EndString())
}
s.Buffer.WriteString("\n")
}
func (s *FmtTextState) writeString (str string, color []FmtTextColor) {
if len(color) == 0 || !color[0].IsValid() {
s.Buffer.WriteString(str)
return
}
s.Buffer.WriteString(color[0].BeginString())
s.Buffer.WriteString(str)
s.Buffer.WriteString(color[0].EndString())
}

@ -0,0 +1,3 @@
module typex
go 1.22

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name color.go
// File name string.go
// Created by DEV
// Modified 27/03/2024
// Modified 29/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,27 +21,17 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package typex
import "fmt"
const (
ColorBlack TextColor = iota + 30
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
func SplitString (source string, size int) []string {
var lines []string
for i := 0; i < len(source); i += size {
line := source[i:i+size]
lines = append(lines, line)
}
return lines
}
type (
TextColor uint8
)
func (c TextColor) Sprint (fm string, params ...any) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) }

@ -10,6 +10,7 @@ use (
./core/syncs
./core/timers
./core/ctxs
./core/typex
./app

Loading…
Cancel
Save

Powered by TurnKey Linux.