ju

package
v0.0.0-...-ebebc20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package ju log 相关函数,用于打印调试信息,信息自动包含时间和在源文件的调用位置,在 IDEA 的输出窗口, 可以通过点击输出窗口迅速定位到编辑器的代码行。 可以设置日志保存到数据库,只需要在 SetLogParam 函数传入一个数据库实例。 To 版本的函数支持保存到不同的表,输出特性和没有 To 的函数相同 LogF 版本的函数可以格式化日志信息 log 字段

Index

Constants

View Source
const (
	FormatDateTime = "2006-01-02 15:04:05"
	FormatDate     = "2006-01-02"
)
View Source
const (
	DbTypeMysql  = 0
	DbTypeSqlite = 1
)

指明数据库的类型,目前支持Mysq和SQLite两种,其它数据库可能运行不正常,未测试

View Source
const (
	TextBlack = iota + 30
	TextRed
	TextGreen
	TextYellow
	TextBlue
	TextMagenta
	TextCyan
	TextWhite
)

这些颜色在某些 console 窗口可能并不起作用

Variables

View Source
var FilterIgnoreDiction = map[string]bool{}

FilterIgnoreDiction 过滤字典,有些日志不需要,统一过滤掉。规则是如果一个日志字串包含字典里的任何一个关键字,都会被忽略掉

Functions

func CRC16

func CRC16(data []byte) uint16

CRC16 函数返回源数据的 16 位校验码

func CRC16Append

func CRC16Append(data []byte) []byte

CRC16Append 直接把 2 字节的校验码添加到源数据末尾

func CRC16Bytes

func CRC16Bytes(data []byte) []byte

CRC16Bytes 函数返回字节形式的 16 位校验码

func CRC16Check

func CRC16Check(data []byte) bool

CRC16Check 校验CRC16,末尾2字节必须包含校验码

func CheckErrTrace

func CheckErrTrace(err error, trace uint) bool

CheckErrTrace trace = 1 记录使用这个函数的位置,trace = 2 记录上一级

func CheckError

func CheckError(err error) bool

func ClearLog

func ClearLog()

ClearLog 清空数据库中的所有记录

func ClearLogTo

func ClearLogTo(table string)

ClearLogTo 清空指定日志数据库中的所有记录

func CreateFolder

func CreateFolder(folder string) error

CreateFolder 生成路径指定的文件夹,包括所有上级文件夹

func DeleteLog

func DeleteLog(idStart, idStop int64) int64

DeleteLog 删除默认的日志记录,idStart 到 idStop 的log都会被删除,包含这两个 id,要删除一个id 设置 idStart = id = idStop

func DeleteLogTo

func DeleteLogTo(table string, idStart, idStop int64) int64

DeleteLogTo 删除指定表的日志记录,idStart 到 idStop 的log都会被删除,包含这两个 id,要删除一个id 设置 idStart = id = idStop

func Exec

func Exec(sqlCase string, v ...interface{}) (sql.Result, error)

func FileExist

func FileExist(path string) bool

func GbkToUtf8

func GbkToUtf8(s []byte) (string, error)

GbkToUtf8 函数把 GBK 编码的字节数据转换为 UTF8 编码的字串,对于合法的 GBK 字节数据,这个函数不会失败, 但是如果源数据含有不能正常转换的数据,函数会反应一个错误和一个空字串。

func GetMysqlDb

func GetMysqlDb() *sql.DB

func GetNowDate

func GetNowDate() string

func GetTrace

func GetTrace(trace int) []string

GetTrace trace: 获取堆栈层数,default is 3

func HexParse

func HexParse(h string) []byte

HexParse 解析 Hex 字串

func HexString

func HexString(v interface{}) (str string)

HexString v 可以是某种整数类型或者字串,或者字节数组

func HttpGet

func HttpGet(url string) []byte

func HttpPostBody

func HttpPostBody(api string, data []byte) ([]byte, error)

func HttpPostForm

func HttpPostForm(api string, params map[string]string) ([]byte, error)

func IntToBytes

func IntToBytes(v interface{}) (buf []byte)

IntToBytes v 必须是某种 int 类型

func JsonLoadConf

func JsonLoadConf(fn string, conf interface{}) bool

func JsonParseBytes

func JsonParseBytes(src []byte, pobj interface{}) bool

func JsonParseString

func JsonParseString(src string, pobj interface{}) bool

func JsonSaveConf

func JsonSaveConf(fn string, conf interface{}) bool

func JsonToBytes

func JsonToBytes(obj interface{}, format bool) []byte

func JsonToString

func JsonToString(obj interface{}, format bool) string

func LogBlack

func LogBlack(v ...interface{})

func LogBlackTo

func LogBlackTo(tabId string, v ...interface{})

func LogBlue

func LogBlue(v ...interface{})

func LogBlueTo

func LogBlueTo(tabId string, v ...interface{})

func LogCyan

func LogCyan(v ...interface{})

func LogCyanTo

func LogCyanTo(tabId string, v ...interface{})

func LogFBlack

func LogFBlack(fs string, v ...interface{})

func LogFBlackTo

func LogFBlackTo(table, fs string, v ...interface{})

func LogFBlue

func LogFBlue(fs string, v ...interface{})

func LogFBlueTo

func LogFBlueTo(table, fs string, v ...interface{})

func LogFCyan

func LogFCyan(fs string, v ...interface{})

func LogFCyanTo

func LogFCyanTo(table, fs string, v ...interface{})

func LogFGreen

func LogFGreen(fs string, v ...interface{})

func LogFGreenTo

func LogFGreenTo(table, fs string, v ...interface{})

func LogFMagenta

func LogFMagenta(fs string, v ...interface{})

func LogFMagentaTo

func LogFMagentaTo(table, fs string, v ...interface{})

func LogFRed

func LogFRed(fs string, v ...interface{})

func LogFRedTo

func LogFRedTo(table, fs string, v ...interface{})

func LogFWhite

func LogFWhite(fs string, v ...interface{})

func LogFWhiteTo

func LogFWhiteTo(table, fs string, v ...interface{})

func LogFYellow

func LogFYellow(fs string, v ...interface{})

func LogFYellowTo

func LogFYellowTo(table, fs string, v ...interface{})

func LogGreen

func LogGreen(v ...interface{})

func LogGreenTo

func LogGreenTo(tabId string, v ...interface{})

func LogMagenta

func LogMagenta(v ...interface{})

func LogMagentaTo

func LogMagentaTo(tabId string, v ...interface{})

func LogRed

func LogRed(v ...interface{})

func LogRedTo

func LogRedTo(tabId string, v ...interface{})

func LogTrace

func LogTrace(color int, trace uint, v ...interface{})

LogTrace 打印调用堆栈信息,一般用于追踪函数调用 color: 颜色 trace: 0, 则记录当前位置,1 是上级函数调用位置,依次类推

func LogWhite

func LogWhite(v ...interface{})

func LogWhiteTo

func LogWhiteTo(tabId string, v ...interface{})

func LogYellow

func LogYellow(v ...interface{})

func LogYellowTo

func LogYellowTo(tabId string, v ...interface{})

func OpenMysql

func OpenMysql(user, pass, host, dbname string)

func OpenSqlite

func OpenSqlite(path, dbname string) (*sql.DB, error)

func OsIsWin

func OsIsWin() bool

func OutputColor

func OutputColor(color int, v ...interface{})

func Query

func Query(sqlCase string, qc QueryCall, v ...interface{}) error

Query 查询需要返回数据的语句,数据从回调函数的 rows 里获取,无需执行 rows 的 Close 函数, 这个设计的目的是减少遗忘 Close 的可能,因为遗忘 Close 不会对程序有立即的影响,直到 Mysql 资源被耗尽,对于海量的查询语句来说,定位哪里忘记 Close 是非常困难的。

func Rand58String

func Rand58String(n int) string

Rand58String 返回一个随机字串,使用不容易混淆的58个字符

func RandString

func RandString(length int, base string) string

func ReadFile

func ReadFile(fn string) []byte

func SaveFile

func SaveFile(fn string, data []byte) (err error)

SaveFile 函数保存数据到指定文件,要求目标目录必须存在,否则失败。目标文件如果已经存在,会被新数据覆盖

func SaveFileAndFolder

func SaveFileAndFolder(fn string, data []byte) (err error)

SaveFileAndFolder 函数会自动创建目标文件所在的目录文件夹,如果目标文件已经存在,数据会被覆盖, 此函数和 SaveFileToFolder 的区别是无需分开传输目录和文件参数

func SaveFileExistFail

func SaveFileExistFail(fn string, data []byte) (err error)

SaveFileExistFail 防止同名文件被覆盖,只有目标文件不存在才会成功

func SaveFileToFolder

func SaveFileToFolder(folder, fn string, data []byte) (err error)

SaveFileToFolder 函数会自动创建目标文件所在的目录文件夹,如果目标文件已经存在,数据会被覆盖

func SaveFileToFolderExistFail

func SaveFileToFolderExistFail(folder, fn string, data []byte) (err error)

SaveFileToFolderExistFail 保存到文件,并且生成所有上级文件夹(如果不存在),但是如果文件存在,函数失败, 用于防止已经存在的文件被覆盖

func SaveLogTo

func SaveLogTo(tabName, log, trace string, color int)

func SaveLogsTo

func SaveLogsTo(table string, logs []*LogInfo)

SaveLogsTo 一次保存多条日志数据到数据库,使用批量语句优化数据库性能

func SetLogParam

func SetLogParam(param LogParam)

SetLogParam Set log parameter

func TcpConnect

func TcpConnect(ip string) (net.Conn, error)

func TcpStartServer

func TcpStartServer(port string, cb TcpOnConnect)

func TestUG

func TestUG()

func UGEncode

func UGEncode(s string, success UGSuccessProc, failed UGFailedProc)

UGEncode 和 Utf8ToGbk 功能相同,转换 utf8 为 gbk,但是这函数会逐字符转换, 成功调用 success 回调,失败调用 failed 回调。

func Utf8ToGbk

func Utf8ToGbk(s string) ([]byte, error)

Utf8ToGbk 函数转换 Utf8 编码的字串(golang 字串内存编码)为 GBK 编码数据, 如果源字串 s 包含无法用 GBK 编码表示的字符,则函数会失败,此时返回的数据为一个长度为 0 的字节数组,但是不是 nil。

Types

type JsonArray

type JsonArray []interface{}

func NewJsonArray

func NewJsonArray() JsonArray

func (*JsonArray) Marshal

func (ja *JsonArray) Marshal(v interface{})

func (*JsonArray) ParseBytes

func (ja *JsonArray) ParseBytes(b []byte) error

func (*JsonArray) ParseString

func (ja *JsonArray) ParseString(str string) error

func (*JsonArray) ToBytes

func (ja *JsonArray) ToBytes() []byte

func (*JsonArray) ToString

func (ja *JsonArray) ToString() string

func (*JsonArray) UnMarshal

func (ja *JsonArray) UnMarshal(st interface{}) error

type JsonObject

type JsonObject map[string]interface{}

func NewJsonObject

func NewJsonObject() JsonObject

func (*JsonObject) Clear

func (js *JsonObject) Clear()

func (*JsonObject) Delete

func (js *JsonObject) Delete(key string)

func (*JsonObject) GetArray

func (js *JsonObject) GetArray(key string) ([]interface{}, bool)

func (*JsonObject) GetBool

func (js *JsonObject) GetBool(key string) (bool, bool)

func (*JsonObject) GetFloat64

func (js *JsonObject) GetFloat64(key string) (float64, bool)

func (*JsonObject) GetInterface

func (js *JsonObject) GetInterface(key string) interface{}

func (*JsonObject) GetJson

func (js *JsonObject) GetJson(key string) (JsonObject, bool)

func (*JsonObject) GetStrInt64

func (js *JsonObject) GetStrInt64(key string) (int64, bool)

这个函数只是用于取回字串是数字的值,在json字串里面对应的属性仍然是字串,这是因为float64转int64是存在精度误差的

func (*JsonObject) GetString

func (js *JsonObject) GetString(key string) (string, bool)

func (*JsonObject) Marshal

func (js *JsonObject) Marshal(v interface{})

func (*JsonObject) ParseAnyString

func (js *JsonObject) ParseAnyString(str string) (interface{}, bool)

如果 str 不是用大括号括起来的 Object 字串,ParseString 就会失败, ParseAnyString 会把字串作为一个 key 来分析,这样只要是合法的 Json 字串,就能够返回正确的对象,比如布尔、字串,数字等等。

func (*JsonObject) ParseBytes

func (js *JsonObject) ParseBytes(b []byte) error

func (*JsonObject) ParseString

func (js *JsonObject) ParseString(str string) error

func (*JsonObject) SetStrInt64

func (js *JsonObject) SetStrInt64(key string, v int64)

func (*JsonObject) SetValue

func (js *JsonObject) SetValue(key string, v interface{})

这个函数会设置map对应key 值,但是可能和读取时是不一样的,因为反编译json字串的对象都是map,而设置的不一定, 你可以设置一个其它对象,它会被json序列化,而反序列化并不能还原这个对象,要反序列化为特定的对象使用 SetValueTo 函数

func (*JsonObject) SetValueTo

func (js *JsonObject) SetValueTo(key string, v interface{}) (r bool)

func (*JsonObject) ToBytes

func (js *JsonObject) ToBytes() []byte

func (*JsonObject) ToString

func (js *JsonObject) ToString() string

func (*JsonObject) ToStruct

func (js *JsonObject) ToStruct(st interface{}) error

type LogInfo

type LogInfo struct {
	Id        int    `json:"id"`
	Color     int    `json:"color"`
	Log       string `json:"log"`
	Trace     string `json:"trace"`
	CreatedAt string `json:"created_at"`
}

func GetLog

func GetLog(page, count int, file string) ([]*LogInfo, int)

GetLog 读取数据库中保存的日志 page 第几页,count 读取的日志数量,本质上函数读取的范围是 count * page 到 count * (page+1) 所以如果读取开头,page 应设置为 0 file 指明要获取某一个源文件的日志,空表示获取全部日志 返回值为日志数据,和日志总数

func GetLogTo

func GetLogTo(table string, page, count int, file string) ([]*LogInfo, int)

type LogParam

type LogParam struct {
	DbType        int
	LogDb         *sql.DB
	LogTable      string
	SaveToLog     bool
	ShowOnConsole bool
	MaxLogCount   int64
}

LogParam LogDb: A valid database value, it can't be nil, other feilds can be default DbType: Mysql:0 or SQLite:1, other database maybe not work, default is Mysql LogTable: table name of log, default is jsuse_log SaveToLog: log whether save to database, default is false ShowOnConsole: log whether show on console, default is true

type QueryCall

type QueryCall func(rows *sql.Rows)

type QueryFunc

type QueryFunc func(string, QueryCall, ...interface{}) error

type TcpOnConnect

type TcpOnConnect func(conn net.Conn)

type UGFailedProc

type UGFailedProc func(r rune, s string)

type UGSuccessProc

type UGSuccessProc func(gbk []byte, s string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL