Skip to content

Commit

Permalink
2023.08.25
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1yan committed Aug 25, 2023
0 parents commit 785315d
Show file tree
Hide file tree
Showing 20 changed files with 794 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/Sowhp.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Sowhp
Empty file.
46 changes: 46 additions & 0 deletions concert/directoriescreat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package concert

import (
"Sowhp/concert/logger"
"fmt"
"os"
)

// MkdirResport 创建报告存放目录
func MkdirResport() {
// 获取当前目录地址
dir, err := os.Getwd()
if err != nil {
// panic(err)
logger.DebugError(err)
}
logger.Debug(fmt.Sprintf("当前路径:%s", dir))
Dir_mk(dir + "/result/")
logger.Debug("当前目录下创建 /result/ 目录成功!")

}

// GetPath 获取当前路径位置
func GetPath() string {
// 获取当前目录地址
dir, err := os.Getwd()
if err != nil {
// panic(err)
logger.DebugError(err)
}
return dir
}

// dir_mk 判断目录是否存在,若不存在则进行创建
func Dir_mk(path string) {
// 判断目录是否存在
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.Mkdir(path, 0777)
if err != nil {
// panic(err)
logger.DebugError(err)
}
logger.Debug(fmt.Sprintf("当前以创建好该目录:%s", path))
return
}
}
8 changes: 8 additions & 0 deletions concert/logger/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package logger

var (
LogLevel int // log等级,默认设置3级
NoColor bool // 是否开启log输出非颜色版设置
OutputFileName string // 用于设置log输出名称设置
NoSave bool // not save file // logsync.go 中设置不进行日志写入的设置, 注:在常规的logger中并没有设置该参数
)
12 changes: 12 additions & 0 deletions concert/logger/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package logger

type Level int

const (
LevelFatal Level = iota // 值 0
LevelError // 值 1
LevelInfo // 值 2
LevelWarning // 值 3
LevelDebug // 值 4
LevelVerbose // 值 5
)
184 changes: 184 additions & 0 deletions concert/logger/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package logger

import (
"fmt"
"github.com/gookit/color"
"os"
"path"
"regexp"
"runtime"
"strings"
"time"
)

var ( // 输出颜色设置
Red = color.Red.Render
Cyan = color.Cyan.Render
Yellow = color.Yellow.Render
White = color.White.Render
Blue = color.Blue.Render
Purple = color.Style{color.Magenta, color.OpBold}.Render
LightRed = color.Style{color.Red, color.OpBold}.Render
LightGreen = color.Style{color.Green, color.OpBold}.Render
LightWhite = color.Style{color.White, color.OpBold}.Render
LightCyan = color.Style{color.Cyan, color.OpBold}.Render
LightYellow = color.Style{color.Yellow, color.OpBold}.Render
//LightBlue = color.Style{color.Blue, color.OpBold}.Render
)

var (
defaultLevel = LevelWarning // 输出等级
noWrite int // 不进行写入log
)

// SetLevel 设置输出等级
func SetLevel(l Level) {
defaultLevel = l
}

func getCallerInfo(skip int) (info string) {
_, file, lineNo, ok := runtime.Caller(skip)
if !ok {
info = "runtime.Caller() failed"
}

// funcName := runtime.FuncForPC(pc).Name()
fileName := path.Base(file) // Base函数返回路径的最后一个元素
// return fmt.Sprintf("FuncName:%s, file:%s, line:%d", funcName, fileName, lineNo)
return fmt.Sprintf("%s line:%d", fileName, lineNo)
}

// log 日志输出效验判断
func log(l Level, w int, detail string) {
switch LogLevel { // 判断配置文件中的日志输出等级,并设置到日志等级
case 0:
SetLevel(0)
case 1:
SetLevel(1)
case 2:
SetLevel(2)
case 3:
SetLevel(3)
case 4:
SetLevel(4)
case 5:
SetLevel(5)
}

if l > defaultLevel { // 判断输入等级是否大于设置等级,若大于则当前日志则不进行输出
return
}
if NoColor { // 判断是否关闭颜色输出
fmt.Println(clean(detail))
return
} else {
fmt.Println(detail)
}

if noWrite == 0 {
// 目前只写入 info 信息 和 Success 的信息
strTrim := fmt.Sprintf("[%s] ", Cyan(getDate())) // 匹配log日志前面的日期
detail := strings.TrimPrefix(detail, strTrim) // 去除日期信息
writeLogFile(clean(detail), OutputFileName) // 写入到本地文件
}

if l == LevelFatal {
os.Exit(0)
}
}

// Fatal 严重级别日志 log等级:0
func Fatal(detail string) {
noWrite = 1
log(LevelFatal, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightRed("FATAL"), detail))
}

// Error 错误日志 log等级:1
func Error(detail string) {
noWrite = 1
log(LevelError, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightRed("ERROR"), detail))
}

// Info 消息日志 log等级:2
func Info(detail string) {
noWrite = 0
log(LevelInfo, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightGreen("INFO"), detail))
}

// Warning 告警日志 log等级:3
func Warning(detail string) {
noWrite = 1
log(LevelWarning, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightYellow("WARNING"), detail))
}

// Debug 调试日志 log等级:4
func Debug(detail string) {
noWrite = 1
log(LevelDebug, noWrite, fmt.Sprintf("[%s] [%s] [%s] %s", Cyan(getDate()), LightWhite("DEBUG"), Yellow(getCallerInfo(2)), detail))
}

// Verbose 详细调试信息日志 log等级:5
func Verbose(detail string) {
noWrite = 1
log(LevelVerbose, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightCyan("VERBOSE"), detail))
}

// Success 成功信息日志 log等级:2
func Success(detail string) {
noWrite = 0
log(LevelInfo, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightGreen("+"), detail))
}

// Failed 失败信息日志 log等级:2
func Failed(detail string) {
noWrite = 1
log(LevelInfo, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightRed("-"), detail))
}

// Common 普通信息日志 log等级:2
func Common(detail string) {
noWrite = 1
log(LevelInfo, noWrite, fmt.Sprintf("[%s] [%s] %s", Cyan(getDate()), LightGreen("*"), detail))
}

func getTime() string {
return time.Now().Format("15:04:05")
}

func getDate() string {
return time.Now().Format("2006.1.2")
}

func DebugError(err error) bool {
/* Processing error display */
if err != nil {
pc, _, line, _ := runtime.Caller(1)
Debug(fmt.Sprintf("%s%s%s",
White(runtime.FuncForPC(pc).Name()),
LightWhite(fmt.Sprintf(" line:%d ", line)),
White(err)))
return true
}
return false
}

// Clean by https://github.com/acarl005/stripansi/blob/master/stripansi.go
func clean(str string) string {
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var re = regexp.MustCompile(ansi)
return re.ReplaceAllString(str, "")
}

func writeLogFile(result string, filename string) {
var text = []byte(result + "\n")
fl, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Open %s error, %v\n", filename, err)
return
}
_, err = fl.Write(text)
fl.Close()
if err != nil {
fmt.Printf("Write %s error, %v\n", filename, err)
}
}
64 changes: 64 additions & 0 deletions concert/logger/logsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package logger

import (
"fmt"
"os"
"strings"
"sync"
"time"
)

var Num int64
var End int64
var Results = make(chan *string)
var Start = true
var LogSucTime int64
var LogErrTime int64
var WaitTime int64
var Silent bool
var LogWG sync.WaitGroup

func init() {
go SaveLog()
}

func LogSuccess(result string) {
LogWG.Add(1)
LogSucTime = time.Now().Unix()
Results <- &result
}

func SaveLog() {
for result := range Results {
if Silent == false || strings.Contains(*result, "[+]") || strings.Contains(*result, "[*]") {
fmt.Println(*result)
}
if !NoSave {
WriteFile(*result, OutputFileName)
}
LogWG.Done()
}
}

func WriteFile(result string, filename string) {
var text = []byte(result + "\n")
fl, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Open %s error, %v\n", filename, err)
return
}
_, err = fl.Write(text)
fl.Close()
if err != nil {
fmt.Printf("Write %s error, %v\n", filename, err)
}
}

func LogError(errinfo interface{}) {
if WaitTime == 0 {
fmt.Println(fmt.Sprintf(" %v/%v %v", End, Num, errinfo))
} else if (time.Now().Unix()-LogSucTime) > WaitTime && (time.Now().Unix()-LogErrTime) > WaitTime {
fmt.Println(fmt.Sprintf(" %v/%v %v", End, Num, errinfo))
LogErrTime = time.Now().Unix()
}
}
Loading

0 comments on commit 785315d

Please sign in to comment.