Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Output of DA Light Client and Relayer written to dedicated log files #91

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func Cmd() *cobra.Command {
rollappConfig, err := utils.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
rpcEndpoint := cmd.Flag(rpcEndpointFlag).Value.String()
startRollappCmd := getStartCelestiaLCCmd(rollappConfig, rpcEndpoint)
utils.RunBashCmdAsync(startRollappCmd, printOutput, parseError)
startDACmd := getStartCelestiaLCCmd(rollappConfig, rpcEndpoint)
logFilePath := filepath.Join(rollappConfig.Home, consts.ConfigDirName.DALightNode, "light_client.log")
utils.RunBashCmdAsync(startDACmd, printOutput, parseError, utils.WithLogging(logFilePath))
},
}
utils.AddGlobalFlags(runCmd)
Expand Down
8 changes: 4 additions & 4 deletions cmd/relayer/start/create_ibc_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

// Creates an IBC channel between the hub and the client, and return the source channel ID.
func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error) {
func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig, logFileOption utils.CommandOption) (string, error) {
createClientsCmd := getCreateClientsCmd(rollappConfig, rollappConfig.RollappID, rollappConfig.HubData.ID)
fmt.Println("Creating clients...")
if err := utils.ExecBashCmdWithOSOutput(createClientsCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createClientsCmd, logFileOption); err != nil {
return "", err
}
dstConnectionId, err := GetDstConnectionIDFromYAMLFile(filepath.Join(rollappConfig.Home, consts.ConfigDirName.Relayer,
Expand All @@ -23,7 +23,7 @@ func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error)
if dstConnectionId == "" {
createConnectionCmd := getCreateConnectionCmd(rollappConfig)
fmt.Println("Creating connection...")
if err := utils.ExecBashCmdWithOSOutput(createConnectionCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createConnectionCmd, logFileOption); err != nil {
return "", err
}
}
Expand All @@ -34,7 +34,7 @@ func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error)
if srcChannelId == "" {
createChannelCmd := getCreateChannelCmd(rollappConfig)
fmt.Println("Creating channel...")
if err := utils.ExecBashCmdWithOSOutput(createChannelCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createChannelCmd, logFileOption); err != nil {
return "", err
}
srcChannelId, err = GetSourceChannelForConnection(dstConnectionId, rollappConfig)
Expand Down
10 changes: 6 additions & 4 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ func Start() *cobra.Command {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := utils.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
srcChannelId, err := createIBCChannelIfNeeded(rollappConfig)
relayerLogFilePath := filepath.Join(home, consts.ConfigDirName.Relayer, "relayer.log")
logFileOption := utils.WithLogging(relayerLogFilePath)
srcChannelId, err := createIBCChannelIfNeeded(rollappConfig, logFileOption)
utils.PrettifyErrorIfExists(err)
updateClientsCmd := getUpdateClientsCmd(rollappConfig)
utils.RunCommandEvery(updateClientsCmd.Path, updateClientsCmd.Args[1:], 60)
utils.RunCommandEvery(updateClientsCmd.Path, updateClientsCmd.Args[1:], 60, logFileOption)
relayPacketsCmd := getRelayPacketsCmd(rollappConfig, srcChannelId)
utils.RunCommandEvery(relayPacketsCmd.Path, relayPacketsCmd.Args[1:], 30)
utils.RunCommandEvery(relayPacketsCmd.Path, relayPacketsCmd.Args[1:], 30, logFileOption)
startCmd := getRlyStartCmd(rollappConfig)
utils.RunBashCmdAsync(startCmd, func() {
fmt.Printf("💈 The relayer is running successfully on you local machine on channel %s!", srcChannelId)
}, parseError)
}, parseError, logFileOption)
},
}
utils.AddGlobalFlags(registerCmd)
Expand Down
82 changes: 57 additions & 25 deletions cmd/utils/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"errors"
"fmt"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"log"
"os"
"os/exec"
"strings"
Expand All @@ -17,12 +19,16 @@ func GetRelayerDefaultFlags(root string) []string {
}
}

func RunCommandEvery(command string, args []string, intervalSec int) {
func RunCommandEvery(command string, args []string, intervalSec int, options ...CommandOption) {
go func() {
for {
cmd := exec.Command(command, args...)
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
errmw := io.MultiWriter(&stderr, cmd.Stderr)
cmd.Stderr = errmw
err := cmd.Run()
if err != nil {
// get the cmd args joined by space
Expand All @@ -39,27 +45,16 @@ func GetCommonDymdFlags(rollappConfig RollappConfig) []string {
}
}

func ExecBashCommand(cmd *exec.Cmd) (bytes.Buffer, error) {
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
return stdout, fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String())
}
return stdout, nil
}
type CommandOption func(cmd *exec.Cmd)

func ExecBashCommandWithOSOutput(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string) {
func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string,
options ...CommandOption) {
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
mw := io.MultiWriter(cmd.Stderr, &stderr)
cmd.Stderr = mw
err := cmd.Start()
if err != nil {
errMsg := parseError(stderr.String())
Expand All @@ -73,11 +68,48 @@ func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg s
}
}

func ExecBashCmdWithOSOutput(cmd *exec.Cmd) error {
func WithLogging(logFile string) CommandOption {
return func(cmd *exec.Cmd) {
logger := getLogger(logFile)
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
}
}

func getLogger(filepath string) *log.Logger {
lumberjackLogger := &lumberjack.Logger{
Filename: filepath,
MaxSize: 500,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
}
multiWriter := io.MultiWriter(lumberjackLogger)
logger := log.New(multiWriter, "", log.LstdFlags)
return logger
}

func ExecBashCommand(cmd *exec.Cmd) (bytes.Buffer, error) {
var stderr bytes.Buffer
cmd.Stdout = os.Stdout
mw := io.MultiWriter(os.Stderr, &stderr)
cmd.Stderr = mw
var stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
return stdout, fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String())
}
return stdout, nil
}

func ExecBashCmdWithOSOutput(cmd *exec.Cmd, options ...CommandOption) error {
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
outmw := io.MultiWriter(cmd.Stdout, os.Stdout)
cmd.Stdout = outmw
errmw := io.MultiWriter(os.Stderr, &stderr, cmd.Stderr)
cmd.Stderr = errmw
err := cmd.Run()
if err != nil {
return fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ require (
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down