-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/rsteube/carapace/internal/env" | ||
"github.com/rsteube/carapace/internal/uid" | ||
"github.com/rsteube/carapace/pkg/ps" | ||
) | ||
|
||
var LOG = log.New(ioutil.Discard, "", log.Flags()) | ||
|
||
func init() { | ||
if !env.Log() { | ||
return | ||
} | ||
|
||
tmpdir := fmt.Sprintf("%v/carapace", os.TempDir()) | ||
if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
file := fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable()) | ||
if logfileWriter, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o666); err != nil { | ||
log.Fatal(err.Error()) | ||
} else { | ||
LOG = log.New(logfileWriter, ps.DetermineShell()+" ", log.Flags()|log.Lmsgprefix|log.Lmicroseconds) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,5 @@ | ||
package carapace | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
import "github.com/rsteube/carapace/internal/log" | ||
|
||
"github.com/rsteube/carapace/internal/env" | ||
"github.com/rsteube/carapace/internal/uid" | ||
"github.com/rsteube/carapace/pkg/ps" | ||
) | ||
|
||
var LOG = log.New(ioutil.Discard, "", log.Flags()) | ||
|
||
func init() { | ||
if !env.Log() { | ||
return | ||
} | ||
|
||
tmpdir := fmt.Sprintf("%v/carapace", os.TempDir()) | ||
if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
file := fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable()) | ||
if logfileWriter, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o666); err != nil { | ||
log.Fatal(err.Error()) | ||
} else { | ||
LOG = log.New(logfileWriter, ps.DetermineShell()+" ", log.Flags()|log.Lmsgprefix|log.Lmicroseconds) | ||
} | ||
} | ||
var LOG = log.LOG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package execlog | ||
|
||
import ( | ||
"github.com/rsteube/carapace/internal/log" | ||
"github.com/rsteube/carapace/pkg/util" | ||
"github.com/rsteube/carapace/third_party/golang.org/x/sys/execabs" | ||
) | ||
|
||
type Cmd struct { | ||
*execabs.Cmd | ||
} | ||
|
||
// Command is like exec.Command but logs args on execution. | ||
func Command(name string, arg ...string) *Cmd { | ||
cmd := &Cmd{ | ||
execabs.Command(name, arg...), | ||
} | ||
return cmd | ||
} | ||
|
||
func (c *Cmd) Run() error { | ||
log.LOG.Printf("executing %#v", util.FormatCmd(c.Args...)) | ||
return c.Cmd.Run() | ||
} | ||
|
||
func (c *Cmd) Start() error { | ||
log.LOG.Printf("executing %#v", util.FormatCmd(c.Args...)) | ||
return c.Cmd.Start() | ||
} | ||
|
||
func LookPath(file string) (string, error) { | ||
return execabs.LookPath(file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func FormatCmd(args ...string) string { | ||
replacer := strings.NewReplacer( | ||
"$", "\\$", | ||
"`", "\\`", | ||
) | ||
|
||
formatted := make([]string, 0, len(args)) | ||
for _, arg := range args { | ||
switch { | ||
case strings.ContainsAny(arg, `"' `+"\n\r\t"): | ||
formatted = append(formatted, replacer.Replace(fmt.Sprintf("%#v", arg))) | ||
default: | ||
formatted = append(formatted, arg) | ||
} | ||
} | ||
return strings.Join(formatted, " ") | ||
} |