Skip to content

Commit

Permalink
[C++] Add C++ single file logger factory (apache#10712)
Browse files Browse the repository at this point in the history
* Add SampleFileLogger C++ example

* Expose SingleFileLoggerFactory as the public interface

* Make logger factory public

* Change logger factory's name

* Use PImpl idiom for LoggerFactory classes
  • Loading branch information
BewareMyPower authored and ciaocloud committed Oct 16, 2021
1 parent 94c7301 commit b7aa794
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pulsar-client-cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ set(SAMPLE_PRODUCER_SOURCES
SampleProducer.cc
)

set(SAMPLE_FILE_LOGGER_SOURCES
SampleFileLogger.cc
)

set(SAMPLE_PRODUCER_C_SOURCES
SampleProducerCApi.c
)
Expand All @@ -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})
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/examples/SampleAsyncProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/examples/SampleConsumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/examples/SampleConsumerListener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions pulsar-client-cpp/examples/SampleFileLogger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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 <pulsar/Client.h>

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 FileLoggerFactory(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;
}
2 changes: 1 addition & 1 deletion pulsar-client-cpp/examples/SampleProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions pulsar-client-cpp/include/pulsar/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <pulsar/MessageBuilder.h>
#include <pulsar/ClientConfiguration.h>
#include <pulsar/Schema.h>
#include <pulsar/ConsoleLoggerFactory.h>
#include <pulsar/FileLoggerFactory.h>
#include <string>

namespace pulsar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace pulsar {

class ConsoleLoggerFactoryImpl;

/**
* The default LoggerFactory of Client if `USE_LOG4CXX` macro was not defined during compilation.
*
Expand All @@ -37,22 +39,23 @@ namespace pulsar {
* level simply.
*
* ```c++
* #include <pulsar/SimpleLoggerFactory.h>
* #include <pulsar/ConsoleLoggerFactory.h>
*
* ClientConfiguration conf;
* conf.setLogger(new SimpleLoggerFactory(Logger::LEVEL_DEBUG));
* conf.setLogger(new ConsoleLoggerFactory(Logger::LEVEL_DEBUG));
* Client client("pulsar://localhost:6650", conf);
* ```
*/
class SimpleLoggerFactory : public LoggerFactory {
class PULSAR_PUBLIC ConsoleLoggerFactory : public LoggerFactory {
public:
explicit SimpleLoggerFactory() = default;
explicit SimpleLoggerFactory(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<ConsoleLoggerFactoryImpl> impl_;
};

} // namespace pulsar
65 changes: 65 additions & 0 deletions pulsar-client-cpp/include/pulsar/FileLoggerFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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 <pulsar/Logger.h>

namespace pulsar {

class FileLoggerFactoryImpl;

/**
* A logger factory that is appending logs to a single file.
*
* The log format is "yyyy-mm-dd hh:MM:ss.xxx <level> <thread-id> <file>:<line> | <msg>", like
*
* ```
* 2021-03-24 17:35:46.571 INFO [0x10a951e00] ConnectionPool:85 | Created connection for ...
* ```
*
* Example:
*
* ```c++
* #include <pulsar/FileLoggerFactory.h>
*
* ClientConfiguration conf;
* conf.setLogger(new FileLoggerFactory(Logger::LEVEL_DEBUG, "pulsar-client-cpp.log"));
* Client client("pulsar://localhost:6650", conf);
* ```
*/
class PULSAR_PUBLIC FileLoggerFactory : public pulsar::LoggerFactory {
public:
/**
* Create a FileLoggerFactory instance.
*
* @param level the log level
* @param logFilePath the log file's path
*/
FileLoggerFactory(Logger::Level level, const std::string& logFilePath);

~FileLoggerFactory();

pulsar::Logger* getLogger(const std::string& filename) override;

private:
std::unique_ptr<FileLoggerFactoryImpl> impl_;
};

} // namespace pulsar
6 changes: 3 additions & 3 deletions pulsar-client-cpp/lib/ClientImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "PartitionedConsumerImpl.h"
#include "MultiTopicsConsumerImpl.h"
#include "PatternMultiTopicsConsumerImpl.h"
#include <pulsar/SimpleLoggerFactory.h>
#include <pulsar/ConsoleLoggerFactory.h>
#include <boost/algorithm/string/predicate.hpp>
#include <sstream>
#include <lib/HTTPLookupService.h>
Expand Down Expand Up @@ -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));
Expand Down
32 changes: 32 additions & 0 deletions pulsar-client-cpp/lib/ConsoleLoggerFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 <pulsar/ConsoleLoggerFactory.h>
#include "lib/ConsoleLoggerFactoryImpl.h"

namespace pulsar {

ConsoleLoggerFactory::ConsoleLoggerFactory(Logger::Level level)
: impl_(new ConsoleLoggerFactoryImpl(level)) {}

ConsoleLoggerFactory::~ConsoleLoggerFactory() {}

Logger* ConsoleLoggerFactory::getLogger(const std::string& fileName) { return impl_->getLogger(fileName); }

} // namespace pulsar
37 changes: 37 additions & 0 deletions pulsar-client-cpp/lib/ConsoleLoggerFactoryImpl.h
Original file line number Diff line number Diff line change
@@ -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 <pulsar/Logger.h>
#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
31 changes: 31 additions & 0 deletions pulsar-client-cpp/lib/FileLoggerFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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 <pulsar/FileLoggerFactory.h>
#include "lib/FileLoggerFactoryImpl.h"

namespace pulsar {

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
44 changes: 44 additions & 0 deletions pulsar-client-cpp/lib/FileLoggerFactoryImpl.h
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <ios>
#include <string>
#include <pulsar/Logger.h>

#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
4 changes: 2 additions & 2 deletions pulsar-client-cpp/lib/LogUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <atomic>
#include <iostream>
#include <pulsar/SimpleLoggerFactory.h>
#include <pulsar/ConsoleLoggerFactory.h>

#include "Log4CxxLogger.h"

Expand Down Expand Up @@ -50,7 +50,7 @@ void LogUtils::setLoggerFactory(std::unique_ptr<LoggerFactory> loggerFactory) {

LoggerFactory* LogUtils::getLoggerFactory() {
if (s_loggerFactory.load() == nullptr) {
std::unique_ptr<LoggerFactory> newFactory(new SimpleLoggerFactory());
std::unique_ptr<LoggerFactory> newFactory(new ConsoleLoggerFactory());
setLoggerFactory(std::move(newFactory));
}
return s_loggerFactory.load();
Expand Down
Loading

0 comments on commit b7aa794

Please sign in to comment.