1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <regex.h>
8 
9 #include <memory>
10 
11 // TODO(dje): Conversion to std::string needs to be done in conjunction with
12 // converting trace-reader.
13 #include <fbl/string.h>
14 
15 namespace trace_testing {
16 
17 // Squelcher is used to filter out elements of a trace record that may
18 // vary run to run or even within a run and are not germaine to determining
19 // correctness. The canonical example is record timestamps.
20 // The term "squelch" derives from radio circuitry used to remove noise.
21 
22 class Squelcher {
23 public:
24     // |regex_str| is a regular expression consistenting of one or more
25     // subexpressions, the text in the parenthesis of each matching expressions
26     // is replaced with '<>'.
27     // Best illustration is an example. This example removes decimal numbers,
28     // koids, timestamps ("ts"), and lowercase hex numbers.
29     // const char regex[] = "([0-9]+/[0-9]+)"
30     //   "|koid\\(([0-9]+)\\)"
31     //   "|koid: ([0-9]+)"
32     //   "|ts: ([0-9]+)"
33     //   "|(0x[0-9a-f]+)";
34     // So "ts: 123 42 mumble koid(456) foo koid: 789, bar 0xabcd"
35     // becomes "ts: <> <> mumble koid(<>) foo koid: <>, bar <>".
36     static std::unique_ptr<Squelcher> Create(const char* regex_str);
37 
38     ~Squelcher();
39 
40     fbl::String Squelch(const char* str);
41 
42 private:
43     Squelcher() = default;
44 
45     // The compiled regex.
46     regex_t regex_;
47 };
48 
49 }  // namespace trace_testing
50