Solved

Boost log - how to make Line number and Function name appear in the log message

Posted on 2014-01-20
2
2,745 Views
Last Modified: 2014-01-21
I find some words explaining  to use BOOST_LOG_FUNCTION() to mark the scope of the function as you enter the function

.Prior to the log message, you hae to include  BOOST_LOG_NAMED_SCOPE();

Any good example to see how it should be done.  I can not figure it hoe to add line number and function name (or file name) into the log message.

I wonder if it is possible to selective added the line_number and function name to certain log message but not all.  How can that be done with boost
0
Comment
Question by:tommym121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39795986
You'll find a decent example here http://sourceforge.net/p/boost-log/discussion/710022/thread/c728c333#7821. Anyway, the scoop is

// TestLogging.cpp
#include <exception>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp>
#include <boost/ref.hpp>
#include <boost/log/core.hpp>
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/core/record.hpp>
#include <boost/log/utility/init/from_stream.hpp>
#include <boost/log/utility/init/common_attributes.hpp>
#include <boost/log/utility/init/formatter_parser.hpp>
namespace logging   = boost::log;
namespace attrs     = boost::log::attributes;
namespace src       = boost::log::sources;
using boost::shared_ptr;
enum severity_level
{
    normal,
    notification,
    warning,
    error,
    critical
};
BOOST_LOG_DECLARE_GLOBAL_LOGGER(test_lg, src::severity_logger< >)
struct scope_list_formatter
{
    typedef void result_type;
    typedef attrs::basic_named_scope<char>::scope_stack scope_stack;
    explicit scope_list_formatter(std::string const& name) :
        name_(name)
    {
    }
    void operator()(std::ostream& strm, logging::record const& rec) const
    {
        logging::extract<scope_stack> (
                name_,
                rec.attribute_values(),
                bind(&scope_list_formatter::format, boost::ref(strm),
                _1)
        );
    }
private:
    static void format(std::ostream& strm, scope_stack const& scopes)
    {
        scope_stack::const_iterator it = scopes.begin(), end = scopes.end();
        for (; it != end; ++it)
        {
            strm << it->scope_name << " [" << it->file_name << ":" << it->line
                    << "]\n";
        }
    }
private:
    std::string name_;
};
typedef logging::formatter_types< char > types;
types::formatter_type my_scopes_formatter_factory(
        types::string_type const& attr_name, types::formatter_factory_args const& args)
{
    return types::formatter_type(scope_list_formatter(attr_name));
}
int init_logging()
{
    std::ifstream settings("logging.conf");
    if (!settings.is_open())
    {
        std::cout << "Could not open logging.conf file" << std::endl;
        return -1;
    }
    logging::init_from_stream(settings);
    logging::add_common_attributes();
    //shared_ptr< logging::attribute > attr_scope(new attrs::named_scope);
    //logging::core::get()->add_global_attribute("Scope", attr_scope);
    shared_ptr< logging::attribute > attr_scope(new attrs::named_scope);
    logging::core::get()->add_global_attribute("MyScopes", attr_scope);
    logging::register_formatter_factory("MyScopes", &my_scopes_formatter_factory);
    return 0;
}
void try_logging()
{
    BOOST_LOG_FUNCTION();
    src::severity_logger< >& lg = get_test_lg();
    BOOST_LOG_SEV(lg, critical)         << "This is a critical severity record";
    BOOST_LOG_NAMED_SCOPE("random name");
    BOOST_LOG_SEV(lg, error)            << "This is a error severity record";
}
int main(int argc, char* argv[])
{
    init_logging();
    try_logging();
}

# logging.conf
[Core]
DisableLogging=false
Filter="%Severity% >= 2"
[Sink:ConsoleSettings]
Destination=Console
Format="[%Severity%] :: %File% %Scope% %Line% :: %_%"
Filter="%Tag% | %Severity% > 0"
[Sink:TextFileSettings]
Destination=TextFile
FileName=test.log
AutoFlush=true
Format="[%TimeStamp%] [%Severity%] :: %MyScopes% :: %_%"
Asynchronous=false

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 39797374
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question