Skip to content

Commit

Permalink
utils example
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pelanek committed Jun 13, 2024
1 parent 455a1b2 commit dbacce2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
20 changes: 19 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,28 @@ target_link_libraries(test1 PRIVATE
)

add_executable(test2
variantsExample.cpp
contentExample.cpp
)

target_link_libraries(test2 PRIVATE
telemetry::telemetry
telemetry::appFs
)

add_executable(test3
holderExample.cpp
)

target_link_libraries(test3 PRIVATE
telemetry::telemetry
telemetry::appFs
)

add_executable(test4
utilsExample.cpp
)

target_link_libraries(test4 PRIVATE
telemetry::telemetry
telemetry::appFs
)
2 changes: 1 addition & 1 deletion examples/basicExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main() {
std::string fusePath = "fusedir";

// Linking root dir to the chosen directory on disk.
telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath);
telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath, true, false);
fuse.start();
// The filesystem is still just empty.
// /
Expand Down
24 changes: 20 additions & 4 deletions examples/hiolderExample.cpp → examples/holderExample.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file
* @author Daniel Pelanek <xpeland00@vutbr.cz>
* @brief
* @brief Shows how to use the holder class
*
* SPDX-License-Identifier: BSD-3-Clause
*/
Expand All @@ -12,17 +12,25 @@
class IOwnTelemetryFiles {
public:
void configTelemetry(const std::shared_ptr<telemetry::Directory>& rootDir) {
// Create a telemetry file
const telemetry::FileOps fileOps
= {nullptr, nullptr};

auto someFile = rootDir->addFile("stats", fileOps);
auto someFile = rootDir->addFile("someFile", fileOps);

// We also need to add the file to the holder.
m_telemetryHolder.add(someFile);
}

// If you want to disable callback of files before the
// is destroyed.
void disableFiles() {
m_telemetryHolder.disableFiles();
}

private:
telemetry::Holder m_telemetryHolder;
}
};

int main() {
// Same as basic example
Expand All @@ -34,8 +42,16 @@ int main() {
telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath);
fuse.start();

const IOwnTelemetryFiles object;
// Object that collects it's own telemetry or owns
// telemetry files or directories.
// The files callbacks get disabled when the telemetry holder
// is destroyed.
IOwnTelemetryFiles object;

// Configuration of telemetry inside of the object.
object.configTelemetry(telemetryRootNode);

// Disable callbacks before the objects destructor
object.disableFiles();
}

83 changes: 83 additions & 0 deletions examples/utilsExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @file
* @author Daniel Pelanek <xpeland00@vutbr.cz>
* @brief
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <telemetry.hpp>
#include <appFs.hpp>

// example help
#include <string>
#include <iostream>

void traverseDir(std::shared_ptr<telemetry::Directory>& dir, int depth) {
// Using utils to check if a directory is a root directory
if(telemetry::utils::isRootDirectory(dir->getFullPath())) {
std::cout << "start\n";
}

// looping through files and dirs in directory
// listEntries returns their names as string
for(auto node_name : dir->listEntries()) {
auto node = dir->getEntry(node_name);

std::cout << std::string(depth * 4, ' ') << node_name << "\n";

// Using utils to check if a node is a directory
if(telemetry::utils::isDirectory(node)) {
// dynamic_pointer_cast is needed to get a file or dir from node
auto newDir = std::dynamic_pointer_cast<telemetry::Directory>(node);
traverseDir(newDir, depth + 1);

// Using utils to check if a node is a file
} else if(telemetry::utils::isFile(node)) {
auto file = std::dynamic_pointer_cast<telemetry::File>(node);

// Manually reading file content
try {
std::cout << telemetry::contentToString(file->read());
} catch(std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
}
}

int main() {
// Same as basic example
std::shared_ptr<telemetry::Directory> telemetryRootNode;
telemetryRootNode = telemetry::Directory::create();

std::string fusePath = "fusedir";

telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath);
fuse.start();

// Let's create a more complex dir structure
telemetry::FileOps const emptyFileOps
= {nullptr, nullptr};

auto file1 = telemetryRootNode->addFile("file1", emptyFileOps);
auto file2 = telemetryRootNode->addFile("file2", emptyFileOps);
auto dir1 = telemetryRootNode->addDir("dir1");

auto file3 = dir1->addFile("file3", emptyFileOps);
auto dir2 = dir1->addDir("dir2");
auto dir3 = dir1->addDir("dir3");

const int nFiles = 5;
for(int index = 0; index < nFiles; index++) {
auto file = dir2->addFile("file" + std::to_string(index + 4), emptyFileOps);
}

auto file9 = dir3->addFile("file9", emptyFileOps);

// And now traverse the directory with the use of telemetry utils
traverseDir(telemetryRootNode, 0);

// Utils can also give you a node from path
auto node = telemetry::utils::getNodeFromPath(telemetryRootNode, "/dir1/dir2/file5");
}

0 comments on commit dbacce2

Please sign in to comment.