| // Copyright 2025 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // Package log provides a wrapper over logging interfaces to simplify usage in recovery lib. |
| // |
| // If Logger not set then default golang log package is using. |
| package karte |
| |
| import ( |
| "context" |
| |
| "go.chromium.org/infra/cros/recovery/logger/metrics" |
| ) |
| |
| // failureKeyType is a unique type for a context key. |
| type failureKeyType string |
| |
| const ( |
| // failureKey is key to access to failure from context. |
| failureKey failureKeyType = "recovery_failure" |
| ) |
| |
| // FailureSaver a function to provide contextless saver of failure metrics. |
| type FailureSaver func(failure *metrics.Failure) error |
| |
| // WithFailure sets failure to the context. |
| // If failure is not provided process will be finished with panic. |
| func WithFailure(ctx context.Context, f FailureSaver) context.Context { |
| if f == nil { |
| panic("failure is not provided") |
| } |
| return context.WithValue(ctx, failureKey, f) |
| } |
| |
| // Get failure saver from context. |
| func GetFailureSaver(ctx context.Context) FailureSaver { |
| if failure, ok := ctx.Value(failureKey).(FailureSaver); ok { |
| return failure |
| } |
| return nil |
| } |