]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
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 | #ifndef JAEGERTRACING_TRACEID_H | |
18 | #define JAEGERTRACING_TRACEID_H | |
19 | ||
20 | #include <cstdint> | |
21 | #include <iomanip> | |
22 | #include <iostream> | |
23 | ||
24 | namespace jaegertracing { | |
25 | ||
26 | class TraceID { | |
27 | public: | |
28 | static TraceID fromStream(std::istream& in); | |
29 | ||
30 | TraceID() | |
31 | : TraceID(0, 0) | |
32 | { | |
33 | } | |
34 | ||
35 | TraceID(uint64_t high, uint64_t low) | |
36 | : _high(high) | |
37 | , _low(low) | |
38 | { | |
39 | } | |
40 | ||
41 | bool isValid() const { return _high != 0 || _low != 0; } | |
42 | ||
43 | template <typename Stream> | |
44 | void print(Stream& out) const | |
45 | { | |
46 | if (_high == 0) { | |
47 | out << std::hex << _low; | |
48 | } | |
49 | else { | |
50 | out << std::hex << _high << std::setw(16) << std::setfill('0') | |
51 | << std::hex << _low; | |
52 | } | |
53 | } | |
54 | ||
55 | uint64_t high() const { return _high; } | |
56 | ||
57 | uint64_t low() const { return _low; } | |
58 | ||
59 | bool operator==(const TraceID& rhs) const | |
60 | { | |
61 | return _high == rhs._high && _low == rhs._low; | |
62 | } | |
63 | ||
64 | private: | |
65 | uint64_t _high; | |
66 | uint64_t _low; | |
67 | }; | |
68 | ||
69 | } // namespace jaegertracing | |
70 | ||
71 | inline std::ostream& operator<<(std::ostream& out, | |
72 | const jaegertracing::TraceID& traceID) | |
73 | { | |
74 | traceID.print(out); | |
75 | return out; | |
76 | } | |
77 | ||
78 | inline std::istream& operator<<(std::istream& in, | |
79 | jaegertracing::TraceID& traceID) | |
80 | { | |
81 | traceID = jaegertracing::TraceID::fromStream(in); | |
82 | return in; | |
83 | } | |
84 | ||
85 | #endif // JAEGERTRACING_TRACEID_H |