Repository navigation
fix: Enhance logging examples and configuration for cross-platform use #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
63e17d0
feat: add cross-platform logging example and enhance logger configura…
ajeetsinghyadav ca685f9
feat: enhance file sink configuration to support append mode
ajeetsinghyadav bd89f5b
feat: update flush methods to accept log level for better control ove…
ajeetsinghyadav 13cb1b1
feat: enhance logging file path configuration using std::filesystem f…
ajeetsinghyadav ad6d9bb
refactor: update flush method signature to remove default log level p…
ajeetsinghyadav 9e608da
chore: include <cstdio> header in text_color.cpp for improved functio…
ajeetsinghyadav 8d3f4be
refactor: use scoped calls for isatty and fileno in detectColorSuppor…
ajeetsinghyadav ff70fa8
docs: update iOS/visionOS logging example to clarify C wrapper usage …
ajeetsinghyadav 6613090
docs: enhance ConsoleLogSink flush documentation to clarify output st…
ajeetsinghyadav 857f08e
fix: change default flush level from Trace to Error in LoggerConfig f…
ajeetsinghyadav cf634f4
refactor: unify flush method signatures across log sinks to improve c…
ajeetsinghyadav e251893
test: add tests for setAppLogDirectory to verify path setting and res…
ajeetsinghyadav 46b6960
fix: remove NO_COLOR environment variable check from detectColorSuppo…
ajeetsinghyadav a4bf7f6
refactor: remove unnecessary includes for non-Windows platforms in te…
ajeetsinghyadav a7a26b5
refactor: update detectColorSupport function to simplify color detect…
ajeetsinghyadav cf3e6a5
refactor: remove unnecessary blank line in text_color.cpp
ajeetsinghyadav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
| #============================================================================== | ||
| # 06_platform_logging - Cross-platform logging initialisation example | ||
| # | ||
| # This example runs as a standalone binary on desktop platforms only | ||
| # (macOS, Linux, Windows). On iOS, visionOS, Android, and Web it is | ||
| # excluded because those targets are integrated into app bundles, not | ||
| # standalone executables. The bridging snippets in main.cpp show how | ||
| # to wire up logging from the platform layer on those targets. | ||
| #============================================================================== | ||
|
|
||
| if(NOT VNE_PLATFORM_IOS AND NOT VNE_PLATFORM_VISIONOS | ||
| AND NOT VNE_PLATFORM_ANDROID AND NOT VNE_PLATFORM_WEB) | ||
|
|
||
| add_executable(06_PlatformLogging main.cpp) | ||
|
|
||
| target_link_libraries(06_PlatformLogging | ||
| PRIVATE | ||
| vne::logging | ||
| ) | ||
|
|
||
| target_include_directories(06_PlatformLogging | ||
| PRIVATE | ||
| $<BUILD_INTERFACE:${VNE_INCLUDE_DIR}> | ||
| $<BUILD_INTERFACE:${VNE_SRC_DIR}> | ||
| ) | ||
|
|
||
| endif() | ||
This file contains hidden or 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,121 @@ | ||
| /* --------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Ajeet Singh Yadav. All rights reserved. | ||
| * Licensed under the Apache License, Version 2.0 (the "License") | ||
| * | ||
| * Author: Ajeet Singh Yadav | ||
| * Created: May 2026 | ||
| * | ||
| * Autodoc: yes | ||
| * | ||
| * ---------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| /* ---------------------------------------------------------------------- | ||
| * Example: Cross-platform logging setup | ||
| * | ||
| * Demonstrates how to initialise the logger on each supported platform. | ||
| * Mobile and sandboxed platforms (iOS, visionOS, Android, Web) cannot | ||
| * resolve a writable log directory on their own — the app must obtain the | ||
| * correct path from the platform and pass it via setAppLogDirectory() | ||
| * before calling configureLogger(). | ||
| * | ||
| * Platform bridging snippets | ||
| * -------------------------- | ||
| * | ||
| * iOS / visionOS (export a C wrapper from C++, call it from Swift before init): | ||
| * | ||
| * // C++ side (in your library/app, exported for Swift to call) | ||
| * extern "C" void vne_set_log_dir(const char* path) { | ||
| * if (path != nullptr) { | ||
| * vne::log::Logging::setAppLogDirectory(path); | ||
| * } | ||
| * } | ||
| * | ||
| * // Swift side (declare the C function and pass the resolved path) | ||
| * import Foundation | ||
| * func vne_set_log_dir(_ path: UnsafePointer<CChar>) | ||
| * | ||
| * // --- in AppDelegate / @main --- | ||
| * let dir = FileManager.default | ||
| * .urls(for: .documentDirectory, in: .userDomainMask)[0] | ||
| * .appendingPathComponent("logs").path | ||
| * dir.withCString { vne_set_log_dir($0) } | ||
| * | ||
| * Android (JNI, call from your Activity / Application.onCreate): | ||
| * | ||
| * extern "C" JNIEXPORT void JNICALL | ||
| * Java_com_example_app_MainActivity_initLogging(JNIEnv* env, jobject ctx) { | ||
| * jclass cls = env->GetObjectClass(ctx); | ||
| * jmethodID mid = env->GetMethodID(cls, "getFilesDir", "()Ljava/io/File;"); | ||
| * jobject file = env->CallObjectMethod(ctx, mid); | ||
| * jmethodID pathMid = env->GetMethodID(env->GetObjectClass(file), | ||
| * "getAbsolutePath", | ||
| * "()Ljava/lang/String;"); | ||
| * jstring jpath = (jstring)env->CallObjectMethod(file, pathMid); | ||
| * const char* path = env->GetStringUTFChars(jpath, nullptr); | ||
| * vne::log::Logging::setAppLogDirectory(std::string(path) + "/logs"); | ||
| * env->ReleaseStringUTFChars(jpath, path); | ||
| * } | ||
| * | ||
| * Web (Emscripten): | ||
| * File logging is not supported. The logger automatically falls back to | ||
| * console-only output — no extra setup needed. | ||
| * | ||
| * ---------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| #include <filesystem> | ||
|
|
||
| #include <vertexnova/logging/logging.h> | ||
|
|
||
| namespace { | ||
| CREATE_VNE_LOGGER_CATEGORY("platform.example") | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Platform-specific log directory resolution | ||
| // --------------------------------------------------------------------------- | ||
| // On desktop platforms the library resolves the directory automatically. | ||
| // On mobile/web the app must call setAppLogDirectory() before this runs. | ||
| // This helper just shows what happens per platform at runtime. | ||
| static vne::log::LogSinkType chooseSink() { | ||
| #if defined(VNE_PLATFORM_WEB) | ||
| // No filesystem — console only. | ||
| return vne::log::LogSinkType::eConsole; | ||
| #elif defined(VNE_PLATFORM_VISIONOS) || defined(VNE_PLATFORM_ANDROID) | ||
| // File logging is only available if the app provided a directory. | ||
| std::string dir = vne::log::Logging::getPlatformSpecificLogDirectory(); | ||
| return dir.empty() ? vne::log::LogSinkType::eConsole : vne::log::LogSinkType::eBoth; | ||
| #else | ||
| return vne::log::LogSinkType::eBoth; | ||
| #endif | ||
| } | ||
|
|
||
| int main() { | ||
| // On iOS / visionOS / Android the calling app must have already invoked | ||
| // vne::log::Logging::setAppLogDirectory(<writable path>) before main() | ||
| // or before this point. See the bridging snippets in the file header. | ||
|
|
||
| std::string log_dir = vne::log::Logging::getPlatformSpecificLogDirectory(); | ||
|
|
||
| vne::log::LoggerConfig config = vne::log::Logging::defaultLoggerConfig(); | ||
| config.sink = chooseSink(); | ||
| if (!log_dir.empty()) { | ||
| config.file_path = (std::filesystem::path(log_dir) / "platform_example.log").string(); | ||
| } | ||
| config.log_level = vne::log::LogLevel::eTrace; | ||
| config.file_append = false; // overwrite on each launch; set true to keep history across runs | ||
|
|
||
| vne::log::Logging::configureLogger(config); | ||
|
|
||
| VNE_LOG_INFO << "Logging initialised"; | ||
| VNE_LOG_INFO << "Log directory: " << (log_dir.empty() ? "(console only)" : log_dir); | ||
|
|
||
| VNE_LOG_TRACE << "Trace message"; | ||
| VNE_LOG_DEBUG << "Debug message"; | ||
| VNE_LOG_WARN << "Warning message"; | ||
| VNE_LOG_ERROR << "Error message"; | ||
|
|
||
| vne::log::Logging::shutdown(); | ||
| return 0; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.