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

Adding a timestamp to debug messages #61

Merged
merged 14 commits into from
Jul 5, 2023
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
36 changes: 35 additions & 1 deletion src/Debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#define MAX_DEBUG_MESSAGE_SIZE 1024

#define FORMATTED_TIME_SIZE 1024

int ros_DebugSocket = -1;
struct sockaddr_in ros_debug_destAddr1;

Expand Down Expand Up @@ -46,7 +48,39 @@ void Ros_Debug_BroadcastMsg(char* fmt, ...)

if (ros_DebugSocket == -1)
Ros_Debug_Init();

// Timestamp
//The timestamp for the message "Found Micro-Ros PC Agent" will be the epoch time (THU 1970-01-01 00:00:00.000) as the global flags
//are set to indicate that the Micro-Ros PC Agent is connected but the first sync of the host time using the micro-ROS agent is yet to occur
struct tm synced_time;
struct timeval tv;
char timestamp[FORMATTED_TIME_SIZE];
builtin_interfaces__msg__Time debug_msg_timestamp;
if (g_Ros_Communication_AgentIsConnected)
{
//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(&debug_msg_timestamp.sec, &synced_time));
ted-miller marked this conversation as resolved.
Show resolved Hide resolved
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%03d ", (int)debug_msg_timestamp.nanosec / 1000000);
}
else
{
//rmw_uros_epoch_nanos cannot sync with agent because it's not connected
gettimeofday(&tv, NULL);
strftime(timestamp, FORMATTED_TIME_SIZE, "%a %Y-%m-%d %H:%M:%S", localtime_r(&tv.tv_sec, &synced_time));
snprintf(timestamp + strlen(timestamp), FORMATTED_TIME_SIZE - strlen(timestamp), ".%03d ", tv.tv_usec / 1000);
}
// Pre - pending the timestamp to the debug message
size_t timestamp_length = strnlen(timestamp, FORMATTED_TIME_SIZE);
size_t debug_message_length = strnlen(str, MAX_DEBUG_MESSAGE_SIZE);
if (timestamp_length + debug_message_length + 1 < MAX_DEBUG_MESSAGE_SIZE)
{
// Move existing contents of str buffer to the end by Timestamp_Length to make space
//for the timestamp and avoiding overwriting the debug message during the move
memmove(str + timestamp_length, str, debug_message_length);
// Copy the timestamp stored in Formatted_time buffer to the beginning of str buffer
memcpy(str, timestamp, timestamp_length);
}
gavanderhoorn marked this conversation as resolved.
Show resolved Hide resolved
mpSendTo(ros_DebugSocket, str, strlen(str), 0, (struct sockaddr*) &ros_debug_destAddr1, sizeof(struct sockaddr_in));

if (g_nodeConfigSettings.log_to_stdout)
Expand Down
9 changes: 9 additions & 0 deletions src/MotoROS_PlatformLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ extern ULONG tickGet();
extern STATUS Ros_setsockopt(int s, int level, int optname, char* optval, int optlen);


#if defined (DX100) || defined (FS100)
// VxWorks 5.5/6.8
extern int localtime_r(const time_t* timer, struct tm* timeBuffer);
#else
// >= VxWorks 6.9
struct tm* localtime_r(const time_t* timep, struct tm* result);
#endif


// Attempts to determine whether the specific link is UP (ie: cable is connected).
// IFF return value is 'OK', 'is_up' will reflect link state. In all other cases,
// 'is_up' may be left uninitialised.
Expand Down