NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/exporters/ostream/common_utils.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include <sstream>
5 #include <string>
6 #include <vector>
7 #include "opentelemetry/nostd/variant.h"
8 #include "opentelemetry/sdk/common/attribute_utils.h"
9 
10 #pragma once
12 namespace exporter
13 {
14 namespace ostream_common
15 {
16 /*
17  print_value is used to print out the value of an attribute within a vector.
18  These values are held in a variant which makes the process of printing them much more
19  complicated.
20 */
21 
22 template <typename T>
23 void print_value(const T &item, std::ostream &sout)
24 {
25  sout << item;
26 }
27 
28 template <typename T>
29 void print_value(const std::vector<T> &vec, std::ostream &sout)
30 {
31  sout << '[';
32  size_t i = 1;
33  size_t sz = vec.size();
34  for (auto v : vec)
35  {
36  sout << v;
37  if (i != sz)
38  sout << ',';
39  i++;
40  };
41  sout << ']';
42 }
43 
44 template <typename T>
45 void print_value(const nostd::span<T> &vec, std::ostream &sout)
46 {
47  sout << '[';
48  size_t i = 1;
49  size_t sz = vec.size();
50  for (auto v : vec)
51  {
52  sout << v;
53  if (i != sz)
54  sout << ',';
55  i++;
56  };
57  sout << ']';
58 }
59 
60 // Prior to C++14, generic lambda is not available so fallback to functor.
61 #if (!defined(_MSVC_LANG) && (__cplusplus < 201402L)) || \
62  (defined(_MSVC_LANG) && (_MSVC_LANG < 201402L))
63 
65 {
66 public:
67  OwnedAttributeValueVisitor(std::ostream &sout) : sout_(sout) {}
68 
69  template <typename T>
70  void operator()(T &&arg)
71  {
72  print_value(arg, sout_);
73  }
74 
75 private:
76  std::ostream &sout_;
77 };
78 
80 {
81 public:
82  AttributeValueVisitor(std::ostream &sout) : sout_(sout) {}
83 
84  template <typename T>
85  void operator()(T &&arg)
86  {
87  print_value(arg, sout_);
88  }
89 
90  void operator()(nostd::string_view &&arg) { sout_.write(arg.data(), arg.size()); }
91 
92 private:
93  std::ostream &sout_;
94 };
95 
96 #endif
97 
99  std::ostream &sout)
100 {
101 #if (!defined(_MSVC_LANG) && (__cplusplus < 201402L)) || \
102  (defined(_MSVC_LANG) && (_MSVC_LANG < 201402L))
104 #else
106  [&sout](auto &&arg) {
107  /* explicit this is needed by some gcc versions (observed with v5.4.0)*/
108  print_value(arg, sout);
109  },
110  value);
111 #endif
112 }
113 
114 inline void print_value(const opentelemetry::common::AttributeValue &value, std::ostream &sout)
115 {
116 #if (!defined(_MSVC_LANG) && (__cplusplus < 201402L)) || \
117  (defined(_MSVC_LANG) && (_MSVC_LANG < 201402L))
119 #else
121  [&sout](auto &&arg) {
122  /* explicit this is needed by some gcc versions (observed with v5.4.0)*/
123  print_value(arg, sout);
124  },
125  value);
126 #endif
127 }
128 
129 } // namespace ostream_common
130 } // namespace exporter
exporter::ostream_common::OwnedAttributeValueVisitor::OwnedAttributeValueVisitor
OwnedAttributeValueVisitor(std::ostream &sout)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:67
exporter
Definition: includes/opentelemetry/exporters/memory/in_memory_data.h:11
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::span< T >
exporter::ostream_common::OwnedAttributeValueVisitor
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:64
exporter::ostream_common::AttributeValueVisitor
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:79
common::AttributeValue
nostd::variant< bool, int32_t, int64_t, uint32_t, double, const char *, nostd::string_view, nostd::span< const bool >, nostd::span< const int32_t >, nostd::span< const int64_t >, nostd::span< const uint32_t >, nostd::span< const double >, nostd::span< const nostd::string_view >, uint64_t, nostd::span< const uint64_t >, nostd::span< const uint8_t > > AttributeValue
OpenTelemetry signals can be enriched by adding attributes.
Definition: includes/opentelemetry/common/attribute_value.h:58
exporter::ostream_common::OwnedAttributeValueVisitor::operator()
void operator()(T &&arg)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:70
nostd::span::size
size_t size() const noexcept
Definition: includes/opentelemetry/nostd/span.h:174
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
exporter::ostream_common::AttributeValueVisitor::AttributeValueVisitor
AttributeValueVisitor(std::ostream &sout)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:82
exporter::ostream_common::print_value
void print_value(const T &item, std::ostream &sout)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:23
sdk::common::OwnedAttributeValue
nostd::variant< bool, int32_t, uint32_t, int64_t, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< uint32_t >, std::vector< int64_t >, std::vector< double >, std::vector< std::string >, uint64_t, std::vector< uint64_t >, std::vector< uint8_t > > OwnedAttributeValue
A counterpart to AttributeValue that makes sure a value is owned.
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:50
exporter::ostream_common::AttributeValueVisitor::operator()
void operator()(nostd::string_view &&arg)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:90
absl::visit
variant_internal::VisitResult< Visitor, Variants... > visit(Visitor &&vis, Variants &&... vars)
Definition: includes/opentelemetry/nostd/internal/absl/types/variant.h:406
exporter::ostream_common::AttributeValueVisitor::operator()
void operator()(T &&arg)
Definition: includes/opentelemetry/exporters/ostream/common_utils.h:85
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37