From ce4a3950de741c313d4b8751006e8e60d98af10b Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Wed, 26 May 2021 15:27:06 +0800 Subject: [PATCH 1/5] Add SampleFileLogger C++ example --- pulsar-client-cpp/examples/CMakeLists.txt | 6 + .../examples/SampleAsyncProducer.cc | 2 +- pulsar-client-cpp/examples/SampleConsumer.cc | 2 +- .../examples/SampleConsumerListener.cc | 2 +- .../examples/SampleFileLogger.cc | 111 ++++++++++++++++++ pulsar-client-cpp/examples/SampleProducer.cc | 2 +- 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 pulsar-client-cpp/examples/SampleFileLogger.cc diff --git a/pulsar-client-cpp/examples/CMakeLists.txt b/pulsar-client-cpp/examples/CMakeLists.txt index ec4f8dec21e99..bd124649cac56 100644 --- a/pulsar-client-cpp/examples/CMakeLists.txt +++ b/pulsar-client-cpp/examples/CMakeLists.txt @@ -37,6 +37,10 @@ set(SAMPLE_PRODUCER_SOURCES SampleProducer.cc ) +set(SAMPLE_FILE_LOGGER_SOURCES + SampleFileLogger.cc +) + set(SAMPLE_PRODUCER_C_SOURCES SampleProducerCApi.c ) @@ -57,6 +61,7 @@ add_executable(SampleAsyncProducer ${SAMPLE_ASYNC_PRODUCER_SOURCES}) add_executable(SampleConsumer ${SAMPLE_CONSUMER_SOURCES}) add_executable(SampleConsumerListener ${SAMPLE_CONSUMER_LISTENER_SOURCES}) add_executable(SampleProducer ${SAMPLE_PRODUCER_SOURCES}) +add_executable(SampleFileLogger ${SAMPLE_FILE_LOGGER_SOURCES}) add_executable(SampleProducerCApi ${SAMPLE_PRODUCER_C_SOURCES}) add_executable(SampleConsumerCApi ${SAMPLE_CONSUMER_C_SOURCES}) add_executable(SampleConsumerListenerCApi ${SAMPLE_CONSUMER_LISTENER_C_SOURCES}) @@ -66,6 +71,7 @@ target_link_libraries(SampleAsyncProducer ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumer ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumerListener ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleProducer ${CLIENT_LIBS} pulsarShared) +target_link_libraries(SampleFileLogger ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleProducerCApi ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumerCApi ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumerListenerCApi ${CLIENT_LIBS} pulsarShared) diff --git a/pulsar-client-cpp/examples/SampleAsyncProducer.cc b/pulsar-client-cpp/examples/SampleAsyncProducer.cc index ccc2c0fa7dc0d..9701ccbe6422d 100644 --- a/pulsar-client-cpp/examples/SampleAsyncProducer.cc +++ b/pulsar-client-cpp/examples/SampleAsyncProducer.cc @@ -35,7 +35,7 @@ int main() { Client client("pulsar://localhost:6650"); Producer producer; - Result result = client.createProducer("persistent://prop/r1/ns1/my-topic", producer); + Result result = client.createProducer("persistent://public/default/my-topic", producer); if (result != ResultOk) { LOG_ERROR("Error creating producer: " << result); return -1; diff --git a/pulsar-client-cpp/examples/SampleConsumer.cc b/pulsar-client-cpp/examples/SampleConsumer.cc index e61b793987fd6..1dcc550f81154 100644 --- a/pulsar-client-cpp/examples/SampleConsumer.cc +++ b/pulsar-client-cpp/examples/SampleConsumer.cc @@ -30,7 +30,7 @@ int main() { Client client("pulsar://localhost:6650"); Consumer consumer; - Result result = client.subscribe("persistent://prop/r1/ns1/my-topic", "consumer-1", consumer); + Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", consumer); if (result != ResultOk) { LOG_ERROR("Failed to subscribe: " << result); return -1; diff --git a/pulsar-client-cpp/examples/SampleConsumerListener.cc b/pulsar-client-cpp/examples/SampleConsumerListener.cc index 5d60ccd12c26f..9ce2291636ab1 100644 --- a/pulsar-client-cpp/examples/SampleConsumerListener.cc +++ b/pulsar-client-cpp/examples/SampleConsumerListener.cc @@ -38,7 +38,7 @@ int main() { Consumer consumer; ConsumerConfiguration config; config.setMessageListener(listener); - Result result = client.subscribe("persistent://prop/r1/ns1/my-topic", "consumer-1", config, consumer); + Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", config, consumer); if (result != ResultOk) { LOG_ERROR("Failed to subscribe: " << result); return -1; diff --git a/pulsar-client-cpp/examples/SampleFileLogger.cc b/pulsar-client-cpp/examples/SampleFileLogger.cc new file mode 100644 index 0000000000000..57563743c4f68 --- /dev/null +++ b/pulsar-client-cpp/examples/SampleFileLogger.cc @@ -0,0 +1,111 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include + +#include +#include +#include +#include +#include + +#include + +class FileLogger : public pulsar::Logger { + public: + FileLogger(std::ofstream& os, Level level, const std::string& filename) + : os_(os), level_(level), filename_(filename) {} + + bool isEnabled(Level level) override { return level >= level_; } + + void log(Level level, int line, const std::string& message) override { + std::ostringstream oss; + auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + oss << currentTime() << " " << convertLevelToString(level) << " [" << std::this_thread::get_id() + << "] " << filename_ << ":" << line << " " << message << "\n"; + os_ << oss.str(); + os_.flush(); + } + + private: + std::ostream& os_; + const Level level_; + const std::string filename_; + + static const char* currentTime() { + auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + char* s = ctime(&now); // ctime() returns a string with a newline at the end + auto newLinePos = strlen(s) - 1; + s[newLinePos] = '\0'; + return s; + } + + static const char* convertLevelToString(Level level) { + switch (level) { + case Level::LEVEL_DEBUG: + return "DEBUG"; + case Level::LEVEL_INFO: + return "INFO"; + case Level::LEVEL_WARN: + return "WARN"; + case Level::LEVEL_ERROR: + return "ERROR"; + default: + return "???"; + } + } +}; + +/** + * A logger factory that is appending logs to a single file. + */ +class SingleFileLoggerFactory : public pulsar::LoggerFactory { + public: + /** + * Create a SingleFileLoggerFactory instance. + * + * @param level the log level + * @param logFilePath the log file's path + */ + SingleFileLoggerFactory(pulsar::Logger::Level level, const std::string& logFilePath) + : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} + + ~SingleFileLoggerFactory() { os_.close(); } + + virtual pulsar::Logger* getLogger(const std::string& filename) override { + return new FileLogger(os_, level_, filename); + } + + private: + const pulsar::Logger::Level level_; + std::ofstream os_; +}; + +using namespace pulsar; + +int main(int argc, char* argv[]) { + ClientConfiguration clientConf; + // The logs whose level is >= INFO will be written to pulsar-cpp-client.log + clientConf.setLogger(new SingleFileLoggerFactory(Logger::Level::LEVEL_INFO, "pulsar-cpp-client.log")); + + Client client("pulsar://localhost:6650", clientConf); + Producer producer; + client.createProducer("my-topic", producer); // just to create some logs + client.close(); + return 0; +} diff --git a/pulsar-client-cpp/examples/SampleProducer.cc b/pulsar-client-cpp/examples/SampleProducer.cc index bc1ece26ea43d..ff5048792d114 100644 --- a/pulsar-client-cpp/examples/SampleProducer.cc +++ b/pulsar-client-cpp/examples/SampleProducer.cc @@ -30,7 +30,7 @@ int main() { Client client("pulsar://localhost:6650"); Producer producer; - Result result = client.createProducer("persistent://prop/r1/ns1/my-topic", producer); + Result result = client.createProducer("persistent://public/default/my-topic", producer); if (result != ResultOk) { LOG_ERROR("Error creating producer: " << result); return -1; From dea40e46048363847c96f504f016df5d34edeb44 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Thu, 27 May 2021 16:13:45 +0800 Subject: [PATCH 2/5] Expose SingleFileLoggerFactory as the public interface --- .../examples/SampleFileLogger.cc | 78 ----------------- pulsar-client-cpp/include/pulsar/Client.h | 2 + .../include/pulsar/SingleFileLoggerFactory.h | 68 +++++++++++++++ pulsar-client-cpp/lib/SimpleLogger.h | 86 +++++++++++++++++++ pulsar-client-cpp/lib/SimpleLoggerFactory.cc | 63 +------------- .../lib/SingleFileLoggerFactory.cc | 28 ++++++ 6 files changed, 187 insertions(+), 138 deletions(-) create mode 100644 pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h create mode 100644 pulsar-client-cpp/lib/SimpleLogger.h create mode 100644 pulsar-client-cpp/lib/SingleFileLoggerFactory.cc diff --git a/pulsar-client-cpp/examples/SampleFileLogger.cc b/pulsar-client-cpp/examples/SampleFileLogger.cc index 57563743c4f68..8f028c810e69e 100644 --- a/pulsar-client-cpp/examples/SampleFileLogger.cc +++ b/pulsar-client-cpp/examples/SampleFileLogger.cc @@ -16,86 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -#include - -#include -#include -#include -#include -#include - #include -class FileLogger : public pulsar::Logger { - public: - FileLogger(std::ofstream& os, Level level, const std::string& filename) - : os_(os), level_(level), filename_(filename) {} - - bool isEnabled(Level level) override { return level >= level_; } - - void log(Level level, int line, const std::string& message) override { - std::ostringstream oss; - auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - oss << currentTime() << " " << convertLevelToString(level) << " [" << std::this_thread::get_id() - << "] " << filename_ << ":" << line << " " << message << "\n"; - os_ << oss.str(); - os_.flush(); - } - - private: - std::ostream& os_; - const Level level_; - const std::string filename_; - - static const char* currentTime() { - auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - char* s = ctime(&now); // ctime() returns a string with a newline at the end - auto newLinePos = strlen(s) - 1; - s[newLinePos] = '\0'; - return s; - } - - static const char* convertLevelToString(Level level) { - switch (level) { - case Level::LEVEL_DEBUG: - return "DEBUG"; - case Level::LEVEL_INFO: - return "INFO"; - case Level::LEVEL_WARN: - return "WARN"; - case Level::LEVEL_ERROR: - return "ERROR"; - default: - return "???"; - } - } -}; - -/** - * A logger factory that is appending logs to a single file. - */ -class SingleFileLoggerFactory : public pulsar::LoggerFactory { - public: - /** - * Create a SingleFileLoggerFactory instance. - * - * @param level the log level - * @param logFilePath the log file's path - */ - SingleFileLoggerFactory(pulsar::Logger::Level level, const std::string& logFilePath) - : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} - - ~SingleFileLoggerFactory() { os_.close(); } - - virtual pulsar::Logger* getLogger(const std::string& filename) override { - return new FileLogger(os_, level_, filename); - } - - private: - const pulsar::Logger::Level level_; - std::ofstream os_; -}; - using namespace pulsar; int main(int argc, char* argv[]) { diff --git a/pulsar-client-cpp/include/pulsar/Client.h b/pulsar-client-cpp/include/pulsar/Client.h index 881d484925334..fb5458d7c91a9 100644 --- a/pulsar-client-cpp/include/pulsar/Client.h +++ b/pulsar-client-cpp/include/pulsar/Client.h @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include namespace pulsar { diff --git a/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h b/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h new file mode 100644 index 0000000000000..6b0c9ce6dc167 --- /dev/null +++ b/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h @@ -0,0 +1,68 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include +#include +#include + +namespace pulsar { + +/** + * A logger factory that is appending logs to a single file. + * + * The log format is "yyyy-mm-dd hh:MM:ss.xxx : | ", like + * + * ``` + * 2021-03-24 17:35:46.571 INFO [0x10a951e00] ConnectionPool:85 | Created connection for ... + * ``` + * + * Example: + * + * ```c++ + * #include + * + * ClientConfiguration conf; + * conf.setLogger(new SingleFileLoggerFactory(Logger::LEVEL_DEBUG, "pulsar-client-cpp.log")); + * Client client("pulsar://localhost:6650", conf); + * ``` + */ +class SingleFileLoggerFactory : public pulsar::LoggerFactory { + public: + /** + * Create a SingleFileLoggerFactory instance. + * + * @param level the log level + * @param logFilePath the log file's path + */ + SingleFileLoggerFactory(Logger::Level level, const std::string& logFilePath) + : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} + + ~SingleFileLoggerFactory() { os_.close(); } + + pulsar::Logger* getLogger(const std::string& filename) override; + + private: + const pulsar::Logger::Level level_; + std::ofstream os_; +}; + +} // namespace pulsar diff --git a/pulsar-client-cpp/lib/SimpleLogger.h b/pulsar-client-cpp/lib/SimpleLogger.h new file mode 100644 index 0000000000000..b750336a37f68 --- /dev/null +++ b/pulsar-client-cpp/lib/SimpleLogger.h @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace pulsar { + +inline std::ostream &operator<<(std::ostream &s, Logger::Level level) { + switch (level) { + case Logger::LEVEL_DEBUG: + s << "DEBUG"; + break; + case Logger::LEVEL_INFO: + s << "INFO "; + break; + case Logger::LEVEL_WARN: + s << "WARN "; + break; + case Logger::LEVEL_ERROR: + s << "ERROR"; + break; + } + + return s; +} + +class SimpleLogger : public Logger { + public: + SimpleLogger(std::ostream &os, const std::string &filename, Level level) + : os_(os), filename_(filename), level_(level) {} + + bool isEnabled(Level level) { return level >= level_; } + + void log(Level level, int line, const std::string &message) { + std::stringstream ss; + + printTimestamp(ss); + ss << " " << level << " [" << std::this_thread::get_id() << "] " << filename_ << ":" << line << " | " + << message << "\n"; + + os_ << ss.str(); + os_.flush(); + } + + private: + std::ostream &os_; + const std::string filename_; + const Level level_; + + static std::ostream &printTimestamp(std::ostream &s) { + boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); + + const boost::format f = + boost::format("%04d-%02d-%02d %02d:%02d:%02d.%03d") % now.date().year_month_day().year % + now.date().year_month_day().month.as_number() % now.date().year_month_day().day.as_number() % + now.time_of_day().hours() % now.time_of_day().minutes() % now.time_of_day().seconds() % + (now.time_of_day().fractional_seconds() / 1000); + + s << f.str(); + return s; + } +}; + +} // namespace pulsar diff --git a/pulsar-client-cpp/lib/SimpleLoggerFactory.cc b/pulsar-client-cpp/lib/SimpleLoggerFactory.cc index 5fb5b67810925..bea8bc12fd7a9 100644 --- a/pulsar-client-cpp/lib/SimpleLoggerFactory.cc +++ b/pulsar-client-cpp/lib/SimpleLoggerFactory.cc @@ -18,69 +18,12 @@ */ #include - -#include -#include -#include -#include -#include +#include "lib/SimpleLogger.h" namespace pulsar { -inline std::ostream &operator<<(std::ostream &s, Logger::Level level) { - switch (level) { - case Logger::LEVEL_DEBUG: - s << "DEBUG"; - break; - case Logger::LEVEL_INFO: - s << "INFO "; - break; - case Logger::LEVEL_WARN: - s << "WARN "; - break; - case Logger::LEVEL_ERROR: - s << "ERROR"; - break; - } - - return s; +Logger *SimpleLoggerFactory::getLogger(const std::string &file) { + return new SimpleLogger(std::cout, file, level_); } -class SimpleLogger : public Logger { - public: - SimpleLogger(const std::string &filename, Level level) : filename_(filename), level_(level) {} - - bool isEnabled(Level level) { return level >= level_; } - - void log(Level level, int line, const std::string &message) { - std::stringstream ss; - - printTimestamp(ss); - ss << " " << level << " [" << std::this_thread::get_id() << "] " << filename_ << ":" << line << " | " - << message << "\n"; - - std::cout << ss.str(); - std::cout.flush(); - } - - private: - const std::string filename_; - const Level level_; - - static std::ostream &printTimestamp(std::ostream &s) { - boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); - - const boost::format f = - boost::format("%04d-%02d-%02d %02d:%02d:%02d.%03d") % now.date().year_month_day().year % - now.date().year_month_day().month.as_number() % now.date().year_month_day().day.as_number() % - now.time_of_day().hours() % now.time_of_day().minutes() % now.time_of_day().seconds() % - (now.time_of_day().fractional_seconds() / 1000); - - s << f.str(); - return s; - } -}; - -Logger *SimpleLoggerFactory::getLogger(const std::string &file) { return new SimpleLogger(file, level_); } - } // namespace pulsar diff --git a/pulsar-client-cpp/lib/SingleFileLoggerFactory.cc b/pulsar-client-cpp/lib/SingleFileLoggerFactory.cc new file mode 100644 index 0000000000000..b07d3237d9cb2 --- /dev/null +++ b/pulsar-client-cpp/lib/SingleFileLoggerFactory.cc @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include +#include "lib/SimpleLogger.h" + +namespace pulsar { + +pulsar::Logger* SingleFileLoggerFactory::getLogger(const std::string& filename) { + return new SimpleLogger(os_, filename, level_); +} + +} // namespace pulsar From 44be80f2cacb1bb3ab5d254fcdc81d5afbed9b77 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Thu, 27 May 2021 17:12:04 +0800 Subject: [PATCH 3/5] Make logger factory public --- pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h | 2 +- pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h b/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h index 1ab11dd249755..f05ec0acfd0d5 100644 --- a/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h @@ -44,7 +44,7 @@ namespace pulsar { * Client client("pulsar://localhost:6650", conf); * ``` */ -class SimpleLoggerFactory : public LoggerFactory { +class PULSAR_PUBLIC SimpleLoggerFactory : public LoggerFactory { public: explicit SimpleLoggerFactory() = default; explicit SimpleLoggerFactory(Logger::Level level) : level_(level) {} diff --git a/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h b/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h index 6b0c9ce6dc167..2aebbdf5b92d2 100644 --- a/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h @@ -45,7 +45,7 @@ namespace pulsar { * Client client("pulsar://localhost:6650", conf); * ``` */ -class SingleFileLoggerFactory : public pulsar::LoggerFactory { +class PULSAR_PUBLIC SingleFileLoggerFactory : public pulsar::LoggerFactory { public: /** * Create a SingleFileLoggerFactory instance. From 8160da54ec748f072f7f6006648a6f17d737d342 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Fri, 28 May 2021 20:42:21 +0800 Subject: [PATCH 4/5] Change logger factory's name --- pulsar-client-cpp/examples/SampleFileLogger.cc | 2 +- pulsar-client-cpp/include/pulsar/Client.h | 4 ++-- ...{SimpleLoggerFactory.h => ConsoleLoggerFactory.h} | 10 +++++----- ...SingleFileLoggerFactory.h => FileLoggerFactory.h} | 12 ++++++------ pulsar-client-cpp/lib/ClientImpl.cc | 6 +++--- ...impleLoggerFactory.cc => ConsoleLoggerFactory.cc} | 4 ++-- ...ngleFileLoggerFactory.cc => FileLoggerFactory.cc} | 4 ++-- pulsar-client-cpp/lib/LogUtils.cc | 4 ++-- pulsar-client-cpp/tests/CustomLoggerTest.cc | 12 ++++++------ 9 files changed, 29 insertions(+), 29 deletions(-) rename pulsar-client-cpp/include/pulsar/{SimpleLoggerFactory.h => ConsoleLoggerFactory.h} (84%) rename pulsar-client-cpp/include/pulsar/{SingleFileLoggerFactory.h => FileLoggerFactory.h} (81%) rename pulsar-client-cpp/lib/{SimpleLoggerFactory.cc => ConsoleLoggerFactory.cc} (89%) rename pulsar-client-cpp/lib/{SingleFileLoggerFactory.cc => FileLoggerFactory.cc} (88%) diff --git a/pulsar-client-cpp/examples/SampleFileLogger.cc b/pulsar-client-cpp/examples/SampleFileLogger.cc index 8f028c810e69e..fcc3d417b76ca 100644 --- a/pulsar-client-cpp/examples/SampleFileLogger.cc +++ b/pulsar-client-cpp/examples/SampleFileLogger.cc @@ -23,7 +23,7 @@ using namespace pulsar; int main(int argc, char* argv[]) { ClientConfiguration clientConf; // The logs whose level is >= INFO will be written to pulsar-cpp-client.log - clientConf.setLogger(new SingleFileLoggerFactory(Logger::Level::LEVEL_INFO, "pulsar-cpp-client.log")); + clientConf.setLogger(new FileLoggerFactory(Logger::Level::LEVEL_INFO, "pulsar-cpp-client.log")); Client client("pulsar://localhost:6650", clientConf); Producer producer; diff --git a/pulsar-client-cpp/include/pulsar/Client.h b/pulsar-client-cpp/include/pulsar/Client.h index fb5458d7c91a9..1bc2614538054 100644 --- a/pulsar-client-cpp/include/pulsar/Client.h +++ b/pulsar-client-cpp/include/pulsar/Client.h @@ -28,8 +28,8 @@ #include #include #include -#include -#include +#include +#include #include namespace pulsar { diff --git a/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h b/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h similarity index 84% rename from pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h rename to pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h index f05ec0acfd0d5..0ee6e47ff099e 100644 --- a/pulsar-client-cpp/include/pulsar/SimpleLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h @@ -37,17 +37,17 @@ namespace pulsar { * level simply. * * ```c++ - * #include + * #include * * ClientConfiguration conf; - * conf.setLogger(new SimpleLoggerFactory(Logger::LEVEL_DEBUG)); + * conf.setLogger(new ConsoleLoggerFactory(Logger::LEVEL_DEBUG)); * Client client("pulsar://localhost:6650", conf); * ``` */ -class PULSAR_PUBLIC SimpleLoggerFactory : public LoggerFactory { +class PULSAR_PUBLIC ConsoleLoggerFactory : public LoggerFactory { public: - explicit SimpleLoggerFactory() = default; - explicit SimpleLoggerFactory(Logger::Level level) : level_(level) {} + explicit ConsoleLoggerFactory() = default; + explicit ConsoleLoggerFactory(Logger::Level level) : level_(level) {} Logger* getLogger(const std::string& fileName) override; diff --git a/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h b/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h similarity index 81% rename from pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h rename to pulsar-client-cpp/include/pulsar/FileLoggerFactory.h index 2aebbdf5b92d2..9e8823d7ff413 100644 --- a/pulsar-client-cpp/include/pulsar/SingleFileLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h @@ -38,25 +38,25 @@ namespace pulsar { * Example: * * ```c++ - * #include + * #include * * ClientConfiguration conf; - * conf.setLogger(new SingleFileLoggerFactory(Logger::LEVEL_DEBUG, "pulsar-client-cpp.log")); + * conf.setLogger(new FileLoggerFactory(Logger::LEVEL_DEBUG, "pulsar-client-cpp.log")); * Client client("pulsar://localhost:6650", conf); * ``` */ -class PULSAR_PUBLIC SingleFileLoggerFactory : public pulsar::LoggerFactory { +class PULSAR_PUBLIC FileLoggerFactory : public pulsar::LoggerFactory { public: /** - * Create a SingleFileLoggerFactory instance. + * Create a FileLoggerFactory instance. * * @param level the log level * @param logFilePath the log file's path */ - SingleFileLoggerFactory(Logger::Level level, const std::string& logFilePath) + FileLoggerFactory(Logger::Level level, const std::string& logFilePath) : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} - ~SingleFileLoggerFactory() { os_.close(); } + ~FileLoggerFactory() { os_.close(); } pulsar::Logger* getLogger(const std::string& filename) override; diff --git a/pulsar-client-cpp/lib/ClientImpl.cc b/pulsar-client-cpp/lib/ClientImpl.cc index 5edda08e50ff3..d9a2b570d8c48 100644 --- a/pulsar-client-cpp/lib/ClientImpl.cc +++ b/pulsar-client-cpp/lib/ClientImpl.cc @@ -26,7 +26,7 @@ #include "PartitionedConsumerImpl.h" #include "MultiTopicsConsumerImpl.h" #include "PatternMultiTopicsConsumerImpl.h" -#include +#include #include #include #include @@ -101,11 +101,11 @@ ClientImpl::ClientImpl(const std::string& serviceUrl, const ClientConfiguration& loggerFactory = Log4CxxLoggerFactory::create(clientConfiguration_.getLogConfFilePath()); } else { // Use default simple console logger - loggerFactory.reset(new SimpleLoggerFactory); + loggerFactory.reset(new ConsoleLoggerFactory); } #else // Use default simple console logger - loggerFactory.reset(new SimpleLoggerFactory); + loggerFactory.reset(new ConsoleLoggerFactory); #endif } LogUtils::setLoggerFactory(std::move(loggerFactory)); diff --git a/pulsar-client-cpp/lib/SimpleLoggerFactory.cc b/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc similarity index 89% rename from pulsar-client-cpp/lib/SimpleLoggerFactory.cc rename to pulsar-client-cpp/lib/ConsoleLoggerFactory.cc index bea8bc12fd7a9..91219fd18a2e1 100644 --- a/pulsar-client-cpp/lib/SimpleLoggerFactory.cc +++ b/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc @@ -17,12 +17,12 @@ * under the License. */ -#include +#include #include "lib/SimpleLogger.h" namespace pulsar { -Logger *SimpleLoggerFactory::getLogger(const std::string &file) { +Logger *ConsoleLoggerFactory::getLogger(const std::string &file) { return new SimpleLogger(std::cout, file, level_); } diff --git a/pulsar-client-cpp/lib/SingleFileLoggerFactory.cc b/pulsar-client-cpp/lib/FileLoggerFactory.cc similarity index 88% rename from pulsar-client-cpp/lib/SingleFileLoggerFactory.cc rename to pulsar-client-cpp/lib/FileLoggerFactory.cc index b07d3237d9cb2..358c9b0aba878 100644 --- a/pulsar-client-cpp/lib/SingleFileLoggerFactory.cc +++ b/pulsar-client-cpp/lib/FileLoggerFactory.cc @@ -16,12 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -#include +#include #include "lib/SimpleLogger.h" namespace pulsar { -pulsar::Logger* SingleFileLoggerFactory::getLogger(const std::string& filename) { +pulsar::Logger* FileLoggerFactory::getLogger(const std::string& filename) { return new SimpleLogger(os_, filename, level_); } diff --git a/pulsar-client-cpp/lib/LogUtils.cc b/pulsar-client-cpp/lib/LogUtils.cc index 60e7d5169eb0c..7967fbe75459a 100644 --- a/pulsar-client-cpp/lib/LogUtils.cc +++ b/pulsar-client-cpp/lib/LogUtils.cc @@ -20,7 +20,7 @@ #include #include -#include +#include #include "Log4CxxLogger.h" @@ -50,7 +50,7 @@ void LogUtils::setLoggerFactory(std::unique_ptr loggerFactory) { LoggerFactory* LogUtils::getLoggerFactory() { if (s_loggerFactory.load() == nullptr) { - std::unique_ptr newFactory(new SimpleLoggerFactory()); + std::unique_ptr newFactory(new ConsoleLoggerFactory()); setLoggerFactory(std::move(newFactory)); } return s_loggerFactory.load(); diff --git a/pulsar-client-cpp/tests/CustomLoggerTest.cc b/pulsar-client-cpp/tests/CustomLoggerTest.cc index 8b4cd792faa05..f2a97d1978675 100644 --- a/pulsar-client-cpp/tests/CustomLoggerTest.cc +++ b/pulsar-client-cpp/tests/CustomLoggerTest.cc @@ -17,7 +17,7 @@ * under the License. */ #include -#include +#include #include #include #include @@ -68,29 +68,29 @@ TEST(CustomLoggerTest, testCustomLogger) { ASSERT_EQ(logLines.size(), 2); } -TEST(CustomLoggerTest, testSimpleLoggerFactory) { - std::unique_ptr factory(new SimpleLoggerFactory); +TEST(CustomLoggerTest, testConsoleLoggerFactory) { + std::unique_ptr factory(new ConsoleLoggerFactory); std::unique_ptr logger(factory->getLogger(__FILE__)); ASSERT_FALSE(logger->isEnabled(Logger::LEVEL_DEBUG)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_INFO)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_WARN)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_ERROR)); - factory.reset(new SimpleLoggerFactory(Logger::LEVEL_DEBUG)); + factory.reset(new ConsoleLoggerFactory(Logger::LEVEL_DEBUG)); logger.reset(factory->getLogger(__FILE__)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_DEBUG)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_INFO)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_WARN)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_ERROR)); - factory.reset(new SimpleLoggerFactory(Logger::LEVEL_WARN)); + factory.reset(new ConsoleLoggerFactory(Logger::LEVEL_WARN)); logger.reset(factory->getLogger(__FILE__)); ASSERT_FALSE(logger->isEnabled(Logger::LEVEL_DEBUG)); ASSERT_FALSE(logger->isEnabled(Logger::LEVEL_INFO)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_WARN)); ASSERT_TRUE(logger->isEnabled(Logger::LEVEL_ERROR)); - factory.reset(new SimpleLoggerFactory(Logger::LEVEL_ERROR)); + factory.reset(new ConsoleLoggerFactory(Logger::LEVEL_ERROR)); logger.reset(factory->getLogger(__FILE__)); ASSERT_FALSE(logger->isEnabled(Logger::LEVEL_DEBUG)); ASSERT_FALSE(logger->isEnabled(Logger::LEVEL_INFO)); From a44fe6eba8799172550d19d84971cd53e8045b9a Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Sat, 29 May 2021 00:31:48 +0800 Subject: [PATCH 5/5] Use PImpl idiom for LoggerFactory classes --- .../include/pulsar/ConsoleLoggerFactory.h | 9 ++-- .../include/pulsar/FileLoggerFactory.h | 13 +++--- pulsar-client-cpp/lib/ConsoleLoggerFactory.cc | 11 +++-- .../lib/ConsoleLoggerFactoryImpl.h | 37 ++++++++++++++++ pulsar-client-cpp/lib/FileLoggerFactory.cc | 11 +++-- pulsar-client-cpp/lib/FileLoggerFactoryImpl.h | 44 +++++++++++++++++++ 6 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 pulsar-client-cpp/lib/ConsoleLoggerFactoryImpl.h create mode 100644 pulsar-client-cpp/lib/FileLoggerFactoryImpl.h diff --git a/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h b/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h index 0ee6e47ff099e..7f3f407d9cd00 100644 --- a/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/ConsoleLoggerFactory.h @@ -23,6 +23,8 @@ namespace pulsar { +class ConsoleLoggerFactoryImpl; + /** * The default LoggerFactory of Client if `USE_LOG4CXX` macro was not defined during compilation. * @@ -46,13 +48,14 @@ namespace pulsar { */ class PULSAR_PUBLIC ConsoleLoggerFactory : public LoggerFactory { public: - explicit ConsoleLoggerFactory() = default; - explicit ConsoleLoggerFactory(Logger::Level level) : level_(level) {} + explicit ConsoleLoggerFactory(Logger::Level level = Logger::LEVEL_INFO); + + ~ConsoleLoggerFactory(); Logger* getLogger(const std::string& fileName) override; private: - Logger::Level level_{Logger::LEVEL_INFO}; + std::unique_ptr impl_; }; } // namespace pulsar diff --git a/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h b/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h index 9e8823d7ff413..aa6282e007592 100644 --- a/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h +++ b/pulsar-client-cpp/include/pulsar/FileLoggerFactory.h @@ -19,13 +19,12 @@ #pragma once -#include -#include -#include #include namespace pulsar { +class FileLoggerFactoryImpl; + /** * A logger factory that is appending logs to a single file. * @@ -53,16 +52,14 @@ class PULSAR_PUBLIC FileLoggerFactory : public pulsar::LoggerFactory { * @param level the log level * @param logFilePath the log file's path */ - FileLoggerFactory(Logger::Level level, const std::string& logFilePath) - : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} + FileLoggerFactory(Logger::Level level, const std::string& logFilePath); - ~FileLoggerFactory() { os_.close(); } + ~FileLoggerFactory(); pulsar::Logger* getLogger(const std::string& filename) override; private: - const pulsar::Logger::Level level_; - std::ofstream os_; + std::unique_ptr impl_; }; } // namespace pulsar diff --git a/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc b/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc index 91219fd18a2e1..397c7feed070f 100644 --- a/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc +++ b/pulsar-client-cpp/lib/ConsoleLoggerFactory.cc @@ -18,12 +18,15 @@ */ #include -#include "lib/SimpleLogger.h" +#include "lib/ConsoleLoggerFactoryImpl.h" namespace pulsar { -Logger *ConsoleLoggerFactory::getLogger(const std::string &file) { - return new SimpleLogger(std::cout, file, level_); -} +ConsoleLoggerFactory::ConsoleLoggerFactory(Logger::Level level) + : impl_(new ConsoleLoggerFactoryImpl(level)) {} + +ConsoleLoggerFactory::~ConsoleLoggerFactory() {} + +Logger* ConsoleLoggerFactory::getLogger(const std::string& fileName) { return impl_->getLogger(fileName); } } // namespace pulsar diff --git a/pulsar-client-cpp/lib/ConsoleLoggerFactoryImpl.h b/pulsar-client-cpp/lib/ConsoleLoggerFactoryImpl.h new file mode 100644 index 0000000000000..61c1d90d745e5 --- /dev/null +++ b/pulsar-client-cpp/lib/ConsoleLoggerFactoryImpl.h @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include "lib/SimpleLogger.h" + +namespace pulsar { + +class ConsoleLoggerFactoryImpl { + public: + ConsoleLoggerFactoryImpl(Logger::Level level) : level_(level) {} + + Logger* getLogger(const std::string& fileName) { return new SimpleLogger(std::cout, fileName, level_); } + + private: + Logger::Level level_; +}; + +} // namespace pulsar diff --git a/pulsar-client-cpp/lib/FileLoggerFactory.cc b/pulsar-client-cpp/lib/FileLoggerFactory.cc index 358c9b0aba878..a82613f00a078 100644 --- a/pulsar-client-cpp/lib/FileLoggerFactory.cc +++ b/pulsar-client-cpp/lib/FileLoggerFactory.cc @@ -17,12 +17,15 @@ * under the License. */ #include -#include "lib/SimpleLogger.h" +#include "lib/FileLoggerFactoryImpl.h" namespace pulsar { -pulsar::Logger* FileLoggerFactory::getLogger(const std::string& filename) { - return new SimpleLogger(os_, filename, level_); -} +FileLoggerFactory::FileLoggerFactory(Logger::Level level, const std::string& logFilePath) + : impl_(new FileLoggerFactoryImpl(level, logFilePath)) {} + +FileLoggerFactory::~FileLoggerFactory() {} + +Logger* FileLoggerFactory::getLogger(const std::string& filename) { return impl_->getLogger(filename); } } // namespace pulsar diff --git a/pulsar-client-cpp/lib/FileLoggerFactoryImpl.h b/pulsar-client-cpp/lib/FileLoggerFactoryImpl.h new file mode 100644 index 0000000000000..75329c65fc88e --- /dev/null +++ b/pulsar-client-cpp/lib/FileLoggerFactoryImpl.h @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#pragma once + +#include +#include +#include +#include + +#include "lib/SimpleLogger.h" + +namespace pulsar { + +class FileLoggerFactoryImpl { + public: + FileLoggerFactoryImpl(Logger::Level level, const std::string& logFilePath) + : level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} + + ~FileLoggerFactoryImpl() { os_.close(); } + + Logger* getLogger(const std::string& filename) { return new SimpleLogger(os_, filename, level_); } + + private: + const Logger::Level level_; + std::ofstream os_; +}; + +} // namespace pulsar