Skip to content

Instantly share code, notes, and snippets.

@heisters
Created February 17, 2017 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heisters/974e6931cc3888a3e66f1dd8508ee3c1 to your computer and use it in GitHub Desktop.
Save heisters/974e6931cc3888a3e66f1dd8508ee3c1 to your computer and use it in GitHub Desktop.
Custom Cinder logger for use with ImGui
#include "UILogger.h"
MyApp::MyApp()
{
ci::log::makeLogger< ui::Logger >();
}
void MyApp::update() {
for ( auto & log : log::manager()->getLoggers< ui::Logger >() ) {
log->draw( "Log" );
}
}
#pragma once
#include "cinder/Log.h"
#include "CinderImGui.h"
namespace ImGui {
class Logger : public ci::log::Logger {
private:
ImGuiTextBuffer m_buffer;
ImGuiTextFilter m_filter;
ImVector<int> m_lineOffsets; // Index to lines offset
bool m_shouldScrollToBottom;
public:
void clear() { m_buffer.clear(); m_lineOffsets.clear(); }
void write( const ci::log::Metadata &meta, const std::string &text ) override
{
int old_size = m_buffer.size();
std::ostringstream stream;
writeDefault( stream, meta, text );
m_buffer.append( stream.str().c_str() );
for ( int new_size = m_buffer.size(); old_size < new_size; old_size++ )
if ( m_buffer[ old_size ] == '\n' )
m_lineOffsets.push_back( old_size );
m_shouldScrollToBottom = true;
}
void draw( const char* title, bool* p_opened = NULL )
{
ui::SetNextWindowSize( ImVec2( 500, 400 ), ImGuiSetCond_FirstUseEver );
ui::Begin( title, p_opened );
if ( ui::Button( "Clear" ) ) clear();
ui::SameLine();
bool copy = ui::Button( "Copy" );
ui::SameLine();
m_filter.Draw( "Filter", -100.0f );
ui::Separator();
ui::BeginChild( "scrolling" );
ui::PushStyleVar( ImGuiStyleVar_ItemSpacing, ImVec2( 0, 1 ) );
if ( copy ) ui::LogToClipboard();
if ( m_filter.IsActive() )
{
const char* buf_begin = m_buffer.begin();
const char* line = buf_begin;
for ( int line_no = 0; line != NULL; line_no++ )
{
const char* line_end = ( line_no < m_lineOffsets.Size ) ? buf_begin + m_lineOffsets[ line_no ] : NULL;
if ( m_filter.PassFilter( line, line_end ) )
ui::TextUnformatted( line, line_end );
line = line_end && line_end[ 1 ] ? line_end + 1 : NULL;
}
}
else
{
ui::TextUnformatted( m_buffer.begin() );
}
if ( m_shouldScrollToBottom )
ui::SetScrollHere( 1.0f );
m_shouldScrollToBottom = false;
ui::PopStyleVar();
ui::EndChild();
ui::End();
}
};
}
@heisters
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment