diff --git a/cmd/relayer/start/create_ibc_channel.go b/cmd/relayer/start/create_ibc_channel.go index 3d4321de..c6f4a3af 100644 --- a/cmd/relayer/start/create_ibc_channel.go +++ b/cmd/relayer/start/create_ibc_channel.go @@ -8,13 +8,18 @@ import ( "path/filepath" ) -func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error) { +func getLogFilePath(home string) string { + return filepath.Join(home, consts.ConfigDirName.Relayer, "relayer.log") +} + +func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig, logFileOption utils.CommandOption) (string, error) { /** Creates an IBC channel between the hub and the client, and return the source channel ID. */ + 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, @@ -25,7 +30,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 } } @@ -36,7 +41,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) diff --git a/cmd/relayer/start/start.go b/cmd/relayer/start/start.go index 65d38b1e..b2ac4817 100644 --- a/cmd/relayer/start/start.go +++ b/cmd/relayer/start/start.go @@ -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 := getLogFilePath(rollappConfig.Home) + 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) diff --git a/cmd/utils/bash_commands.go b/cmd/utils/bash_commands.go index d1e51903..30f5ee54 100644 --- a/cmd/utils/bash_commands.go +++ b/cmd/utils/bash_commands.go @@ -18,12 +18,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 @@ -40,9 +44,10 @@ func GetCommonDymdFlags(rollappConfig RollappConfig) []string { } } -type CommandOptions func(cmd *exec.Cmd) +type CommandOption func(cmd *exec.Cmd) -func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string, options ...CommandOptions) { +func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string, + options ...CommandOption) { for _, option := range options { option(cmd) } @@ -62,7 +67,7 @@ func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg s } } -func WithLogging(logFile string) CommandOptions { +func WithLogging(logFile string) CommandOption { return func(cmd *exec.Cmd) { logger := getLogger(logFile) cmd.Stdout = logger.Writer() diff --git a/cmd/utils/error_handling.go b/cmd/utils/error_handling.go index 5feb6478..d7a2269b 100644 --- a/cmd/utils/error_handling.go +++ b/cmd/utils/error_handling.go @@ -34,11 +34,15 @@ func ExecBashCommand(cmd *exec.Cmd) (bytes.Buffer, error) { return stdout, nil } -func ExecBashCmdWithOSOutput(cmd *exec.Cmd) error { +func ExecBashCmdWithOSOutput(cmd *exec.Cmd, options ...CommandOption) error { + for _, option := range options { + option(cmd) + } var stderr bytes.Buffer - cmd.Stdout = os.Stdout - mw := io.MultiWriter(os.Stderr, &stderr) - cmd.Stderr = mw + 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())