Addressing the std2 concern raised in https://github.com/KjellKod/g3log/issues/212 (#246)

This commit is contained in:
Kjell Hedström 2018-02-20 22:02:19 -07:00 committed by GitHub
parent 217f52fb12
commit f2b860a2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 38 additions and 90 deletions

View File

@ -127,12 +127,12 @@ Example usage where a [logrotate sink (g3sinks)](https://github.com/KjellKod/g3s
#include <g3log/g3log.hpp>
#include <g3log/logworker.h>
#include <g3sinks/logrotate.hpp>
#include <g3log/std2_make_unique.hpp
#include <memory>
int main(int argc, char**argv) {
using namespace g3;
std::unique_ptr<LogWorker> logworker{ LogWorker::createLogWorker() };
auto sinkHandle = logworker->addSink(std2::make_unique<LogRotate>(),
auto sinkHandle = logworker->addSink(std::make_unique<LogRotate>(),
&LogRotate::save);
// initialize the logger before it can receive LOG calls

View File

@ -110,7 +110,7 @@ struct CustomSink {
// in main.cpp, main() function
auto sinkHandle = logworker->addSink(std2::make_unique<CustomSink>(),
auto sinkHandle = logworker->addSink(std::make_unique<CustomSink>(),
&CustomSink::ReceiveLogMessage);
```
@ -124,14 +124,14 @@ Example usage where a custom sink is added. A function is called though the sink
// main.cpp
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include <g3log/std2_make_unique.hpp>
#include <memory>
#include "CustomSink.h"
int main(int argc, char**argv) {
using namespace g3;
std::unique_ptr<LogWorker> logworker{ LogWorker::createLogWorker() };
auto sinkHandle = logworker->addSink(std2::make_unique<CustomSink>(),
auto sinkHandle = logworker->addSink(std::make_unique<CustomSink>(),
&CustomSink::ReceiveLogMessage);
// initialize the logger before it can receive LOG calls
@ -170,7 +170,7 @@ Example usage where a the default file logger is used **and** a custom sink is a
// main.cpp
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include <g3log/std2_make_unique.hpp>
#include <memory>
#include "CustomSink.h"
@ -185,7 +185,7 @@ int main(int argc, char**argv) {
LOG(DEBUG) << "Make log call, then add another sink";
worker->addSink(std2::make_unique<CustomSink>(),
worker->addSink(std::make_unique<CustomSink>(),
&CustomSink::ReceiveLogMessage);
...

View File

@ -38,7 +38,6 @@ find_path(G3LOG_INCLUDE_DIR
g3log/sink.hpp
g3log/sinkwrapper.hpp
g3log/stacktrace_windows.hpp
g3log/std2_make_unique.hpp
g3log/stlpatch_future.hpp
g3log/time.hpp
)

View File

@ -66,7 +66,7 @@ int main(int argc, char **argv)
std::unique_ptr<LogWorker> logworker {LogWorker::createLogWorker()};
auto sinkHandle = logworker->addSink(std2::make_unique<FileSink>(argv[0], path_to_log_file),
auto sinkHandle = logworker->addSink(std::make_unique<FileSink>(argv[0], path_to_log_file),
&FileSink::fileWrite);
initializeLogging(logworker.get());

View File

@ -19,7 +19,6 @@
* ********************************************* */
#include "g3log/g3log.hpp"
#include "g3log/std2_make_unique.hpp"
#include "g3log/logworker.hpp"
#include "g3log/crashhandler.hpp"
#include "g3log/logmessage.hpp"
@ -159,7 +158,7 @@ namespace g3 {
void saveMessage(const char* entry, const char* file, int line, const char* function, const LEVELS& level,
const char* boolean_expression, int fatal_signal, const char* stack_trace) {
LEVELS msgLevel {level};
LogMessagePtr message {std2::make_unique<LogMessage>(file, line, function, msgLevel)};
LogMessagePtr message {std::make_unique<LogMessage>(file, line, function, msgLevel)};
message.get()->write().append(entry);
message.get()->setExpression(boolean_expression);
@ -183,7 +182,7 @@ namespace g3 {
"A recursive crash detected. It is likely the hook set with 'setFatalPreLoggingHook(...)' is responsible\n\n")
.append("---First crash stacktrace: ").append(first_stack_trace).append("\n---End of first stacktrace\n");
}
FatalMessagePtr fatal_message { std2::make_unique<FatalMessage>(*(message._move_only.get()), fatal_signal) };
FatalMessagePtr fatal_message { std::make_unique<FatalMessage>(*(message._move_only.get()), fatal_signal) };
// At destruction, flushes fatal message to g3LogWorker
// either we will stay here until the background worker has received the fatal
// message, flushed the crash message to the sinks and exits with the same fatal signal

View File

@ -16,7 +16,7 @@
#include "g3log/sinkhandle.hpp"
#include "g3log/filesink.hpp"
#include "g3log/logmessage.hpp"
#include "g3log/std2_make_unique.hpp"
#include <memory>
#include <memory>
#include <string>
@ -98,7 +98,7 @@ namespace g3 {
using namespace g3::internal;
auto sink = std::make_shared<Sink<T>> (std::move(real_sink), call);
addWrappedSink(sink);
return std2::make_unique<SinkHandle<T>> (sink);
return std::make_unique<SinkHandle<T>> (sink);
}

View File

@ -1,47 +0,0 @@
/** ==========================================================================
* 2013 This is PUBLIC DOMAIN to use at your own risk and comes
* with no warranties. This code is yours to share, use and modify with no
* strings attached and no restrictions or obligations.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
*
* make_unique will be in C++14, this implementation is copied as I understood
* Stephan T. Lavavej's description of it.
*
* PUBLIC DOMAIN and NOT under copywrite protection.
*
*
* Example: usage
* auto an_int = make_unique<int>(123);
* auto a_string = make_unique<string>(5, 'x');
* auto an_int_array = make_unique<int[]>(11, 22, 33);
* ********************************************* */
#pragma once
#include <memory>
#include <type_traits>
namespace std2 {
namespace impl_fut_stl {
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique_helper(std::false_type, Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique_helper(std::true_type, Args &&... args) {
static_assert(std::extent<T>::value == 0, "make_unique<T[N]>() is forbidden, please use make_unique<T[]>(),");
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[sizeof...(Args)] {std::forward<Args>(args)...});
}
}
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique(Args &&... args) {
return impl_fut_stl::make_unique_helper<T>(
std::is_array<T>(), std::forward<Args>(args)...);
}
}

View File

@ -121,7 +121,7 @@ namespace g3 {
}
std::unique_ptr<FileSinkHandle>LogWorker::addDefaultLogger(const std::string& log_prefix, const std::string& log_directory, const std::string& default_id) {
return addSink(std2::make_unique<g3::FileSink>(log_prefix, log_directory, default_id), &FileSink::fileWrite);
return addSink(std::make_unique<g3::FileSink>(log_prefix, log_directory, default_id), &FileSink::fileWrite);
}

View File

@ -16,7 +16,6 @@
#include <atomic>
#include "testing_helpers.h"
#include "g3log/std2_make_unique.hpp"
#include "g3log/sink.hpp"
#include "g3log/sinkwrapper.hpp"
#include "g3log/sinkhandle.hpp"
@ -24,14 +23,13 @@
#include "g3log/generated_definitions.hpp"
using namespace std;
using namespace std2;
using namespace testing_helpers;
class CoutSink {
stringstream buffer;
unique_ptr<ScopedOut> scope_ptr;
CoutSink() : scope_ptr(std2::make_unique<ScopedOut>(std::cout, &buffer)) {}
CoutSink() : scope_ptr(std::make_unique<ScopedOut>(std::cout, &buffer)) {}
public:
void clear() { buffer.str(""); }
std::string string() { return buffer.str(); }
@ -92,7 +90,7 @@ namespace g3 {
auto wait_result = g3::spawn_task(add_sink_call, _bg.get());
wait_result.wait();
auto handle = std2::make_unique< SinkHandle<T> >(sink);
auto handle = std::make_unique< SinkHandle<T> >(sink);
return handle;
}
};
@ -126,7 +124,7 @@ TEST(ConceptSink, OneSink__VerifyMsgIn) {
TEST(ConceptSink, DualSink__VerifyMsgIn) {
Worker worker;
auto h1 = worker.addSink(CoutSink::createSink(), &CoutSink::save);
auto h2 = worker.addSink(std2::make_unique<StringSink>(), &StringSink::append);
auto h2 = worker.addSink(std::make_unique<StringSink>(), &StringSink::append);
worker.save("Hello World!");
@ -140,7 +138,7 @@ TEST(ConceptSink, DualSink__VerifyMsgIn) {
}
TEST(ConceptSink, DeletedSink__Exptect_badweak_ptr___exception) {
auto worker = std2::make_unique<Worker>();
auto worker = std::make_unique<Worker>();
auto h1 = worker->addSink(CoutSink::createSink(), &CoutSink::save);
worker->save("Hello World!");
worker.reset();
@ -172,7 +170,7 @@ TEST(ConceptSink, OneHundredSinks_part1) {
for (auto& flag : flags) {
auto& count = counts[index++];
// ignore the handle
worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
worker->addSink(std::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
}
worker->save("Hello to 100 receivers :)");
worker->save("Hello to 100 receivers :)");
@ -209,12 +207,12 @@ TEST(ConceptSink, OneHundredSinks_part2) {
for (auto& flag : flags) {
auto& count = counts[index++];
// ignore the handle
worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
worker->addSink(std::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
}
// 100 logs
for (int index = 0; index < NumberOfItems; ++index) {
LogMessagePtr message{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message{std::make_unique<LogMessage>("test", 0, "test", DEBUG)};
message.get()->write().append("Hello to 100 receivers :)");
worker->save(message);
}
@ -240,12 +238,12 @@ TEST(ConceptSink, OneSinkWithHandleOutOfScope) {
{
auto worker = g3::LogWorker::createLogWorker();
{
auto handle = worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
auto handle = worker->addSink(std::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
}
EXPECT_FALSE(flag->load());
EXPECT_TRUE(0 == count->load());
LogMessagePtr message{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message{std::make_unique<LogMessage>("test", 0, "test", DEBUG)};
message.get()->write().append("this message should trigger an atomic increment at the sink");
worker->save(message);
}

View File

@ -128,7 +128,7 @@ TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
}
TEST(TestOf_SinkHandleDifferentId, Expecting_DifferentId) {
auto sink = std2::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
auto sink = std::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
auto name = sink->fileName();
ASSERT_STREQ( name.substr(0, 26).c_str(), "./AnotherLogFile.logger_id");
g_cleaner_ptr->addLogToClean(name);

View File

@ -10,7 +10,7 @@
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include <g3log/filesink.hpp>
#include <g3log/std2_make_unique.hpp>
#include <memory>
#include <gtest/gtest.h>
#include <string>
@ -34,10 +34,10 @@ TEST(DynamicLoadOfLibrary, JustLoadAndExit) {
{ // scope to flush logs at logworker exit
auto worker = g3::LogWorker::createLogWorker();
auto handle = worker->addSink(std2::make_unique<LogMessageCounter>(std::ref(receiver)), &LogMessageCounter::countMessages);
auto handle = worker->addSink(std::make_unique<LogMessageCounter>(std::ref(receiver)), &LogMessageCounter::countMessages);
// add another sink just for more throughput of data
auto fileHandle = worker->addSink(std2::make_unique<g3::FileSink>("runtimeLoadOfDynamiclibs", "/tmp"), &g3::FileSink::fileWrite);
auto fileHandle = worker->addSink(std::make_unique<g3::FileSink>("runtimeLoadOfDynamiclibs", "/tmp"), &g3::FileSink::fileWrite);
g3::initializeLogging(worker.get());
void* libHandle = dlopen("libtester_sharedlib.so", RTLD_LAZY | RTLD_GLOBAL);

View File

@ -19,7 +19,7 @@
#include "testing_helpers.h"
#include "g3log/logmessage.hpp"
#include "g3log/logworker.hpp"
#include "g3log/std2_make_unique.hpp"
using namespace testing_helpers;
using namespace std;
@ -29,10 +29,10 @@ using namespace g3;
AtomicIntPtr count = make_shared < atomic<int >> (0);
{
auto worker = g3::LogWorker::createLogWorker();
auto handle = worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
auto handle = worker->addSink(std::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
EXPECT_FALSE(flag->load());
EXPECT_TRUE(0 == count->load());
LogMessagePtr message{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message{std::make_unique<LogMessage>("test", 0, "test", DEBUG)};
message.get()->write().append("this message should trigger an atomic increment at the sink");
worker->save(message);
}
@ -66,11 +66,11 @@ TEST(ConceptSink, OneHundredSinks) {
for (auto& flag : flags) {
auto& count = counts[index++];
// ignore the handle
worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
worker->addSink(std::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
}
LOG(G3LOG_DEBUG) << "start message";
LogMessagePtr message1{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message2{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message1{std::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message2{std::make_unique<LogMessage>("test", 0, "test", DEBUG)};
auto& write1 = message1.get()->write();
write1.append("Hello to 100 receivers :)");
worker->save(message1);
@ -108,7 +108,7 @@ TEST(ConceptSink, VoidCall__NoCall_ExpectingNoAdd) {
std::atomic<int> counter{0};
{
std::unique_ptr<g3::LogWorker> worker{g3::LogWorker::createLogWorker()};
auto handle = worker->addSink(std2::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
auto handle = worker->addSink(std::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
}
EXPECT_EQ(counter, 0);
}
@ -117,7 +117,7 @@ TEST(ConceptSink, VoidCall__OneCall_ExpectingOneAdd) {
std::atomic<int> counter{0};
{
std::unique_ptr<g3::LogWorker> worker{g3::LogWorker::createLogWorker()};
auto handle = worker->addSink(std2::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
auto handle = worker->addSink(std::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
std::future<void> ignored = handle->call(&VoidReceiver::incrementAtomic);
}
EXPECT_EQ(counter, 1);
@ -127,7 +127,7 @@ TEST(ConceptSink, VoidCall__TwoCalls_ExpectingTwoAdd) {
std::atomic<int> counter{0};
{
std::unique_ptr<g3::LogWorker> worker{g3::LogWorker::createLogWorker()};
auto handle = worker->addSink(std2::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
auto handle = worker->addSink(std::make_unique<VoidReceiver>(&counter), &VoidReceiver::receiveMsg);
auto voidFuture1 = handle->call(&VoidReceiver::incrementAtomic);
auto voidFuture2 = handle->call(&VoidReceiver::incrementAtomic);
voidFuture1.wait();
@ -154,7 +154,7 @@ TEST(ConceptSink, IntCall__TwoCalls_ExpectingTwoAdd) {
std::atomic<int> counter{0};
{
std::unique_ptr<g3::LogWorker> worker{g3::LogWorker::createLogWorker()};
auto handle = worker->addSink(std2::make_unique<IntReceiver>(&counter), &IntReceiver::receiveMsgDoNothing);
auto handle = worker->addSink(std::make_unique<IntReceiver>(&counter), &IntReceiver::receiveMsgDoNothing);
std::future<int> intFuture1 = handle->call(&IntReceiver::incrementAtomic);
EXPECT_EQ(intFuture1.get(), 1);
EXPECT_EQ(counter, 1);
@ -211,7 +211,7 @@ TEST(ConceptSink, DISABLED_AggressiveThreadCallsDuringShutdown) {
std::cout << create << " ";
std::unique_ptr<g3::LogWorker> worker{g3::LogWorker::createLogWorker()};
auto handle = worker->addSink(std2::make_unique<IntReceiver>(&atomicCounter), &IntReceiver::receiveMsgIncrementAtomic);
auto handle = worker->addSink(std::make_unique<IntReceiver>(&atomicCounter), &IntReceiver::receiveMsgIncrementAtomic);
g3::initializeLogging(worker.get());
// wait till some LOGS streaming in

View File

@ -9,7 +9,6 @@
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include <g3log/std2_make_unique.hpp>
#include <g3log/logmessage.hpp>
#include "testing_helpers.h"
@ -111,7 +110,7 @@ namespace testing_helpers {
}
RestoreFileLogger::RestoreFileLogger(std::string directory)
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std2::make_unique<g3::FileSink>("UNIT_TEST_LOGGER", directory), &g3::FileSink::fileWrite)) {
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std::make_unique<g3::FileSink>("UNIT_TEST_LOGGER", directory), &g3::FileSink::fileWrite)) {
using namespace g3;
g3::initializeLogging(_scope->_currentWorker.get());
clearMockFatal();