Solved

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

Posted on 2014-01-20
2
2,531 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now