quill icon indicating copy to clipboard operation
quill copied to clipboard

Console colours with custom formatter

Open mrcakb opened this issue 2 years ago • 1 comments

Hello,

I am trying to apply console colour with custom formatter. I looked in example and tried but i got an error:

GetConsoleScreenBufferInfo failed with error message "The handle is invalid.", errno "6"

Example code is below:

quill::ConsoleColours console_colours;
    console_colours.set_default_colours();
    std::shared_ptr<quill::Handler> coloury = quill::stdout_handler("coloury", console_colours);
    coloury->set_pattern(log_formatter_[0].toStdString(), log_formatter_[1].toStdString(), quill::Timezone::GmtTime);

    quill::Config cfg;
    cfg.default_handlers = {coloury};
    quill::configure(cfg);

    logger_ = quill::create_logger("quill");
    quill::start();

Also i tried to call :

static_cast<quill::ConsoleHandler*>(coloury.get())->enable_console_colours();

But it takes no effect. What am I missing?

Thanks, Mirac

mrcakb avatar Jan 04 '24 15:01 mrcakb

Where did you get this code from?

You should use example_console_colours_with_custom_formatter.cpp example.

Are you sure sure your console allows colors? Are you running your application from a command prompt? Try doing everything exactly the same, but without colors.

AlemSnyder avatar Jan 27 '24 19:01 AlemSnyder

I tried the above code on latest master and it works for me without any issues on Linux and I can see the console colours. What operating system are you using ?

Edit: okay it seems i have to check on windows .. GetConsoleScreenBufferInfo is for windows only

This is what i tried

int main()
{
  quill::ConsoleColours console_colours;
  console_colours.set_default_colours();
  std::shared_ptr<quill::Handler> coloury = quill::stdout_handler("coloury", console_colours);
  coloury->set_pattern("%(ascii_time) [%(process)] [%(thread)] %(logger_name) - %(message)", // format
                       "%Y-%m-%d %H:%M:%S.%Qms",  // timestamp format
                       quill::Timezone::GmtTime); // timestamp's timezone

  quill::Config cfg;
  cfg.default_handlers = {coloury};
  quill::configure(cfg);
  
  // Start the logging backend thread
  quill::start();

  quill::Logger* logger = quill::create_logger("quill");

  // Change the LogLevel to print everything
  logger->set_log_level(quill::LogLevel::TraceL3);

  // We also use backtrace
  logger->init_backtrace(2, quill::LogLevel::Critical);
  LOG_BACKTRACE(logger, "Backtrace log {}", 1);
  LOG_BACKTRACE(logger, "Backtrace log {}", 2);

  LOG_TRACE_L3(logger, "This is a log trace l3 example {}", 1);
  LOG_TRACE_L2(logger, "This is a log trace l2 example {} {}", 2, 2.3);
  LOG_TRACE_L1(logger, "This is a log trace l1 example {}", 3);
  LOG_DEBUG(logger, "This is a log debug example {}", 4);
  LOG_INFO(logger, "This is a log info example {}", 5);
  LOG_WARNING(logger, "This is a log warning example {}", 6);
  LOG_ERROR(logger, "This is a log error example {}", 7);
  LOG_CRITICAL(logger, "This is a log critical example {}", 8);
}

odygrd avatar Mar 14 '24 09:03 odygrd

I tried the code you provided on VS Studio 2022 and it is working for me

image

odygrd avatar Mar 15 '24 23:03 odygrd

closing this, please reopen if it is still an issue

odygrd avatar Mar 23 '24 12:03 odygrd