loggedexec

package
v0.0.0-...-f79670e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2016 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

loggedexec is a wrapper around os/exec which logs command execution, the command’s stdout/stderr into files and provides better error messages when command execution fails. This makes debugging easier for end users.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoggedCmd

type LoggedCmd struct {
	*exec.Cmd

	// Logger will be used to log invocation commands for human
	// consumption. Defaults to logging to os.Stderr. Use
	// ioutil.Discard to hide logs.
	Logger *log.Logger

	// LogDir is the directory in which log files will be
	// created. Defaults to os.TempDir().
	LogDir string

	// LogFmt is the prefix used for naming log files in
	// LogDir. Defaults to "%03d-" and must contain precisely one "%d"
	// which will be replaced with the invocation count.
	LogFmt string
}

LoggedCmd is like (os/exec).Cmd, but its Run() method additionally:

  • Logs each invocation’s command for human consumption.
  • Logs each invocation’s working directory, Args, Env and timing into a file.
  • Logs each invocation’s stdout/stderr into a file.
  • Wraps the returned error (if any) with the command and pointers to the log files with more details (including the first line of stdout/stderr).

All files are created in LogDir.

func Command

func Command(name string, arg ...string) *LoggedCmd

Command is like (os/exec).Command, but returns a LoggedCmd.

Example
package main

import (
	"fmt"

	"github.com/Debian/mergebot/loggedexec"
)

func main() {
	cmd := loggedexec.Command("ls", "/tmp/nonexistant")
	cmd.Env = []string{"LANG=C"}
	if err := cmd.Run(); err != nil {
		fmt.Println(err)
	}
}
Output:

Running "ls /tmp/nonexistant": exit status 2
See "/tmp/000-ls.invocation.log" for invocation details.
See "/tmp/000-ls.stdoutstderr.log" for full stdout/stderr.
First stdout/stderr line: "ls: cannot access /tmp/nonexistant: No such file or directory"

func (*LoggedCmd) Run

func (l *LoggedCmd) Run() error

Run is a wrapper around (os/exec).Cmd’s Run().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL