forked from apache/brpc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmvariable.h
125 lines (98 loc) · 4.21 KB
/
mvariable.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Date: 2021/11/17 14:37:53
#ifndef BVAR_MVARIABLE_H
#define BVAR_MVARIABLE_H
#include <ostream> // std::ostream
#include <sstream> // std::ostringstream
#include <list> // std::list
#include <string> // std::string
#include "butil/macros.h" // DISALLOW_COPY_AND_ASSIGN
#include "butil/strings/string_piece.h" // butil::StringPiece
namespace bvar {
class Dumper;
struct DumpOptions;
class MVariable {
public:
explicit MVariable(const std::list<std::string>& labels);
virtual ~MVariable();
// Implement this method to print the mvariable info into ostream.
virtual void describe(std::ostream&) = 0;
// string form of describe().
std::string get_description();
// Get mvariable name
const std::string& name() const { return _name; }
// Get mvariable labels
const std::list<std::string>& labels() const { return _labels; }
// Get number of mvariable labels
size_t count_labels() const { return _labels.size(); }
// Expose this mvariable globally so that it's counted in following
// functions:
// list_exposed
// count_exposed
// Return 0 on success, -1 otherwise.
int expose(const butil::StringPiece& name) {
return expose_impl(butil::StringPiece(), name);
}
// Expose this mvariable globally with a prefix
// Return 0 on success, -1 otherwise.
int expose_as(const butil::StringPiece& prefix,
const butil::StringPiece& name) {
return expose_impl(prefix, name);
}
// Dump this mvariable
virtual size_t dump(Dumper* dumper, const DumpOptions* options) = 0;
// Hide this variable so that it's not counted in *_exposed functions.
// Returns false if this variable is already hidden.
// CAUTION!! Subclasses must call hide() manually to avoid displaying
// a variable that is just destructing.
bool hide();
// Get number of exposed mvariables.
static size_t count_exposed();
// Put names of all exposed mvariable into `name'.
static void list_exposed(std::vector<std::string> *names);
// Find all exposed mvariables matching `white_wildcards' but
// `black_wildcards' and send them to `dumper'.
// Use default options when `options' is NULL.
// Return number of dumped mvariables, -1 on error.
static size_t dump_exposed(Dumper* dumper, const DumpOptions* options);
// Find an exposed mvariable by `name' and put its description into `os'.
// Returns 0 on found, -1 otherwise.
static int describe_exposed(const std::string& name,
std::ostream& os);
// String form. Returns empty string when not found.
static std::string describe_exposed(const std::string& name);
#ifdef UNIT_TEST
// Hide all mvariables so that all mvariables not counted in following
// functions:
// list_exposed
// count_exposed
// CAUTION!!! Just For Debug!!!
static void hide_all();
#endif
protected:
int expose_impl(const butil::StringPiece& prefix,
const butil::StringPiece& name);
protected:
std::string _name;
std::list<std::string> _labels;
// mbvar uses bvar, bvar uses TLS, thus copying/assignment need to copy TLS stuff as well,
// which is heavy. We disable copying/assignment now.
DISALLOW_COPY_AND_ASSIGN(MVariable);
};
} // namespace bvar
#endif // BVAR_MVARIABLE_H