diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/db/log.go | 52 | ||||
-rw-r--r-- | models/issues/pull.go | 39 | ||||
-rw-r--r-- | models/migrations/base/tests.go | 6 | ||||
-rw-r--r-- | models/organization/team.go | 16 | ||||
-rw-r--r-- | models/perm/access/repo_permission.go | 55 | ||||
-rw-r--r-- | models/perm/access_mode.go | 9 | ||||
-rw-r--r-- | models/repo/repo.go | 16 | ||||
-rw-r--r-- | models/unit/unit.go | 7 | ||||
-rw-r--r-- | models/user/user.go | 14 |
9 files changed, 74 insertions, 140 deletions
diff --git a/models/db/log.go b/models/db/log.go index fec2ea3c3d..dd95f64ca8 100644 --- a/models/db/log.go +++ b/models/db/log.go @@ -14,67 +14,62 @@ import ( // XORMLogBridge a logger bridge from Logger to xorm type XORMLogBridge struct { - showSQLint *int32 - logger log.Logger + showSQL atomic.Bool + 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{ - showSQLint: &showSQLint, - logger: log.GetLogger("xorm"), - } + l := &XORMLogBridge{logger: log.GetLogger("xorm")} + l.showSQL.Store(showSQL) + return l } const stackLevel = 8 // Log a message with defined skip and at logging level -func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error { - return l.logger.Log(skip+1, level, format, v...) +func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) { + l.logger.Log(skip+1, level, format, v...) } // Debug show debug log func (l *XORMLogBridge) Debug(v ...interface{}) { - _ = l.Log(stackLevel, log.DEBUG, fmt.Sprint(v...)) + l.Log(stackLevel, log.DEBUG, "%s", fmt.Sprint(v...)) } // Debugf show debug log func (l *XORMLogBridge) Debugf(format string, v ...interface{}) { - _ = l.Log(stackLevel, log.DEBUG, format, v...) + l.Log(stackLevel, log.DEBUG, format, v...) } // Error show error log func (l *XORMLogBridge) Error(v ...interface{}) { - _ = l.Log(stackLevel, log.ERROR, fmt.Sprint(v...)) + l.Log(stackLevel, log.ERROR, "%s", fmt.Sprint(v...)) } // Errorf show error log func (l *XORMLogBridge) Errorf(format string, v ...interface{}) { - _ = l.Log(stackLevel, log.ERROR, format, v...) + l.Log(stackLevel, log.ERROR, format, v...) } // Info show information level log func (l *XORMLogBridge) Info(v ...interface{}) { - _ = l.Log(stackLevel, log.INFO, fmt.Sprint(v...)) + l.Log(stackLevel, log.INFO, "%s", fmt.Sprint(v...)) } // Infof show information level log func (l *XORMLogBridge) Infof(format string, v ...interface{}) { - _ = l.Log(stackLevel, log.INFO, format, v...) + l.Log(stackLevel, log.INFO, format, v...) } // Warn show warning log func (l *XORMLogBridge) Warn(v ...interface{}) { - _ = l.Log(stackLevel, log.WARN, fmt.Sprint(v...)) + l.Log(stackLevel, log.WARN, "%s", fmt.Sprint(v...)) } // Warnf show warnning log func (l *XORMLogBridge) Warnf(format string, v ...interface{}) { - _ = l.Log(stackLevel, log.WARN, format, v...) + l.Log(stackLevel, log.WARN, format, v...) } // Level get logger level @@ -86,10 +81,12 @@ func (l *XORMLogBridge) Level() xormlog.LogLevel { return xormlog.LOG_INFO case log.WARN: return xormlog.LOG_WARNING - case log.ERROR, log.CRITICAL: + case log.ERROR: return xormlog.LOG_ERR + case log.NONE: + return xormlog.LOG_OFF } - return xormlog.LOG_OFF + return xormlog.LOG_UNKNOWN } // SetLevel set the logger level @@ -98,16 +95,13 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) { // ShowSQL set if record SQL func (l *XORMLogBridge) ShowSQL(show ...bool) { - showSQL := int32(1) - if len(show) > 0 && !show[0] { - showSQL = 0 + if len(show) == 0 { + show = []bool{true} } - atomic.StoreInt32(l.showSQLint, showSQL) + l.showSQL.Store(show[0]) } // IsShowSQL if record SQL func (l *XORMLogBridge) IsShowSQL() bool { - showSQL := atomic.LoadInt32(l.showSQLint) - - return showSQL == 1 + return l.showSQL.Load() } diff --git a/models/issues/pull.go b/models/issues/pull.go index 855d37867d..218a265741 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -224,40 +224,27 @@ func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error { return err } -// ColorFormat writes a colored string to identify this struct -func (pr *PullRequest) ColorFormat(s fmt.State) { +func (pr *PullRequest) String() string { if pr == nil { - log.ColorFprintf(s, "PR[%d]%s#%d[%s...%s:%s]", - log.NewColoredIDValue(0), - log.NewColoredValue("<nil>/<nil>"), - log.NewColoredIDValue(0), - log.NewColoredValue("<nil>"), - log.NewColoredValue("<nil>/<nil>"), - log.NewColoredValue("<nil>"), - ) - return - } - - log.ColorFprintf(s, "PR[%d]", log.NewColoredIDValue(pr.ID)) + return "<PullRequest nil>" + } + + s := new(strings.Builder) + fmt.Fprintf(s, "<PullRequest [%d]", pr.ID) if pr.BaseRepo != nil { - log.ColorFprintf(s, "%s#%d[%s...", log.NewColoredValue(pr.BaseRepo.FullName()), - log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch)) + fmt.Fprintf(s, "%s#%d[%s...", pr.BaseRepo.FullName(), pr.Index, pr.BaseBranch) } else { - log.ColorFprintf(s, "Repo[%d]#%d[%s...", log.NewColoredIDValue(pr.BaseRepoID), - log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch)) + fmt.Fprintf(s, "Repo[%d]#%d[%s...", pr.BaseRepoID, pr.Index, pr.BaseBranch) } if pr.HeadRepoID == pr.BaseRepoID { - log.ColorFprintf(s, "%s]", log.NewColoredValue(pr.HeadBranch)) + fmt.Fprintf(s, "%s]", pr.HeadBranch) } else if pr.HeadRepo != nil { - log.ColorFprintf(s, "%s:%s]", log.NewColoredValue(pr.HeadRepo.FullName()), log.NewColoredValue(pr.HeadBranch)) + fmt.Fprintf(s, "%s:%s]", pr.HeadRepo.FullName(), pr.HeadBranch) } else { - log.ColorFprintf(s, "Repo[%d]:%s]", log.NewColoredIDValue(pr.HeadRepoID), log.NewColoredValue(pr.HeadBranch)) + fmt.Fprintf(s, "Repo[%d]:%s]", pr.HeadRepoID, pr.HeadBranch) } -} - -// String represents the pr as a simple string -func (pr *PullRequest) String() string { - return log.ColorFormatAsString(pr) + s.WriteByte('>') + return s.String() } // MustHeadUserName returns the HeadRepo's username if failed return blank diff --git a/models/migrations/base/tests.go b/models/migrations/base/tests.go index 08eded7fdc..dd99a1eda2 100644 --- a/models/migrations/base/tests.go +++ b/models/migrations/base/tests.go @@ -24,6 +24,8 @@ import ( "xorm.io/xorm" ) +// FIXME: this file shouldn't be in a normal package, it should only be compiled for tests + // PrepareTestEnv prepares the test environment and reset the database. The skip parameter should usually be 0. // Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from. // @@ -110,7 +112,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En } func MainTest(m *testing.M) { - log.Register("test", testlogger.NewTestLogger) + log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter) giteaRoot := base.SetupGiteaRoot() if giteaRoot == "" { @@ -154,7 +156,7 @@ func MainTest(m *testing.M) { os.Exit(1) } setting.LoadDBSetting() - setting.InitLogs(true) + setting.InitLoggersForTest() exitStatus := m.Run() diff --git a/models/organization/team.go b/models/organization/team.go index 5e3c9ecffe..3624fdc466 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -94,21 +94,11 @@ func init() { db.RegisterModel(new(TeamInvite)) } -// ColorFormat provides a basic color format for a Team -func (t *Team) ColorFormat(s fmt.State) { +func (t *Team) LogString() string { if t == nil { - log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v", - log.NewColoredIDValue(0), - "<nil>", - log.NewColoredIDValue(0), - 0) - return + return "<Team nil>" } - log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v", - log.NewColoredIDValue(t.ID), - t.Name, - log.NewColoredIDValue(t.OrgID), - t.AccessMode) + return fmt.Sprintf("<Team %d:%s OrgID=%d AccessMode=%s>", t.ID, t.Name, t.OrgID, t.AccessMode.LogString()) } // LoadUnits load a list of available units for a team diff --git a/models/perm/access/repo_permission.go b/models/perm/access/repo_permission.go index ee76e48200..64df5355bb 100644 --- a/models/perm/access/repo_permission.go +++ b/models/perm/access/repo_permission.go @@ -102,45 +102,28 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool { return p.CanWrite(unit.TypeIssues) } -// ColorFormat writes a colored string for these Permissions -func (p *Permission) ColorFormat(s fmt.State) { - noColor := log.ColorBytes(log.Reset) - - format := "perm_model.AccessMode: %-v, %d Units, %d UnitsMode(s): [ " - args := []interface{}{ - p.AccessMode, - log.NewColoredValueBytes(len(p.Units), &noColor), - log.NewColoredValueBytes(len(p.UnitsMode), &noColor), - } - if s.Flag('+') { - for i, unit := range p.Units { - config := "" - if unit.Config != nil { - configBytes, err := unit.Config.ToDB() - config = string(configBytes) - if err != nil { - config = err.Error() - } +func (p *Permission) LogString() string { + format := "<Permission AccessMode=%s, %d Units, %d UnitsMode(s): [ " + args := []any{p.AccessMode.String(), len(p.Units), len(p.UnitsMode)} + + for i, unit := range p.Units { + config := "" + if unit.Config != nil { + configBytes, err := unit.Config.ToDB() + config = string(configBytes) + if err != nil { + config = err.Error() } - format += "\nUnits[%d]: ID: %d RepoID: %d Type: %-v Config: %s" - args = append(args, - log.NewColoredValueBytes(i, &noColor), - log.NewColoredIDValue(unit.ID), - log.NewColoredIDValue(unit.RepoID), - unit.Type, - config) - } - for key, value := range p.UnitsMode { - format += "\nUnitMode[%-v]: %-v" - args = append(args, - key, - value) } - } else { - format += "..." + format += "\nUnits[%d]: ID: %d RepoID: %d Type: %s Config: %s" + args = append(args, i, unit.ID, unit.RepoID, unit.Type.LogString(), config) + } + for key, value := range p.UnitsMode { + format += "\nUnitMode[%-v]: %-v" + args = append(args, key.LogString(), value.LogString()) } - format += " ]" - log.ColorFprintf(s, format, args...) + format += " ]>" + return fmt.Sprintf(format, args...) } // GetUserRepoPermission returns the user permissions to the repository diff --git a/models/perm/access_mode.go b/models/perm/access_mode.go index c482e296bc..a37bc1f0e1 100644 --- a/models/perm/access_mode.go +++ b/models/perm/access_mode.go @@ -5,8 +5,6 @@ package perm import ( "fmt" - - "code.gitea.io/gitea/modules/log" ) // AccessMode specifies the users access mode @@ -40,11 +38,8 @@ func (mode AccessMode) String() string { } } -// ColorFormat provides a ColorFormatted version of this AccessMode -func (mode AccessMode) ColorFormat(s fmt.State) { - log.ColorFprintf(s, "%d:%s", - log.NewColoredIDValue(mode), - mode) +func (mode AccessMode) LogString() string { + return fmt.Sprintf("<AccessMode:%d:%s>", mode, mode.String()) } // ParseAccessMode returns corresponding access mode to given permission string. diff --git a/models/repo/repo.go b/models/repo/repo.go index bcf8e5bbe8..d3e6daa95b 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -196,19 +196,11 @@ func (repo *Repository) SanitizedOriginalURL() string { return u.String() } -// ColorFormat returns a colored string to represent this repo -func (repo *Repository) ColorFormat(s fmt.State) { +func (repo *Repository) LogString() string { if repo == nil { - log.ColorFprintf(s, "%d:%s/%s", - log.NewColoredIDValue(0), - "<nil>", - "<nil>") - return - } - log.ColorFprintf(s, "%d:%s/%s", - log.NewColoredIDValue(repo.ID), - repo.OwnerName, - repo.Name) + return "<Repository nil>" + } + return fmt.Sprintf("<Repository %d:%s/%s>", repo.ID, repo.OwnerName, repo.Name) } // IsBeingMigrated indicates that repository is being migrated diff --git a/models/unit/unit.go b/models/unit/unit.go index 7cd679116f..5f5e20de1e 100644 --- a/models/unit/unit.go +++ b/models/unit/unit.go @@ -62,11 +62,8 @@ func (u Type) String() string { return fmt.Sprintf("Unknown Type %d", u) } -// ColorFormat provides a ColorFormatted version of this Type -func (u Type) ColorFormat(s fmt.State) { - log.ColorFprintf(s, "%d:%s", - log.NewColoredIDValue(u), - u) +func (u Type) LogString() string { + return fmt.Sprintf("<UnitType:%d:%s>", u, u.String()) } var ( diff --git a/models/user/user.go b/models/user/user.go index 7428e51065..0be36e69ab 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -151,17 +151,11 @@ type SearchOrganizationsOptions struct { All bool } -// ColorFormat writes a colored string to identify this struct -func (u *User) ColorFormat(s fmt.State) { +func (u *User) LogString() string { if u == nil { - log.ColorFprintf(s, "%d:%s", - log.NewColoredIDValue(0), - log.NewColoredValue("<nil>")) - return - } - log.ColorFprintf(s, "%d:%s", - log.NewColoredIDValue(u.ID), - log.NewColoredValue(u.Name)) + return "<User nil>" + } + return fmt.Sprintf("<User %d:%s>", u.ID, u.Name) } // BeforeUpdate is invoked from XORM before updating this object. |