blob: 8358dd6e2abb9a942d293d645be938e77ad4aec8 [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"
23
24 "google.golang.org/grpc/internal/grpclog"
25)
26
27// componentData records the settings for a component.
28type componentData struct {
29 name string
30}
31
32var cache = map[string]*componentData{}
33
34func (c *componentData) InfoDepth(depth int, args ...interface{}) {
35 args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
36 grpclog.InfoDepth(depth+1, args...)
37}
38
39func (c *componentData) WarningDepth(depth int, args ...interface{}) {
40 args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
41 grpclog.WarningDepth(depth+1, args...)
42}
43
44func (c *componentData) ErrorDepth(depth int, args ...interface{}) {
45 args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
46 grpclog.ErrorDepth(depth+1, args...)
47}
48
49func (c *componentData) FatalDepth(depth int, args ...interface{}) {
50 args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
51 grpclog.FatalDepth(depth+1, args...)
52}
53
54func (c *componentData) Info(args ...interface{}) {
55 c.InfoDepth(1, args...)
56}
57
58func (c *componentData) Warning(args ...interface{}) {
59 c.WarningDepth(1, args...)
60}
61
62func (c *componentData) Error(args ...interface{}) {
63 c.ErrorDepth(1, args...)
64}
65
66func (c *componentData) Fatal(args ...interface{}) {
67 c.FatalDepth(1, args...)
68}
69
70func (c *componentData) Infof(format string, args ...interface{}) {
71 c.InfoDepth(1, fmt.Sprintf(format, args...))
72}
73
74func (c *componentData) Warningf(format string, args ...interface{}) {
75 c.WarningDepth(1, fmt.Sprintf(format, args...))
76}
77
78func (c *componentData) Errorf(format string, args ...interface{}) {
79 c.ErrorDepth(1, fmt.Sprintf(format, args...))
80}
81
82func (c *componentData) Fatalf(format string, args ...interface{}) {
83 c.FatalDepth(1, fmt.Sprintf(format, args...))
84}
85
86func (c *componentData) Infoln(args ...interface{}) {
87 c.InfoDepth(1, args...)
88}
89
90func (c *componentData) Warningln(args ...interface{}) {
91 c.WarningDepth(1, args...)
92}
93
94func (c *componentData) Errorln(args ...interface{}) {
95 c.ErrorDepth(1, args...)
96}
97
98func (c *componentData) Fatalln(args ...interface{}) {
99 c.FatalDepth(1, args...)
100}
101
102func (c *componentData) V(l int) bool {
103 return 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 {
111 if cData, ok := cache[componentName]; ok {
112 return cData
113 }
114 c := &componentData{componentName}
115 cache[componentName] = c
116 return c
117}