MADARA  3.4.1
PrefixPrint.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_PREFIX_PRINT_H_
4 #define _MADARA_FILTERS_PREFIX_PRINT_H_
5 
13 #include <sstream>
14 
15 #include "AggregateFilter.h"
17 #include "madara/utility/Utility.h"
19 
20 namespace madara
21 {
22 namespace filters
23 {
29 {
30 public:
38  PrefixPrint(const std::vector<std::string>& source_prefixes = {},
39  bool print_verbose = true)
40  : prefixes(source_prefixes), verbose(print_verbose)
41  {
42  }
43 
47  virtual ~PrefixPrint ()
48  {
49  }
50 
56  inline virtual void filter(knowledge::KnowledgeMap& records,
57  const transport::TransportContext& transport_context,
59  {
60  std::stringstream buffer;
61 
62  if (verbose)
63  {
64  buffer << "PrefixFilter Arguments:\n";
65 
66  // Operation Type
67  buffer << " Operation Type: ";
69  transport_context.get_operation());
70  buffer << "\n";
71 
72  // Send Bandwidth
73  buffer << " Send Bandwidth: ";
74  buffer << transport_context.get_send_bandwidth();
75  buffer << " B/s\n";
76 
77  // Receive Bandwidth
78  buffer << " Receive Bandwidth: ";
79  buffer << transport_context.get_receive_bandwidth();
80  buffer << " B/s\n";
81 
82  // Update Time
83  buffer << " Update Time: ";
84  buffer << transport_context.get_message_time();
85  buffer << "\n";
86 
87  // Current Time
88  buffer << " Current Time: ";
89  buffer << transport_context.get_current_time();
90  buffer << "\n";
91 
92  // Knowledge Domain
93  buffer << " Knowledge Domain: ";
94  buffer << transport_context.get_domain();
95  buffer << "\n";
96 
97  // Originator
98  buffer << " Originator: ";
99  buffer << transport_context.get_originator();
100  buffer << "\n";
101 
102  buffer << " Updates:\n";
103  }
104 
105  // because of the usage of erase, don't auto inc record in for loop
106  for (auto record : records)
107  {
108  // check for valid prefix
109  bool accepted_prefix = false;
110 
111  if (prefixes.size() > 0)
112  {
113  for (auto prefix : prefixes)
114  {
115  if (utility::begins_with(record.first, prefix))
116  {
117  accepted_prefix = true;
118  break;
119  }
120  }
121  }
122  else
123  {
124  accepted_prefix = true;
125  }
126 
127  // if not valid, remove the record and update iterator
128  if (!accepted_prefix)
129  {
132  "PrefixPrint::filter: "
133  "not printing variable %s\n",
134  record.first.c_str())
135  } // end not valid prefix
136  else
137  {
138  // valid prefix so keep the record and proceed to next
139  buffer << " " << record.first << " = ";
140  buffer << record.second.to_string() << "\n";
141  } // end valid prefix
142  } // end iteration over all filter records
143 
145  logger::LOG_ALWAYS, buffer.str().c_str());
146  } // end filter function
147 
152  std::vector<std::string> prefixes;
153  bool verbose;
154 };
155 }
156 }
157 
158 #endif // _MADARA_FILTERS_PREFIX_PRINT_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
Abstract base class for implementing aggregate record filters via a functor interface.
Filter for printing variables as they arrive and excluding prefixes that are unwanted via a user-spec...
Definition: PrefixPrint.h:29
PrefixPrint(const std::vector< std::string > &source_prefixes={}, bool print_verbose=true)
Constructor.
Definition: PrefixPrint.h:38
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &transport_context, knowledge::Variables &)
Prints records and transport context information.
Definition: PrefixPrint.h:56
std::vector< std::string > prefixes
Prefixes to print.
Definition: PrefixPrint.h:152
virtual ~PrefixPrint()
Destructor.
Definition: PrefixPrint.h:47
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
Provides context about the transport.
uint64_t get_send_bandwidth(void) const
Gets the send/rebroadcast bandwidth in bytes per second.
const std::string & get_domain(void) const
Returns the network domain.
int64_t get_operation(void) const
Get operation that the context is performing.
uint64_t get_current_time(void) const
Gets the current timestamp.
const std::string & get_originator(void) const
Returns the current message originator.
uint64_t get_receive_bandwidth(void) const
Gets the receive bandwidth in bytes per second.
uint64_t get_message_time(void) const
Gets the message timestamp.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
std::string MADARA_EXPORT get_operation_type(int type)
Return the type of operation (usually from TransportContext)
Definition: Transport.h:227
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:638
Copyright(c) 2020 Galois.