1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "glow/Runtime/TraceExporter.h"
18
19#include <vector>
20
21namespace glow {
22
23void TraceExporterRegistry::registerTraceExporter(TraceExporter *exporter) {
24 /// This function can be called in static init, hence do not use
25 /// glog here as it may not have been initialized yet.
26 std::lock_guard<std::mutex> g(mutex_);
27 exporters_.push_back(exporter);
28}
29
30void TraceExporterRegistry::revokeTraceExporter(TraceExporter *exporter) {
31 std::lock_guard<std::mutex> g(mutex_);
32 exporters_.erase(std::remove(exporters_.begin(), exporters_.end(), exporter),
33 exporters_.end());
34}
35
36bool TraceExporterRegistry::shouldTrace() {
37 bool should = false;
38 DLOG(INFO) << "shouldTrace(): total exporter count = " << exporters_.size();
39 for (auto const &exporter : exporters_) {
40 should |= exporter->shouldTrace();
41 }
42 return should;
43}
44
45void TraceExporterRegistry::exportTrace(TraceContext *tcontext) {
46 DLOG(INFO) << "exportTrace(): total exporter count = " << exporters_.size();
47 if (!tcontext) {
48 return;
49 }
50 for (auto const &exporter : exporters_) {
51 exporter->exportTrace(tcontext);
52 }
53}
54
55std::shared_ptr<TraceExporterRegistry> TraceExporterRegistry::getInstance() {
56 static auto texp = std::make_shared<TraceExporterRegistry>();
57 return texp;
58}
59
60} // namespace glow
61