Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

636 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