AnyCollect  1.1.2
Source.h
Go to the documentation of this file.
1 //
2 // Source.h
3 //
4 // Created on October 31st 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <chrono>
24 #include <fstream>
25 #include <memory>
26 #include <string_view>
27 #include <vector>
28 
29 #include <pstreams/pstream.h>
30 
31 #include "Config.h"
32 #include "Expression.h"
33 
34 
35 namespace AnyCollect {
39  class Source {
40  public:
44  enum SourceType {
47  };
48 
49  protected:
51  std::string path_;
52  std::vector<std::string> pathParts_;
53  std::ifstream file_;
54  redi::ipstream process_;
55  std::vector<char> buffer_;
56 
57  std::string_view contents_;
58  std::chrono::system_clock::time_point timestamp_;
59 
60  std::vector<std::shared_ptr<Expression>> expressions_;
61 
62  size_t readFile(bool firstTime = false);
63  size_t executeCommand(bool firstTime = false);
64 
65  public:
71  Source(const std::string& filePath) noexcept;
72 
79  Source(const std::string& program, const std::vector<std::string>& arguments) noexcept;
80 
81 
88  static std::vector<std::string> filePathsMatchingGlobbingPattern(const std::string& pattern) noexcept;
89 
90 
94  SourceType type();
95 
99  const std::string& path() const noexcept;
100 
104  const std::vector<std::string>& pathParts() const noexcept;
105 
109  const std::string_view& contents() const noexcept;
110 
114  std::chrono::system_clock::time_point timestamp() const noexcept;
115 
119  std::vector<std::shared_ptr<Expression>>& expressions() noexcept;
120 
124  const std::vector<std::shared_ptr<Expression>>& expressions() const noexcept;
125 
126 
133  bool reset() noexcept;
134 
141  bool update() noexcept;
142 
143 
149  std::string_view::const_iterator begin() const noexcept;
150 
156  std::string_view::const_iterator end() const noexcept;
157 
164  std::string_view::const_iterator getLine(std::string_view::const_iterator begin) const noexcept;
165  };
166 }
std::string_view contents_
Read-only view on the buffer_.
Definition: Source.h:57
std::ifstream file_
For file sources, the file descriptor.
Definition: Source.h:53
const std::string & path() const noexcept
Returns the file path or command to execute.
Definition: Source.cc:72
bool reset() noexcept
Resets the source: attempts to open the file or execute the command, allocates enough space for conte...
Definition: Source.cc:132
Class used to represent an expression (regex)
Definition: Expression.h:33
SourceType
Enum of existing source type.
Definition: Source.h:44
std::string_view::const_iterator getLine(std::string_view::const_iterator begin) const noexcept
Returns an iterator to the end of the specified line.
Definition: Source.cc:190
std::vector< char > buffer_
Buffer for file contents or command output.
Definition: Source.h:55
std::string_view::const_iterator begin() const noexcept
Returns the iterator to the beginning of the source&#39;s contents.
Definition: Source.cc:182
STL namespace.
std::chrono::system_clock::time_point timestamp() const noexcept
Returns the timestamp of stored contents.
Definition: Source.cc:84
bool update() noexcept
Updates the source (read the file or execute command) and store the results.
Definition: Source.cc:161
redi::ipstream process_
For command sources, the child process.
Definition: Source.h:54
Command output source.
Definition: Source.h:46
std::vector< std::shared_ptr< Expression > > expressions_
Array of expressions used on the source&#39;s contents.
Definition: Source.h:60
Class used to represent a source (file or command output) from which metrics can be matched...
Definition: Source.h:39
std::string_view::const_iterator end() const noexcept
Returns the iterator to the end of the source&#39;s contents.
Definition: Source.cc:186
static std::vector< std::string > filePathsMatchingGlobbingPattern(const std::string &pattern) noexcept
Returns all file paths that match a pattern as defined by POSIX.
Definition: Source.cc:51
const std::vector< std::string > & pathParts() const noexcept
For file sources, returns the different parts of the file&#39;s path.
Definition: Source.cc:76
const std::string_view & contents() const noexcept
Returns the stored contents (file contents or command output)
Definition: Source.cc:80
SourceType type_
The type of the source.
Definition: Source.h:50
File contents source.
Definition: Source.h:45
std::chrono::system_clock::time_point timestamp_
Last contents or output fetching time.
Definition: Source.h:58
std::vector< std::string > pathParts_
For file sources, the parts of the file&#39;s path.
Definition: Source.h:52
Source(const std::string &filePath) noexcept
Construct a new Source object of file type.
Definition: Source.cc:31
std::string path_
Path of the file or command to execute.
Definition: Source.h:51
size_t readFile(bool firstTime=false)
For file sources, put the file contents into the buffer_.
Definition: Source.cc:97
SourceType type()
Returns the source type.
Definition: Source.cc:68
std::vector< std::shared_ptr< Expression > > & expressions() noexcept
Returns the array of the receiver&#39;s expressions.
Definition: Source.cc:88
size_t executeCommand(bool firstTime=false)
For command sources, put the command output into the buffer_.
Definition: Source.cc:118