[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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,150 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

873 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