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

Avoid double timestamps in debug log output #129

Merged
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
8 changes: 4 additions & 4 deletions src/Debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ void Ros_Debug_BroadcastMsg(char* fmt, ...)
//get synchronized time from the agent
int64_t nanosecs = rmw_uros_epoch_nanos();
Ros_Nanos_To_Time_Msg(nanosecs, &debug_msg_timestamp);
strftime(timestamp, FORMATTED_TIME_SIZE, "%a %Y-%m-%d %H:%M:%S", localtime_r((const time_t*)&debug_msg_timestamp.sec, &synced_time));
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%03d ", (int)debug_msg_timestamp.nanosec / 1000000);
strftime(timestamp, FORMATTED_TIME_SIZE, "%Y-%m-%d %H:%M:%S", localtime_r((const time_t*)&debug_msg_timestamp.sec, &synced_time));
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%06d ", (int)debug_msg_timestamp.nanosec / 1000);
}
else
{
//rmw_uros_epoch_nanos cannot sync with agent because it's not connected
clock_gettime(CLOCK_REALTIME, &tp);
strftime(timestamp, FORMATTED_TIME_SIZE, "%a %Y-%m-%d %H:%M:%S", localtime_r(&tp.tv_sec, &synced_time));
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%03d ", tp.tv_nsec / 1000000);
strftime(timestamp, FORMATTED_TIME_SIZE, "%Y-%m-%d %H:%M:%S", localtime_r(&tp.tv_sec, &synced_time));
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%06d ", tp.tv_nsec / 1000);
}
// Pre - pending the timestamp to the debug message
size_t timestamp_length = Ros_strnlen(timestamp, FORMATTED_TIME_SIZE);
Expand Down
23 changes: 10 additions & 13 deletions tools/debug_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

Address = Tuple[str, int]

# length of the stamp in a log msg: 'YYYY-MM-DD HH:MM:SS.nnnnnn'
# (where 'nnnnnn' is the microsecond part)
STAMP_STR_LEN=26

def main():
default_bcast_port=21789
Expand Down Expand Up @@ -73,19 +76,13 @@ def main():
os.remove(path=fname)


def to_human_readable_stamp(stamp) -> str:
return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d %H:%M:%S.%f')


def file_sink(f, msg, recv_time, source_addr):
stamp = to_human_readable_stamp(recv_time)
def file_sink(f, msg, stamp, source_addr):
ip, port = source_addr
print(f'[{stamp}] [{ip}:{port}]: {msg}', file=f)
f.flush()


def console_sink_cb(msg, recv_time, source_addr):
stamp = to_human_readable_stamp(recv_time)
def console_sink_cb(msg, stamp, source_addr):
ip, port = source_addr
print(f'[{stamp}] [{ip}:{port}]: {msg}')

Expand All @@ -104,14 +101,14 @@ def connection_made(self, transport: asyncio.transports.DatagramTransport):
def datagram_received(self, data: Union[bytes, Text], addr: Address):
# note: we assume all sinks appreciate strings instead of raw data
msg = data.decode('ascii')
stamp_recvd = time.time()
sent_stamp = msg[:STAMP_STR_LEN]
msg = msg[STAMP_STR_LEN+1:]
source_addr = (addr[0], addr[1])
self._write_to_syncs(
msg=msg, recv_time=stamp_recvd, source_addr=source_addr)
self._write_to_sinks(msg=msg, stamp=sent_stamp, source_addr=source_addr)

def _write_to_syncs(self, msg, recv_time, source_addr):
def _write_to_sinks(self, msg, stamp, source_addr):
for cb in self._sink_cbs:
cb(msg=msg, recv_time=recv_time, source_addr=source_addr)
cb(msg=msg, stamp=stamp, source_addr=source_addr)


if __name__ == '__main__':
Expand Down