Skip to content

Commit

Permalink
refactor(tools): add VerbosityFilter in LogState
Browse files Browse the repository at this point in the history
  • Loading branch information
berdal84 committed Jan 18, 2025
1 parent 092710a commit 4acb419
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/tools/core/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ LogState& tools::get_log_state()
return state;
}

bool tools::show_log_message(const MessageData& message, Verbosity filter)
bool tools::show_log_message(const MessageData& message, const VerbosityFilter& filter)
{
if ( message.verbosity <= get_log_verbosity( message.category ) )
if ( message.verbosity == filter || filter == Verbosity_FilterAll )
return true;
return filter.data[message.verbosity];
return false;
}

Expand Down
29 changes: 27 additions & 2 deletions src/tools/core/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,31 @@ namespace tools
Verbosity_Diagnostic = 3, // highest level (rarely logged)

Verbosity_COUNT,
Verbosity_FilterAll = -1
};

struct VerbosityFilter
{
bool data[Verbosity_COUNT]; // TODO: we could use flags

VerbosityFilter(bool default_value = false)
{
reset_all(default_value);
}

bool all_checked() const
{
for( int i = 0; i < Verbosity_COUNT; ++i )
if ( !data[i] )
return false;
return true;
}

void reset_all(bool default_value = false)
{
for( int i = 0; i < Verbosity_COUNT; ++i )
data[i] = default_value;
}

};

struct MessageData
Expand All @@ -74,6 +98,7 @@ namespace tools
Verbosity verbosity = TOOLS_LOG_VERBOSITY_DEFAULT;
std::map<std::string, Verbosity> verbosity_by_category = {};
std::deque<MessageData> messages = {};
VerbosityFilter verbosity_filter{true}; // true => checked by default
};

LogState& get_log_state();
Expand All @@ -82,7 +107,7 @@ namespace tools
Verbosity get_log_verbosity(const char* category);
static Verbosity get_log_verbosity() { return get_log_state().verbosity; }
void flush(); // Ensure all messages have been printed out
bool show_log_message(const MessageData&, Verbosity filter); // return true if messages needs to be displayed depending on filter and global verbosity
bool show_log_message(const MessageData&, const VerbosityFilter& filter); // return true if messages needs to be displayed depending on filter and global verbosity

template<typename...Args>
void log(Verbosity verbosity, const char* category, const char* format, Args... args) // print a message like "[time|verbosity|category] message"
Expand Down
16 changes: 10 additions & 6 deletions src/tools/gui/AppView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,27 @@ void AppView::begin_draw()
if ( ImGui::Begin( k_status_window_name ) && !get_log_state().messages.empty())
{
const float line_height = ImGui::GetTextLineHeightWithSpacing();
static tools::Verbosity verbosity_filter = Verbosity_FilterAll;

if ( ImGui::BeginChild("filters", ImVec2(-1, line_height * 1.2f )) )
{
ImGui::BeginGroup();
ImGui::Text("Filter Messages: "); ImGui::SameLine();

auto draw_filter = [](const char* label, tools::Verbosity verbosity)
auto draw_filter = [&](const char* label, tools::Verbosity verbosity)
{
bool checked = verbosity_filter == verbosity;
ImGui::Checkbox(label, &get_log_state().verbosity_filter.data[verbosity] );
};

auto draw_filter_all = [&](const char* label)
{
bool checked = get_log_state().verbosity_filter.all_checked();
if ( ImGui::Checkbox(label, &checked ) )
{
verbosity_filter = verbosity;
get_log_state().verbosity_filter.reset_all(checked);
}
};

draw_filter("All" , Verbosity_FilterAll ); ImGui::SameLine();
draw_filter_all("All" ); ImGui::SameLine();
draw_filter("Errors" , Verbosity_Error ); ImGui::SameLine();
draw_filter("Warnings" , Verbosity_Warning ); ImGui::SameLine();
draw_filter("Messages" , Verbosity_Message ); ImGui::SameLine();
Expand All @@ -420,7 +424,7 @@ void AppView::begin_draw()
while ( message_displayed_count < message_to_display_count && it != get_log_state().messages.rend() )
{
const MessageData& message = *it;
if ( show_log_message( message, verbosity_filter ) )
if ( show_log_message( message, get_log_state().verbosity_filter ) )
{
ImRect line_rect{
ImGui::GetCursorScreenPos(),
Expand Down

0 comments on commit 4acb419

Please sign in to comment.