Skip to content
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

Can you add support for colored output to console? #408

Closed
jdukat opened this issue Sep 20, 2016 · 2 comments
Closed

Can you add support for colored output to console? #408

jdukat opened this issue Sep 20, 2016 · 2 comments

Comments

@jdukat
Copy link

jdukat commented Sep 20, 2016

Linux shell and Windows10 console output can be colorized in a standard way. Can you add support and configurability for this?

@moritzhoewer
Copy link

moritzhoewer commented Oct 6, 2016

You can use the #define ELPP_CUSTOM_COUT_LINE(msg) as a workaround

add a helper-method like this somewhere in your code

// Colors for output
#define RESET "\033[0m"
#define RED "\033[31m"
#define ORANGE "\033[33m"
#define GRAY "\033[90m"

std::string make_coloured(std::string msg){
    std::string prefix = msg.substr(0, 7);
    if(prefix == "[ERROR]"){
        msg = RED + msg + RESET;
    } else if(prefix == "[WARN ]"){
        msg = ORANGE + msg + RESET;
    } else if(prefix == "[TRACE]"){
        msg = GRAY + msg + RESET;
    }
    return msg;
}

and then before you #include "easylogging++.h" you do
#define ELPP_CUSTOM_COUT_LINE(msg) make_coloured(msg)
My log messages start with [%level], so I can use that to get the logging level in my helper function.

I had some more options to set before the #include, and it started getting messy so I just put it all in a "custom header" and included that instead of "easylogging++.h"

#ifndef CUSTOM_EASYLOGGING_H
#define CUSTOM_EASYLOGGING_H

// declare my custom output method
std::string make_coloured(std::string msg);

// easylogging options
#define ELPP_CUSTOM_COUT_LINE(msg) make_coloured(msg)
#define ELPP_DISABLE_DEFAULT_CRASH_HANDLING

// include actual easylogging
#include "easylogging++.h"

// initialize
INITIALIZE_EASYLOGGINGPP

#endif //CUSTOM_EASYLOGGING_H

@jdukat jdukat closed this as completed Oct 7, 2016
@chausner
Copy link

chausner commented Sep 30, 2023

Version 9.26 introduced inbuilt support for this via the LoggingFlag::ColoredTerminalOutput setting. However, the colors for the different log levels are currently hardcoded and cannot be changed. Also, it appears to target Linux terminals and Cygwin and does not work out-of-the-box with Windows Terminal yet. So the above approach may still be a viable alternative, depending on your needs.

I opened a PR to improve the compatibility on Windows: #843

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants