Solved

logging with microseconds using log4c

Posted on 2010-09-15
10
1,150 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
 

Author Comment

by:PMembrey
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:Anthony2000
Comment Utility
What platform are running on?
0
 

Author Comment

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

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
We do need precision timings but ended up going for nanoseconds without a generic timestamp - works very nicely!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now