mirror of
https://github.com/nasa/trick.git
synced 2024-12-24 07:16:41 +00:00
Added sub second information to message time stamps. (#1773)
* Added sub second information to message time stamps. * Removed "0." for sub-second portion of time stamp.
This commit is contained in:
parent
b46ba50fd0
commit
eed8707638
@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ Trick::MessagePublisher::~MessagePublisher() {
|
|||||||
|
|
||||||
void Trick::MessagePublisher::set_print_format() {
|
void Trick::MessagePublisher::set_print_format() {
|
||||||
num_digits = (int)round(log10((double)tics_per_sec)) ;
|
num_digits = (int)round(log10((double)tics_per_sec)) ;
|
||||||
snprintf(print_format, sizeof(print_format), "|L %%3d|%%s|%%s|%%s|T %%d|%%lld.%%0%dlld| ", num_digits) ;
|
snprintf(print_format, sizeof(print_format), "|L %%3d|%%s.%%06Lu|%%s|%%s|T %%d|%%lld.%%0%dlld| ", num_digits) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Trick::MessagePublisher::init() {
|
int Trick::MessagePublisher::init() {
|
||||||
@ -49,14 +50,22 @@ int Trick::MessagePublisher::publish(int level , std::string message) {
|
|||||||
char header_buf[MAX_MSG_HEADER_SIZE];
|
char header_buf[MAX_MSG_HEADER_SIZE];
|
||||||
char hostname[64];
|
char hostname[64];
|
||||||
time_t date ;
|
time_t date ;
|
||||||
|
// timeval contains both tv_sec and tv_usec
|
||||||
|
// tv_sec represents seconds since the epoch and is used for time stamp without sub-second.
|
||||||
|
// tv_usec are microseconds past the last second and is used for printing out sub-second.
|
||||||
|
struct timeval time_val;
|
||||||
std::string header ;
|
std::string header ;
|
||||||
long long tics = exec_get_time_tics() ;
|
long long tics = exec_get_time_tics() ;
|
||||||
|
|
||||||
/** @li Create message header with level, date, host, sim name, process id, sim time. */
|
/** @li Create message header with level, date, host, sim name, process id, sim time. */
|
||||||
date = time(NULL) ;
|
gettimeofday(&time_val, NULL);
|
||||||
|
|
||||||
|
// tv_sec represents seconds since the epoch
|
||||||
|
date = time_val.tv_sec;
|
||||||
|
|
||||||
strftime(date_buf, (size_t) 20, "%Y/%m/%d,%H:%M:%S", localtime(&date));
|
strftime(date_buf, (size_t) 20, "%Y/%m/%d,%H:%M:%S", localtime(&date));
|
||||||
(void) gethostname(hostname, (size_t) 48);
|
(void) gethostname(hostname, (size_t) 48);
|
||||||
snprintf(header_buf, sizeof(header_buf), print_format , level, date_buf, hostname,
|
snprintf(header_buf, sizeof(header_buf), print_format , level, date_buf, time_val.tv_usec, hostname,
|
||||||
sim_name.c_str(), exec_get_process_id(), tics/tics_per_sec ,
|
sim_name.c_str(), exec_get_process_id(), tics/tics_per_sec ,
|
||||||
(long long)((double)(tics % tics_per_sec) * (double)(pow(10 , num_digits)/tics_per_sec)) ) ;
|
(long long)((double)(tics % tics_per_sec) * (double)(pow(10 , num_digits)/tics_per_sec)) ) ;
|
||||||
header = header_buf ;
|
header = header_buf ;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
// message_publish when you don't have a message publisher class
|
// message_publish when you don't have a message publisher class
|
||||||
#define MAX_MSG_HEADER_SIZE 256
|
#define MAX_MSG_HEADER_SIZE 256
|
||||||
@ -13,18 +14,27 @@ extern "C" int message_publish_standalone(int level, const char * format_msg, ..
|
|||||||
char date_buf[MAX_MSG_HEADER_SIZE];
|
char date_buf[MAX_MSG_HEADER_SIZE];
|
||||||
char hostname[64];
|
char hostname[64];
|
||||||
time_t date ;
|
time_t date ;
|
||||||
|
// timeval contains both tv_sec and tv_usec
|
||||||
|
// tv_sec represents seconds since the epoch and is used for time stamp without sub-second.
|
||||||
|
// tv_usec are microseconds past the last second and is used for printing out sub-second.
|
||||||
|
struct timeval time_val;
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start(args, format_msg);
|
va_start(args, format_msg);
|
||||||
(void) vsnprintf(msg_buf, MAX_MSG_SIZE, format_msg, args);
|
(void) vsnprintf(msg_buf, MAX_MSG_SIZE, format_msg, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
date = time(NULL) ;
|
gettimeofday(&time_val, NULL);
|
||||||
|
|
||||||
|
// tv_sec represents seconds since the epoch
|
||||||
|
date = time_val.tv_sec;
|
||||||
|
|
||||||
strftime(date_buf, (size_t) 20, "%Y/%m/%d,%H:%M:%S", localtime(&date));
|
strftime(date_buf, (size_t) 20, "%Y/%m/%d,%H:%M:%S", localtime(&date));
|
||||||
(void) gethostname(hostname, (size_t) 48);
|
(void) gethostname(hostname, (size_t) 48);
|
||||||
fprintf(stdout, "|L %d|%s| |%s|T %d|%.2f| %s" , level,
|
fprintf(stdout, "|L %d|%s.%06Lu| |%s|T %d|%.2f| %s" , level,
|
||||||
// so that we don't call any exec routines, use process id 0 and sim time 0.0
|
// so that we don't call any exec routines, use process id 0 and sim time 0.0
|
||||||
date_buf, hostname , 0, 0.0, msg_buf) ;
|
date_buf, time_val.tv_usec, hostname , 0, 0.0, msg_buf) ;
|
||||||
fflush(stdout) ;
|
fflush(stdout) ;
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
|
Loading…
Reference in New Issue
Block a user