Skip to content

Commit

Permalink
adding logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Oct 18, 2023
1 parent 319cd95 commit 5883310
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 62 deletions.
6 changes: 3 additions & 3 deletions api/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package api

import (
"encoding/json"
"fmt"
"github.com/JackalLabs/sequoia/api/types"
"github.com/JackalLabs/sequoia/file_system"
"github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"net/http"
)

Expand All @@ -20,14 +20,14 @@ func DumpDBHandler(db *badger.DB) func(http.ResponseWriter, *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
return
}

err = json.NewEncoder(w).Encode(dump)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}

}
Expand Down
13 changes: 7 additions & 6 deletions api/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dgraph-io/badger/v4"
"github.com/gorilla/mux"
storageTypes "github.com/jackalLabs/canine-chain/v3/x/storage/types"
"github.com/rs/zerolog/log"
"net/http"
)

Expand All @@ -24,7 +25,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
return
}
Expand All @@ -38,7 +39,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
return
}
Expand All @@ -51,7 +52,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
}

Expand All @@ -70,7 +71,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
}

Expand All @@ -85,7 +86,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
}

Expand All @@ -96,7 +97,7 @@ func PostFileHandler(db *badger.DB, q *queue.Queue, address string) func(http.Re

err = json.NewEncoder(w).Encode(resp)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package api

import (
"encoding/json"
"fmt"
"github.com/JackalLabs/sequoia/api/types"
"github.com/JackalLabs/sequoia/file_system"
"github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"net/http"
)

Expand All @@ -20,14 +20,14 @@ func ListFilesHandler(db *badger.DB) func(http.ResponseWriter, *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}
return
}

err = json.NewEncoder(w).Encode(files)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
}

}
Expand Down
6 changes: 3 additions & 3 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package api

import (
"encoding/json"
"fmt"
"github.com/JackalLabs/sequoia/api/types"
"github.com/desmos-labs/cosmos-go-wallet/wallet"
"github.com/rs/zerolog/log"
"net/http"
)

Expand All @@ -18,7 +18,7 @@ func IndexHandler(address string) func(http.ResponseWriter, *http.Request) {

err := json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
return
}
}
Expand All @@ -40,7 +40,7 @@ func VersionHandler(wallet *wallet.Wallet) func(http.ResponseWriter, *http.Reque

err = json.NewEncoder(w).Encode(v)
if err != nil {
fmt.Println(err)
log.Error().Err(err)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"fmt"
"github.com/JackalLabs/sequoia/config"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +21,7 @@ func InitCmd() *cobra.Command {
panic(err)
}

fmt.Println(wallet.AccAddress())
log.Info().Msg(wallet.AccAddress())

},
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cmd

import (
"fmt"
"github.com/JackalLabs/sequoia/cmd/wallet"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"os"
)

func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

}

func RootCmd() *cobra.Command {
r := &cobra.Command{
Use: "sequoia",
Expand All @@ -20,7 +26,8 @@ func RootCmd() *cobra.Command {

func Execute(rootCmd *cobra.Command) {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)

log.Error().Err(err)
os.Exit(1)
}
}
3 changes: 2 additions & 1 deletion cmd/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wallet
import (
"fmt"
"github.com/JackalLabs/sequoia/config"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +29,7 @@ func addressCmd() *cobra.Command {
panic(err)
}

fmt.Printf("Provider Address: %s\n", wallet.AccAddress())
log.Info().Msg(fmt.Sprintf("Provider Address: %s", wallet.AccAddress()))
},
}
}
3 changes: 2 additions & 1 deletion config/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
sequoiaWallet "github.com/JackalLabs/sequoia/wallet"
"github.com/desmos-labs/cosmos-go-wallet/wallet"
"github.com/rs/zerolog/log"
"os"
"path"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func createWallet(directory string) error {
filePath := path.Join(directory, SeedFileName)

if newWallet {
fmt.Printf("A new wallet was just created, please enter your seed phrase into %s to start!\n", filePath)
log.Info().Msg(fmt.Sprintf("A new wallet was just created, please enter your seed phrase into %s to start!", filePath))
os.Exit(0)
}

Expand Down
14 changes: 9 additions & 5 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"github.com/desmos-labs/cosmos-go-wallet/wallet"
"github.com/dgraph-io/badger/v4"
storageTypes "github.com/jackalLabs/canine-chain/v3/x/storage/types"
"github.com/rs/zerolog/log"
"os"
"os/signal"
"syscall"
"time"
)

type App struct {
Expand Down Expand Up @@ -56,7 +58,7 @@ func initProviderOnChain(wallet *wallet.Wallet, ip string, totalSpace int64) err
return err
}

fmt.Println(res.TxHash)
log.Info().Msg(res.TxHash)

return nil
}
Expand All @@ -83,7 +85,7 @@ func (a *App) Start() {

res, err := cl.Providers(context.Background(), queryParams)
if err != nil {
fmt.Println("Provider does not exist on network or is not connected...")
log.Info().Msg("Provider does not exist on network or is not connected...")
err := initProviderOnChain(w, cfg.Ip, cfg.TotalSpace)
if err != nil {
panic(err)
Expand All @@ -92,7 +94,7 @@ func (a *App) Start() {

myUrl := res.Providers.Ip

fmt.Printf("Provider started as: %s\n", myAddress)
log.Info().Msg(fmt.Sprintf("Provider started as: %s", myAddress))

a.q = queue.NewQueue(w, cfg.QueueInterval)
a.prover = proofs.NewProver(w, a.db, a.q, cfg.ProofInterval)
Expand All @@ -107,11 +109,13 @@ func (a *App) Start() {

done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Press ctrl+c to quit...")
<-done // Will block here until user hits ctrl+c

a.db.Close()
a.q.Stop()
a.prover.Stop()
a.strayManager.Stop()

time.Sleep(time.Second * 10) // give the program some time to shut down
a.db.Close()

}
9 changes: 5 additions & 4 deletions file_system/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/JackalLabs/sequoia/utils"
"github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/wealdtech/go-merkletree"
"github.com/wealdtech/go-merkletree/sha3"
"io"
Expand Down Expand Up @@ -79,19 +80,19 @@ func WriteFile(db *badger.DB, reader io.Reader, signee string, address string, c

err = txn.Set(treeKey(cid), exportedTree)
if err != nil {
fmt.Printf("Cannot set tree %s | %e\n", cid, err)
log.Info().Msg(fmt.Sprintf("Cannot set tree %s | %e", cid, err))
}

for i, chunk := range chunks {
err := txn.Set(chunkKey(cid, i), chunk)
if err != nil {
fmt.Printf("Cannot set chunk %d | %e", i, err)
log.Info().Msg(fmt.Sprintf("Cannot set chunk %d | %e", i, err))
}
}

err = txn.Set(fileKey(cid), []byte(fid))
if err != nil {
fmt.Printf("Cannot set cid %s | %e\n", cid, err)
log.Info().Msg(fmt.Sprintf("Cannot set cid %s | %e", cid, err))
}

merkle = r
Expand All @@ -104,7 +105,7 @@ func WriteFile(db *badger.DB, reader io.Reader, signee string, address string, c
}

func DeleteFile(db *badger.DB, cid string) error {
fmt.Printf("Removing %s from disk...\n", cid)
log.Info().Msg(fmt.Sprintf("Removing %s from disk...", cid))
return db.Update(func(txn *badger.Txn) error {

err := txn.Delete(treeKey(cid))
Expand Down
11 changes: 6 additions & 5 deletions file_system/file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/hex"
"fmt"
"github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

Expand Down Expand Up @@ -45,7 +47,7 @@ func TestWriteFile(t *testing.T) {
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
fmt.Printf("key=%s\n", k)
log.Info().Msg(fmt.Sprintf("key=%s", k))
}
return nil
})
Expand All @@ -54,7 +56,7 @@ func TestWriteFile(t *testing.T) {
s, err := ListFiles(db)
require.NoError(t, err)

fmt.Println(s)
log.Info().Msg(strings.Join(s, ","))

}

Expand Down Expand Up @@ -96,7 +98,7 @@ func TestFIDCID(t *testing.T) {

require.Equal(t, fileData, fileDataFromFid)

fmt.Println(s)
log.Info().Msg(strings.Join(s, ","))

}

Expand All @@ -111,7 +113,6 @@ func TestLargeFile(t *testing.T) {
defer db.Close()

for i := 1; i < 1024*20; i++ {
fmt.Printf("random size of %d\n", i)
bi := make([]byte, i)
// then we can call rand.Read.
_, err = rand.Read(bi)
Expand All @@ -127,7 +128,7 @@ func TestLargeFile(t *testing.T) {
fileData, err := GetFileData(db, cid)
require.NoError(t, err)

require.Equal(t, fmt.Sprintf("%x\n", bi), fmt.Sprintf("%x\n", fileData))
require.Equal(t, bi, fileData)

}

Expand Down
Loading

0 comments on commit 5883310

Please sign in to comment.