-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example compile setup and basic example
- Loading branch information
Daniel Pelanek
committed
Jun 13, 2024
1 parent
f41537e
commit 0683e69
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_executable(test | ||
basicExample.cpp | ||
) | ||
|
||
target_link_libraries(test PRIVATE | ||
telemetry::telemetry | ||
telemetry::appFs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <telemetry.hpp> | ||
#include <appFs.hpp> | ||
#include <memory> | ||
|
||
// example help | ||
#include <chrono> | ||
#include <csignal> | ||
#include <atomic> | ||
std::atomic<bool> g_gotSIGINT(false); | ||
void signalHandler(int signum) { | ||
(void)signum; | ||
g_gotSIGINT.store(true); | ||
} | ||
|
||
|
||
// The return value has to be telemetry::Content or one of its variants. | ||
telemetry::Content getTimeElapsed(std::chrono::time_point<std::chrono::system_clock>& startTime) { | ||
auto now = std::chrono::system_clock::now(); | ||
auto timeElapsed = std::chrono::duration_cast<std::chrono::seconds>(now - startTime).count(); | ||
|
||
// telemetry::Content is std::variant<Scalar, ScalarWithUnit, Array, Dict>. | ||
// How they are used is shown in another example. | ||
return telemetry::Scalar(timeElapsed); | ||
} | ||
|
||
// Resets start time to the current time. | ||
void clearTime(std::chrono::time_point<std::chrono::system_clock>& startTime) { | ||
startTime = std::chrono::system_clock::now(); | ||
} | ||
|
||
int main() { | ||
// Creating root dir for filesystem. | ||
std::shared_ptr<telemetry::Directory> telemetryRootNode; | ||
telemetryRootNode = telemetry::Directory::create(); | ||
|
||
// path to root dir, which has to exist before the program starts. | ||
// The path is local to where the program is called from. | ||
std::string fusePath = "fusedir"; | ||
|
||
// Linking root dir to the chosen directory on disk. | ||
telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath); | ||
fuse.start(); | ||
// The filesystem is still just empty. | ||
// / | ||
|
||
// So let's a directory named input into the root dir. | ||
std::shared_ptr<telemetry::Directory> inputDir = telemetryRootNode->addDir("input"); | ||
// Now the filesystem looks like this. | ||
// / | ||
// └─ input/ | ||
|
||
|
||
// Every file can have two lambdas attached to it. | ||
// | ||
// One for reading -> What gets called when something tries to read the file on disk. | ||
// Here we write the return value of getTime to the file. | ||
// | ||
// One for clearing -> What gets called when you want to reset telemetry data. | ||
// In this case we reset the startTime. | ||
auto startTime = std::chrono::system_clock::now(); | ||
telemetry::FileOps const fileOps | ||
= {[&startTime]() { return getTimeElapsed(startTime); }, | ||
[&startTime]() { return clearTime(startTime); }}; | ||
|
||
// The read and clear functions are optional. In the case you don't use them pass | ||
// a null pointer instead. | ||
telemetry::FileOps const anotherFileOps | ||
= {nullptr, nullptr}; | ||
|
||
|
||
// Files be put into the root directory. | ||
const std::shared_ptr<telemetry::File> timeFile = telemetryRootNode->addFile("time", fileOps); | ||
// Now it looks like this. | ||
// / | ||
// ├─ input/ | ||
// └─ time | ||
|
||
// Or into another directory. | ||
const std::shared_ptr<telemetry::File> anotherTimeFile = inputDir->addFile("time", anotherFileOps); | ||
// Now it looks like this. | ||
// / | ||
// ├─ input/ | ||
// │ └─ time | ||
// └─ time | ||
|
||
// Don't forget to create directory named fusedir | ||
// Waiting for ctrl+c. In the meantime you can open another terminal and | ||
// navigate to the newly linked directory. Just reading the new time file | ||
// should print the time elapsed in seconds since the start of the program. | ||
std::signal(SIGINT, signalHandler); | ||
while(!g_gotSIGINT.load()){}; | ||
|
||
return 0; | ||
} |