boostorg / log

Boost Logging library
https://www.boost.org/libs/log/
182 stars 105 forks source link

Log Formatting [Conan 2.x, C++20, Boost 1.83] #215

Closed matheusdiogenesandrade closed 1 year ago

matheusdiogenesandrade commented 1 year ago

Environment

Current structure

The MWE can be found here.

[settings]
os=Linux
arch=x86_64
compiler=gcc
compiler.version=11
compiler.cppstd=20
compiler.libcxx=libstdc++11
build_type=Release

[options]

[conf]
[requires]
boost/1.83.0

[generators]
CMakeDeps
CMakeToolchain
cmake_minimum_required(VERSION 3.23)
project(BoostConan)

##### SETs
set (CMAKE_BUILD_TYPE Release) 
set (CMAKE_CXX_STANDARD 20)
set (BUILD_SHARED_LIBS On)

##### Additional packages 

find_package (Boost COMPONENTS log REQUIRED)

##### Include Dirs 

include_directories(
    SYSTEM
    includes
    "${PROJECT_BINARY_DIR}"
    )

##### Compile options 

add_compile_options(-Wall
    -DIL_STD
    -Wfatal-errors
    -pedantic
    -fopenmp
    -DDEBUG
    -fdiagnostics-color=always
    -lz
    -std=c++20
    -lm 
    -lpthread 
    -ldl
    -Wno-sign-compare
    )

file(GLOB_RECURSE SOURCES "src/*.cpp")

add_executable(main ${SOURCES})

target_link_libraries(
    main 
    Boost::log
    pthread 
    m 
    dl)
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;

void init_logging() {
    logging::register_simple_formatter_factory<logging::trivial::severity_level, char>("Severity");

    logging::add_file_log(
            keywords::file_name = "sample.log",
            keywords::auto_flush = true,
            keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] [%ProcessID%] [%LineID%] %Message%"
            );

   logging::add_common_attributes();
}

int main (int argc, const char *argv[])
{
    init_logging();

    BOOST_LOG_TRIVIAL(debug) << "Test";
    return 0;
}

Process

mkdir build
conan install . --profile conanprofile.txt --output-folder=build --build=missing
cd build
cmake .. --preset conan-release
make

Expected result

[ 50%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[100%] Linking CXX executable main
[100%] Built target main

Actual result

/usr/bin/ld: CMakeFiles/main.dir/src/main.cpp.o: en la función `init_logging()': 
main.cpp:(.text+0x186): reference to 
`void boost::log::v2s_mt_posix::register_formatter_factory<char>(
boost::log::v2s_mt_posix::attribute_name const&, boost::shared_ptr<boost::log::v2s_mt_posix::formatter_factory<char> > const&
)' not defined

/usr/bin/ld: CMakeFiles/main.dir/src/main.cpp.o: in the function
`boost::shared_ptr<boost::log::v2s_mt_posix::sinks::synchronous_sink<boost::log::v2s_mt_posix::sinks::text_file_backend> >
boost::log::v2s_mt_posix::aux::add_file_log<boost::parameter::aux::flat_like_arg_list<boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::file_name,
boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::file_name, char const [11]>,
std::integral_constant<bool, true> >, 
boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::auto_flush, 
boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::auto_flush, bool const>, 
std::integral_constant<bool, true> >, 
boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::format, 
boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::format, char const [75]>, 
std::integral_constant<bool, true> > > >
(boost::parameter::aux::flat_like_arg_list<boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::file_name, boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::file_name, char const [11]>, 
std::integral_constant<bool, true> >, 
boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::auto_flush, 
boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::auto_flush, bool const>, 
std::integral_constant<bool, true> >, 
boost::parameter::aux::flat_like_arg_tuple<boost::log::v2s_mt_posix::keywords::tag::format, 
boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::format, char const [75]>, 
std::integral_constant<bool, true> > > const&)':

main.cpp:(.text._ZN5boost3log12v2s_mt_posix3aux12add_file_logINS_9parameter3aux18flat_like_arg_listIJNS5_19flat_like_arg_tupleINS1_8keywords3tag9file_nameENS5_15tagged_argumentISA_A11_KcEESt17integral_constantIbLb1EEEENS7_INS9_10auto_flushENSB_ISI_KbEESG_EENS7_INS9_6formatENSB_ISM_A75_SC_EESG_EEEEEEENS_10shared_ptrINS1_5sinks16synchronous_sinkINSS_17text_file_backendEEEEERKT_[_ZN5boost3log12v2s_mt_posix3aux12add_file_logINS_9parameter3aux18flat_like_arg_listIJNS5_19flat_like_arg_tupleINS1_8keywords3tag9file_nameENS5_15tagged_argumentISA_A11_KcEESt17integral_constantIbLb1EEEENS7_INS9_10auto_flushENSB_ISI_KbEESG_EENS7_INS9_6formatENSB_ISM_A75_SC_EESG_EEEEEEENS_10shared_ptrINS1_5sinks16synchronous_sinkINSS_17text_file_backendEEEEERKT_]+0x5b): 
reference to 
`boost::log::v2s_mt_posix::basic_formatter<char> boost::log::v2s_mt_posix::parse_formatter<char>(char const*, char const*)' 
not defined
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:107: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Goal

Fix the compilation without changing the syntax BOOST_LOG_TRIVIAL(debug) << "Test";.

Thanks and regards.

Lastique commented 1 year ago

You're missing Boost::log_setup in your target_link_libraries.

matheusdiogenesandrade commented 1 year ago

Thank you very much.