Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to print log with current time graceful? #124

Closed
lxlenovostar opened this issue Mar 29, 2018 · 3 comments
Closed

How to print log with current time graceful? #124

lxlenovostar opened this issue Mar 29, 2018 · 3 comments

Comments

@lxlenovostar
Copy link

I want to print log with current time in this way:

uint64_t now = tmr_seconds();
DEBUG_WARNING("time:%llu, statistics  ok\n");
 static uint64_t tmr_seconds(void)
 {
     struct timeval now;
     uint64_t usec;
 
     if (0 != gettimeofday(&now, NULL)) {
         DEBUG_WARNING("jiffies: gettimeofday() failed (%m)\n", errno);
         return 0;
     }
 
     usec  = (uint64_t)now.tv_sec;
 
     return usec;
 }

Are there other more elegant ways?
Thank you

@alfredh
Copy link
Contributor

alfredh commented Apr 2, 2018

check out the DBG_TIME option in dbg_init:

/** Debug flags */
enum dbg_flags {
	DBG_NONE = 0,                 /**< No debug flags         */
	DBG_TIME = 1<<0,              /**< Print timestamp flag   */
	DBG_ANSI = 1<<1,              /**< Print ANSI color codes */
	DBG_ALL  = DBG_TIME|DBG_ANSI  /**< All flags enabled      */
};

void dbg_init(int level, enum dbg_flags flags);

If you enable DBG_TIME it will prefix all loglines with the timestamp.

Alfred

@lxlenovostar
Copy link
Author

I set the options withe dbg_init:
dbg_init(DBG_DEBUG, DBG_ALL);

I think this time is program running time, not system time.

Thank you.

@alfredh
Copy link
Contributor

alfredh commented Apr 5, 2018

for questions about how to use the API it is better to use the mailing list:

http://lists.creytiv.com/mailman/listinfo/re-devel

here is a simple test program using fmt_gmtime and fmt_human_time

#include <re.h>


int main(void)
{
	uint32_t seconds = 3600 + 60 + 5;

	re_printf("gmtime:      %H\n", fmt_gmtime, NULL);
	re_printf("human_time:  %H\n", fmt_human_time, &seconds);
}

and the output:

./test 
gmtime:      Thu, 05 Apr 2018 06:48:48 GMT
human_time:  1 hour 1 min 5 secs

@alfredh alfredh closed this as completed Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants