Skip to content

Commit

Permalink
Fix build errors from date/date.h C++20 compatibility (microsoft#20139)
Browse files Browse the repository at this point in the history
### Description
For C++ standards >= 20, use `std::chrono::operator<<` in place of
`date::operator<<` to fix ambiguous operator compile error.

### Motivation and Context
The external dependency HowardHinnant/date has a conflict with
std::chrono for >=C++20.
Solves microsoft#20137
  • Loading branch information
afantino951 authored and Rob Vinluan committed Apr 26, 2024
1 parent 9afa41e commit 528b8a5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmake/onnxruntime_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ target_include_directories(onnxruntime_common
${OPTIONAL_LITE_INCLUDE_DIR})


target_link_libraries(onnxruntime_common PUBLIC safeint_interface ${GSL_TARGET} ${ABSEIL_LIBS})
target_link_libraries(onnxruntime_common PUBLIC safeint_interface ${GSL_TARGET} ${ABSEIL_LIBS} date::date)

add_dependencies(onnxruntime_common ${onnxruntime_EXTERNAL_DEPENDENCIES})

Expand Down
11 changes: 11 additions & 0 deletions include/onnxruntime/core/common/logging/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "core/common/logging/macros.h"

#include "date/date.h"

/*
Logging overview and expected usage:
Expand Down Expand Up @@ -56,6 +58,15 @@ namespace logging {

using Timestamp = std::chrono::time_point<std::chrono::system_clock>;

// TODO: When other compilers support std::chrono::operator<<, update this.
// TODO: Check support for other compilers' version before enable C++20 for other compilers.
// Xcode added support for C++20's std::chrono::operator<< in SDK version 14.4.
#if __cplusplus >= 202002L && __MAC_OS_X_VERSION_MAX_ALLOWED >= 140400L
namespace timestamp_ns = std::chrono;
#else
namespace timestamp_ns = ::date;
#endif

#ifndef NDEBUG
ORT_ATTRIBUTE_UNUSED static bool vlog_enabled = true; // Set directly based on your needs.
#else
Expand Down
3 changes: 1 addition & 2 deletions onnxruntime/core/common/logging/sinks/ostream_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

#include "core/common/logging/sinks/ostream_sink.h"
#include "date/date.h"

namespace onnxruntime {
namespace logging {
Expand All @@ -24,7 +23,7 @@ struct Color {

void OStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) {
// operator for formatting of timestamp in ISO8601 format including microseconds
using date::operator<<;
using timestamp_ns::operator<<;

// Two options as there may be multiple calls attempting to write to the same sink at once:
// 1) Use mutex to synchronize access to the stream.
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/platform/apple/logging/apple_log_sink.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace logging {

void AppleLogSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) {
using date::operator<<;
using timestamp_ns::operator<<;
std::ostringstream msg;
msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
<< message.Location().ToString() << "] " << message.Message();
Expand Down
3 changes: 1 addition & 2 deletions onnxruntime/test/common/logging/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <sstream>

#include "date/date.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -30,7 +29,7 @@ class MockSink : public ::onnxruntime::logging::ISink {
#endif

ACTION(PrintArgs) {
using date::operator<<;
using onnxruntime::logging::timestamp_ns::operator<<;

// const Timestamp &timestamp, const std::string &logger_id, const Message &message
// arg0 arg1 arg2
Expand Down
4 changes: 1 addition & 3 deletions onnxruntime/test/util/include/test/capturing_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "core/common/logging/logging.h"
#include "core/common/logging/isink.h"

#include "date/date.h"

namespace onnxruntime {
namespace test {

Expand All @@ -17,7 +15,7 @@ class CapturingSink : public logging::ISink {
public:
void SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) override {
// operator for formatting of timestamp in ISO8601 format including microseconds
using date::operator<<;
using timestamp_ns::operator<<;
std::ostringstream msg;

msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
Expand Down

0 comments on commit 528b8a5

Please sign in to comment.