This commit is contained in:
Liang Ding 2022-07-17 12:22:32 +08:00
parent c8ea858976
commit 505b973c2d
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
70 changed files with 671 additions and 942 deletions

View file

@ -21,6 +21,8 @@ import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"github.com/siyuan-note/logging"
)
var SK = []byte("696D897C9AA0611B")
@ -30,13 +32,13 @@ func AESEncrypt(str string) string {
buf.Grow(4096)
_, err := hex.NewEncoder(buf).Write([]byte(str))
if nil != err {
LogErrorf("encrypt failed: %s", err)
logging.LogErrorf("encrypt failed: %s", err)
return ""
}
data := buf.Bytes()
block, err := aes.NewCipher(SK)
if nil != err {
LogErrorf("encrypt failed: %s", err)
logging.LogErrorf("encrypt failed: %s", err)
return ""
}
cbc := cipher.NewCBCEncrypter(block, []byte("RandomInitVector"))
@ -56,7 +58,7 @@ func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
func AESDecrypt(cryptStr string) []byte {
crypt, err := hex.DecodeString(cryptStr)
if nil != err {
LogErrorf("decrypt failed: %s", err)
logging.LogErrorf("decrypt failed: %s", err)
return nil
}

View file

@ -25,6 +25,7 @@ import (
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/siyuan-note/logging"
)
func IsEmptyDir(p string) bool {
@ -79,7 +80,7 @@ func LatestTmpFile(p string) string {
dir, base := filepath.Split(p)
files, err := os.ReadDir(dir)
if nil != err {
LogErrorf("read dir [%s] failed: %s", dir, err)
logging.LogErrorf("read dir [%s] failed: %s", dir, err)
return ""
}
@ -100,12 +101,12 @@ func LatestTmpFile(p string) string {
sort.Slice(tmps, func(i, j int) bool {
info1, err := tmps[i].Info()
if nil != err {
LogErrorf("read file info [%s] failed: %s", tmps[i].Name(), err)
logging.LogErrorf("read file info [%s] failed: %s", tmps[i].Name(), err)
return false
}
info2, err := tmps[j].Info()
if nil != err {
LogErrorf("read file info [%s] failed: %s", tmps[j].Name(), err)
logging.LogErrorf("read file info [%s] failed: %s", tmps[j].Name(), err)
return false
}
return info1.ModTime().After(info2.ModTime())
@ -208,7 +209,7 @@ func SizeOfDirectory(path string, includeBigFile bool) (int64, error) {
return nil
})
if nil != err {
LogErrorf("size of dir [%s] failed: %s", path, err)
logging.LogErrorf("size of dir [%s] failed: %s", path, err)
}
return size, err
}

View file

@ -24,6 +24,7 @@ import (
"github.com/88250/gulu"
"github.com/ConradIrwin/font/sfnt"
"github.com/flopp/go-findfont"
"github.com/siyuan-note/logging"
ttc "golang.org/x/image/font/sfnt"
textUnicode "golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@ -54,7 +55,7 @@ func loadFonts(currentLanguage string) (ret []string) {
if strings.HasSuffix(strings.ToLower(f), ".ttc") {
data, err := os.ReadFile(f)
if nil != err {
LogErrorf("read font file [%s] failed: %s", f, err)
logging.LogErrorf("read font file [%s] failed: %s", f, err)
continue
}
collection, err := ttc.ParseCollection(data)

View file

@ -1,340 +0,0 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
import (
"bytes"
"fmt"
"io"
stdlog "log"
"os"
"runtime"
"runtime/debug"
"strings"
"github.com/88250/gulu"
"github.com/getsentry/sentry-go"
)
func ShortStack() string {
output := string(debug.Stack())
lines := strings.Split(output, "\n")
if 5 < len(lines) {
lines = lines[5:]
}
buf := bytes.Buffer{}
for _, l := range lines {
if strings.Contains(l, "gin-gonic") {
break
}
buf.WriteString(" ")
buf.WriteString(l)
buf.WriteByte('\n')
}
return buf.String()
}
var (
logger *Logger
logFile *os.File
)
func LogTracef(format string, v ...interface{}) {
if !logger.IsTraceEnabled() {
return
}
defer closeLogger()
openLogger()
logger.Tracef(format, v...)
}
func LogDebugf(format string, v ...interface{}) {
if !logger.IsDebugEnabled() {
return
}
defer closeLogger()
openLogger()
logger.Debugf(format, v...)
}
func LogInfof(format string, v ...interface{}) {
defer closeLogger()
openLogger()
logger.Infof(format, v...)
}
func LogErrorf(format string, v ...interface{}) {
defer closeLogger()
openLogger()
logger.Errorf(format, v...)
}
func LogWarnf(format string, v ...interface{}) {
if !logger.IsWarnEnabled() {
return
}
defer closeLogger()
openLogger()
logger.Warnf(format, v...)
}
func LogFatalf(format string, v ...interface{}) {
openLogger()
logger.Fatalf(format, v...)
}
func openLogger() {
if gulu.File.IsExist(LogPath) {
if size := gulu.File.GetFileSize(LogPath); 1024*1024*2 <= size {
// 日志文件大于 2M 的话删了重建
os.Remove(LogPath)
}
}
var err error
logFile, err = os.OpenFile(LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if nil != err {
stdlog.Fatalf("create log file [%s] failed: %s", LogPath, err)
}
logger = NewLogger(io.MultiWriter(os.Stdout, logFile))
}
func closeLogger() {
logFile.Close()
}
func Recover() {
if e := recover(); nil != e {
stack := stack()
msg := fmt.Sprintf("PANIC RECOVERED: %v\n\t%s\n", e, stack)
LogErrorf(msg)
}
}
var (
dunno = []byte("???")
centerDot = []byte("·")
dot = []byte(".")
slash = []byte("/")
)
// stack implements Stack, skipping 2 frames.
func stack() []byte {
buf := &bytes.Buffer{} // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
for i := 2; ; i++ { // Caller we care about is the user, 2 frames up
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := os.ReadFile(file)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
line-- // in stack trace, lines are 1-indexed but our array is 0-indexed
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
}
return buf.Bytes()
}
// source returns a space-trimmed slice of the n'th line.
func source(lines [][]byte, n int) []byte {
if n < 0 || n >= len(lines) {
return dunno
}
return bytes.Trim(lines[n], " \t")
}
// function returns, if possible, the name of the function containing the PC.
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Since the package path might contains dots (e.g. code.google.com/...),
// we first remove the path prefix if there is one.
if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 {
name = name[lastslash+1:]
}
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}
// Logging level.
const (
Off = iota
Trace
Debug
Info
Warn
Error
Fatal
)
// the global default logging level, it will be used for creating logger.
var logLevel = Debug
// Logger represents a simple logger with level.
// The underlying logger is the standard Go logging "log".
type Logger struct {
level int
logger *stdlog.Logger
}
// NewLogger creates a logger.
func NewLogger(out io.Writer) *Logger {
ret := &Logger{level: logLevel, logger: stdlog.New(out, "", stdlog.Ldate|stdlog.Ltime|stdlog.Lshortfile)}
return ret
}
// SetLogLevel sets the logging level of all loggers.
func SetLogLevel(level string) {
logLevel = getLevel(level)
}
// getLevel gets logging level int value corresponding to the specified level.
func getLevel(level string) int {
level = strings.ToLower(level)
switch level {
case "off":
return Off
case "trace":
return Trace
case "debug":
return Debug
case "info":
return Info
case "warn":
return Warn
case "error":
return Error
case "fatal":
return Fatal
default:
return Info
}
}
// SetLevel sets the logging level of a logger.
func (l *Logger) SetLevel(level string) {
l.level = getLevel(level)
}
// IsTraceEnabled determines whether the trace level is enabled.
func (l *Logger) IsTraceEnabled() bool {
return l.level <= Trace
}
// IsDebugEnabled determines whether the debug level is enabled.
func (l *Logger) IsDebugEnabled() bool {
return l.level <= Debug
}
// IsWarnEnabled determines whether the debug level is enabled.
func (l *Logger) IsWarnEnabled() bool {
return l.level <= Warn
}
// Tracef prints trace level message with format.
func (l *Logger) Tracef(format string, v ...interface{}) {
if Trace < l.level {
return
}
l.logger.SetPrefix("T ")
l.logger.Output(3, fmt.Sprintf(format, v...))
}
// Debugf prints debug level message with format.
func (l *Logger) Debugf(format string, v ...interface{}) {
if Debug < l.level {
return
}
l.logger.SetPrefix("D ")
l.logger.Output(3, fmt.Sprintf(format, v...))
}
// Infof prints info level message with format.
func (l *Logger) Infof(format string, v ...interface{}) {
if Info < l.level {
return
}
l.logger.SetPrefix("I ")
l.logger.Output(3, fmt.Sprintf(format, v...))
}
// Warnf prints warning level message with format.
func (l *Logger) Warnf(format string, v ...interface{}) {
if Warn < l.level {
return
}
l.logger.SetPrefix("W ")
msg := fmt.Sprintf(format, v...)
l.logger.Output(3, msg)
}
// Errorf prints error level message with format.
func (l *Logger) Errorf(format string, v ...interface{}) {
if Error < l.level {
return
}
l.logger.SetPrefix("E ")
msg := fmt.Sprintf(format, v...)
l.logger.Output(3, msg)
sentry.CaptureMessage(msg)
}
// Fatalf prints fatal level message with format and exit process with code 1.
func (l *Logger) Fatalf(format string, v ...interface{}) {
if Fatal < l.level {
return
}
l.logger.SetPrefix("F ")
msg := fmt.Sprintf(format, v...)
l.logger.Output(3, msg)
sentry.CaptureMessage(msg)
closeLogger()
os.Exit(ExitCodeFatal)
}

View file

@ -25,6 +25,7 @@ import (
"strings"
"github.com/88250/gulu"
"github.com/siyuan-note/logging"
)
var (
@ -96,7 +97,7 @@ func GetLocalIPs() (ret []string) {
ret = []string{}
addrs, err := net.InterfaceAddrs()
if nil != err {
LogWarnf("get interface addresses failed: %s", err)
logging.LogWarnf("get interface addresses failed: %s", err)
return
}
for _, addr := range addrs {

View file

@ -16,7 +16,10 @@
package util
import "github.com/88250/gulu"
import (
"github.com/88250/gulu"
"github.com/siyuan-note/logging"
)
type PushMode int
@ -64,7 +67,7 @@ func NewCmdResult(cmdName string, cmdId float64, pushMode, reloadPushMode PushMo
func (r *Result) Bytes() []byte {
ret, err := gulu.JSON.MarshalJSON(r)
if nil != err {
LogErrorf("marshal result [%+v] failed [%s]", r, err)
logging.LogErrorf("marshal result [%+v] failed [%s]", r, err)
}
return ret
}

View file

@ -21,6 +21,7 @@ import (
"time"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
var cachedRhyResult = map[string]interface{}{}
@ -39,7 +40,7 @@ func GetRhyResult(force bool) (map[string]interface{}, error) {
request := httpclient.NewCloudRequest()
_, err := request.SetResult(&cachedRhyResult).Get(AliyunServer + "/apis/siyuan/version?ver=" + Ver)
if nil != err {
LogErrorf("get version info failed: %s", err)
logging.LogErrorf("get version info failed: %s", err)
return nil, err
}
rhyResultCacheTime = now

View file

@ -26,6 +26,7 @@ import (
"github.com/88250/gulu"
"github.com/denisbrodbeck/machineid"
"github.com/dustin/go-humanize"
"github.com/siyuan-note/logging"
)
const DatabaseVer = "20220501" // 修改表结构的话需要修改这里
@ -43,7 +44,7 @@ func logBootInfo() {
s, _ := SizeOfDirectory(DataDir, true)
dataDirSize := humanize.Bytes(uint64(s))
LogInfof("kernel is booting:\n"+
logging.LogInfof("kernel is booting:\n"+
" * ver [%s]\n"+
" * arch [%s]\n"+
" * runtime mode [%s]\n"+
@ -78,13 +79,13 @@ func GetDeviceID() string {
func SetNetworkProxy(proxyURL string) {
if err := os.Setenv("HTTPS_PROXY", proxyURL); nil != err {
logger.Errorf("set env [HTTPS_PROXY] failed: %s", err)
logging.LogErrorf("set env [HTTPS_PROXY] failed: %s", err)
}
if err := os.Setenv("HTTP_PROXY", proxyURL); nil != err {
logger.Errorf("set env [HTTP_PROXY] failed: %s", err)
logging.LogErrorf("set env [HTTP_PROXY] failed: %s", err)
}
if "" != proxyURL {
logger.Infof("use network proxy [%s]", proxyURL)
logging.LogInfof("use network proxy [%s]", proxyURL)
}
}

View file

@ -24,6 +24,7 @@ import (
"github.com/88250/gulu"
"github.com/88250/melody"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/logging"
)
func GetRemoteAddr(session *melody.Session) string {
@ -70,14 +71,14 @@ func tryToListenPort() bool {
time.Sleep(time.Second * 3)
listener, err = net.Listen("tcp", "127.0.0.1:"+ServerPort)
if nil != err {
LogErrorf("try to listen port [%s] failed: %s", ServerPort, err)
logging.LogErrorf("try to listen port [%s] failed: %s", ServerPort, err)
return false
}
}
if err = listener.Close(); nil != err {
time.Sleep(time.Second * 1)
if err = listener.Close(); nil != err {
LogErrorf("close listen port [%s] failed: %s", ServerPort, err)
logging.LogErrorf("close listen port [%s] failed: %s", ServerPort, err)
}
}
return true

View file

@ -33,6 +33,7 @@ import (
figure "github.com/common-nighthawk/go-figure"
goPS "github.com/mitchellh/go-ps"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
//var Mode = "dev"
@ -87,6 +88,7 @@ func Boot() {
SSL = *ssl
LogPath = filepath.Join(TempDir, "siyuan.log")
logging.SetLogPath(LogPath)
AppearancePath = filepath.Join(ConfDir, "appearance")
if "dev" == Mode {
ThemesPath = filepath.Join(WorkingDir, "appearance", "themes")
@ -100,7 +102,7 @@ func Boot() {
checkPort()
bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true)
LogInfof("\n" + bootBanner.String())
logging.LogInfof("\n" + bootBanner.String())
logBootInfo()
go cleanOld()
@ -136,13 +138,13 @@ func GetBootProgress() float64 {
func SetBooted() {
bootDetails = "Finishing boot..."
bootProgress = 100
LogInfof("kernel booted")
logging.LogInfof("kernel booted")
}
func GetHistoryDir(suffix string) (ret string, err error) {
ret = filepath.Join(HistoryDir, time.Now().Format("2006-01-02-150405")+"-"+suffix)
if err = os.MkdirAll(ret, 0755); nil != err {
LogErrorf("make history dir failed: %s", err)
logging.LogErrorf("make history dir failed: %s", err)
return
}
return
@ -316,33 +318,33 @@ func checkPort() {
return
}
LogInfof("port [%s] is opened, try to check version of running kernel", ServerPort)
logging.LogInfof("port [%s] is opened, try to check version of running kernel", ServerPort)
result := NewResult()
_, err := httpclient.NewBrowserRequest().
SetResult(result).
SetHeader("User-Agent", UserAgent).
Get("http://127.0.0.1:" + ServerPort + "/api/system/version")
if nil != err || 0 != result.Code {
LogErrorf("connect to port [%s] for checking running kernel failed", ServerPort)
logging.LogErrorf("connect to port [%s] for checking running kernel failed", ServerPort)
KillByPort(ServerPort)
return
}
if nil == result.Data {
LogErrorf("connect ot port [%s] for checking running kernel failed", ServerPort)
logging.LogErrorf("connect ot port [%s] for checking running kernel failed", ServerPort)
os.Exit(ExitCodeUnavailablePort)
}
runningVer := result.Data.(string)
if runningVer == Ver {
LogInfof("version of the running kernel is the same as this boot [%s], exit this boot", runningVer)
logging.LogInfof("version of the running kernel is the same as this boot [%s], exit this boot", runningVer)
os.Exit(ExitCodeOk)
}
LogInfof("found kernel [%s] is running, try to exit it", runningVer)
logging.LogInfof("found kernel [%s] is running, try to exit it", runningVer)
processes, err := goPS.Processes()
if nil != err {
LogErrorf("close kernel [%s] failed: %s", runningVer, err)
logging.LogErrorf("close kernel [%s] failed: %s", runningVer, err)
os.Exit(ExitCodeUnavailablePort)
}
@ -354,7 +356,7 @@ func checkPort() {
if currentPid != kernelPid {
pid := strconv.Itoa(kernelPid)
Kill(pid)
LogInfof("killed kernel [name=%s, pid=%s, ver=%s], continue to boot", name, pid, runningVer)
logging.LogInfof("killed kernel [name=%s, pid=%s, ver=%s], continue to boot", name, pid, runningVer)
}
}
}
@ -383,7 +385,7 @@ func KillByPort(port string) {
name = proc.Executable()
}
Kill(pid)
LogInfof("killed process [name=%s, pid=%s]", name, pid)
logging.LogInfof("killed process [name=%s, pid=%s]", name, pid)
}
}
@ -404,7 +406,7 @@ func PidByPort(port string) (ret string) {
CmdAttr(cmd)
data, err := cmd.CombinedOutput()
if nil != err {
LogErrorf("netstat failed: %s", err)
logging.LogErrorf("netstat failed: %s", err)
return
}
output := string(data)
@ -424,7 +426,7 @@ func PidByPort(port string) (ret string) {
CmdAttr(cmd)
data, err := cmd.CombinedOutput()
if nil != err {
LogErrorf("lsof failed: %s", err)
logging.LogErrorf("lsof failed: %s", err)
return
}
output := string(data)

View file

@ -24,6 +24,7 @@ import (
figure "github.com/common-nighthawk/go-figure"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, lang string) {
@ -49,6 +50,7 @@ func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, l
AndroidNativeLibDir = nativeLibDir
AndroidPrivateDataDir = privateDataDir
LogPath = filepath.Join(TempDir, "siyuan.log")
logging.SetLogPath(LogPath)
AppearancePath = filepath.Join(ConfDir, "appearance")
ThemesPath = filepath.Join(AppearancePath, "themes")
IconsPath = filepath.Join(AppearancePath, "icons")
@ -57,6 +59,6 @@ func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, l
Lang = lang
initPathDir()
bootBanner := figure.NewFigure("SiYuan", "", true)
LogInfof("\n" + bootBanner.String())
logging.LogInfof("\n" + bootBanner.String())
logBootInfo()
}