?
Solved

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

Posted on 2014-01-20
2
Medium Priority
?
2,936 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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