1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/event/label"
16 // Exporter is a function that handles events.
17 // It may return a modified context and event.
18 type Exporter func(context.Context, Event, label.Map) context.Context
21 exporter unsafe.Pointer
24 // SetExporter sets the global exporter function that handles all events.
25 // The exporter is called synchronously from the event call site, so it should
26 // return quickly so as not to hold up user code.
27 func SetExporter(e Exporter) {
28 p := unsafe.Pointer(&e)
30 // &e is always valid, and so p is always valid, but for the early abort
31 // of ProcessEvent to be efficient it needs to make the nil check on the
32 // pointer without having to dereference it, so we make the nil function
36 atomic.StorePointer(&exporter, p)
39 // deliver is called to deliver an event to the supplied exporter.
40 // it will fill in the time.
41 func deliver(ctx context.Context, exporter Exporter, ev Event) context.Context {
42 // add the current time to the event
44 // hand the event off to the current exporter
45 return exporter(ctx, ev, ev)
48 // Export is called to deliver an event to the global exporter if set.
49 func Export(ctx context.Context, ev Event) context.Context {
50 // get the global exporter and abort early if there is not one
51 exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
52 if exporterPtr == nil {
55 return deliver(ctx, *exporterPtr, ev)
58 // ExportPair is called to deliver a start event to the supplied exporter.
59 // It also returns a function that will deliver the end event to the same
61 // It will fill in the time.
62 func ExportPair(ctx context.Context, begin, end Event) (context.Context, func()) {
63 // get the global exporter and abort early if there is not one
64 exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
65 if exporterPtr == nil {
68 ctx = deliver(ctx, *exporterPtr, begin)
69 return ctx, func() { deliver(ctx, *exporterPtr, end) }