Skip to content

Commit

Permalink
Add human readable time (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
danthony06 authored Sep 20, 2023
1 parent 36b88ba commit 0e7ac45
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/swri_console/log_database_proxy_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class LogDatabaseProxyModel : public QAbstractListModel
void minTimeUpdated();
void setDisplayTime(bool display);
void setAbsoluteTime(bool absolute);
void setHumanReadableTime(bool human_readable_time);
void setDisplayLogger(bool logger_name);
void setDisplayFunction(bool function_name);
void setColorizeLogs(bool colorize_logs);
Expand All @@ -110,6 +111,7 @@ class LogDatabaseProxyModel : public QAbstractListModel
bool colorize_logs_;
bool display_time_;
bool display_absolute_time_;
bool human_readable_time_;
bool display_logger_;
bool display_function_;
bool use_regular_expressions_;
Expand Down
1 change: 1 addition & 0 deletions include/swri_console/settings_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace swri_console
public:
static const QString DISPLAY_TIMESTAMPS;
static const QString ABSOLUTE_TIMESTAMPS;
static const QString HUMAN_READABLE_TIME;
static const QString DISPLAY_LOGGER;
static const QString DISPLAY_FUNCTION;
static const QString USE_REGEXPS;
Expand Down
6 changes: 6 additions & 0 deletions src/console_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ ConsoleWindow::ConsoleWindow(LogDatabase *db)
QObject::connect(ui.action_AbsoluteTimestamps, SIGNAL(toggled(bool)),
db_proxy_, SLOT(setAbsoluteTime(bool)));

QObject::connect(ui.action_Use_human_readable_time, SIGNAL(toggled(bool)),
db_proxy_, SLOT(setHumanReadableTime(bool)));

QObject::connect(ui.action_ShowTimestamps, SIGNAL(toggled(bool)),
db_proxy_, SLOT(setDisplayTime(bool)));

Expand Down Expand Up @@ -626,6 +629,7 @@ void ConsoleWindow::loadSettings()
// First, load all the boolean settings...
loadBooleanSetting(SettingsKeys::DISPLAY_TIMESTAMPS, ui.action_ShowTimestamps);
loadBooleanSetting(SettingsKeys::ABSOLUTE_TIMESTAMPS, ui.action_AbsoluteTimestamps);
loadBooleanSetting(SettingsKeys::HUMAN_READABLE_TIME, ui.action_Use_human_readable_time);
loadBooleanSetting(SettingsKeys::USE_REGEXPS, ui.action_RegularExpressions);
loadBooleanSetting(SettingsKeys::COLORIZE_LOGS, ui.action_ColorizeLogs);
loadBooleanSetting(SettingsKeys::FOLLOW_NEWEST, ui.checkFollowNewest);
Expand All @@ -634,11 +638,13 @@ void ConsoleWindow::loadSettings()
// into a single integer mask under the hood. First they have to be loaded from the settings,
// then set in the UI, then the mask has to actually be applied.
QSettings settings;
bool displayTimestamp = settings.value(SettingsKeys::DISPLAY_TIMESTAMPS, true).toBool();
bool showDebug = settings.value(SettingsKeys::SHOW_DEBUG, true).toBool();
bool showInfo = settings.value(SettingsKeys::SHOW_INFO, true).toBool();
bool showWarn = settings.value(SettingsKeys::SHOW_WARN, true).toBool();
bool showError = settings.value(SettingsKeys::SHOW_ERROR, true).toBool();
bool showFatal = settings.value(SettingsKeys::SHOW_FATAL, true).toBool();
ui.action_Use_human_readable_time->setEnabled(displayTimestamp);
ui.checkDebug->setChecked(showDebug);
ui.checkInfo->setChecked(showInfo);
ui.checkWarn->setChecked(showWarn);
Expand Down
48 changes: 40 additions & 8 deletions src/log_database_proxy_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <chrono>
#include <ctime>

#include <rclcpp/rclcpp.hpp>

//#include <rosbag2/logging.hpp>
//#include <rosbag2/types.hpp>
//#include <rosbag2/writer.hpp>
//#include <rosbag2_storage/logging.hpp>

#include <swri_console/log_database_proxy_model.h>
#include <swri_console/log_database.h>
#include <swri_console/settings_keys.h>
Expand All @@ -59,6 +57,7 @@ LogDatabaseProxyModel::LogDatabaseProxyModel(LogDatabase *db)
, colorize_logs_(true)
, display_time_(true)
, display_absolute_time_(false)
, human_readable_time_(false)
, display_logger_(true)
, display_function_(true)
, use_regular_expressions_(false)
Expand Down Expand Up @@ -110,6 +109,23 @@ void LogDatabaseProxyModel::setAbsoluteTime(bool absolute)
}
}

void LogDatabaseProxyModel::setHumanReadableTime(bool human_readable_time)
{
if (human_readable_time == human_readable_time_)
{
return;
}

human_readable_time_ = human_readable_time;

QSettings settings;
settings.setValue(SettingsKeys::HUMAN_READABLE_TIME, human_readable_time_);

if (display_time_ && msg_mapping_.size())
{
Q_EMIT dataChanged(index(0), index(msg_mapping_.size()));
}
}

void LogDatabaseProxyModel::setColorizeLogs(bool colorize_logs)
{
Expand Down Expand Up @@ -410,9 +426,25 @@ QVariant LogDatabaseProxyModel::data(

char stamp[128];
if (display_absolute_time_) {
snprintf(stamp, sizeof(stamp),
"%f",
item.stamp.seconds());
if (human_readable_time_) {
char date_str[std::size("yyyy-mm-dd hh:mm:ss")];
const time_t time = static_cast<time_t>(item.stamp.seconds());
int32_t milliseconds = static_cast<int>(1000.0 * (item.stamp.seconds() - static_cast<double>(item.stamp.seconds())));
std::strftime(std::data(date_str),
std::size(date_str),
"%F %T",
std::localtime(&time));
snprintf(stamp,
sizeof(stamp),
"%s:%03d",
date_str,
milliseconds);
} else {
snprintf(stamp,
sizeof(stamp),
"%f",
item.stamp.seconds());
}
} else {
rclcpp::Duration t = item.stamp - db_->minTime();

Expand Down
1 change: 1 addition & 0 deletions src/settings_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace swri_console
{
const QString SettingsKeys::DISPLAY_TIMESTAMPS = "Timestamps/DisplayTimestamps";
const QString SettingsKeys::ABSOLUTE_TIMESTAMPS = "Timestamps/AbsoluteTimestamps";
const QString SettingsKeys::HUMAN_READABLE_TIME = "Timestamps/HumanReadableTime";
const QString SettingsKeys::DISPLAY_LOGGER = "Identification/Logger";
const QString SettingsKeys::DISPLAY_FUNCTION = "Identification/Function";
const QString SettingsKeys::USE_REGEXPS = "Filters/UseRegexps";
Expand Down
27 changes: 26 additions & 1 deletion ui/console_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
<x>0</x>
<y>0</y>
<width>1035</width>
<height>24</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
Expand Down Expand Up @@ -326,6 +326,7 @@
<string>Options</string>
</property>
<addaction name="action_ShowTimestamps"/>
<addaction name="action_Use_human_readable_time"/>
<addaction name="action_AbsoluteTimestamps"/>
<addaction name="action_ShowLoggerName"/>
<addaction name="action_ShowFunctionName"/>
Expand Down Expand Up @@ -493,6 +494,14 @@
<string>Show function name</string>
</property>
</action>
<action name="action_Use_human_readable_time">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Use human readable time</string>
</property>
</action>
</widget>
<tabstops>
<tabstop>nodeList</tabstop>
Expand Down Expand Up @@ -536,5 +545,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>action_ShowTimestamps</sender>
<signal>triggered(bool)</signal>
<receiver>action_Use_human_readable_time</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>-1</x>
<y>-1</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 0e7ac45

Please sign in to comment.