0

I'm referring to below link and exactly used same code provided in answer.
how to use boost log from multiple files with Gloa
logger.h

#pragma once
#include <boost/log/trivial.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup.hpp>
#include <boost/log/core.hpp>

#define INFO BOOST_LOG_SEV(my_log::get(), boost::log::trivial::info)
#define WARN BOOST_LOG_SEV(my_log::get(), boost::log::trivial::warning)
#define ERROR BOOST_LOG_SEV(my_log::get(), boost::log::trivial::error)
#define OMS_LOG_FILE    "/home/ashish/logs/cm_oms/cm_%N.log"

typedef boost::log::sources::**severity_logger_mt**<boost::log::trivial::severity_level> logger_t;
BOOST_LOG_GLOBAL_LOGGER(my_log, logger_t)

logger.cpp

#include "logger.h"
namespace logging   = boost::log;
namespace keywords  = boost::log::keywords;
namespace sinks     = boost::log::sinks;
namespace expressions      = boost::log::expressions;

BOOST_LOG_GLOBAL_LOGGER_INIT(my_log, logger_t) {
    logger_t lg;
    logging::add_file_log(
        keywords::file_name = OMS_LOG_FILE,
        keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %Message%",
        keywords::auto_flush = true
    );
    logging::add_common_attributes();
    logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
    return lg;
}

main.cpp

#include "logger.h"
...
INFO << "Using Global logger";

Difference is I'm using visual studio code and Ubuntu. Below is my tasks.json file where I'm adding all required linker library:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++-11 build active file",
            "command": "/usr/bin/g++-11",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/main.cpp",
                "${workspaceFolder}/DriverCode/driver.cpp",
                "${workspaceFolder}/utility/logger.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-DBOOST_LOG_DYN_LINK",                
                "-lssl", "-lcrypto", "-lboost_log_setup", "-lboost_log", "-lboost_system", "-lboost_filesystem", "-lboost_date_time",
                "-lboost_thread"
            ],

Runtime crash

File:- /usr/include/boost/log/utility/formatting_ostream.hpp
Line:- explicit sentry(basic_formatting_ostream& strm) : base_type(strm.stream())
Error: Segmentation fault

Call stack

libstdc++.so.6!std::ostream::sentry::sentry(std::ostream&) (Unknown Source:0)
boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::sentry::sentry(boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::sentry * const this, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> > & strm) (/usr/include/boost/log/utility/formatting_ostream.hpp:160)
boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::formatted_write(boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> > * const this, const boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::char_type * p, std::streamsize size) (/usr/include/boost/log/utility/formatting_ostream.hpp:731)
boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::operator<<(boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> > * const this, const char * p) (/usr/include/boost/log/utility/formatting_ostream.hpp:440)
boost::log::v2_mt_posix::basic_record_ostream<char>::operator<<(boost::log::v2_mt_posix::basic_record_ostream<char> * const this, const char * p) (/usr/include/boost/log/sources/record_ostream.hpp:218)
main(int argc, char ** argv) (/home/ashish/OMS/main.cpp:42)

I have referred all stack overflow posts and other R&D, but not getting the answer to solve this runtime crash. I'm using Ubuntu and C++20.
Installation path for logs and other boost libs:

ashish@ashish-Inspiron-14-5420:~$ locate libboost_log
/usr/lib/x86_64-linux-gnu/libboost_log.a
/usr/lib/x86_64-linux-gnu/libboost_log.so
/usr/lib/x86_64-linux-gnu/libboost_log.so.1.74.0
/usr/lib/x86_64-linux-gnu/libboost_log_setup.a
/usr/lib/x86_64-linux-gnu/libboost_log_setup.so
/usr/lib/x86_64-linux-gnu/libboost_log_setup.so.1.74.0
  • Is there a ` my_logger.cpp`? – drescherjm Sep 21 '22 at 13:16
  • 1
    "Do we need to define #define BOOST_LOG_DYN_LINK 1 in top of main.cpp as well?" - I don't know. But *if* it needs to be defined everywhere, then just get the compiler to do it by setting the appropriate command line option (with `g++` that would be the `-D` option). – Jesper Juhl Sep 21 '22 at 13:17

0 Answers0