Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:43:00+00:00 2026-05-25T14:43:00+00:00

I have one daemon written in C. I am logging the events in a

  • 0

I have one daemon written in C. I am logging the events in a log file, but now I want to add date and time while writing event to log file. How can I achieve that?

Current log file:-

Event one occurred: result:
Event two occurred: result:

I want the log file to look like:-

Sep 14 11:35:55 Event one occurred: result:
Sep 14 11:35:55 Event two occurred: result:

My environment is C and Linux.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:43:01+00:00Added an answer on May 25, 2026 at 2:43 pm

    You need to look into using date and either gmtime or localtime to get the actual date and time.

    Then strftime can format it for you.

    Sample program follows:

    #include <stdio.h>
    #include <time.h>
    
    int main (void) {
        char buff[20];
        struct tm *sTm;
    
        time_t now = time (0);
        sTm = gmtime (&now);
    
        strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
        printf ("%s %s\n", buff, "Event occurred now");
    
        return 0;
    }
    

    This outputs:

    2011-09-14 04:52:11 Event occurred now
    

    I prefer the use of UTC rather than local time since it allows you to tie together events from geographically separated machine without worrying about timezone differences. In other words, use gmtime rather than localtime unless you’re very certain you won’t be crossing timezones.

    I also tend to prefer the YYYY-MM-DD HH:MM:SS format since it’s easier to sort than month names, vital for extraction and manipulation tools.


    If you have an implementation that provides the optional bounds-checking functions (as per Appendix K of C11), you can probably use gmtime_s in preference. It allows you to specify your own buffer for receiving the result and is thus safer in re-entrant and/or threaded code.

    To use that, you need to change your code to something like:

    #include <stdio.h>
    #define __STDC_WANT_LIB_EXT1__ 1
    #include <time.h>
    
    int main (void) {
        char buff[20];
        struct tm sTm;
    
        time_t now = time (0);
        gmtime_s (&now, &sTm);
    
        strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", &sTm);
        printf ("%s %s\n", buff, "Event occurred now");
    
        return 0;
    }
    

    Although you should be aware that the folks at Microsoft have somehow managed to get the arguments for gmtime_s around the wrong way. You’ll need to take that into account.

    POSIX (and Linux) also provides a gmtime_r function which performs in the same way as the standard gmtime_s function (with the arguments in the correct order).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have one time consuming step that flattens a bunch of files. basically i'd like
I have one control named thumbviewer inside repeater. I want to set its imageurl
I have written a logging application in Python that is meant to start at
I need to write a daemon that supposed to have one TCP socket and
I try to write a daemon in python. But I have no idea how
I have written an XMPP daemon (using JAXL) for sending and recieving messages which
Say I have a local daemon running on my machine, and I want to
I have a Solaris daemon written in Java6. Clients can connect to it using
I have a daemon that reads a configuration file in order to know where
I have one class that has a list of objects of Daemon type. class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.