|
20 | 20 | #include "trace.h"
|
21 | 21 | #include <iostream>
|
22 | 22 | #include <sstream>
|
| 23 | +#include <tbox/base/log.h> |
23 | 24 | #include <tbox/base/json.hpp>
|
24 | 25 | #include <tbox/base/catch_throw.h>
|
25 | 26 | #include <tbox/terminal/session.h>
|
@@ -74,6 +75,34 @@ bool Trace::initialize(Context &ctx, const Json &cfg)
|
74 | 75 |
|
75 | 76 | if (is_enable)
|
76 | 77 | sink.enable();
|
| 78 | + |
| 79 | + if (util::json::HasObjectField(js_trace, "filter")) { |
| 80 | + auto &js_filter = js_trace.at("filter"); |
| 81 | + |
| 82 | + std::string filter_strategy; |
| 83 | + if (util::json::GetField(js_filter, "strategy", filter_strategy)) { |
| 84 | + if (filter_strategy == "permit") { |
| 85 | + sink.setFilterStrategy(trace::Sink::FilterStrategy::kPermit); |
| 86 | + } else if (filter_strategy == "reject") { |
| 87 | + sink.setFilterStrategy(trace::Sink::FilterStrategy::kReject); |
| 88 | + } else { |
| 89 | + LogWarn("config 'trace.filter.strategy' field is invalid"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (util::json::HasArrayField(js_filter, "exempts")) { |
| 94 | + auto &js_exempts = js_filter.at("exempts"); |
| 95 | + trace::Sink::ExemptSet exempt_set; |
| 96 | + for (auto &js_item : js_exempts) { |
| 97 | + std::string module; |
| 98 | + if (util::json::Get(js_item, module)) |
| 99 | + exempt_set.insert(module); |
| 100 | + else |
| 101 | + LogWarn("config 'trace.filter.exempts' item is invalid"); |
| 102 | + } |
| 103 | + sink.setFilterExemptSet(exempt_set); |
| 104 | + } |
| 105 | + } |
77 | 106 | }
|
78 | 107 | return true;
|
79 | 108 | }
|
@@ -155,6 +184,96 @@ void Trace::initShell(TerminalNodes &term)
|
155 | 184 | profile.help = "get current directory";
|
156 | 185 | terminal::AddFuncNode(term, trace_node, "get_dir_path", profile);
|
157 | 186 | }
|
| 187 | + |
| 188 | + { |
| 189 | + auto filter_node = term.createDirNode(); |
| 190 | + term.mountNode(trace_node, filter_node, "filter"); |
| 191 | + |
| 192 | + { |
| 193 | + terminal::StringFuncNodeProfile profile; |
| 194 | + profile.get_func = [] () { |
| 195 | + auto strategy = trace::Sink::GetInstance().getFilterStrategy(); |
| 196 | + if (strategy == trace::Sink::FilterStrategy::kPermit) |
| 197 | + return "permit"; |
| 198 | + else |
| 199 | + return "reject"; |
| 200 | + }; |
| 201 | + profile.set_func = [] (const std::string &strategy) { |
| 202 | + if (strategy == "permit") { |
| 203 | + trace::Sink::GetInstance().setFilterStrategy(trace::Sink::FilterStrategy::kPermit); |
| 204 | + return true; |
| 205 | + |
| 206 | + } else if (strategy == "reject") { |
| 207 | + trace::Sink::GetInstance().setFilterStrategy(trace::Sink::FilterStrategy::kReject); |
| 208 | + return true; |
| 209 | + |
| 210 | + } else { |
| 211 | + return false; |
| 212 | + } |
| 213 | + }; |
| 214 | + |
| 215 | + profile.usage = \ |
| 216 | + "Usage : strategy # print current filter strategy.\r\n" |
| 217 | + " strategy permit|reject # set filter strategy.\r\n"; |
| 218 | + profile.help = "print or set filter strategy"; |
| 219 | + terminal::AddFuncNode(term, filter_node, "strategy", profile); |
| 220 | + } |
| 221 | + |
| 222 | + { |
| 223 | + auto exempts_node = term.createDirNode(); |
| 224 | + term.mountNode(filter_node, exempts_node, "exempts"); |
| 225 | + |
| 226 | + { |
| 227 | + auto func_node = term.createFuncNode( |
| 228 | + [] (const terminal::Session &s, const terminal::Args &) { |
| 229 | + auto exempt_set = trace::Sink::GetInstance().getFilterExemptSet(); |
| 230 | + std::ostringstream oss; |
| 231 | + for (auto &item : exempt_set) |
| 232 | + oss << item << "\r\n"; |
| 233 | + oss << "\r\n"; |
| 234 | + s.send(oss.str()); |
| 235 | + } |
| 236 | + ); |
| 237 | + term.mountNode(exempts_node, func_node, "print"); |
| 238 | + } |
| 239 | + |
| 240 | + { |
| 241 | + terminal::StringFuncNodeProfile profile; |
| 242 | + profile.set_func = [] (const std::string &module) { |
| 243 | + auto &sink = trace::Sink::GetInstance(); |
| 244 | + auto exempt_set = sink.getFilterExemptSet(); |
| 245 | + exempt_set.insert(module); |
| 246 | + sink.setFilterExemptSet(exempt_set); |
| 247 | + return true; |
| 248 | + }; |
| 249 | + profile.usage = "Usage : add <module>\r\n"; |
| 250 | + profile.help = "add module in exempts"; |
| 251 | + terminal::AddFuncNode(term, exempts_node, "add", profile); |
| 252 | + } |
| 253 | + |
| 254 | + { |
| 255 | + terminal::StringFuncNodeProfile profile; |
| 256 | + profile.set_func = [] (const std::string &module) { |
| 257 | + auto &sink = trace::Sink::GetInstance(); |
| 258 | + auto exempt_set = sink.getFilterExemptSet(); |
| 259 | + exempt_set.erase(module); |
| 260 | + sink.setFilterExemptSet(exempt_set); |
| 261 | + return true; |
| 262 | + }; |
| 263 | + profile.usage = "Usage : remove <module>\r\n"; |
| 264 | + profile.help = "remove module from exempts"; |
| 265 | + terminal::AddFuncNode(term, exempts_node, "remove", profile); |
| 266 | + } |
| 267 | + |
| 268 | + terminal::AddFuncNode(term, exempts_node, "reset", |
| 269 | + [] { |
| 270 | + auto &sink = trace::Sink::GetInstance(); |
| 271 | + sink.setFilterExemptSet(trace::Sink::ExemptSet()); |
| 272 | + return true; |
| 273 | + } |
| 274 | + ); |
| 275 | + } |
| 276 | + } |
158 | 277 | }
|
159 | 278 |
|
160 | 279 | }
|
|
0 commit comments