diff --git a/CMakeLists.txt b/CMakeLists.txt index f842169..43bc81e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) diff --git a/Makefile b/Makefile index 8afd119..24e258e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..a2db1ea --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(test + basicExample.cpp +) + +target_link_libraries(test PRIVATE + telemetry::telemetry + telemetry::appFs +) diff --git a/examples/basicExample.cpp b/examples/basicExample.cpp new file mode 100644 index 0000000..6f9eb6e --- /dev/null +++ b/examples/basicExample.cpp @@ -0,0 +1,94 @@ +#include +#include +#include + +// example help +#include +#include +#include +std::atomic 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& startTime) { + auto now = std::chrono::system_clock::now(); + auto timeElapsed = std::chrono::duration_cast(now - startTime).count(); + + // telemetry::Content is std::variant. + // 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& startTime) { + startTime = std::chrono::system_clock::now(); +} + +int main() { + // Creating root dir for filesystem. + std::shared_ptr 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 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 timeFile = telemetryRootNode->addFile("time", fileOps); + // Now it looks like this. + // / + // ├─ input/ + // └─ time + + // Or into another directory. + const std::shared_ptr 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; +}