LAPP 打印服务 支持条码打印和表单打印 通过Socket或windows打印方式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.3 KiB

  1. package glog
  2. import (
  3. "bytes"
  4. "strings"
  5. "github.com/gookit/color"
  6. )
  7. var (
  8. // Info color style
  9. StyleInfo = &color.Style{color.OpReset, color.Green}
  10. StyleWarn = &color.Style{color.OpBold, color.Yellow}
  11. StyleError = &color.Style{color.OpBold, color.Red}
  12. StyleBlue = &color.Style{color.OpBold, color.Blue} //蓝色
  13. StyleMagenta = &color.Style{color.OpBold, color.Magenta} //品红
  14. StyleCyan = &color.Style{color.OpReset, color.Cyan} //青色
  15. )
  16. func min(args ...int) int {
  17. t := -1
  18. for _, arg := range args {
  19. if arg >= 0 && (arg < t || t == -1) {
  20. t = arg
  21. }
  22. }
  23. return t
  24. }
  25. var hiddenType = make(map[string]bool)
  26. func SetSubTypeHidden(subTypes ...string) {
  27. if len(subTypes) > 0 {
  28. for _, subType := range subTypes {
  29. hiddenType[subType] = true
  30. }
  31. }
  32. }
  33. func SetSubTypeShow(subTypes ...string) {
  34. if len(subTypes) > 0 {
  35. for _, subType := range subTypes {
  36. delete(hiddenType, subType)
  37. }
  38. }
  39. }
  40. // <i>content</i>: info
  41. // <w>content</w>: warn
  42. // <e>content</e>: error
  43. // <b>content</b>: blue
  44. // <m>content</m>: magenta
  45. // <c>content</c>: cyan
  46. func osStdoutWrite(s Severity, subType string, data []byte) {
  47. for st, _ := range hiddenType {
  48. if subType == st {
  49. return
  50. }
  51. }
  52. switch s {
  53. case DebugLog:
  54. StyleCyan.Print(subType + " ")
  55. StyleCyan.Print("DBG")
  56. case InfoLog:
  57. StyleInfo.Print(subType + " ")
  58. StyleInfo.Print("INF")
  59. case WarningLog:
  60. StyleWarn.Print(subType + " ")
  61. StyleWarn.Print("WAR")
  62. case ErrorLog:
  63. StyleError.Print(subType + " ")
  64. StyleError.Print("ERR")
  65. case FatalLog:
  66. StyleError.Print(subType + " ")
  67. StyleError.Print("FAT")
  68. case OtherLog:
  69. StyleCyan.Print(subType + " ")
  70. StyleCyan.Print("OTR")
  71. default:
  72. }
  73. ds := string(data)
  74. LABEL_LOOP:
  75. for {
  76. ie := strings.Index(ds, "<e>")
  77. iw := strings.Index(ds, "<w>")
  78. ii := strings.Index(ds, "<i>")
  79. ib := strings.Index(ds, "<b>")
  80. im := strings.Index(ds, "<m>")
  81. ic := strings.Index(ds, "<c>")
  82. i := min(ie, iw, ii, ib, im, ic)
  83. if i < 0 {
  84. break
  85. }
  86. je := strings.Index(ds, "</e>")
  87. jw := strings.Index(ds, "</w>")
  88. ji := strings.Index(ds, "</i>")
  89. jb := strings.Index(ds, "</b>")
  90. jm := strings.Index(ds, "</m>")
  91. jc := strings.Index(ds, "</c>")
  92. j := min(je, jw, ji, jb, jm, jc)
  93. if j < 0 || j < (i+3) {
  94. break
  95. }
  96. switch ds[i+1] {
  97. case 'i':
  98. color.Print(ds[:i])
  99. StyleInfo.Print(ds[i+3 : j])
  100. ds = ds[j+4:]
  101. case 'w':
  102. color.Print(ds[:i])
  103. StyleWarn.Print(ds[i+3 : j])
  104. ds = ds[j+4:]
  105. case 'e':
  106. color.Print(ds[:i])
  107. StyleError.Print(ds[i+3 : j])
  108. ds = ds[j+4:]
  109. case 'b':
  110. color.Print(ds[:i])
  111. StyleBlue.Print(ds[i+3 : j])
  112. ds = ds[j+4:]
  113. case 'm':
  114. color.Print(ds[:i])
  115. StyleMagenta.Print(ds[i+3 : j])
  116. ds = ds[j+4:]
  117. case 'c':
  118. color.Print(ds[:i])
  119. StyleCyan.Print(ds[i+3 : j])
  120. ds = ds[j+4:]
  121. default:
  122. break LABEL_LOOP
  123. }
  124. }
  125. color.Print(ds)
  126. }
  127. func osStdoutWriteNoStyle(data []byte) {
  128. color.Print(string(data))
  129. }
  130. func filterStdout(data []byte) []byte {
  131. data = bytes.Replace(data, []byte{'<', 'e', '>'}, nil, -1)
  132. data = bytes.Replace(data, []byte{'<', 'w', '>'}, nil, -1)
  133. data = bytes.Replace(data, []byte{'<', 'i', '>'}, nil, -1)
  134. data = bytes.Replace(data, []byte{'<', '/', 'e', '>'}, nil, -1)
  135. data = bytes.Replace(data, []byte{'<', '/', 'w', '>'}, nil, -1)
  136. data = bytes.Replace(data, []byte{'<', '/', 'i', '>'}, nil, -1)
  137. return data
  138. }