Solved

logging with microseconds using log4c

Posted on 2010-09-15
10
1,199 Views
Last Modified: 2012-05-10
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? :)
0
Comment
Question by:PMembrey
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:Varghese Jacob
ID: 33689402
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
 
LVL 40

Expert Comment

by:evilrix
ID: 33690508
>> vajacob

PatternLayout is a class in log4cpp and asker needs help with log4c
"logging with microseconds using log4c"
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33691171
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PMembrey
ID: 33692820
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33693155
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33693690
What platform are running on?
0
 

Author Comment

by:PMembrey
ID: 33696661
It's running on Linux - either Debian 5 or Fedora 13
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 33698801
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33701267
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
 

Author Closing Comment

by:PMembrey
ID: 33867108
We do need precision timings but ended up going for nanoseconds without a generic timestamp - works very nicely!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 101
how to understand recursion 12 232
Acrinis True image 2 108
Loading flat file data in tables 2 58
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

735 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