GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: EUPL-1.2
 4
 5package logger
 6
 7import (
 8	"context"
 9	"fmt"
10)
11
12type contextKey string
13
14func (c contextKey) String() string {
15	return string(c)
16}
17
18var (
19	contextKeyCaller = contextKey("caller")
20)
21
22func AddCaller(ctx context.Context, caller string) context.Context {
23	if parentCaller, ok := extractCaller(ctx); ok {
24		caller = fmt.Sprintf("%s -> %s", parentCaller, caller)
25	}
26	return context.WithValue(ctx, contextKeyCaller, caller)
27}
28
29func extractCaller(ctx context.Context) (string, bool) {
30	caller, ok := ctx.Value(contextKeyCaller).(string)
31	return caller, ok
32}