Skip to content

Commit 60dfe00

Browse files
authored
Merge pull request #223 from erizocosmico/feature/logs
*: add logging
2 parents f98ba56 + 039b605 commit 60dfe00

File tree

8 files changed

+83
-34
lines changed

8 files changed

+83
-34
lines changed

Gopkg.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gitbase/main.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/jessevdk/go-flags"
@@ -13,8 +12,8 @@ const (
1312

1413
func main() {
1514
parser := flags.NewNamedParser(name, flags.Default)
16-
parser.AddCommand("server", "Start SQL server.", "", &CmdServer{})
17-
parser.AddCommand("version", "Show the version information.", "", &CmdVersion{})
15+
parser.AddCommand("server", "Start SQL server.", "", &cmdServer{})
16+
parser.AddCommand("version", "Show the version information.", "", &cmdVersion{})
1817

1918
_, err := parser.Parse()
2019
if err != nil {
@@ -25,15 +24,3 @@ func main() {
2524
os.Exit(1)
2625
}
2726
}
28-
29-
type cmd struct {
30-
Verbose bool `short:"v" description:"Activates the verbose mode"`
31-
}
32-
33-
func (c *cmd) print(format string, a ...interface{}) {
34-
if !c.Verbose {
35-
return
36-
}
37-
38-
fmt.Printf(format, a...)
39-
}

cmd/gitbase/server.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66
"strconv"
77

8+
"github.com/sirupsen/logrus"
9+
810
"github.com/src-d/gitbase"
911
"github.com/src-d/gitbase/internal/function"
1012
"github.com/src-d/gitbase/internal/rule"
@@ -19,9 +21,8 @@ import (
1921
// not be empty.
2022
var enableUnstableSquash = os.Getenv("UNSTABLE_SQUASH_ENABLE") != ""
2123

22-
// CmdServer defines server command
23-
type CmdServer struct {
24-
cmd
24+
type cmdServer struct {
25+
Verbose bool `short:"v" description:"Activates the verbose mode"`
2526

2627
Git string `short:"g" long:"git" description:"Path where the git repositories are located, one per dir"`
2728
Host string `short:"h" long:"host" default:"localhost" description:"Host where the server is going to listen"`
@@ -34,12 +35,14 @@ type CmdServer struct {
3435
name string
3536
}
3637

37-
func (c *CmdServer) buildDatabase() error {
38+
func (c *cmdServer) buildDatabase() error {
3839
if c.engine == nil {
3940
c.engine = sqle.New()
4041
}
4142

42-
c.print("opening %q repository...\n", c.Git)
43+
if c.Git != "" {
44+
logrus.WithField("dir", c.Git).Debug("added folder containing git repositories")
45+
}
4346

4447
var err error
4548

@@ -51,18 +54,25 @@ func (c *CmdServer) buildDatabase() error {
5154
}
5255

5356
c.engine.AddDatabase(gitbase.NewDatabase(c.name))
57+
logrus.WithField("db", c.name).Debug("registered database to catalog")
5458
c.engine.Catalog.RegisterFunctions(function.Functions)
59+
logrus.Debug("registered all available functions in catalog")
5560

5661
if enableUnstableSquash {
62+
logrus.Warn("unstable squash tables rule is enabled")
5763
c.engine.Analyzer.AddRule(rule.SquashJoinsRule, rule.SquashJoins)
5864
}
5965

6066
return nil
6167
}
6268

63-
// Execute starts the server
64-
func (c *CmdServer) Execute(args []string) error {
69+
func (c *cmdServer) Execute(args []string) error {
70+
if c.Verbose {
71+
logrus.SetLevel(logrus.DebugLevel)
72+
}
73+
6574
if err := c.buildDatabase(); err != nil {
75+
logrus.WithField("error", err).Fatal("unable to start database server")
6676
return err
6777
}
6878

@@ -85,5 +95,7 @@ func (c *CmdServer) Execute(args []string) error {
8595
return err
8696
}
8797

98+
logrus.Debug("starting server")
99+
88100
return s.Start()
89101
}

cmd/gitbase/version.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package main
22

3-
import (
4-
"fmt"
5-
)
3+
import "fmt"
64

7-
var version string
8-
var build string
5+
var (
6+
version string
7+
build string
8+
)
99

10-
type CmdVersion struct{}
10+
type cmdVersion struct{}
1111

12-
func (c *CmdVersion) Execute(args []string) error {
12+
func (c *cmdVersion) Execute(args []string) error {
1313
fmt.Printf("%s (%s) - build %s\n", name, version, build)
14-
1514
return nil
1615
}

iterator.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitbase
22

33
import (
4+
"fmt"
45
"io"
56
"path/filepath"
67
"strings"
@@ -12,6 +13,8 @@ import (
1213
"gopkg.in/src-d/go-git.v4/plumbing/object"
1314
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1415
"gopkg.in/src-d/go-mysql-server.v0/sql"
16+
17+
"github.com/sirupsen/logrus"
1518
)
1619

1720
// ChainableIter is an iterator meant to have a chaining-friendly API.
@@ -391,6 +394,10 @@ func (i *refIter) Advance() error {
391394
}
392395

393396
if ref.Type() != plumbing.HashReference {
397+
logrus.WithFields(logrus.Fields{
398+
"type": ref.Type(),
399+
"ref": ref.Name(),
400+
}).Debug("ignoring reference, it's not a hash reference")
394401
continue
395402
}
396403

@@ -507,6 +514,10 @@ func (i *repoRefsIter) Advance() error {
507514
}
508515

509516
if ref.Type() != plumbing.HashReference {
517+
logrus.WithFields(logrus.Fields{
518+
"type": ref.Type(),
519+
"ref": ref.Name(),
520+
}).Debug("ignoring reference, it's not a hash reference")
510521
continue
511522
}
512523

@@ -630,6 +641,10 @@ func (i *remoteRefsIter) Advance() error {
630641
}
631642

632643
if ref.Type() != plumbing.HashReference {
644+
logrus.WithFields(logrus.Fields{
645+
"type": ref.Type(),
646+
"ref": ref.Name(),
647+
}).Debug("ignoring reference, it's not a hash reference")
633648
continue
634649
}
635650

@@ -799,6 +814,10 @@ func (i *refCommitsIter) Advance() error {
799814
_, err = resolveCommit(i.repo, i.refs.Ref().Hash())
800815
if err != nil {
801816
if errInvalidCommit.Is(err) {
817+
logrus.WithFields(logrus.Fields{
818+
"ref": i.refs.Ref().Name(),
819+
"hash": i.refs.Ref().Hash(),
820+
}).Debug("skipping reference, it's not pointing to a commit")
802821
continue
803822
}
804823

@@ -904,6 +923,10 @@ func (i *refHeadCommitsIter) Advance() error {
904923
i.commit, err = resolveCommit(i.repo, i.refs.Ref().Hash())
905924
if err != nil {
906925
if errInvalidCommit.Is(err) {
926+
logrus.WithFields(logrus.Fields{
927+
"ref": i.refs.Ref().Name(),
928+
"hash": i.refs.Ref().Hash(),
929+
}).Debug("skipping reference, it's not pointing to a commit")
907930
continue
908931
}
909932
return err
@@ -1619,6 +1642,10 @@ func resolveCommit(repo *git.Repository, hash plumbing.Hash) (*object.Commit, er
16191642
case *object.Tag:
16201643
return resolveCommit(repo, obj.Target)
16211644
default:
1645+
logrus.WithFields(logrus.Fields{
1646+
"hash": hash,
1647+
"type": fmt.Sprintf("%T", obj),
1648+
}).Debug("expecting hash to belong to a commit object")
16221649
return nil, errInvalidCommit.New(obj)
16231650
}
16241651
}

references.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gitbase
33
import (
44
"strings"
55

6+
"github.com/sirupsen/logrus"
67
"gopkg.in/src-d/go-mysql-server.v0/sql"
78

89
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -154,6 +155,10 @@ func (i *referenceIter) Next() (sql.Row, error) {
154155
}
155156

156157
if o.Type() != plumbing.HashReference {
158+
logrus.WithFields(logrus.Fields{
159+
"type": o.Type(),
160+
"ref": o.Name(),
161+
}).Debug("ignoring reference, it's not a hash reference")
157162
continue
158163
}
159164

@@ -224,6 +229,10 @@ func (i *filteredReferencesIter) Next() (sql.Row, error) {
224229
}
225230

226231
if o.Type() != plumbing.HashReference {
232+
logrus.WithFields(logrus.Fields{
233+
"type": o.Type(),
234+
"ref": o.Name(),
235+
}).Debug("ignoring reference, it's not a hash reference")
227236
continue
228237
}
229238

repository_pool.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88
"sync"
99

10+
"github.com/sirupsen/logrus"
1011
"gopkg.in/src-d/go-git.v4"
1112
"gopkg.in/src-d/go-mysql-server.v0/sql"
1213
)
@@ -83,8 +84,11 @@ func (p *RepositoryPool) AddDir(path string) error {
8384
for _, f := range dirs {
8485
if f.IsDir() {
8586
name := filepath.Join(path, f.Name())
86-
// TODO: log that the repo could not be opened
87-
p.AddGit(name)
87+
if _, err := p.AddGit(name); err != nil {
88+
logrus.WithField("path", name).Error("repository could not be opened")
89+
} else {
90+
logrus.WithField("path", name).Debug("repository added")
91+
}
8892
}
8993
}
9094

session.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55
"time"
66

7+
"github.com/sirupsen/logrus"
78
"google.golang.org/grpc/connectivity"
89
bblfsh "gopkg.in/bblfsh/client-go.v2"
910
errors "gopkg.in/src-d/go-errors.v1"
@@ -79,12 +80,16 @@ func (s *Session) BblfshClient() (*bblfsh.Client, error) {
7980
return s.bblfshClient, nil
8081
case connectivity.Connecting:
8182
attempts = 0
83+
logrus.WithField("attempts", totalAttempts).
84+
Debug("bblfsh is connecting, sleeping 100ms")
8285
time.Sleep(100 * time.Millisecond)
8386
default:
8487
if err := s.bblfshClient.Close(); err != nil {
8588
return nil, err
8689
}
8790

91+
logrus.Debug("bblfsh connection is closed, opening a new one")
92+
8893
s.bblfshClient, err = bblfsh.NewClient(s.bblfshEndpoint)
8994
if err != nil {
9095
return nil, err
@@ -98,6 +103,9 @@ func (s *Session) BblfshClient() (*bblfsh.Client, error) {
98103

99104
// Close implements the io.Closer interface.
100105
func (s *Session) Close() error {
106+
s.bblfshMu.Lock()
107+
defer s.bblfshMu.Unlock()
108+
101109
if s.bblfshClient != nil {
102110
return s.bblfshClient.Close()
103111
}

0 commit comments

Comments
 (0)