Boost logo

Boost :

From: John Eddy (johneddy_at_[hidden])
Date: 2005-02-15 13:34:41

Caleb Epstein wrote:

>On Tue, 15 Feb 2005 12:00:15 -0500, John Eddy <johneddy_at_[hidden]> wrote:
>>> 2. Logging with timestamp automatically prefixed. Do you have
>>>something pre-built for this? If not, can you show what code I have to
>>>write to implement it.
>>There is a "helper" for automatically putting a timestamp into an
>>entry. There are also a number of pre-existing entry types that are
>>implementing that helper and of course, I can create any others that you
>>might like. Below is an example of an entry that holds only a timestamp
>>along with any other text supplied to it. The constructor of the entry
>>takes a boost::posix_time::ptime object as the stamp (the default value
>>is the current time) and any leading text that you wish to associate
>>with the entry.
>I think associating the format more directly with the output "device"
>(e.g. the logger) makes more sense. Its a pretty rare case that you'd
>want to log a timestamp with one message and not *every* message.
I see what you mean. Since currently anything of any type will pass
through the log managers it would be simple enough to create and pass
along a simple data holder to the log which would then assemble the
holders' contents into some specified output format, automatically
installing a timestamp or whatever. See this example:

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/logging/log_managers/basic_log_manager.hpp>

using namespace std;
using namespace boost::logging;
using namespace boost::posix_time;

struct my_e_data {
    const char* file;
    int line;
    string text;
    my_e_data(const char* f, int l, string t) : file(f), line(l), text(t) {}

struct my_log {
    void log(const my_e_data& e) {
       cerr << to_simple_string(microsec_clock::local_time())
            << ": FILE - " << e.file
            << ": LINE - " << e.line
            << ": TEXT - " << e.text << "\n\n";

int main(int argc, char* argv)
    my_log alog;
    basic_log_manager<my_log > alm(alog);
    alm.log(my_e_data(__FILE__, __LINE__, "Bad Operation"));
    return 0;

The other current option would simply be to always use a timestamped
entry type. The downside there is that you always have to stream out
the stamp method in order to write it into the text of the entry (<<


Boost list run by bdawes at, gregod at, cpdaniel at, john at