Boost Logging Lib, v2

J

jtorjo2007

Boost Logging v2

Hi all,

It's been a while - I admit, and I apologize for it. Well, finally,
I'm working on version 2 of Boost Logging, taking into account your
suggestions when the first version was rejected.

The extra short version: It's in alpha stage. Get it from here:
http://torjo.com/log2/

Without further ado, I'll present you Boost Logging Library v2, with
- a much nicer separation of concepts and
- a much more flexible interface.
- you don't pay for what you don't use.
- fits a lot of scenarios:
from very simple (dumping all to one log)
to very complex (multiple logs, some enabled/some not, levels, etc).
- you can now define your own LOG_ macros

Here are 2 small examples to show you a bit of v2's power:

----------------- Example 1:
struct write_to_cout_and_file { ... };

// defining the logs
logger<write_to_cout_and_file, manage_enabled_no_ts>
g_single_log("out.txt");
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)

// code
L_("reading word " + word);

----------------- Example 2:
struct write_to_file { ... };
struct write_to_cout { ... };
struct write_to_dbg { ... };

// defining the logs
typedef process_msg< ostream_like_return_str<>, write_to_cout>
processor_cout;
typedef process_msg< ostream_like_return_str<>, write_to_file>
processor_file;
typedef process_msg< ostream_like_return_str<>, write_to_dbg>
processor_dbg;

logger<processor_cout, manage_enabled_no_ts> g_log_app;
logger<processor_file, manage_enabled_no_ts> g_log_err("err.txt");
logger<processor_dbg, manage_enabled_no_ts> g_log_dbg;

#define LAPP_ if ( !g_log_app) ; else g_log_app-
read_msg().gather().out()
#define LERR_ if ( !g_log_err) ; else g_log_err-
read_msg().gather().out()
#define LDBG_ if ( !g_log_dbg) ; else g_log_dbg-
read_msg().gather().out()

// code
LAPP_ << idx << " : reading word " << word;
LERR_ << "error at " << idx << ", while reading " << word;
LDBG_ << "debug info: " << idx << ", reading " << word;

-----------------

The above was the short version. You can also look at the test_*.cpp
files, to see some more examples of how you can use the library.
Download it here : http://torjo.com/log2/

Long version follows:

First, a bit about the workflow:
Step 1: Filtering the message
Step 2: Gathering the message
Step 3: Writing the message


Step 1: Filtering the message
In order to be efficient, before anything happens to the message,
we see if the log it's written to, is enabled.
If so, we process the message.
If not, we ignore all of the message.

This is rather easy, with constructs like this:
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)
#define L_ if ( !g_log_app) ; else g_log_app-
read_msg().gather().out()



Step 2: Gathering the message
Now, that we know the log is enabled, all the message is gathered.
This depends on how you write the message. For instance, you can
gather it in one step, if you want to offer a syntax like this:
L_("this is my message");

Or, you can offer syntactic sugar:
i = 100;
L_ << "this is " << i << " times better that the average bear... ";

In this case, you'll gather all info in a stream of some kind.

Or, you can have more data besides the message string, like: module
name, severity of message, category, etc:
L_(fatal,"chart","Can't find the ChartX control");



Step 3: Writing the message
Now all info about the message is gathered. You can write it to its
destination(s).
What you wanna do with a message, is completely up to you. The library
will offer you possibilities.

You can go for the extremely easy way, and just have a functor which
dumps the message somewhere, like this:

struct write_to_cout {
void operator()(const std::string & msg) const {
std::cout << msg << std::endl ;
}
};

Or:

struct write_to_file {
typedef boost::shared_ptr<std::eek:fstream> ptr;
write_to_file(const std::string & filename) : m_out(new
std::eek:fstream(filename.c_str())) {}
void operator()(const std::string & msg) const {
(*m_out) << msg << std::endl ;
}
mutable ptr m_out;
};

Or, you can choose to Format and/or write your message to Destinations
(these will be explained later)


Download it here : http://torjo.com/log2/
------------------------------------------------------
Logger objects

For each log you want, you'll have a logger object. Note that "log" is
a concept - several logs can write to the same file and/or one log can
write to multiple destinations. I use the term "log" to identify a
type of log message you want to write -- for example, you can have a
"debug log" for writing debug messages; an "info" log, for writing
informations, and so on. The fact that they might end up writing to
the same destinations, is completely transparent to the user, as it
should.

A logger object contains 2 things:
- a filter: an is_enabled() function - to find out if it's enabled.
- a process_msg() object - this contains the logic of what to do with
the message, if the log is enabled.

The 2 concepts are orthogonal - Andrei would call them "policies" ;)

The process_msg() is the object that will process the message if the
log is enabled - it executes steps 2 and 3.
How this happens, is completely up to you - when you define your
macros (more on this later).
------------------------------------------------------
Filtering and Log Levels

I have implented several filters: see filter.hpp file:
- manage_enabled_no_ts - filter (not thread-safe)
- always_enabled - enable logs at compile time
- always_disabled - disable logs at compile time
- debug_enabled, release_enabled
- manage_enabled_ts - thread-safe filter
- etc.

Using v2, you don't really have to use Log Levels, if you don't wish
(see examples above)

A logged message can have an associated Log Level. By default we have
these levels:

info (smallest level),
warning ,
debug ,
error ,
fatal (highest level)

Depending on which level is enabled for your application, some
messages will reach the log: those messages having at least that
level. For instance, if info level is enabled, all logged messages
will reach the log.
If warning level is enabled, all messages are logged, but the
warnings. If debug level is enabled, messages that have levels debug,
error, fatal will be logged.

Note that there's an example of using Log Levels provided with the
code.

------------------------------------------------------
Usage syntax / Macros

Once you've set up what each of your logs does (writes to), it's a
must to have an easy and straightforward way to write to your logs -
logging can be fun.

While you can write directly to your logs, like this:
if ( !g_log_app) ; else g_log_app->read_msg().gather().out() <<
"coolio!" << std::endl;

Usually you'll prefer to have some macros to help you write less and
especially avoid being error-prone.

In the previous version, you've asked to be able to write your own
macros - well, you are.
And it's waaaay easier than before:
If you have a logger object, to test if it's enabled, call its
operator bool(). To see if it's not enabled, call its operator!

I will provide some helpers in the future - they're not there yet.

------------------------------------------------------
Logger objects and macros

So, to define a log macro, you'll say something like this:

logger<some_processor, some_filter_class> g_log;
#define L_ if ( !g_log) ; else g_log->...

Note that "g_log->" is equivalent to "g_log.process_msg()-
" (syntactic sugar)

What follows after "..." is all up to you. For example, you can just
get the message, and call a functor:

struct write_to_cout {
void operator()(const std::string & msg) const {
std::cout << msg << std::endl ;
}
};
logger<write_to_cout, manage_enabled_no_ts> g_single_log;
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)

And in code you'll say things like
L_("some great message");


Or, you can use something a bit more complex, which will gather a
message written using "<<", and then call a functor. I've already
implemented this class:

typedef process_msg< ostream_like_return_str<>, write_to_cout>
processor_cout;
logger<processor_cout, manage_enabled_no_ts> g_log;
#define L_ if ( !g_log) ; else g_log->read_msg().gather().out()

And in code you'll say things like:
L_ << "this" << " is " << "cool";


Or you can implement your own process message class.
------------------------------------------------------
Using process_msg()

Your process_msg class can hold context. And you can also manipulate
logs in code:
logger<...> g_log;

g_log->my_great_log_manipulator();

Thus, you can create your own process message handler classes.
------------------------------------------------------
Formatters and/or destinations

In my previous version, they were called modifiers and appenders.

So, after the message has been gathered (step 2), we do the writing
(step 3).

In step 3, if you use the "format_write" class, you can choose to add
formatters and/or destinations.

A formatter is a functor that formats the message - for instance, it
can prepend the current time, it can prepend the index, or thread ID,
or append an Enter.

A destination is a location where a message can be written, like:
console, a file, a socket, etc.

Dealing with formatters/destinations is easy: once you have a
format_write object, just use:
void add_formatter(formatter fmt)
void del_formatter(formatter fmt)
void add_destination(destination dest)
void del_destination(destination dest)

In the previous version, the formatters were called first, and
destinations last. Now you have more control.
However, this is not thoroughly tested - need to work more on this.

------------------------------------------------------
Examples. I've provided a few examples - you'll find them in the
"tests" directory.

The code compiles and was tested on VC8.

Am still working on formatters and destinations. I'm planning to host
it on sf.net.

Feedback and comments are welcome. Again, you'll find the library
here:
http://torjo.com/log2/
 
B

Barry

Boost Logging v2

Hi all,

It's been a while - I admit, and I apologize for it. Well, finally,
I'm working on version 2 of Boost Logging, taking into account your
suggestions when the first version was rejected.

The extra short version: It's in alpha stage. Get it from here:
http://torjo.com/log2/

Without further ado, I'll present you Boost Logging Library v2, with
- a much nicer separation of concepts and
- a much more flexible interface.
- you don't pay for what you don't use.
- fits a lot of scenarios:
from very simple (dumping all to one log)
to very complex (multiple logs, some enabled/some not, levels, etc).
- you can now define your own LOG_ macros

Here are 2 small examples to show you a bit of v2's power:

----------------- Example 1:
struct write_to_cout_and_file { ... };

// defining the logs
logger<write_to_cout_and_file, manage_enabled_no_ts>
g_single_log("out.txt");
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)

// code
L_("reading word " + word);

----------------- Example 2:
struct write_to_file { ... };
struct write_to_cout { ... };
struct write_to_dbg { ... };

// defining the logs
typedef process_msg< ostream_like_return_str<>, write_to_cout>
processor_cout;
typedef process_msg< ostream_like_return_str<>, write_to_file>
processor_file;
typedef process_msg< ostream_like_return_str<>, write_to_dbg>
processor_dbg;

logger<processor_cout, manage_enabled_no_ts> g_log_app;
logger<processor_file, manage_enabled_no_ts> g_log_err("err.txt");
logger<processor_dbg, manage_enabled_no_ts> g_log_dbg;

#define LAPP_ if ( !g_log_app) ; else g_log_app-
#define LERR_ if ( !g_log_err) ; else g_log_err-
#define LDBG_ if ( !g_log_dbg) ; else g_log_dbg-

// code
LAPP_ << idx << " : reading word " << word;
LERR_ << "error at " << idx << ", while reading " << word;
LDBG_ << "debug info: " << idx << ", reading " << word;

-----------------

The above was the short version. You can also look at the test_*.cpp
files, to see some more examples of how you can use the library.
Download it here : http://torjo.com/log2/

Long version follows:

First, a bit about the workflow:
Step 1: Filtering the message
Step 2: Gathering the message
Step 3: Writing the message


Step 1: Filtering the message
In order to be efficient, before anything happens to the message,
we see if the log it's written to, is enabled.
If so, we process the message.
If not, we ignore all of the message.

This is rather easy, with constructs like this:
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)
#define L_ if ( !g_log_app) ; else g_log_app-



Step 2: Gathering the message
Now, that we know the log is enabled, all the message is gathered.
This depends on how you write the message. For instance, you can
gather it in one step, if you want to offer a syntax like this:
L_("this is my message");

Or, you can offer syntactic sugar:
i = 100;
L_ << "this is " << i << " times better that the average bear... ";

In this case, you'll gather all info in a stream of some kind.

Or, you can have more data besides the message string, like: module
name, severity of message, category, etc:
L_(fatal,"chart","Can't find the ChartX control");



Step 3: Writing the message
Now all info about the message is gathered. You can write it to its
destination(s).
What you wanna do with a message, is completely up to you. The library
will offer you possibilities.

You can go for the extremely easy way, and just have a functor which
dumps the message somewhere, like this:

struct write_to_cout {
void operator()(const std::string & msg) const {
std::cout << msg << std::endl ;
}
};

Or:

struct write_to_file {
typedef boost::shared_ptr<std::eek:fstream> ptr;
write_to_file(const std::string & filename) : m_out(new
std::eek:fstream(filename.c_str())) {}
void operator()(const std::string & msg) const {
(*m_out) << msg << std::endl ;
}
mutable ptr m_out;
};

Or, you can choose to Format and/or write your message to Destinations
(these will be explained later)


Download it here : http://torjo.com/log2/
------------------------------------------------------
Logger objects

For each log you want, you'll have a logger object. Note that "log" is
a concept - several logs can write to the same file and/or one log can
write to multiple destinations. I use the term "log" to identify a
type of log message you want to write -- for example, you can have a
"debug log" for writing debug messages; an "info" log, for writing
informations, and so on. The fact that they might end up writing to
the same destinations, is completely transparent to the user, as it
should.

A logger object contains 2 things:
- a filter: an is_enabled() function - to find out if it's enabled.
- a process_msg() object - this contains the logic of what to do with
the message, if the log is enabled.

The 2 concepts are orthogonal - Andrei would call them "policies" ;)

The process_msg() is the object that will process the message if the
log is enabled - it executes steps 2 and 3.
How this happens, is completely up to you - when you define your
macros (more on this later).
------------------------------------------------------
Filtering and Log Levels

I have implented several filters: see filter.hpp file:
- manage_enabled_no_ts - filter (not thread-safe)
- always_enabled - enable logs at compile time
- always_disabled - disable logs at compile time
- debug_enabled, release_enabled
- manage_enabled_ts - thread-safe filter
- etc.

Using v2, you don't really have to use Log Levels, if you don't wish
(see examples above)

A logged message can have an associated Log Level. By default we have
these levels:

info (smallest level),
warning ,
debug ,
error ,
fatal (highest level)

Depending on which level is enabled for your application, some
messages will reach the log: those messages having at least that
level. For instance, if info level is enabled, all logged messages
will reach the log.
If warning level is enabled, all messages are logged, but the
warnings. If debug level is enabled, messages that have levels debug,
error, fatal will be logged.

Note that there's an example of using Log Levels provided with the
code.

------------------------------------------------------
Usage syntax / Macros

Once you've set up what each of your logs does (writes to), it's a
must to have an easy and straightforward way to write to your logs -
logging can be fun.

While you can write directly to your logs, like this:
if ( !g_log_app) ; else g_log_app->read_msg().gather().out() <<
"coolio!" << std::endl;

Usually you'll prefer to have some macros to help you write less and
especially avoid being error-prone.

In the previous version, you've asked to be able to write your own
macros - well, you are.
And it's waaaay easier than before:
If you have a logger object, to test if it's enabled, call its
operator bool(). To see if it's not enabled, call its operator!

I will provide some helpers in the future - they're not there yet.

------------------------------------------------------
Logger objects and macros

So, to define a log macro, you'll say something like this:

logger<some_processor, some_filter_class> g_log;
#define L_ if ( !g_log) ; else g_log->...

Note that "g_log->" is equivalent to "g_log.process_msg()-

What follows after "..." is all up to you. For example, you can just
get the message, and call a functor:

struct write_to_cout {
void operator()(const std::string & msg) const {
std::cout << msg << std::endl ;
}
};
logger<write_to_cout, manage_enabled_no_ts> g_single_log;
#define L_(x) if ( g_single_log) g_single_log.process_msg()(x)

And in code you'll say things like
L_("some great message");


Or, you can use something a bit more complex, which will gather a
message written using "<<", and then call a functor. I've already
implemented this class:

typedef process_msg< ostream_like_return_str<>, write_to_cout>
processor_cout;
logger<processor_cout, manage_enabled_no_ts> g_log;
#define L_ if ( !g_log) ; else g_log->read_msg().gather().out()

And in code you'll say things like:
L_ << "this" << " is " << "cool";


Or you can implement your own process message class.
------------------------------------------------------
Using process_msg()

Your process_msg class can hold context. And you can also manipulate
logs in code:
logger<...> g_log;

g_log->my_great_log_manipulator();

Thus, you can create your own process message handler classes.
------------------------------------------------------
Formatters and/or destinations

In my previous version, they were called modifiers and appenders.

So, after the message has been gathered (step 2), we do the writing
(step 3).

In step 3, if you use the "format_write" class, you can choose to add
formatters and/or destinations.

A formatter is a functor that formats the message - for instance, it
can prepend the current time, it can prepend the index, or thread ID,
or append an Enter.

A destination is a location where a message can be written, like:
console, a file, a socket, etc.

Dealing with formatters/destinations is easy: once you have a
format_write object, just use:
void add_formatter(formatter fmt)
void del_formatter(formatter fmt)
void add_destination(destination dest)
void del_destination(destination dest)

In the previous version, the formatters were called first, and
destinations last. Now you have more control.
However, this is not thoroughly tested - need to work more on this.

------------------------------------------------------
Examples. I've provided a few examples - you'll find them in the
"tests" directory.

The code compiles and was tested on VC8.

Am still working on formatters and destinations. I'm planning to host
it on sf.net.

Feedback and comments are welcome. Again, you'll find the library
here:
http://torjo.com/log2/
Took a quick walk, I think I found a bug.

boost\logging\ts\ts_win32.hpp
have no

mutex::~mutex()
{
DeleteCriticalSection(GetCriticalSectionPtr());
}

And I don't understand why bothers to use /GetCriticalSectionPtr/, not
directly /&m_cs/?
 
C

coal

Boost Logging v2

Hi all,

It's been a while - I admit, and I apologize for it. Well, finally,
I'm working on version 2 of Boost Logging, taking into account your
suggestions when the first version was rejected.

The extra short version: It's in alpha stage. Get it from here:http://torjo.com/log2/

Without further ado, I'll present you Boost Logging Library v2,

I think you should bring this up on the Boost list. Boosters
should decide if they want the library to be a part of Boost
or not.

Brian Wood
Ebenezer Enterprises
 
J

jtorjo2007

mutex::~mutex()
{
DeleteCriticalSection(GetCriticalSectionPtr());

}

And I don't understand why bothers to use /GetCriticalSectionPtr/, not
directly /&m_cs/?

Yup, you're right - as for not using m_cs - perhaps I'll change it -
this part is quite old code :)
Thanks for pointing it out!

Best,
John
 
J

jtorjo2007

I think you should bring this up on the Boost list. Boosters
should decide if they want the library to be a part of Boost
or not.

Of course :) I will write to the boost list today. I didn't do it
yesterday since I was tired and had to setup a new mail account :)


Best,
John
 
A

apm35

Boost Logging v2

Hi all,

It's been a while - I admit, and I apologize for it. Well, finally,
I'm working on version 2 of Boost Logging, taking into account your
suggestions when the first version was rejected.

I never saw the first version. But I have a comment on this one. Well,
it's an enhancement request really. Please consider aking it so that
the logging activity can be on its own thread. This means the logging
calls will queue messages to that thread and not do the file I/O
themselves. This really matters for some applications. Such apps tend
to have their own logger since none of the Open Source loggers I have
seen do this (not even log4j AFAIK).

Regards,

Andrew Marlow
 
J

jtorjo2007

I never saw the first version. But I have a comment on this one. Well,
it's an enhancement request really. Please consider aking it so that
the logging activity can be on its own thread. This means the logging
calls will queue messages to that thread and not do the file I/O
themselves. This really matters for some applications. Such apps tend
to have their own logger since none of the Open Source loggers I have
seen do this (not even log4j AFAIK).

He he ;) The first version had this feature. And for sure, this
version will have it too.

I need to work a bit more on formatters/destinations, and will provide
that too.
It's not that complex - all I need is a functor -- which gathers the
message and places it on a queue. Then, on a dedicated thread, read
each message, and have the formatters/destinations applied to it.
It'll definitely be there :)

Best,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top