Skip to content

Commit

Permalink
Using wostringstream only on Windows (microsoft#21938)
Browse files Browse the repository at this point in the history
### Description
Using wostringstream only on Windows



### Motivation and Context
From line
[62](https://github.com/microsoft/onnxruntime/pull/21938/files#diff-47776d020ac08134de4059eab473550237f4999c598ab56afad3676d2f193edcR62),
currently, `stream_` can be either `wostringstream` or `ostringstream`
depending on the OS, however, for Unix like system, `stream_` should be
`ostringstream`, instead of.
  • Loading branch information
jchen351 authored Sep 9, 2024
1 parent c7ae9b9 commit 93c4c9c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
43 changes: 43 additions & 0 deletions onnxruntime/core/common/logging/sinks/file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace onnxruntime {
namespace logging {
#ifndef _WIN32
/// <summary>
/// ISink that writes to a file.
/// </summary>
Expand Down Expand Up @@ -47,5 +48,47 @@ class FileSink : public OStreamSink {
std::unique_ptr<std::ofstream> file_;
bool filter_user_data_;
};

#else
/// <summary>
/// ISink that writes to a file.
/// </summary>
/// <seealso cref="ISink" />
class FileSink : public WOStreamSink {
public:
/// <summary>
/// Initializes a new instance of the <see cref="FileSink" /> class.
/// </summary>
/// <param name="filename">The filename to write to.</param>
/// <param name="append">If set to <c>true</c> [append to file]. Otherwise truncate.</param>
/// <param name="filter_user_data">If set to <c>true</c> [removes user data].</param>
/// <remarks>Filtering of user data can alternatively be done at the <see cref="LoggingManager" /> level.</remarks>
FileSink(std::unique_ptr<std::wofstream> file, bool filter_user_data)
: WOStreamSink(*file, /*flush*/ true), file_(std::move(file)), filter_user_data_{filter_user_data} {
}

/// <summary>
/// Initializes a new instance of the <see cref="FileSink" /> class.
/// </summary>
/// <param name="filename">The filename to write to.</param>
/// <param name="append">If set to <c>true</c> [append to file]. Otherwise truncate.</param>
/// <param name="filter_user_data">If set to <c>true</c> [removes user data].</param>
/// <remarks>Filtering of user data can alternatively be done at the <see cref="LoggingManager" /> level.</remarks>
FileSink(const std::wstring& filename, bool append, bool filter_user_data)
: FileSink{std::make_unique<std::wofstream>(filename, std::ios::out | (append ? std::ios::app : std::ios::trunc)),
filter_user_data} {
}

private:
void SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) override {
if (!filter_user_data_ || message.DataType() != DataType::USER) {
WOStreamSink::SendImpl(timestamp, logger_id, message);
}
}

std::unique_ptr<std::wofstream> file_;
bool filter_user_data_;
};
#endif
} // namespace logging
} // namespace onnxruntime
3 changes: 2 additions & 1 deletion onnxruntime/core/common/logging/sinks/ostream_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Color {
};
#endif

#ifndef _WIN32
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 timestamp_ns::operator<<;
Expand Down Expand Up @@ -62,7 +63,7 @@ void OStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logger
stream_->flush();
}
}
#ifdef _WIN32
#else
void WOStreamSink::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<<;
Expand Down
3 changes: 2 additions & 1 deletion onnxruntime/core/common/logging/sinks/ostream_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace onnxruntime {
namespace logging {
#ifndef _WIN32
/// <summary>
/// A std::ostream based ISink
/// </summary>
Expand All @@ -29,7 +30,7 @@ class OStreamSink : public ISink {
std::ostream* stream_;
const bool flush_;
};
#ifdef _WIN32
#else
/// <summary>
/// A std::wostream based ISink
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion onnxruntime/test/common/logging/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,30 @@ TEST_F(LoggingTestsFixture, TestVLog) {
#endif
}

#ifdef _WIN32
class CTestSink : public WOStreamSink {
public:
CTestSink(std::wostringstream& stream) : WOStreamSink(stream, /*flush*/ true) {
}
};
#else
class CTestSink : public OStreamSink {
public:
CTestSink(std::ostringstream& stream) : OStreamSink(stream, /*flush*/ true) {
}
};
#endif

TEST_F(LoggingTestsFixture, TestTruncation) {
const std::string logger_id{"TestTruncation"};
const Severity min_log_level = Severity::kVERBOSE;
constexpr bool filter_user_data = false;

#ifdef _WIN32
std::wostringstream out;
#else
std::ostringstream out;
#endif
auto* sink_ptr = new CTestSink{out};

LoggingManager manager{std::unique_ptr<ISink>(sink_ptr), min_log_level, filter_user_data,
Expand All @@ -261,8 +273,11 @@ TEST_F(LoggingTestsFixture, TestTruncation) {

// attempt to print string longer than hard-coded 2K buffer limit
LOGF(*logger, ERROR, "%s", std::string(4096, 'a').c_str());

#ifdef _WIN32
EXPECT_THAT(out.str(), HasSubstr(L"[...truncated...]"));
#else
EXPECT_THAT(out.str(), HasSubstr("[...truncated...]"));
#endif
}

TEST_F(LoggingTestsFixture, TestStreamMacroFromConditionalWithoutCompoundStatement) {
Expand Down
8 changes: 6 additions & 2 deletions onnxruntime/test/common/logging/sinks_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "core/common/common.h"
#include "core/common/logging/capture.h"
#include "core/common/logging/logging.h"
#include "core/common/logging/sinks/cerr_sink.h"
Expand Down Expand Up @@ -126,9 +126,13 @@ TEST(LoggingTests, TestFileSink) {

// create scoped manager so sink gets destroyed once done
{
#ifdef _WIN32
LoggingManager manager{std::make_unique<FileSink>(onnxruntime::ToWideString(filename), false, false),
min_log_level, false, InstanceType::Temporal};
#else
LoggingManager manager{std::unique_ptr<ISink>{new FileSink{filename, false, false}},
min_log_level, false, InstanceType::Temporal};

#endif
auto logger = manager.CreateLogger(logid);

LOGS(*logger, WARNING) << message;
Expand Down

0 comments on commit 93c4c9c

Please sign in to comment.