Skip to content

Commit

Permalink
example compile setup and basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pelanek committed Jun 13, 2024
1 parent f41537e commit 0683e69
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ option(TELEMETRY_BUILD_SHARED "Build shared library" ON)
option(TELEMETRY_PACKAGE_BUILDER "Enable RPM package builder (make rpm)" ON)
option(TELEMETRY_INSTALL_TARGETS "Generate the install target" ON)
option(TELEMETRY_ENABLE_TESTS "Build Unit tests (make test)" OFF)
option(TELEMETRY_BUILD_EXAMPLES "Build included example files (make example)" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -33,6 +34,10 @@ endif()

include(cmake/dependencies.cmake)

if (TELEMETRY_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

if (TELEMETRY_ENABLE_TESTS)
include(cmake/googletest.cmake)
include(GoogleTest)
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ test: build
@$(MAKE) --no-print-directory -C build
@$(MAKE) test --no-print-directory -C build

example: build
@cd build && $(CMAKE) $(CMAKE_ARGS) -DTELEMETRY_BUILD_EXAMPLES=ON ..
@$(MAKE) --no-print-directory -C build
8 changes: 8 additions & 0 deletions examples/CMakeLists.txt
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
)
94 changes: 94 additions & 0 deletions examples/basicExample.cpp
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;
}

0 comments on commit 0683e69

Please sign in to comment.