Skip to content

Added the ability to redirect logging to a callable. #6244

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
349 changes: 277 additions & 72 deletions common/include/pcl/console/print.h

Large diffs are not rendered by default.

324 changes: 76 additions & 248 deletions common/src/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
*/
#include <pcl/console/print.h>
#include <algorithm>
#include <cstdarg> // for va_list, va_start, va_end
#include <cstdio>
#include <cstdlib>
#include <cctype> // for toupper
#include <map>
#include <map> // For std::map
#include <string>
#include <boost/optional.hpp>

Expand Down Expand Up @@ -87,7 +87,6 @@ convertAttributesColor (int attribute, int fg, int bg=0)

#else
# include <unistd.h>
# include <cstdio>
#endif

// Map to store, for each output stream, whether to use colored output
Expand All @@ -110,6 +109,13 @@ useColoredOutput (FILE *stream)
return colored.get ();
}

pcl::console::Logger&
pcl::console::Logger::getInstance()
{
static Logger instance;
return instance;
}

////////////////////////////////////////////////////////////////////////////////
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template <typename Functor>
void pcl::console::Logger::setCallback(Functor&& callback){
      getInstance().setCallback(std::move(callback));
}

DISCLAIMER: draft, untested code.
@larshg wouldn't this work? Would the template be a problem there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want a free function like:


    template <typename Functor>
    void
    setCallback(Functor&& callback)
    {
      Logger::getInstance().setCallback(std::move(callback));
    }

so you can call:
pcl::console::setCallback(...);

Yes, thats possible.

void
pcl::console::enableColoredOutput (FILE *stream, bool enable)
Expand Down Expand Up @@ -168,212 +174,6 @@ pcl::console::reset_text_color (FILE *stream)
#endif
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_color (FILE *stream, int attr, int fg, const char *format, ...)
{
change_text_color (stream, attr, fg);
va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);

reset_text_color (stream);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_info (const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_INFO)) return;

reset_text_color (stdout);

va_list ap;

va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_info (FILE *stream, const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_INFO)) return;

reset_text_color (stream);

va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_highlight (const char *format, ...)
{
//if (!isVerbosityLevelEnabled (L_ALWAYS)) return;

change_text_color (stdout, TT_BRIGHT, TT_GREEN);
fprintf (stdout, "> ");
reset_text_color (stdout);

va_list ap;

va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_highlight (FILE *stream, const char *format, ...)
{
//if (!isVerbosityLevelEnabled (L_ALWAYS)) return;

change_text_color (stream, TT_BRIGHT, TT_GREEN);
fprintf (stream, "> ");
reset_text_color (stream);

va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_error (const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_ERROR)) return;

change_text_color (stderr, TT_BRIGHT, TT_RED);
va_list ap;

va_start (ap, format);
vfprintf (stderr, format, ap);
va_end (ap);

reset_text_color (stderr);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_error (FILE *stream, const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_ERROR)) return;

change_text_color (stream, TT_BRIGHT, TT_RED);
va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);

reset_text_color (stream);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_warn (const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_WARN)) return;

change_text_color (stderr, TT_BRIGHT, TT_YELLOW);
va_list ap;

va_start (ap, format);
vfprintf (stderr, format, ap);
va_end (ap);

reset_text_color (stderr);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_warn (FILE *stream, const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_WARN)) return;

change_text_color (stream, TT_BRIGHT, TT_YELLOW);
va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);

reset_text_color (stream);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_value (const char *format, ...)
{
//if (!isVerbosityLevelEnabled (L_ALWAYS)) return;

change_text_color (stdout, TT_RESET, TT_CYAN);
va_list ap;

va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);

reset_text_color (stdout);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_value (FILE *stream, const char *format, ...)
{
//if (!isVerbosityLevelEnabled (L_ALWAYS)) return;

change_text_color (stream, TT_RESET, TT_CYAN);
va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);

reset_text_color (stream);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_debug (const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_DEBUG)) return;

change_text_color (stdout, TT_RESET, TT_GREEN);
va_list ap;

va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);

reset_text_color (stdout);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print_debug (FILE *stream, const char *format, ...)
{
if (!isVerbosityLevelEnabled (L_DEBUG)) return;

change_text_color (stream, TT_RESET, TT_GREEN);
va_list ap;

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);

reset_text_color (stream);
}

////////////////////////////////////////////////////////////////////////////////
namespace pcl
{
Expand Down Expand Up @@ -456,68 +256,96 @@ pcl::console::initVerbosityLevel ()
return true;
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print (pcl::console::VERBOSITY_LEVEL level, FILE *stream, const char *format, ...)
pcl::console::Logger::print(FILE* stream, const LogRecord& logEntry)
{
if (!isVerbosityLevelEnabled (level)) return;
switch (level)
{
if (!pcl::console::isVerbosityLevelEnabled(logEntry.level))
return;

if (logcallback)
logcallback(logEntry);
else {
switch (logEntry.level) {
case L_DEBUG:
change_text_color (stream, TT_RESET, TT_GREEN);
change_text_color(stream, TT_RESET, TT_GREEN);
break;
case L_WARN:
change_text_color (stream, TT_BRIGHT, TT_YELLOW);
change_text_color(stream, TT_BRIGHT, TT_YELLOW);
break;
case L_ERROR:
change_text_color (stream, TT_BRIGHT, TT_RED);
change_text_color(stream, TT_BRIGHT, TT_RED);
break;
case L_ALWAYS:
case L_INFO:
case L_VERBOSE:
default:
break;
}
}

va_list ap;
fputs(logEntry.message.c_str(), stream);

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);
reset_text_color(stream);
}
}

reset_text_color (stream);
void
pcl::console::Logger::print(const LogRecord& logEntry)
{
FILE* stream =
(logEntry.level == L_WARN || logEntry.level == L_ERROR) ? stderr : stdout;

print(stream, logEntry);
}

////////////////////////////////////////////////////////////////////////////////
void
pcl::console::print (pcl::console::VERBOSITY_LEVEL level, const char *format, ...)
pcl::console::Logger::print_highlight(FILE* stream, const LogRecord& logEntry)
{
if (!isVerbosityLevelEnabled (level)) return;
FILE *stream = (level == L_WARN || level == L_ERROR) ? stderr : stdout;
switch (level)
{
case L_DEBUG:
change_text_color (stream, TT_RESET, TT_GREEN);
break;
case L_WARN:
change_text_color (stream, TT_BRIGHT, TT_YELLOW);
break;
case L_ERROR:
change_text_color (stream, TT_BRIGHT, TT_RED);
break;
case L_ALWAYS:
case L_INFO:
case L_VERBOSE:
default:
break;
if (logcallback)
logcallback(logEntry);
else {
change_text_color(stream, TT_BRIGHT, TT_GREEN);
fputs("> ", stream);
reset_text_color(stream);

fputs(logEntry.message.c_str(), stream);
}
}

void
pcl::console::Logger::print_highlight(const LogRecord& logEntry)
{
print_highlight(stdout, logEntry);
}

void
pcl::console::Logger::print_value(FILE* stream, const LogRecord& logEntry)
{
if (logcallback)
logcallback(logEntry);
else {
change_text_color(stream, TT_RESET, TT_CYAN);

va_list ap;
fputs(logEntry.message.c_str(), stream);

va_start (ap, format);
vfprintf (stream, format, ap);
va_end (ap);
reset_text_color(stream);
}
}

reset_text_color (stream);
void
pcl::console::Logger::print_value(const LogRecord& logEntry)
{
print_value(stdout, logEntry);
}

void
pcl::console::Logger::print_color(
FILE* stream, int attr, int fg, const LogRecord& logEntry)
{
if (logcallback)
logcallback(logEntry);
else {
change_text_color(stream, attr, fg);
fputs(logEntry.message.c_str(), stream);
reset_text_color(stream);
}
}
Loading