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

uag: add timestamps to SIP trace #412

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ static inline bool str_isset(const char *s)

/* time */
int fmt_gmtime(struct re_printf *pf, void *ts);
int fmt_timestamp(struct re_printf *pf, void *ts);
int fmt_human_time(struct re_printf *pf, const uint32_t *seconds);


Expand Down
54 changes: 49 additions & 5 deletions src/fmt/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
* Copyright (C) 2010 Creytiv.com
*/

#include <re_types.h>
#include <re_fmt.h>

#define _POSIX_C_SOURCE 200809L
#define __USE_POSIX 1 /**< Use POSIX flag */
#include <time.h>

#ifdef WIN32
#include <windows.h>
#endif

#include <re_types.h>
#include <re_fmt.h>


static const char *dayv[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

Expand Down Expand Up @@ -37,10 +42,10 @@ int fmt_gmtime(struct re_printf *pf, void *ts)

#ifdef WIN32
if (gmtime_s(&tm, ts))
return EINVAL;
return EINVAL;
#else
if (!gmtime_r(ts, &tm))
return EINVAL;
return EINVAL;
#endif

return re_hprintf(pf, "%s, %02u %s %u %02u:%02u:%02u GMT",
Expand Down Expand Up @@ -86,3 +91,42 @@ int fmt_human_time(struct re_printf *pf, const uint32_t *seconds)

return err;
}


/**
* Print local time stamp including milli seconds relative to user's timezone
*
* @param pf Print function for output
* @param arg Not used
*
* @return 0 if success, otherwise errorcode
*/
int fmt_timestamp(struct re_printf *pf, void *arg)
{
int h, m, s;
uint64_t ms;
#ifdef WIN32
SYSTEMTIME st;
GetSystemTime(&st);
ms = st.wMilliseconds;
h = st.wHour;
m = st.wMinute;
s = st.wSecond;

#else
struct timespec tspec;
struct tm tm;

(void)clock_gettime(CLOCK_REALTIME, &tspec);
ms = tspec.tv_nsec / 1000000;
if (!localtime_r(&tspec.tv_sec, &tm))
return EINVAL;

h = tm.tm_hour;
m = tm.tm_min;
s = tm.tm_sec;
#endif
(void) arg;

return re_hprintf(pf, "%02u:%02u:%02u.%03d", h, m, s, ms);
}