Skip to content

Commit

Permalink
Implementing Boost Logger support
Browse files Browse the repository at this point in the history
  • Loading branch information
BobBrownConsulting committed Apr 4, 2019
1 parent d6d0eca commit 23d96e8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
1 change: 0 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ls
cd ext/boost/; ./bootstrap.sh;
cd ext/boost/; ./b2 --with-system link=static threading=multi toolset=darwin cxxflags="-arch x86_64" target-os=darwin address-model=32_64
cd ext/boost/; ./b2 --with-thread link=static threading=multi toolset=darwin cxxflags="-arch x86_64" target-os=darwin address-model=32_64
Expand Down
100 changes: 96 additions & 4 deletions src/utils/Trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
#include "BBCMacros.h"
#include "Singleton.h"

#ifdef BBC_USE_BOOST
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/async_frontend.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
#endif

#ifdef BBC_DEBUG
#define BBC_TRACE(mask, ...) BBC_MACRO_BLOCK(Trace::instance().writeTrace(mask, __VA_ARGS__);)
#else
Expand Down Expand Up @@ -135,14 +152,32 @@ if (0 == strcmp(iStr.c_str(), STRINGIFY(iVal))) \
return kCategory_None;
}

private:
static void BoostCallback(const char* iMessage)
{
BOOST_LOG_TRIVIAL(error) << iMessage << std::endl;
}

public:

bool initializeWithBuffer(const std::string& iTraceConfig, TraceCallback iCallback = nullptr, bool iUseBoost = true)
{
if (initalized_)
return true;

processConfig(iTraceConfig);

callback_ = iCallback;
if (iUseBoost)
{
callback_ = BoostCallback;
boostCallback_ = iCallback;
initBoost();
}
else
{
callback_ = iCallback;
}

initalized_ = true;

return true;
Expand All @@ -167,14 +202,29 @@ if (0 == strcmp(iStr.c_str(), STRINGIFY(iVal))) \

fileStream.close();

callback_ = iCallback;
if (iUseBoost)
{
callback_ = BoostCallback;
boostCallback_ = iCallback;
initBoost();
}
else
{
callback_ = iCallback;
}

initalized_ = true;

return true;
}

void reset()
{
#ifdef BBC_USE_BOOST
boost::log::core::get()->remove_all_sinks();
boostCallback_ = nullptr;
#endif

initalized_ = false;

traceAll_ = false;
Expand Down Expand Up @@ -205,13 +255,12 @@ if (0 == strcmp(iStr.c_str(), STRINGIFY(iVal))) \

va_end(argList);


if (callback_)
{
callback_(traceMessage);
}
else
{
{
std::cout << traceMessage << std::endl;
}
}
Expand Down Expand Up @@ -263,6 +312,48 @@ if (0 == strcmp(iStr.c_str(), STRINGIFY(iVal))) \
}
}

void initBoost(const std::string& iLogFilePath = "")
{
#ifdef BBC_USE_BOOST
if (iLogFilePath.length())
{
logging::add_file_log
(
//keywords::file_name = "sample_%N.log", /*< file name pattern >*/
keywords::file_name = iLogFilePath, /*< file name pattern >*/
keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
keywords::auto_flush = true,
keywords::format = "%Message%" /*< log record format >*/
);
}
else
{
struct Sink: public sinks::basic_formatted_sink_backend<char, sinks::concurrent_feeding>
{
void consume (const boost::log::record_view& rec, const std::string& str)
{
if (Trace::instance().boostCallback_)
{
Trace::instance().boostCallback_(str.c_str());
}
}
};

typedef sinks::asynchronous_sink<Sink> sink_t;
boost::shared_ptr<sink_t> sink (new sink_t());
boost::log::core::get()->add_sink (sink);
}

logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);

logging::add_common_attributes();
#endif
}

static const int32_t sTraceMessageSize{2048};

bool initalized_{false};
Expand All @@ -271,4 +362,5 @@ if (0 == strcmp(iStr.c_str(), STRINGIFY(iVal))) \
std::vector<TraceMask> masks_;

TraceCallback callback_{nullptr};
TraceCallback boostCallback_{nullptr};
};
20 changes: 14 additions & 6 deletions test/UnitTests/proj/MacOS/BBCTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,10 @@
19F59A6822540709002ACE29 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CODE_SIGN_STYLE = Automatic;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -468,10 +471,11 @@
);
LIBRARY_SEARCH_PATHS = ../../../../ext/boost/stage/lib;
OTHER_LDFLAGS = (
"-lboost_thread",
"-lboost_filesystem",
"-lboost_system",
"-lboost_log",
"-lboost_log_setup",
"-lboost_system",
"-lboost_filesystem",
"-lboost_thread",
);
PRODUCT_NAME = "$(TARGET_NAME)";
};
Expand All @@ -480,7 +484,10 @@
19F59A6922540709002ACE29 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CODE_SIGN_STYLE = Automatic;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_PREPROCESSOR_DEFINITIONS = BBC_USE_BOOST;
HEADER_SEARCH_PATHS = (
../../../../ext/googletest/googletest,
Expand All @@ -490,10 +497,11 @@
);
LIBRARY_SEARCH_PATHS = ../../../../ext/boost/stage/lib;
OTHER_LDFLAGS = (
"-lboost_thread",
"-lboost_filesystem",
"-lboost_system",
"-lboost_log",
"-lboost_log_setup",
"-lboost_system",
"-lboost_filesystem",
"-lboost_thread",
);
PRODUCT_NAME = "$(TARGET_NAME)";
};
Expand Down
3 changes: 2 additions & 1 deletion test/UnitTests/src/Trace_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ TEST(TraceTest, TraceTest_Test4)

TEST(TraceTest, TraceTest_Test1)
{
Trace::instance().initializeWithBuffer(" kCategory_Basic@kPriority_Low \n #kCategory_Basic@kPriority_High\nkCategory_Basic@kPriority_Medium", TestTraceCallback);
Trace::instance().initializeWithBuffer(" kCategory_Basic@kPriority_Low \n #kCategory_Basic@kPriority_High\nkCategory_Basic@kPriority_Medium"
, TestTraceCallback);

std::string worldStr = "world";

Expand Down

0 comments on commit 23d96e8

Please sign in to comment.