diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index a8af2e6c28..177bf6a968 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -7,10 +7,12 @@ cc_library( name = "ostream_log_record_exporter", srcs = [ "src/log_record_exporter.cc", + "src/log_record_exporter_factory.cc", ], hdrs = [ "include/opentelemetry/exporters/ostream/common_utils.h", "include/opentelemetry/exporters/ostream/log_record_exporter.h", + "include/opentelemetry/exporters/ostream/log_record_exporter_factory.h", ], strip_include_prefix = "include", tags = ["ostream"], diff --git a/exporters/ostream/CMakeLists.txt b/exporters/ostream/CMakeLists.txt index 10e0792de8..478c6b7ec2 100644 --- a/exporters/ostream/CMakeLists.txt +++ b/exporters/ostream/CMakeLists.txt @@ -27,7 +27,7 @@ if(OPENTELEMETRY_INSTALL) DIRECTORY include/opentelemetry/exporters/ostream DESTINATION include/opentelemetry/exporters PATTERN "*.h" - PATTERN "log_Exporter.h" EXCLUDE) + PATTERN "log_record_exporter.h" EXCLUDE) endif() if(BUILD_TESTING) @@ -77,7 +77,8 @@ if(BUILD_TESTING) endif() if(WITH_LOGS_PREVIEW) - add_library(opentelemetry_exporter_ostream_logs src/log_record_exporter.cc) + add_library(opentelemetry_exporter_ostream_logs + src/log_record_exporter.cc src/log_record_exporter_factory.cc) set_target_properties(opentelemetry_exporter_ostream_logs PROPERTIES EXPORT_NAME ostream_log_record_exporter) set_target_version(opentelemetry_exporter_ostream_logs) @@ -96,9 +97,10 @@ if(WITH_LOGS_PREVIEW) LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install( - DIRECTORY include/opentelemetry/exporters/ostream - DESTINATION include/opentelemetry/exporters - PATTERN "log_record_exporter.h") + FILES + "include/opentelemetry/exporters/ostream/log_record_exporter.h" + "include/opentelemetry/exporters/ostream/log_record_exporter_factory.h" + DESTINATION include/opentelemetry/exporters/ostream) endif() if(BUILD_TESTING) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter_factory.h b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter_factory.h new file mode 100644 index 0000000000..4c7d403d41 --- /dev/null +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter_factory.h @@ -0,0 +1,46 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#ifdef ENABLE_LOGS_PREVIEW +# include +# include + +# include "opentelemetry/sdk/version/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace logs +{ +class LogRecordExporter; +} // namespace logs +} // namespace sdk + +namespace exporter +{ +namespace logs +{ + +/** + * Factory class for OStreamLogRecordExporter. + */ +class OPENTELEMETRY_EXPORT OStreamLogRecordExporterFactory +{ +public: + /** + * Creates an OStreamLogRecordExporter writing to the default location. + */ + static std::unique_ptr Create(); + + /** + * Creates an OStreamLogRecordExporter writing to the given location. + */ + static std::unique_ptr Create(std::ostream &sout); +}; + +} // namespace logs +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/exporters/ostream/src/log_record_exporter_factory.cc b/exporters/ostream/src/log_record_exporter_factory.cc new file mode 100644 index 0000000000..884f59a9a1 --- /dev/null +++ b/exporters/ostream/src/log_record_exporter_factory.cc @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_LOGS_PREVIEW +# include "opentelemetry/exporters/ostream/log_record_exporter_factory.h" +# include "opentelemetry/exporters/ostream/log_record_exporter.h" + +namespace logs_sdk = opentelemetry::sdk::logs; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace logs +{ + +std::unique_ptr OStreamLogRecordExporterFactory::Create() +{ + return Create(std::cout); +} + +std::unique_ptr OStreamLogRecordExporterFactory::Create( + std::ostream &sout) +{ + std::unique_ptr exporter(new OStreamLogRecordExporter(sout)); + return exporter; +} + +} // namespace logs +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/exporters/ostream/test/ostream_log_test.cc b/exporters/ostream/test/ostream_log_test.cc index 92ff3ece76..c092ec01c3 100644 --- a/exporters/ostream/test/ostream_log_test.cc +++ b/exporters/ostream/test/ostream_log_test.cc @@ -4,6 +4,7 @@ #ifdef ENABLE_LOGS_PREVIEW # include "opentelemetry/exporters/ostream/log_record_exporter.h" +# include "opentelemetry/exporters/ostream/log_record_exporter_factory.h" # include "opentelemetry/logs/provider.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" @@ -507,6 +508,13 @@ TEST(OStreamLogRecordExporter, IntegrationTestWithEventId) } } +// Test using the factory to create the ostream exporter +TEST(OStreamLogRecordExporter, Factory) +{ + auto exporter = OStreamLogRecordExporterFactory::Create(); + ASSERT_NE(exporter, nullptr); +} + } // namespace logs } // namespace exporter OPENTELEMETRY_END_NAMESPACE