blob: 7e7fd8d17470eee77bd90812db3ffbd10cfb91db [file] [log] [blame]
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package helpers
import (
"go.chromium.org/chromiumos/test/execution/errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
// CreateLogFile creates a file and its parent directory for logging purpose.
func CreateLogFile(fullPath string) (*os.File, error) {
if err := os.MkdirAll(fullPath, 0755); err != nil {
return nil, errors.NewStatusError(errors.IOCreateError,
fmt.Errorf("failed to create directory %v: %w", fullPath, err))
}
logFullPathName := filepath.Join(fullPath, "log.txt")
// Log the full output of the command to disk.
logFile, err := os.Create(logFullPathName)
if err != nil {
return nil, errors.NewStatusError(errors.IOCreateError,
fmt.Errorf("failed to create file %v: %w", fullPath, err))
}
return logFile, nil
}
// NewLogger creates a logger. Using go default logger for now.
func NewLogger(logFile *os.File) *log.Logger {
mw := io.MultiWriter(logFile, os.Stderr)
return log.New(mw, "", log.LstdFlags|log.LUTC)
}