mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-27 20:02:15 +00:00
Using wostringstream only on Windows (#21938)
### 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.
This commit is contained in:
parent
c7ae9b977a
commit
93c4c9cb6a
5 changed files with 69 additions and 5 deletions
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
namespace logging {
|
||||
#ifndef _WIN32
|
||||
/// <summary>
|
||||
/// ISink that writes to a file.
|
||||
/// </summary>
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<<;
|
||||
|
|
@ -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<<;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
namespace logging {
|
||||
#ifndef _WIN32
|
||||
/// <summary>
|
||||
/// A std::ostream based ISink
|
||||
/// </summary>
|
||||
|
|
@ -29,7 +30,7 @@ class OStreamSink : public ISink {
|
|||
std::ostream* stream_;
|
||||
const bool flush_;
|
||||
};
|
||||
#ifdef _WIN32
|
||||
#else
|
||||
/// <summary>
|
||||
/// A std::wostream based ISink
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue