MCPcopy Create free account
hub / github.com/celer-pkg/celer / createLogFile

Method createLogFile

pkgs/cmd/exec.go:125–162  ·  view source on GitHub ↗

createLogFile creates and initializes a log file with environment variables and command info.

(cmd *exec.Cmd)

Source from the content-addressed store, hash-verified

123
124// createLogFile creates and initializes a log file with environment variables and command info.
125func (e *executor) createLogFile(cmd *exec.Cmd) (*os.File, error) {
126 if e.logPath == "" {
127 return nil, nil
128 }
129
130 // Create directory hierarchy if needed.
131 if err := os.MkdirAll(filepath.Dir(e.logPath), os.ModePerm); err != nil {
132 return nil, fmt.Errorf("failed to create log directory -> %w", err)
133 }
134 logFile, err := os.Create(e.logPath)
135 if err != nil {
136 return nil, fmt.Errorf("failed to create log file -> %w", err)
137 }
138
139 // Write environment variables.
140 var buffer bytes.Buffer
141 for _, envVar := range cmd.Env {
142 fmt.Fprintf(&buffer, "%s\n", envVar)
143 }
144
145 // Write headers to log file.
146 if _, err := io.WriteString(logFile, fmt.Sprintf("Environment:\n%s\n", buffer.String())); err != nil {
147 logFile.Close()
148 return nil, fmt.Errorf("failed to write environment to log -> %w", err)
149 }
150
151 commandLine := e.command
152 if len(e.args) > 0 {
153 commandLine += " " + strings.Join(e.args, " ")
154 }
155
156 if _, err := io.WriteString(logFile, fmt.Sprintf("%s: %s\n\n", e.title, commandLine)); err != nil {
157 logFile.Close()
158 return nil, fmt.Errorf("failed to write command to log -> %w", err)
159 }
160
161 return logFile, nil
162}
163
164// configureOutputs routes command output to appropriate destinations:
165// - To stdout/stderr if output is nil (Execute)

Callers 6

doExecuteMethod · 0.95
doExecuteMethod · 0.95

Calls 3

MkdirAllMethod · 0.80
SprintfMethod · 0.80
StringMethod · 0.45