Repository navigation
Conversation
|
Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes. |
…e functions Motivation: `cpp/open3d/utility/Logging.h` previously defined `LogError`, `LogWarning`, `LogInfo`, and `LogDebug` as unprefixed preprocessor macros in the global preprocessor scope (`#define LogError(...) Logger::LogError_(...)`) to mimic a "macro in namespace" when callers write `utility::LogError(...)`. Because C++ preprocessor macros do not respect namespaces, including any Open3D header that transitively includes `open3d/utility/Logging.h` (such as `open3d/geometry/TriangleMesh.h` or `open3d/geometry/PointCloud.h`) pollutes the global macro namespace for the remainder of the translation unit. Any subsequently included third-party or application header that declares a method, function, or enum named `LogError`, `LogWarning`, `LogInfo`, or `LogDebug` (for example, custom logger interfaces or Abseil logging utilities) fails to compile because the preprocessor rewrites `foo.LogError(...)` into `foo.Logger::LogError_(__FILE__, __LINE__, ...)`. Why This Is Possible Now (Without Requiring C++20): When `Logging.h` was originally written in 2019 (targeting C++14 and older compilers), preprocessor macros (`__FILE__`, `__LINE__`, `OPEN3D_FUNCTION`) were the only portable way to capture caller source locations because Clang (< 9.0, Sept 2019) and MSVC (< VS 2019 16.6 / `_MSC_VER 1926`, May 2020) did not yet implement the compiler intrinsics (`__builtin_FILE()`, `__builtin_LINE()`, `__builtin_FUNCTION()`) created to back C++20 `std::source_location`. Even though the `<source_location>` header itself requires C++20, all toolchains supported by Open3D today (GCC 7+, Clang 9+, AppleClang, MSVC VS 2019 16.6+, and NVCC) expose `__builtin_FILE()`, `__builtin_LINE()`, and `__builtin_FUNCTION()` in C++17 mode. Why Functionality Is Equivalent: 1. Call-site syntax is unchanged: `utility::LogError(...)`, `open3d::utility::LogError(...)`, and unqualified `LogError(...)` (inside `namespace open3d::utility` or translation units with `using namespace open3d::utility;`) all resolve to the new `inline` function templates in `namespace open3d::utility`. 2. Caller source location (`file`, `line`, `function`) is still captured at the call site without C++20 `<source_location>` or macros: `open3d::utility::detail::LogLocation` wraps `const char *format` with a non-explicit `constexpr` constructor whose default arguments (`__builtin_FILE()`, `__builtin_LINE()`, `__builtin_FUNCTION()`) are evaluated at the caller's source location in C++17 across GCC, Clang, AppleClang, MSVC (VS 2019 16.6+), and NVCC. 3. Formatting and control-flow semantics are untouched: `LogError`, `LogWarning`, `LogInfo`, and `LogDebug` forward `loc.file`, `loc.line`, `loc.function`, `loc.format`, and `std::forward<Args>(args)...` directly to `Logger::LogError_`, `Logger::LogWarning_`, `Logger::LogInfo_`, and `Logger::LogDebug_`, preserving `[[noreturn]]` on `LogError` and existing `fmt` formatting behavior. Testing: - Extended `cpp/tests/utility/Logging.cpp` to verify that `utility::LogError` captures the caller's filename (`Logging.cpp`) and formatted message in the thrown `std::runtime_error`. - Added `Logging.NoGlobalMacroPollution` in `cpp/tests/utility/Logging.cpp` to verify that a class defining `LogError()`, `LogWarning()`, `LogInfo()`, and `LogDebug()` member functions compiles and executes cleanly after including `open3d/utility/Logging.h`.
tbhova
force-pushed
the
fix/logging-macro-pollution
branch
from
October 9, 2026 20:56
9e84af0 to
f7b5b9b
Compare
Author
Done :) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Type
Motivation and Context
cpp/open3d/utility/Logging.hpreviously definedLogError,LogWarning,LogInfo, andLogDebugas unprefixed preprocessor macros in the global preprocessor scope (#define LogError(...) Logger::LogError_(...)) to mimic a "macro in namespace" when callers writeutility::LogError(...).Because C++ preprocessor macros do not respect namespaces, including any Open3D header that transitively includes
open3d/utility/Logging.h(such asopen3d/geometry/TriangleMesh.horopen3d/geometry/PointCloud.h) pollutes the global macro namespace for the remainder of the translation unit. Any subsequently included third-party or application header that declares a method, function, or enum namedLogError,LogWarning,LogInfo, orLogDebug(for example, custom logger interfaces or Abseil logging utilities) fails to compile because the preprocessor rewritesfoo.LogError(...)intofoo.Logger::LogError_(__FILE__, __LINE__, ...).Checklist:
python util/check_style.py --applyto apply Open3D code styleto my code.
updated accordingly.
results (e.g. screenshots or numbers) here.
Description
Replaces the global
#define LogError,#define LogWarning,#define LogInfo, and#define LogDebugmacros incpp/open3d/utility/Logging.hwithinlinevariadic template functions insidenamespace open3d::utility.Why This Is Possible Now (Without Requiring C++20):
When
Logging.hwas originally written in 2019 (targeting C++14 and older compilers), preprocessor macros (__FILE__,__LINE__,OPEN3D_FUNCTION) were the only portable way to capture caller source locations because Clang (< 9.0, Sept 2019) and MSVC (< VS 2019 16.6 /_MSC_VER 1926, May 2020) did not yet implement the compiler intrinsics (__builtin_FILE(),__builtin_LINE(),__builtin_FUNCTION()) created to back C++20std::source_location. Even though the<source_location>header itself requires C++20, all toolchains supported by Open3D today (GCC 7+, Clang 9+, AppleClang, MSVC VS 2019 16.6+, and NVCC) expose__builtin_FILE(),__builtin_LINE(), and__builtin_FUNCTION()in C++17 mode.Why Functionality Is Equivalent:
utility::LogError(...),open3d::utility::LogError(...), and unqualifiedLogError(...)(insidenamespace open3d::utilityor translation units withusing namespace open3d::utility;) all resolve to the newinlinefunction templates innamespace open3d::utility.file,line,function) is still captured at the call site without C++20<source_location>or macros:open3d::utility::detail::LogLocationwrapsconst char *formatwith a non-explicitconstexprconstructor whose default arguments (__builtin_FILE(),__builtin_LINE(),__builtin_FUNCTION()) are evaluated at the caller's source location in C++17 across GCC, Clang, AppleClang, MSVC (VS 2019 16.6+), and NVCC.LogError,LogWarning,LogInfo, andLogDebugforwardloc.file,loc.line,loc.function,loc.format, andstd::forward<Args>(args)...directly toLogger::LogError_,Logger::LogWarning_,Logger::LogInfo_, andLogger::LogDebug_, preserving[[noreturn]]onLogErrorand existingfmtformatting behavior.Testing:
cpp/tests/utility/Logging.cppto verify thatutility::LogErrorcaptures the caller's filename (Logging.cpp) and formatted message in the thrownstd::runtime_error.Logging.NoGlobalMacroPollutionincpp/tests/utility/Logging.cppto verify that a class definingLogError(),LogWarning(),LogInfo(), andLogDebug()member functions compiles and executes cleanly after includingopen3d/utility/Logging.h.