Skip to content

Commit

Permalink
Addressing the std2 concern raised in #212 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjell Hedström authored Feb 21, 2018
1 parent 217f52f commit f2b860a
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 90 deletions.
4 changes: 2 additions & 2 deletions API.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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);

...
Expand Down
1 change: 0 additions & 1 deletion cmake/g3loggerConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion example/main_sigsegv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 2 additions & 3 deletions src/g3log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/g3log/logworker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}


Expand Down
47 changes: 0 additions & 47 deletions src/g3log/std2_make_unique.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/logworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
20 changes: 9 additions & 11 deletions test_unit/test_concept_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
#include <atomic>

#include "testing_helpers.h"
#include "g3log/std2_make_unique.hpp"
#include "g3log/sink.hpp"
#include "g3log/sinkwrapper.hpp"
#include "g3log/sinkhandle.hpp"
#include "g3log/logmessage.hpp"
#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(); }
Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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!");


Expand All @@ -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();
Expand Down Expand Up @@ -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 :)");
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion test_unit/test_filechange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test_unit/test_linux_dynamic_loaded_sharedlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions test_unit/test_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test_unit/testing_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f2b860a

Please sign in to comment.