kdecore: implement KDE_DEBUG_COLOR for KDebug

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-23 02:53:46 +03:00
parent f3905e1760
commit 04c84dfbc1
7 changed files with 50 additions and 17 deletions

View file

@ -33,6 +33,7 @@
#include <QFile>
#include <QMutex>
#include <unistd.h>
#include <stdio.h>
#include <syslog.h>
@ -55,9 +56,9 @@ static QByteArray kDebugHeader(const QByteArray &areaname, const char* const fil
QByteArray result(areaname);
if (kde_debug_methodname) {
result.append(" from ");
const QList<QByteArray> funcnamelist = QByteArray(funcinfo).split(' ');
const QList<QByteArray> funcinfolist = QByteArray(funcinfo).split(' ');
bool foundfunc = false;
foreach (const QByteArray &it, funcnamelist) {
foreach (const QByteArray &it, funcinfolist) {
if (it.contains('(') && it.contains(')')) {
result.append(it);
foundfunc = true;
@ -213,7 +214,40 @@ public:
protected:
qint64 writeData(const char* data, qint64 len) final
{
// TODO: KDE_COLOR_DEBUG
static const bool kde_debug_color = !qgetenv("KDE_DEBUG_COLOR").isEmpty();
if (kde_debug_color) {
static const bool isttyoutput = (
m_level == QtDebugMsg ? ::isatty(::fileno(stdout)) : ::isatty(::fileno(stderr))
);
if (isttyoutput) {
switch (m_level) {
// for reference:
// https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
case QtDebugMsg: {
::fprintf(stdout, "\033[0;32m%s: %s\033[0m\n", m_header.constData(), data);
::fflush(stdout);
break;
}
case QtWarningMsg: {
::fprintf(stdout, "\033[0;93m%s: %s\033[0m\n", m_header.constData(), data);
::fflush(stdout);
break;
}
case QtCriticalMsg: {
::fprintf(stdout, "\033[0;33m%s: %s\033[0m\n", m_header.constData(), data);
::fflush(stdout);
break;
}
case QtFatalMsg: {
::fprintf(stdout, "\033[0;31m%s: %s\033[0m\n", m_header.constData(), data);
::fflush(stdout);
break;
}
}
return len;
}
}
if (m_level == QtDebugMsg) {
::fprintf(stdout, "%s: %s\n", m_header.constData(), data);
::fflush(stdout);

View file

@ -30,12 +30,12 @@
* \addtogroup kdebug Debug message generators
* @{
* KDE debug message streams let you and the user control just how many debug
* messages you see. Debug message can be controled by editing kdebugrc.
*
* You can also control what you see: process name, area name, method name,
* file and line number, timestamp, etc. using environment variables:
* KDE_DEBUG_TIMESTAMP - adds timestamp to the message
* KDE_DEBUG_METHODNAME - adds the method to the message
* messages you see. Debug message can be controled by editing kdebugrc and
* by setting or unsetting environment variables:
* KDE_DEBUG_TIMESTAMP - adds timestamp to the message
* KDE_DEBUG_METHODNAME - adds the method to the message
* KDE_DEBUG_COLOR - colorizes the message, valid only for shell type of
* output and when it is TTY
*/
/**

View file

@ -57,7 +57,7 @@ void KDebugTest::cleanupTestCase()
{
::unsetenv("KDE_DEBUG_METHODNAME");
::unsetenv("KDE_DEBUG_TIMESTAMP");
::unsetenv("KDE_COLOR_DEBUG");
::unsetenv("KDE_DEBUG_COLOR");
}
void KDebugTest::output_data()
@ -84,11 +84,11 @@ void KDebugTest::output()
if (areafancy) {
::setenv("KDE_DEBUG_METHODNAME", "1", 1);
::setenv("KDE_DEBUG_TIMESTAMP", "1", 1);
::setenv("KDE_COLOR_DEBUG", "1", 1);
::setenv("KDE_DEBUG_COLOR", "1", 1);
} else {
::unsetenv("KDE_DEBUG_METHODNAME");
::unsetenv("KDE_DEBUG_TIMESTAMP");
::unsetenv("KDE_COLOR_DEBUG");
::unsetenv("KDE_DEBUG_COLOR");
}
const QString areafilename = QFile::encodeName(KDEBINDIR "/123.log");

View file

@ -79,7 +79,7 @@ int main(int argc, char *argv[]) \
setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test") ), 1); \
setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local") ), 1); \
setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") ), 1); \
unsetenv("KDE_COLOR_DEBUG"); \
unsetenv("KDE_DEBUG_COLOR"); \
QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/share/config/qttestrc")); \
KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
KComponentData cData(&aboutData); \
@ -128,7 +128,7 @@ int main(int argc, char *argv[]) \
setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test" )), 1); \
setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local") ), 1); \
setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") ), 1); \
unsetenv("KDE_COLOR_DEBUG"); \
unsetenv("KDE_DEBUG_COLOR"); \
QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/share/config/qttestrc")); \
KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
KComponentData cData(&aboutData); \

View file

@ -149,7 +149,7 @@ int main(int argc, char *argv[]) \
setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test") ), 1); \
setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test/xdg/local") ), 1); \
setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test/xdg/config") ), 1); \
unsetenv("KDE_COLOR_DEBUG"); \
unsetenv("KDE_DEBUG_COLOR"); \
QFile::remove(QDir::homePath() + QLatin1String("/.kde-unit-test/share/config/qttestrc")); \
KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
KCmdLineArgs::init( argc, argv, &aboutData); \

View file

@ -102,7 +102,7 @@ int main(int argc, char *argv[]) \
setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test") ), 1); \
setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test/xdg/local") ), 1); \
setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QLatin1String("/.kde-unit-test/xdg/config") ), 1); \
unsetenv("KDE_COLOR_DEBUG"); \
unsetenv("KDE_DEBUG_COLOR"); \
QFile::remove(QDir::homePath() + QLatin1String("/.kde-unit-test/share/config/qttestrc")); \
KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
KCmdLineArgs::init( argc, argv, &aboutData); \

View file

@ -107,7 +107,6 @@ int main(int argc, char *argv[])
{
setenv("LC_ALL", "C", 1);
// The difference with QTEST_KDEMAIN is here: not setting $KDEHOME
unsetenv("KDE_COLOR_DEBUG");
KAboutData aboutData( "ktimezonewidgettest", 0, ki18n("qttest"), "version" );
KComponentData cData(&aboutData);
QApplication app( argc, argv );