xref: /aosp_15_r20/external/tink/go/internal/internalregistry/internal_registry.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17// Package internalregistry provides a container for functionality that is required
18// across Tink similar to the `registry` but isn't part of the public API.
19package internalregistry
20
21import (
22	"fmt"
23	"sync"
24
25	"github.com/google/tink/go/monitoring"
26)
27
28var (
29	monitoringClientMu sync.RWMutex
30	monitoringClient   monitoring.Client = defaultClient
31)
32
33type doNothingLogger struct{}
34
35var _ monitoring.Logger = (*doNothingLogger)(nil)
36
37func (l *doNothingLogger) Log(uint32, int) {}
38
39func (l *doNothingLogger) LogFailure() {}
40
41var defaultLogger = &doNothingLogger{}
42
43type doNothingClient struct{}
44
45var _ monitoring.Client = (*doNothingClient)(nil)
46
47func (c *doNothingClient) NewLogger(*monitoring.Context) (monitoring.Logger, error) {
48	return defaultLogger, nil
49}
50
51var defaultClient = &doNothingClient{}
52
53// RegisterMonitoringClient registers a client that can create loggers.
54func RegisterMonitoringClient(client monitoring.Client) error {
55	monitoringClientMu.Lock()
56	defer monitoringClientMu.Unlock()
57	if monitoringClient != nil && monitoringClient != defaultClient {
58		return fmt.Errorf("monitoring client is already registered")
59	}
60	monitoringClient = client
61	return nil
62}
63
64// ClearMonitoringClient removes the registered monitoring client.
65func ClearMonitoringClient() {
66	monitoringClientMu.Lock()
67	defer monitoringClientMu.Unlock()
68	monitoringClient = defaultClient
69}
70
71// GetMonitoringClient returns the registered monitoring client.
72func GetMonitoringClient() monitoring.Client {
73	return monitoringClient
74}
75