Forráskód Böngészése

Copy Separated Console Backend from yaml_util.

ZRY 1 éve
szülő
commit
3f2c8a419f

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 9 - 0
.idea/GoHiedaLogger.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 6 - 0
.idea/markdown.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="MarkdownSettings">
+    <option name="hideErrorsInCodeBlocks" value="true" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/GoHiedaLogger.iml" filepath="$PROJECT_DIR$/.idea/GoHiedaLogger.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 8 - 0
hiedabke_console/go.mod

@@ -0,0 +1,8 @@
+module git.swzry.com/zry/GoHiedaLogger/hiedabke_console
+
+go 1.19
+
+require (
+	git.swzry.com/zry/GoHiedaLogger/hiedalog v0.0.0-20221108101125-31cb2911c8e3
+	github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
+)

+ 4 - 0
hiedabke_console/go.sum

@@ -0,0 +1,4 @@
+git.swzry.com/zry/GoHiedaLogger/hiedalog v0.0.0-20221108101125-31cb2911c8e3 h1:q8mNtC3oo+RRmIFZAUENv/bM1BojmWknidk6Xx/ZSvM=
+git.swzry.com/zry/GoHiedaLogger/hiedalog v0.0.0-20221108101125-31cb2911c8e3/go.mod h1:NMU7558kNXCUuK0qKYQMtYK/kn2lhwelnij295H3pdU=
+github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31 h1:OXcKh35JaYsGMRzpvFkLv/MEyPuL49CThT1pZ8aSml4=
+github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=

+ 80 - 0
hiedabke_console/hiedabke_console.go

@@ -0,0 +1,80 @@
+package hieda_yamlutil
+
+import (
+	"fmt"
+	"git.swzry.com/zry/GoHiedaLogger/hiedalog"
+	"github.com/ttacon/chalk"
+	"io"
+	"strings"
+)
+
+type ConsoleBackend struct {
+	wr            io.Writer
+	s_fc_json_key func(string) string
+	s_fc_json_val func(string) string
+	s_lvd         []func(string) string
+}
+
+func NewConsoleBackend(wr io.Writer) *ConsoleBackend {
+	scb := &ConsoleBackend{
+		wr:            wr,
+		s_fc_json_key: chalk.White.NewStyle().WithTextStyle(chalk.Bold).Style,
+		s_fc_json_val: chalk.White.NewStyle().WithTextStyle(chalk.Italic).WithTextStyle(chalk.Underline).Style,
+		s_lvd: []func(string) string{
+			chalk.Red.NewStyle().WithTextStyle(chalk.Bold).WithTextStyle(chalk.Underline).Style,
+			chalk.Red.NewStyle().WithTextStyle(chalk.Bold).Style,
+			chalk.Red.NewStyle().Style,
+			chalk.Yellow.NewStyle().Style,
+			chalk.Green.NewStyle().Style,
+			chalk.Cyan.NewStyle().Style,
+			chalk.Blue.NewStyle().Style,
+		},
+	}
+	return scb
+}
+
+func (b *ConsoleBackend) EmitStringLog(module string, level hiedalog.HiedaLogLevel, content string) {
+	var lv string
+	if level.LevelNumber < 7 {
+		lv = b.s_lvd[level.LevelNumber](level.Name)
+	} else {
+		lv = level.Name
+	}
+	_, _ = fmt.Fprintf(b.wr, "<%s> [%s] %s\n", lv, module, content)
+}
+
+func (b *ConsoleBackend) EmitComplexLog(module string, level hiedalog.HiedaLogLevel, data map[string]string) {
+	b.EmitStringLog(module, level, b.prettyComplexToString(data))
+}
+
+func (b *ConsoleBackend) prettyComplexToString(data map[string]string) string {
+	sb := strings.Builder{}
+	scn := 0
+	xr := false
+	if len(data) < 5 {
+		for k, v := range data {
+			scn += len(k) + len(v) + 2
+			if scn > 160 {
+				xr = true
+				break
+			}
+			sb.WriteString(b.s_fc_json_key(k))
+			sb.WriteRune(':')
+			sb.WriteString(b.s_fc_json_val(v))
+			sb.WriteString(", ")
+		}
+		if !xr {
+			so := sb.String()
+			return so[:len(so)-2]
+		}
+	}
+	sb = strings.Builder{}
+	sb.WriteRune('\n')
+	for k, v := range data {
+		sb.WriteString(b.s_fc_json_key(k))
+		sb.WriteRune(':')
+		sb.WriteString(b.s_fc_json_val(v))
+		sb.WriteRune('\n')
+	}
+	return sb.String()
+}