本文整理匯總了Golang中io.MultiWriter函數的典型用法代碼示例。如果您正苦於以下問題:Golang MultiWriter函數的具體用法?Golang MultiWriter怎麽用?Golang MultiWriter使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了MultiWriter函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Result
// Result attempts to simulate the running of a command by filling the
// appropriate buffers from its Test fields and forwarding its Test exit codes
func (c *DummyCommand) Result() (*Result, error) {
//command := exec.Command(c.Name, c.Args...)
outbuf := &bytes.Buffer{}
errbuf := &bytes.Buffer{}
combinedbuf := &bytes.Buffer{}
outWriters := []io.Writer{outbuf, combinedbuf}
errWriters := []io.Writer{errbuf, combinedbuf}
if c.Command.TeeOut != nil {
outWriters = append(outWriters, c.TeeOut)
}
if c.Command.TeeErr != nil {
errWriters = append(errWriters, c.TeeErr)
}
outWr := io.MultiWriter(outWriters...)
errWr := io.MultiWriter(errWriters...)
outWr.Write(c.DummyResult.SO)
errWr.Write(c.DummyResult.SE)
return &Result{
Command: &c.Command, // XXX thus the live command...
Stdout: &Output{outbuf},
Stderr: &Output{errbuf},
Combined: &Output{combinedbuf},
Err: c.DummyResult.Err,
ExitCode: c.DummyResult.Status,
}, nil
}
示例2: hashCopyN
// hashCopyN - Calculates Md5sum and SHA256sum for upto partSize amount of bytes.
func (c Client) hashCopyN(writer io.ReadWriter, reader io.Reader, partSize int64) (md5Sum, sha256Sum []byte, size int64, err error) {
// MD5 and SHA256 hasher.
var hashMD5, hashSHA256 hash.Hash
// MD5 and SHA256 hasher.
hashMD5 = md5.New()
hashWriter := io.MultiWriter(writer, hashMD5)
if c.signature.isV4() {
hashSHA256 = sha256.New()
hashWriter = io.MultiWriter(writer, hashMD5, hashSHA256)
}
// Copies to input at writer.
size, err = io.CopyN(hashWriter, reader, partSize)
if err != nil {
// If not EOF return error right here.
if err != io.EOF {
return nil, nil, 0, err
}
}
// Finalize md5shum and sha256 sum.
md5Sum = hashMD5.Sum(nil)
if c.signature.isV4() {
sha256Sum = hashSHA256.Sum(nil)
}
return md5Sum, sha256Sum, size, err
}
示例3: ExecuteCommand
func ExecuteCommand(command *ExecCmd) (output string, err error) {
// Execute the command using a shell
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = "/bin/sh"
flag = "-c"
}
// Setup the reader that will read the lines from the command
pr, pw := io.Pipe()
copyDoneCh := make(chan struct{})
go copyOutput(pr, copyDoneCh)
// Setup the command
cmd := exec.Command(shell, flag, command.Cmd)
out, _ := circbuf.NewBuffer(maxBufSize)
cmd.Stderr = io.MultiWriter(out, pw)
cmd.Stdout = io.MultiWriter(out, pw)
// Run the command to completion
runErr := cmd.Run()
pw.Close()
<-copyDoneCh
if runErr != nil {
return string(out.Bytes()), fmt.Errorf("Error running command '%s': %v. Output: %s", command, runErr, out.Bytes())
}
return string(out.Bytes()), nil
}
示例4: ProjectCmd
func (p *Project) ProjectCmd() *exec.Cmd {
name := p.Name()
if runtime.GOOS != "windows" {
name = "./" + name
}
args := []string{"-config", p.configPath, fmt.Sprintf("-port=%d", p.port)}
if p.noDebug {
args = append(args, "-debug=false", "-template-debug=false", "-log-debug=false")
} else {
if p.profile {
args = append(args, "-debug=false", "-template-debug=false", "-log-debug")
} else {
args = append(args, "-debug", "-template-debug", "-log-debug")
}
}
if p.noCache {
args = append(args, "-cache=dummy://")
}
cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = io.MultiWriter(os.Stdout, &p.out)
cmd.Stderr = io.MultiWriter(os.Stderr, &p.out)
cmd.Dir = p.dir
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, devserver.EnvVar+"=1")
cmd.Env = append(cmd.Env, "GONDOLA_FORCE_TTY=1")
if p.profile {
cmd.Env = append(cmd.Env, "GONDOLA_NO_CACHE_LAYER=1")
}
return cmd
}
示例5: runSudoCommand
func (s *sshConn) runSudoCommand(sess *ssh.Session, stdin io.Writer, cmd string) error {
// Wrap the output
if s.DebugEnabled() {
debugOutWriter := util.NewDebugLogWriter("SSH OUT:", s.Context)
if sess.Stdout != nil {
sess.Stdout = io.MultiWriter(sess.Stdout, debugOutWriter)
} else {
sess.Stdout = debugOutWriter
}
debugErrWriter := util.NewDebugLogWriter("SSH ERR:", s.Context)
if sess.Stderr != nil {
sess.Stderr = io.MultiWriter(sess.Stderr, debugErrWriter)
} else {
sess.Stderr = debugErrWriter
}
}
// We need a checker to enter the password
passwordTyper := util.NewExpectListener(stdin, shell.SudoPasswordPromptMatch, s.server.SSH.Pass+"\n")
if sess.Stdout == nil {
sess.Stdout = passwordTyper
} else {
sess.Stdout = io.MultiWriter(sess.Stdout, passwordTyper)
}
if sess.Stderr == nil {
sess.Stderr = passwordTyper
} else {
sess.Stderr = io.MultiWriter(sess.Stderr, passwordTyper)
}
if err := sess.Run("sudo -S " + cmd); err != nil {
return fmt.Errorf("Error running command %v: %v", cmd, err)
}
return nil
}
示例6: InitLog
func InitLog() {
// ディレクトリの作成
err := os.MkdirAll("log", 0777)
if err != nil {
util.Perror(err)
}
accessLogFile, err := os.OpenFile("log/access.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("[Error]: %s", err))
}
errorLogFile, err := os.OpenFile("log/error.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("[Error]: %s", err))
}
access_out := io.MultiWriter(os.Stdout, accessLogFile)
accessLog.Formatter = &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
accessLog.Out = access_out
error_out := io.MultiWriter(os.Stdout, errorLogFile)
errorLog.Formatter = &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
errorLog.Out = error_out
}
示例7: runCmd
func runCmd(cmd string, args []string, env []string, stdOut, errOut io.Writer) error {
glog.V(5).Infof("Executing local command:")
glog.V(5).Infof(" %s", cmd)
for _, a := range args {
glog.V(5).Infof(" %s", a)
}
if len(env) > 0 {
glog.V(5).Infof("Environment:")
for _, e := range env {
glog.V(5).Infof(" %s", e)
}
}
c := exec.Command(cmd, args...)
outLog := &bytes.Buffer{}
errLog := &bytes.Buffer{}
c.Stdout = io.MultiWriter(stdOut, outLog)
c.Stderr = io.MultiWriter(errOut, errLog)
c.Env = env
err := c.Run()
if glog.V(5) {
if err != nil {
glog.Infof("Error from execution: %v", err)
}
if outLog.Len() > 0 {
glog.Infof("Stdout:\n%s", outLog.String())
}
if errLog.Len() > 0 {
glog.Infof("Errout:\n%s", errLog.String())
}
}
if err != nil {
return newExecError(append([]string{cmd}, args...), err, outLog.Bytes(), errLog.Bytes())
}
return nil
}
示例8: setupLogger
func setupLogger(logDir string) *log.Logger {
var logger *log.Logger
log.SetFlags(log.Ldate | log.Lmicroseconds)
if logDir != "" {
logFile := filepath.Join(logDir, "host.log")
if err := os.MkdirAll(filepath.Dir(logFile), 0755); err != nil {
log.Fatalf("could not not mkdir for logs: %s", err)
}
hostlog, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer hostlog.Close()
log.Printf("Logging to %s\n", logFile)
log.SetOutput(io.MultiWriter(hostlog, os.Stdout))
logger = log.New(io.MultiWriter(hostlog, os.Stderr), "", log.Flags())
} else {
logger = log.New(os.Stderr, "", log.Flags())
}
return logger
}
示例9: Run
// Run executes a Config and returns associated results.
func (c Config) Run() (r Result, err error) {
dockerized, err := dockerize(c.Volume)
if err != nil {
return
}
cmd := exec.Command(
"docker",
"run",
"-v",
dockerized+":/run",
c.Image)
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
return
}
if err = cmd.Start(); err != nil {
return
}
go io.Copy(io.MultiWriter(os.Stdout, &r.Stdout), stdout)
go io.Copy(io.MultiWriter(os.Stdout, &r.Stderr), stderr)
cmd.Wait()
return
}
示例10: hashCopyBuffer
// hashCopyBuffer is identical to hashCopyN except that it stages
// through the provided buffer (if one is required) rather than
// allocating a temporary one. If buf is nil, one is allocated for 5MiB.
func (c Client) hashCopyBuffer(writer io.Writer, reader io.Reader, buf []byte) (md5Sum, sha256Sum []byte, size int64, err error) {
// MD5 and SHA256 hasher.
var hashMD5, hashSHA256 hash.Hash
// MD5 and SHA256 hasher.
hashMD5 = md5.New()
hashWriter := io.MultiWriter(writer, hashMD5)
if c.signature.isV4() {
hashSHA256 = sha256.New()
hashWriter = io.MultiWriter(writer, hashMD5, hashSHA256)
}
// Allocate buf if not initialized.
if buf == nil {
buf = make([]byte, optimalReadBufferSize)
}
// Using copyBuffer to copy in large buffers, default buffer
// for io.Copy of 32KiB is too small.
size, err = copyBuffer(hashWriter, reader, buf)
if err != nil {
return nil, nil, 0, err
}
// Finalize md5 sum and sha256 sum.
md5Sum = hashMD5.Sum(nil)
if c.signature.isV4() {
sha256Sum = hashSHA256.Sum(nil)
}
return md5Sum, sha256Sum, size, err
}
示例11: Setup
func Setup(config *Config) error {
var logOutput io.Writer
// Setup the default logging
logFilter := NewLogFilter()
logFilter.MinLevel = logutils.LogLevel(strings.ToUpper(config.Level))
logFilter.Writer = config.Writer
if !ValidateLevelFilter(logFilter.MinLevel, logFilter) {
levels := make([]string, 0, len(logFilter.Levels))
for _, level := range logFilter.Levels {
levels = append(levels, string(level))
}
return fmt.Errorf("invalid log level %q, valid log levels are %s",
config.Level, strings.Join(levels, ", "))
}
// Check if syslog is enabled
if config.Syslog {
log.Printf("[DEBUG] (logging) enabling syslog on %s", config.SyslogFacility)
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, config.SyslogFacility, config.Name)
if err != nil {
return fmt.Errorf("error setting up syslog logger: %s", err)
}
syslog := &SyslogWrapper{l, logFilter}
logOutput = io.MultiWriter(logFilter, syslog)
} else {
logOutput = io.MultiWriter(logFilter)
}
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
log.SetOutput(logOutput)
return nil
}
示例12: main
//Continually processes postback objects from the local Redis database server
//Precondition: The file at LOG_FILE_NAME must be acessable to write logs
// A local Redis database server must be running on port 6379
//Postcondition: Any Postback objects received from the Redis database's List, REDIS_LIST_NAME, are processed
// Log levels are set up for the rest of the program to use, all pertinent inforamtion is logged
func main() {
logFile, logFileError := os.OpenFile(LOG_FILE_NAME, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if logFileError != nil {
log.Fatalln("Failed to open log file: ", logFileError)
}
defer logFile.Close()
var traceOutput io.Writer
if SHOW_TRACES {
traceOutput = os.Stdout
} else {
traceOutput = ioutil.Discard
}
infoOutput := io.MultiWriter(logFile, os.Stdout)
warningOutput := io.MultiWriter(logFile, os.Stdout)
errorOutput := io.MultiWriter(logFile, os.Stderr)
InitLoggers(traceOutput, infoOutput, warningOutput, errorOutput)
redisServer, err := redis.Dial("tcp", ":6379")
if err != nil {
Error.Fatalln(err)
}
defer redisServer.Close()
for {
processPostbackObject(redisServer)
}
}
示例13: New
// New creates a new Logger which writes to a file and to stderr
func New(path string, production bool) *Logger {
var logWriter io.Writer
stdlog.SetFlags(stdlog.Llongfile)
// doubleWriter writes to stdErr and to a file
logFile, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
if err != nil {
logWriter = io.MultiWriter(os.Stderr)
} else {
// Do not write to Stderr in production
if production {
logWriter = io.MultiWriter(logFile)
} else {
logWriter = io.MultiWriter(os.Stderr, logFile)
}
}
// By default logger logs to console and a file
l := stdlog.New(logWriter, "", stdlog.Ldate|stdlog.Ltime)
if l == nil {
stdlog.Printf("Error setting up log at path %s", path)
}
logger := &Logger{
log: l,
Filter: "",
}
logger.Printf("#info Opened log file at %s", path)
return logger
}
示例14: initLoggers
func initLoggers() {
var writer io.Writer = ioutil.Discard
var errWriter io.Writer = ioutil.Discard
if Config.GetBool("log.enableStdOut") {
writer = io.MultiWriter(writer, os.Stdout)
}
if Config.GetBool("log.enableStdErr") {
errWriter = io.MultiWriter(errWriter, os.Stderr)
}
logFilePath := Config.GetString("log.filePath")
if len(logFilePath) > 0 {
if file, err := os.Open(logFilePath); err == nil {
writer = io.MultiWriter(writer, file)
errWriter = io.MultiWriter(errWriter, file)
}
}
LogV = log.New(writer, "[VERBOSE]:", log.Ldate|log.Ltime|log.Lshortfile)
LogD = log.New(writer, "[DEBUG]:", log.Ldate|log.Ltime|log.Lshortfile)
LogE = log.New(errWriter, "[ERROR]:", log.Ldate|log.Ltime|log.Lshortfile)
LogW = log.New(errWriter, "[WARNING]:", log.Ldate|log.Ltime|log.Lshortfile)
//fmt.Printf("Log enable stdout: %v\n", Config.GetBool("log.enableStdOut"))
//fmt.Printf("Log enable stderr: %v\n", Config.GetBool("log.enableStdErr"))
}
示例15: execute
func (C *CMD) execute() (code int, err error) {
c := exec.Command(C.Name, C.Args...)
c.Stdout = C.Stdout
c.Stderr = C.Stderr
c.Env = os.Environ()
if C.EchoStdout {
c.Stdout = io.MultiWriter(os.Stdout, c.Stdout)
}
if C.EchoStderr {
c.Stderr = io.MultiWriter(os.Stderr, c.Stderr)
}
if C.WriteStdout != nil {
c.Stdout = io.MultiWriter(C.WriteStdout, c.Stdout)
}
if C.WriteStderr != nil {
c.Stderr = io.MultiWriter(C.WriteStderr, c.Stderr)
}
if C.EchoStdout || C.EchoStderr {
cli.Logf("shell> %s", C)
}
if err := c.Start(); err != nil {
cli.Fatalf("Unable to begin command execution; %s", err)
}
err = c.Wait()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), err
}
}
cli.Fatalf("Command failed, unable to get exit code: %s", C)
}
return 0, nil
}