blob: f1ae080dcb816cb8546507a1b4ef9f2b5e0f736e [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
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"
khenaidoo5fc5cea2021-08-11 17:39:16 -040023)
24
25// componentData records the settings for a component.
26type componentData struct {
27 name string
28}
29
30var cache = map[string]*componentData{}
31
Abhay Kumar03713392025-12-30 05:20:58 +000032func (c *componentData) InfoDepth(depth int, args ...any) {
33 args = append([]any{"[" + string(c.name) + "]"}, args...)
34 InfoDepth(depth+1, args...)
khenaidoo5fc5cea2021-08-11 17:39:16 -040035}
36
Abhay Kumar03713392025-12-30 05:20:58 +000037func (c *componentData) WarningDepth(depth int, args ...any) {
38 args = append([]any{"[" + string(c.name) + "]"}, args...)
39 WarningDepth(depth+1, args...)
khenaidoo5fc5cea2021-08-11 17:39:16 -040040}
41
Abhay Kumar03713392025-12-30 05:20:58 +000042func (c *componentData) ErrorDepth(depth int, args ...any) {
43 args = append([]any{"[" + string(c.name) + "]"}, args...)
44 ErrorDepth(depth+1, args...)
khenaidoo5fc5cea2021-08-11 17:39:16 -040045}
46
Abhay Kumar03713392025-12-30 05:20:58 +000047func (c *componentData) FatalDepth(depth int, args ...any) {
48 args = append([]any{"[" + string(c.name) + "]"}, args...)
49 FatalDepth(depth+1, args...)
khenaidoo5fc5cea2021-08-11 17:39:16 -040050}
51
Abhay Kumar03713392025-12-30 05:20:58 +000052func (c *componentData) Info(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040053 c.InfoDepth(1, args...)
54}
55
Abhay Kumar03713392025-12-30 05:20:58 +000056func (c *componentData) Warning(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040057 c.WarningDepth(1, args...)
58}
59
Abhay Kumar03713392025-12-30 05:20:58 +000060func (c *componentData) Error(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040061 c.ErrorDepth(1, args...)
62}
63
Abhay Kumar03713392025-12-30 05:20:58 +000064func (c *componentData) Fatal(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040065 c.FatalDepth(1, args...)
66}
67
Abhay Kumar03713392025-12-30 05:20:58 +000068func (c *componentData) Infof(format string, args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040069 c.InfoDepth(1, fmt.Sprintf(format, args...))
70}
71
Abhay Kumar03713392025-12-30 05:20:58 +000072func (c *componentData) Warningf(format string, args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040073 c.WarningDepth(1, fmt.Sprintf(format, args...))
74}
75
Abhay Kumar03713392025-12-30 05:20:58 +000076func (c *componentData) Errorf(format string, args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040077 c.ErrorDepth(1, fmt.Sprintf(format, args...))
78}
79
Abhay Kumar03713392025-12-30 05:20:58 +000080func (c *componentData) Fatalf(format string, args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040081 c.FatalDepth(1, fmt.Sprintf(format, args...))
82}
83
Abhay Kumar03713392025-12-30 05:20:58 +000084func (c *componentData) Infoln(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040085 c.InfoDepth(1, args...)
86}
87
Abhay Kumar03713392025-12-30 05:20:58 +000088func (c *componentData) Warningln(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040089 c.WarningDepth(1, args...)
90}
91
Abhay Kumar03713392025-12-30 05:20:58 +000092func (c *componentData) Errorln(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040093 c.ErrorDepth(1, args...)
94}
95
Abhay Kumar03713392025-12-30 05:20:58 +000096func (c *componentData) Fatalln(args ...any) {
khenaidoo5fc5cea2021-08-11 17:39:16 -040097 c.FatalDepth(1, args...)
98}
99
100func (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.
108func 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}