File: io_sink.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.17.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 25,988 kB
  • sloc: perl: 533; ansic: 491; python: 432; sh: 327; makefile: 174
file content (63 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package logger

import (
	"encoding/json"
	"io"
	"math"
	"sync"
	"time"
)

// IOSink writes a JSON-encoded message to the io.Writer.
type IOSink struct {
	enc *json.Encoder

	// encMu protects the encoder from concurrent writes. While the logger
	// itself does not concurrently write to the sink, the sink may be used
	// concurrently within the driver.
	encMu sync.Mutex
}

// Compile-time check to ensure IOSink implements the LogSink interface.
var _ LogSink = &IOSink{}

// NewIOSink will create an IOSink object that writes JSON messages to the
// provided io.Writer.
func NewIOSink(out io.Writer) *IOSink {
	return &IOSink{
		enc: json.NewEncoder(out),
	}
}

// Info will write a JSON-encoded message to the io.Writer.
func (sink *IOSink) Info(_ int, msg string, keysAndValues ...interface{}) {
	mapSize := len(keysAndValues) / 2
	if math.MaxInt-mapSize >= 2 {
		mapSize += 2
	}
	kvMap := make(map[string]interface{}, mapSize)

	kvMap[KeyTimestamp] = time.Now().UnixNano()
	kvMap[KeyMessage] = msg

	for i := 0; i < len(keysAndValues); i += 2 {
		kvMap[keysAndValues[i].(string)] = keysAndValues[i+1]
	}

	sink.encMu.Lock()
	defer sink.encMu.Unlock()

	_ = sink.enc.Encode(kvMap)
}

// Error will write a JSON-encoded error message to the io.Writer.
func (sink *IOSink) Error(err error, msg string, kv ...interface{}) {
	kv = append(kv, KeyError, err.Error())
	sink.Info(0, msg, kv...)
}