Skip to content

Commit

Permalink
Cleanup log messaging
Browse files Browse the repository at this point in the history
This change corrects a few logging issues:

 * Standardized formatting errors with '%v'.
 * Standardized failure warning word usage.
 * Corrected an instance of using the standard log library when
   the gitea log library should be used instead.
  • Loading branch information
gabrieljackson authored and bkcsoft committed Feb 2, 2017
1 parent 73d05a5 commit bf6f61c
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 112 deletions.
14 changes: 7 additions & 7 deletions cmd/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func pemBlockForKey(priv interface{}) *pem.Block {
case *ecdsa.PrivateKey:
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
log.Fatalf("Unable to marshal ECDSA private key: %v\n", err)
log.Fatalf("Unable to marshal ECDSA private key: %v", err)
}
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
default:
Expand Down Expand Up @@ -112,7 +112,7 @@ func runCert(ctx *cli.Context) error {
log.Fatalf("Unrecognized elliptic curve: %q", ctx.String("ecdsa-curve"))
}
if err != nil {
log.Fatalf("Failed to generate private key: %s", err)
log.Fatalf("Failed to generate private key: %v", err)
}

var notBefore time.Time
Expand All @@ -121,7 +121,7 @@ func runCert(ctx *cli.Context) error {
} else {
notBefore, err = time.Parse("Jan 2 15:04:05 2006", ctx.String("start-date"))
if err != nil {
log.Fatalf("Failed to parse creation date: %s", err)
log.Fatalf("Failed to parse creation date: %v", err)
}
}

Expand All @@ -130,7 +130,7 @@ func runCert(ctx *cli.Context) error {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("Failed to generate serial number: %s", err)
log.Fatalf("Failed to generate serial number: %v", err)
}

template := x509.Certificate{
Expand Down Expand Up @@ -163,20 +163,20 @@ func runCert(ctx *cli.Context) error {

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
if err != nil {
log.Fatalf("Failed to create certificate: %s", err)
log.Fatalf("Failed to create certificate: %v", err)
}

certOut, err := os.Create("cert.pem")
if err != nil {
log.Fatalf("Failed to open cert.pem for writing: %s", err)
log.Fatalf("Failed to open cert.pem for writing: %v", err)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
log.Println("Written cert.pem")

keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Failed to open key.pem for writing: %v\n", err)
log.Fatalf("Failed to open key.pem for writing: %v", err)
}
pem.Encode(keyOut, pemBlockForKey(priv))
keyOut.Close()
Expand Down
22 changes: 11 additions & 11 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runDump(ctx *cli.Context) error {
}
TmpWorkDir, err := ioutil.TempDir(tmpDir, "gitea-dump-")
if err != nil {
log.Fatalf("Fail to create tmp work directory: %v", err)
log.Fatalf("Failed to create tmp work directory: %v", err)
}
log.Printf("Creating tmp work dir: %s", TmpWorkDir)

Expand All @@ -78,7 +78,7 @@ func runDump(ctx *cli.Context) error {
log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
zip.Verbose = ctx.Bool("verbose")
if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
log.Fatalf("Fail to dump local repositories: %v", err)
log.Fatalf("Failed to dump local repositories: %v", err)
}

targetDBType := ctx.String("database")
Expand All @@ -89,26 +89,26 @@ func runDump(ctx *cli.Context) error {
}

if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
log.Fatalf("Fail to dump database: %v", err)
log.Fatalf("Failed to dump database: %v", err)
}

fileName := fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())
log.Printf("Packing dump files...")
z, err := zip.Create(fileName)
if err != nil {
log.Fatalf("Fail to create %s: %v", fileName, err)
log.Fatalf("Failed to create %s: %v", fileName, err)
}

if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
log.Fatalf("Fail to include gitea-repo.zip: %v", err)
log.Fatalf("Failed to include gitea-repo.zip: %v", err)
}
if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
log.Fatalf("Fail to include gitea-db.sql: %v", err)
log.Fatalf("Failed to include gitea-db.sql: %v", err)
}
customDir, err := os.Stat(setting.CustomPath)
if err == nil && customDir.IsDir() {
if err := z.AddDir("custom", setting.CustomPath); err != nil {
log.Fatalf("Fail to include custom: %v", err)
log.Fatalf("Failed to include custom: %v", err)
}
} else {
log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
Expand All @@ -124,16 +124,16 @@ func runDump(ctx *cli.Context) error {
}

if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
log.Fatalf("Fail to include data directory: %v", err)
log.Fatalf("Failed to include data directory: %v", err)
}

if err := z.AddDir("log", setting.LogRootPath); err != nil {
log.Fatalf("Fail to include log: %v", err)
log.Fatalf("Failed to include log: %v", err)
}
// FIXME: SSH key file.
if err = z.Close(); err != nil {
_ = os.Remove(fileName)
log.Fatalf("Fail to save %s: %v", fileName, err)
log.Fatalf("Failed to save %s: %v", fileName, err)
}

if err := os.Chmod(fileName, 0600); err != nil {
Expand All @@ -143,7 +143,7 @@ func runDump(ctx *cli.Context) error {
log.Printf("Removing tmp work dir: %s", TmpWorkDir)

if err := os.RemoveAll(TmpWorkDir); err != nil {
log.Fatalf("Fail to remove %s: %v", TmpWorkDir, err)
log.Fatalf("Failed to remove %s: %v", TmpWorkDir, err)
}
log.Printf("Finish dumping in file %s", fileName)

Expand Down
8 changes: 4 additions & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func setup(logPath string) {
if setting.UseSQLite3 || setting.UseTiDB {
workDir, _ := setting.WorkDir()
if err := os.Chdir(workDir); err != nil {
log.GitLogger.Fatal(4, "Fail to change directory %s: %v", workDir, err)
log.GitLogger.Fatal(4, "Failed to change directory %s: %v", workDir, err)
}
}

Expand Down Expand Up @@ -134,10 +134,10 @@ func handleUpdateTask(uuid string, user, repoUser *models.User, reponame string,
if err == nil {
resp.Body.Close()
if resp.StatusCode/100 != 2 {
log.GitLogger.Error(2, "Fail to trigger task: not 2xx response code")
log.GitLogger.Error(2, "Failed to trigger task: not 2xx response code")
}
} else {
log.GitLogger.Error(2, "Fail to trigger task: %v", err)
log.GitLogger.Error(2, "Failed to trigger task: %v", err)
}
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func runServ(c *cli.Context) error {

mode, err := models.AccessLevel(user, repo)
if err != nil {
fail("Internal error", "Fail to check access: %v", err)
fail("Internal error", "Failed to check access: %v", err)
} else if mode < requestedMode {
clientMessage := accessDenied
if mode >= models.AccessModeRead {
Expand Down
6 changes: 3 additions & 3 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func newMacaron() *macaron.Macaron {
localeNames, err := options.Dir("locale")

if err != nil {
log.Fatal(4, "Fail to list locale files: %v", err)
log.Fatal(4, "Failed to list locale files: %v", err)
}

localFiles := make(map[string][]byte)
Expand Down Expand Up @@ -655,7 +655,7 @@ func runWeb(ctx *cli.Context) error {
err = fcgi.Serve(nil, m)
case setting.UnixSocket:
if err := os.Remove(listenAddr); err != nil {
log.Fatal(4, "Fail to remove unix socket directory %s: %v", listenAddr, err)
log.Fatal(4, "Failed to remove unix socket directory %s: %v", listenAddr, err)
}
var listener *net.UnixListener
listener, err = net.ListenUnix("unix", &net.UnixAddr{Name: listenAddr, Net: "unix"})
Expand All @@ -674,7 +674,7 @@ func runWeb(ctx *cli.Context) error {
}

if err != nil {
log.Fatal(4, "Fail to start server: %v", err)
log.Fatal(4, "Failed to start server: %v", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/web_graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package cmd

import (
"crypto/tls"
"log"
"net/http"

"code.gitea.io/gitea/modules/log"
"github.com/facebookgo/grace/gracehttp"
)

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
app.Flags = append(app.Flags, []cli.Flag{}...)
err := app.Run(os.Args)
if err != nil {
log.Fatal(4, "Fail to run app with %s: %v", os.Args, err)
log.Fatal(4, "Failed to run app with %s: %v", os.Args, err)
}

}
2 changes: 1 addition & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func attachmentRefactor(x *xorm.Engine) error {

dumpPath := path.Join(setting.LogRootPath, "attachment_path.dump")
ioutil.WriteFile(dumpPath, buf.Bytes(), 0666)
fmt.Println("Fail to rename some attachments, old and new paths are saved into:", dumpPath)
log.Info("Failed to rename some attachments, old and new paths are saved into: %s", dumpPath)
}()
for _, attach := range attachments {
if err = os.MkdirAll(path.Dir(attach.NewPath), os.ModePerm); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ func getEngine() (*xorm.Engine, error) {
return nil, errors.New("this binary version does not build support for SQLite3")
}
if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
return nil, fmt.Errorf("Fail to create directories: %v", err)
return nil, fmt.Errorf("Failed to create directories: %v", err)
}
connStr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc"
case "tidb":
if !EnableTiDB {
return nil, errors.New("this binary version does not build support for TiDB")
}
if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
return nil, fmt.Errorf("Fail to create directories: %v", err)
return nil, fmt.Errorf("Failed to create directories: %v", err)
}
connStr = "goleveldb://" + DbCfg.Path
default:
Expand All @@ -237,7 +237,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
func SetEngine() (err error) {
x, err = getEngine()
if err != nil {
return fmt.Errorf("Fail to connect to database: %v", err)
return fmt.Errorf("Failed to connect to database: %v", err)
}

x.SetMapper(core.GonicMapper{})
Expand All @@ -247,12 +247,12 @@ func SetEngine() (err error) {
logPath := path.Join(setting.LogRootPath, "xorm.log")

if err := os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
return fmt.Errorf("Fail to create dir %s: %v", logPath, err)
return fmt.Errorf("Failed to create dir %s: %v", logPath, err)
}

f, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("Fail to create xorm.log: %v", err)
return fmt.Errorf("Failed to create xorm.log: %v", err)
}
x.SetLogger(xorm.NewSimpleLogger(f))
x.ShowSQL(true)
Expand Down
4 changes: 2 additions & 2 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ func deleteOrg(e *xorm.Session, u *User) error {
path := UserPath(u.Name)

if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("Fail to RemoveAll %s: %v", path, err)
return fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
}

avatarPath := u.CustomAvatarPath()
if com.IsExist(avatarPath) {
if err := os.Remove(avatarPath); err != nil {
return fmt.Errorf("Fail to remove %s: %v", avatarPath, err)
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error
tmpBasePath := path.Join(setting.AppDataPath, "tmp/repos", com.ToStr(time.Now().Nanosecond())+".git")

if err := os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm); err != nil {
return fmt.Errorf("Fail to create dir %s: %v", tmpBasePath, err)
return fmt.Errorf("Failed to create dir %s: %v", tmpBasePath, err)
}

defer os.RemoveAll(path.Dir(tmpBasePath))
Expand Down
Loading

0 comments on commit bf6f61c

Please sign in to comment.