From 4909493a9f75ad188f044b8577e7357f122fb445 Mon Sep 17 00:00:00 2001 From: zeripath Date: Fri, 24 Jun 2022 11:49:47 +0100 Subject: Allow manager logging to set SQL (#20064) This PR adds a new manager command to switch on SQL logging and to turn it off. ``` gitea manager logging log-sql gitea manager logging log-sql --off ``` Signed-off-by: Andrew Thornton --- models/db/engine.go | 9 +++++++++ models/db/log.go | 25 ++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'models/db') diff --git a/models/db/engine.go b/models/db/engine.go index 93cf5ad8bc..2c329300e3 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -287,3 +287,12 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) { _, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID) return maxID, err } + +func SetLogSQL(ctx context.Context, on bool) { + e := GetEngine(ctx) + if x, ok := e.(*xorm.Engine); ok { + x.ShowSQL(on) + } else if sess, ok := e.(*xorm.Session); ok { + sess.Engine().ShowSQL(on) + } +} diff --git a/models/db/log.go b/models/db/log.go index f9febf440e..4c497fdfd7 100644 --- a/models/db/log.go +++ b/models/db/log.go @@ -6,6 +6,7 @@ package db import ( "fmt" + "sync/atomic" "code.gitea.io/gitea/modules/log" @@ -14,15 +15,19 @@ import ( // XORMLogBridge a logger bridge from Logger to xorm type XORMLogBridge struct { - showSQL bool - logger log.Logger + showSQLint *int32 + logger log.Logger } // NewXORMLogger inits a log bridge for xorm func NewXORMLogger(showSQL bool) xormlog.Logger { + showSQLint := int32(0) + if showSQL { + showSQLint = 1 + } return &XORMLogBridge{ - showSQL: showSQL, - logger: log.GetLogger("xorm"), + showSQLint: &showSQLint, + logger: log.GetLogger("xorm"), } } @@ -94,14 +99,16 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) { // ShowSQL set if record SQL func (l *XORMLogBridge) ShowSQL(show ...bool) { - if len(show) > 0 { - l.showSQL = show[0] - } else { - l.showSQL = true + showSQL := int32(1) + if len(show) > 0 && !show[0] { + showSQL = 0 } + atomic.StoreInt32(l.showSQLint, showSQL) } // IsShowSQL if record SQL func (l *XORMLogBridge) IsShowSQL() bool { - return l.showSQL + showSQL := atomic.LoadInt32(l.showSQLint) + + return showSQL == 1 } -- cgit v1.2.3