podman
117 строк · 2.9 Кб
1/*
2*
3* Copyright 2020 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19package grpclog
20
21import (
22"fmt"
23
24"google.golang.org/grpc/internal/grpclog"
25)
26
27// componentData records the settings for a component.
28type componentData struct {
29name string
30}
31
32var cache = map[string]*componentData{}
33
34func (c *componentData) InfoDepth(depth int, args ...any) {
35args = append([]any{"[" + string(c.name) + "]"}, args...)
36grpclog.InfoDepth(depth+1, args...)
37}
38
39func (c *componentData) WarningDepth(depth int, args ...any) {
40args = append([]any{"[" + string(c.name) + "]"}, args...)
41grpclog.WarningDepth(depth+1, args...)
42}
43
44func (c *componentData) ErrorDepth(depth int, args ...any) {
45args = append([]any{"[" + string(c.name) + "]"}, args...)
46grpclog.ErrorDepth(depth+1, args...)
47}
48
49func (c *componentData) FatalDepth(depth int, args ...any) {
50args = append([]any{"[" + string(c.name) + "]"}, args...)
51grpclog.FatalDepth(depth+1, args...)
52}
53
54func (c *componentData) Info(args ...any) {
55c.InfoDepth(1, args...)
56}
57
58func (c *componentData) Warning(args ...any) {
59c.WarningDepth(1, args...)
60}
61
62func (c *componentData) Error(args ...any) {
63c.ErrorDepth(1, args...)
64}
65
66func (c *componentData) Fatal(args ...any) {
67c.FatalDepth(1, args...)
68}
69
70func (c *componentData) Infof(format string, args ...any) {
71c.InfoDepth(1, fmt.Sprintf(format, args...))
72}
73
74func (c *componentData) Warningf(format string, args ...any) {
75c.WarningDepth(1, fmt.Sprintf(format, args...))
76}
77
78func (c *componentData) Errorf(format string, args ...any) {
79c.ErrorDepth(1, fmt.Sprintf(format, args...))
80}
81
82func (c *componentData) Fatalf(format string, args ...any) {
83c.FatalDepth(1, fmt.Sprintf(format, args...))
84}
85
86func (c *componentData) Infoln(args ...any) {
87c.InfoDepth(1, args...)
88}
89
90func (c *componentData) Warningln(args ...any) {
91c.WarningDepth(1, args...)
92}
93
94func (c *componentData) Errorln(args ...any) {
95c.ErrorDepth(1, args...)
96}
97
98func (c *componentData) Fatalln(args ...any) {
99c.FatalDepth(1, args...)
100}
101
102func (c *componentData) V(l int) bool {
103return V(l)
104}
105
106// Component creates a new component and returns it for logging. If a component
107// with the name already exists, nothing will be created and it will be
108// returned. SetLoggerV2 will panic if it is called with a logger created by
109// Component.
110func Component(componentName string) DepthLoggerV2 {
111if cData, ok := cache[componentName]; ok {
112return cData
113}
114c := &componentData{componentName}
115cache[componentName] = c
116return c
117}
118