• C

logging with microseconds using log4c

Hi,

I'm looking for a complete but simple "Hello World" program (the example provided on the log4c website is fine) that produces output that looks like the following:


09:45:26.123456 This is a log entry

In other words I want the timestamp (with microseconds) followed by the message.

Any takers? :)
PMembreyAsked:
Who is Participating?
 
Infinity08Commented:
I've mentioned this in one of your other questions, but since it comes up here too, I'll repeat it here :

I'm curious : are you sure you really need microsecond precision logs ? That's rarely useful, especially because the logging operation itself will likely take much more than a few microseconds.

If you really need a micro-second-precision timer (from the start of the executable generally) mentioned in the logs, you can always add it. Nothing prevents you from doing that.
A micro-second precision timestamp (with date and time of day) though is a very different matter. Very few systems support something like that.

So, you might want to re-consider this ;)
0
 
Varghese JacobAssistant Project ManagerCommented:
Use the PatternLayout for achieving the desired O/P,  which is mentioned in the below link
http://log4cplus.sourceforge.net/docs/html/classlog4cplus_1_1PatternLayout.html

You can assign the layout to log4cplus.appender.appenderName.layout

For your desired O/P I think the following pattern should suffice.
"%H:%M:%S.%q"

I've pasted the format patterns below, for your quick reference

    * %a -- Abbreviated weekday name
    * %A -- Full weekday name
    * %b -- Abbreviated month name
    * %B -- Full month name
    * %c -- Standard date and time string
    * %d -- Day of month as a decimal(1-31)
    * %H -- Hour(0-23)
    * %I -- Hour(1-12)
    * %j -- Day of year as a decimal(1-366)
    * %m -- Month as decimal(1-12)
    * %M -- Minute as decimal(0-59)
    * %p -- Locale's equivalent of AM or PM
    * %q -- milliseconds as decimal(0-999) -- Log4CPLUS specific
    * %Q -- fractional milliseconds as decimal(0-999.999) -- Log4CPLUS specific
    * %S -- Second as decimal(0-59)
    * %U -- Week of year, Sunday being first day(0-53)
    * %w -- Weekday as a decimal(0-6, Sunday being 0)
    * %W -- Week of year, Monday being first day(0-53)
    * %x -- Standard date string
    * %X -- Standard time string
    * %y -- Year in decimal without century(0-99)
    * %Y -- Year including century as decimal
    * %Z -- Time zone name
    * %% -- The percent sign
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> vajacob

PatternLayout is a class in log4cpp and asker needs help with log4c
"logging with microseconds using log4c"
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
Infinity08Commented:
Have a look at the following quick example.

I put some of the configuration in the code so it's easier to see what's going on. But commonly, you'd put the configuration in the configuration file.
#include <stdio.h>

#include "log4c.h"

int main(int argc, char* argv[]) {
    log4c_category_t* logger = NULL;

    if (log4c_init()) {
        fprintf(stderr, "ERROR : log4c initialization failed !\n");
        return 1;
    }
    else {
        logger = log4c_category_new("logger");

        log4c_appender_t* appender = log4c_appender_get("stdout");
        log4c_layout_t* layout = log4c_layout_get("dated");
        log4c_appender_set_layout(appender, layout);
        log4c_category_set_appender(logger, appender);
        log4c_category_set_priority(logger, LOG4C_PRIORITY_DEBUG);

        log4c_category_log(logger, LOG4C_PRIORITY_DEBUG, "Hello World!");

        log4c_category_delete(logger);

        if (log4c_fini()) {
            fprintf(stderr, "ERROR : log4c finalization failed !\n");
        }
    }

    return 0;
}

Open in new window

0
 
PMembreyAuthor Commented:
thanks for the full example - but it looks like from what I've seen that log4c doesn't do microseconds which I really need. If that's the case, I will still award the points to infinity08 - but it would be awesome if I could get microseconds....
0
 
Infinity08Commented:
On my platform, I get the milliseconds. That's expected, because the clock accuracy is a few milliseconds.

There are higher-resolution timers around (depending on what platform you use), but log4c doesn't make use of those by default. You could add the timestamp yourself though, in the message.
0
 
Anthony2000Commented:
What platform are running on?
0
 
PMembreyAuthor Commented:
It's running on Linux - either Debian 5 or Fedora 13
0
 
Anthony2000Commented:
I agree with Infinity08. if your hardware had a real time clock that could return the time in microseconds, then the logging tool could be modified to get this value. But, I am not aware of generic facility for this. Simply modifying log4cpp will get you microsecond resolution without hardware that can provide it.
0
 
PMembreyAuthor Commented:
We do need precision timings but ended up going for nanoseconds without a generic timestamp - works very nicely!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.