| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 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 | |
| 19 | package grpclog |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | ) |
| 24 | |
| 25 | // componentData records the settings for a component. |
| 26 | type componentData struct { |
| 27 | name string |
| 28 | } |
| 29 | |
| 30 | var cache = map[string]*componentData{} |
| 31 | |
| 32 | func (c *componentData) InfoDepth(depth int, args ...any) { |
| 33 | args = append([]any{"[" + string(c.name) + "]"}, args...) |
| 34 | InfoDepth(depth+1, args...) |
| 35 | } |
| 36 | |
| 37 | func (c *componentData) WarningDepth(depth int, args ...any) { |
| 38 | args = append([]any{"[" + string(c.name) + "]"}, args...) |
| 39 | WarningDepth(depth+1, args...) |
| 40 | } |
| 41 | |
| 42 | func (c *componentData) ErrorDepth(depth int, args ...any) { |
| 43 | args = append([]any{"[" + string(c.name) + "]"}, args...) |
| 44 | ErrorDepth(depth+1, args...) |
| 45 | } |
| 46 | |
| 47 | func (c *componentData) FatalDepth(depth int, args ...any) { |
| 48 | args = append([]any{"[" + string(c.name) + "]"}, args...) |
| 49 | FatalDepth(depth+1, args...) |
| 50 | } |
| 51 | |
| 52 | func (c *componentData) Info(args ...any) { |
| 53 | c.InfoDepth(1, args...) |
| 54 | } |
| 55 | |
| 56 | func (c *componentData) Warning(args ...any) { |
| 57 | c.WarningDepth(1, args...) |
| 58 | } |
| 59 | |
| 60 | func (c *componentData) Error(args ...any) { |
| 61 | c.ErrorDepth(1, args...) |
| 62 | } |
| 63 | |
| 64 | func (c *componentData) Fatal(args ...any) { |
| 65 | c.FatalDepth(1, args...) |
| 66 | } |
| 67 | |
| 68 | func (c *componentData) Infof(format string, args ...any) { |
| 69 | c.InfoDepth(1, fmt.Sprintf(format, args...)) |
| 70 | } |
| 71 | |
| 72 | func (c *componentData) Warningf(format string, args ...any) { |
| 73 | c.WarningDepth(1, fmt.Sprintf(format, args...)) |
| 74 | } |
| 75 | |
| 76 | func (c *componentData) Errorf(format string, args ...any) { |
| 77 | c.ErrorDepth(1, fmt.Sprintf(format, args...)) |
| 78 | } |
| 79 | |
| 80 | func (c *componentData) Fatalf(format string, args ...any) { |
| 81 | c.FatalDepth(1, fmt.Sprintf(format, args...)) |
| 82 | } |
| 83 | |
| 84 | func (c *componentData) Infoln(args ...any) { |
| 85 | c.InfoDepth(1, args...) |
| 86 | } |
| 87 | |
| 88 | func (c *componentData) Warningln(args ...any) { |
| 89 | c.WarningDepth(1, args...) |
| 90 | } |
| 91 | |
| 92 | func (c *componentData) Errorln(args ...any) { |
| 93 | c.ErrorDepth(1, args...) |
| 94 | } |
| 95 | |
| 96 | func (c *componentData) Fatalln(args ...any) { |
| 97 | c.FatalDepth(1, args...) |
| 98 | } |
| 99 | |
| 100 | func (c *componentData) V(l int) bool { |
| 101 | return V(l) |
| 102 | } |
| 103 | |
| 104 | // Component creates a new component and returns it for logging. If a component |
| 105 | // with the name already exists, nothing will be created and it will be |
| 106 | // returned. SetLoggerV2 will panic if it is called with a logger created by |
| 107 | // Component. |
| 108 | func Component(componentName string) DepthLoggerV2 { |
| 109 | if cData, ok := cache[componentName]; ok { |
| 110 | return cData |
| 111 | } |
| 112 | c := &componentData{componentName} |
| 113 | cache[componentName] = c |
| 114 | return c |
| 115 | } |