1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/* This file is part of the Boson game. It was originally part of the KDE libraries
Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
Copyright (C) 2002 Andreas Beckermann (b_mann@gmx.de)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
// Include our header without NDEBUG defined to avoid having the kDebugInfo
// functions inlined to noops (which would then conflict with their definition
// here).
#include "bodebug.h"
#include "bodebug.moc"
#ifdef NDEBUG
#undef boDebug
#undef boBacktrace
#endif
#include "bodebugdcopiface.h"
#include "bodebuglog.h"
#include <kapplication.h>
#include <kglobal.h>
#include <kinstance.h>
#include <kstandarddirs.h>
#include <qmessagebox.h>
#include <klocale.h>
#include <qfile.h>
#include <qintdict.h>
#include <qstring.h>
#include <qtextstream.h>
#include <stdlib.h> // abort
#include <unistd.h> // getpid
#include <stdarg.h> // vararg stuff
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <kconfig.h>
#include "kstaticdeleter.h"
#include <config.h>
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif
class BoDebugEntry;
class BoDebugEntry
{
public:
BoDebugEntry (int n, QString d) {number=n; descr=d;}
unsigned int number;
QString descr;
};
static QIntDict<BoDebugEntry> *KDebugCache;
static KStaticDeleter< QIntDict<BoDebugEntry> > kdd;
static QString getDescrFromNum(unsigned int _num)
{
if (!KDebugCache)
{
kdd.setObject(KDebugCache, new QIntDict<BoDebugEntry>);
// Do not call this deleter from ~KApplication
KGlobal::unregisterStaticDeleter(&kdd);
KDebugCache->setAutoDelete(true);
}
BoDebugEntry *ent = KDebugCache->find( _num );
if ( ent )
{
return ent->descr;
}
if ( !KDebugCache->isEmpty() ) // areas already loaded
{
return QString::null;
}
QString filename(locate("config","bodebug.areas"));
if (filename.isEmpty())
{
qDebug("oops - bodebug.areas not found! check your installation!");
QMessageBox::critical( 0L, i18n("Fatal error"), i18n("bodebug.areas not found!\nCheck your installation!"));
exit(1);
}
QFile file(filename);
if (!file.open(IO_ReadOnly))
{
qWarning("Couldn't open %s", filename.local8Bit().data());
file.close();
return "";
}
unsigned long number = 0;
bool longOK;
QTextStream *ts = new QTextStream(&file);
ts->setEncoding( QTextStream::Latin1 );
while (!ts->eof())
{
const QString data(ts->readLine());
int i = 0;
int len = data.length();
QChar ch = data[0];
if (ch == '#' || ch.isNull())
{
continue;
}
while (ch.isSpace())
{
if (!(i < len))
{
continue;
}
++i;
ch = data[i];
}
if (ch.isNumber())
{
int numStart = i ;
while (ch.isNumber())
{
if (!(i < len))
{
continue;
}
++i;
ch = data[i];
}
number = data.mid(numStart,i).toULong(&longOK);
}
while (ch.isSpace())
{
if (!(i < len))
{
continue;
}
++i;
ch = data[i];
}
const QString description(data.mid(i, len));
//qDebug("number: [%i] description: [%s]", number, description.latin1());
KDebugCache->insert(number, new BoDebugEntry(number,description));
}
delete ts;
file.close();
ent = KDebugCache->find( _num );
if ( ent )
{
return ent->descr;
}
return QString::null;
}
struct boDebugPrivate {
boDebugPrivate() :
oldarea(), config(0) { }
~boDebugPrivate() { delete config; }
QString aAreaName;
unsigned int oldarea;
KConfig *config;
};
static boDebugPrivate *boDebug_data = 0;
static KStaticDeleter<boDebugPrivate> pcd;
static void kDebugBackend( unsigned short nLevel, unsigned int nArea, const QString& _output)
{
if ( !boDebug_data )
{
pcd.setObject(boDebug_data, new boDebugPrivate());
// Do not call this deleter from ~KApplication
KGlobal::unregisterStaticDeleter(&pcd);
}
if (!boDebug_data->config && KGlobal::_instance )
{
boDebug_data->config = new KConfig("bodebugrc", false, false);
boDebug_data->config->setGroup("0");
//AB: this is necessary here, otherwise all output with area 0 won't be
//prefixed with anything, unless something with area != 0 is called before
if ( KGlobal::_instance )
{
boDebug_data->aAreaName = KGlobal::instance()->instanceName();
}
}
if (boDebug_data->config && boDebug_data->oldarea != nArea)
{
boDebug_data->config->setGroup( QString::number(static_cast<int>(nArea)) );
boDebug_data->oldarea = nArea;
if ( nArea > 0 && KGlobal::_instance )
{
boDebug_data->aAreaName = getDescrFromNum(nArea);
}
if ((nArea == 0) || boDebug_data->aAreaName.isEmpty())
{
if ( KGlobal::_instance )
{
boDebug_data->aAreaName = KGlobal::instance()->instanceName();
}
}
}
BoDebugLog* log = BoDebugLog::debugLog();
if (log)
{
log->addEntry(_output, nArea, boDebug_data->aAreaName, nLevel);
}
QString sOutput;
int nPriority = 0;
QString aCaption;
/* Determine output */
QString key;
switch( nLevel )
{
case BoDebug::KDEBUG_INFO:
key = "InfoOutput";
aCaption = "Info";
nPriority = LOG_INFO;
sOutput = "";
break;
case BoDebug::KDEBUG_WARN:
key = "WarnOutput";
aCaption = "Warning";
nPriority = LOG_WARNING;
sOutput = "WARNING: ";
break;
case BoDebug::KDEBUG_FATAL:
key = "FatalOutput";
aCaption = "Fatal Error";
nPriority = LOG_CRIT;
sOutput = "FATAL: ";
break;
case BoDebug::KDEBUG_ERROR:
default:
/* Programmer error, use "Error" as default */
key = "ErrorOutput";
aCaption = "Error";
nPriority = LOG_ERR;
sOutput = "ERROR: ";
break;
}
sOutput += _output;
QCString local8Bit = sOutput.local8Bit();
const char* data = local8Bit.data();
short nOutput = boDebug_data->config ? boDebug_data->config->readNumEntry(key, 2) : 2;
// If the application doesn't have a QApplication object it can't use
// a messagebox.
if (!kapp && (nOutput == 1))
{
nOutput = 2;
}
// Output
switch( nOutput )
{
case 0: // File
{
QString aKey;
switch( nLevel )
{
case BoDebug::KDEBUG_INFO:
aKey = "InfoFilename";
break;
case BoDebug::KDEBUG_WARN:
aKey = "WarnFilename";
break;
case BoDebug::KDEBUG_FATAL:
aKey = "FatalFilename";
break;
case BoDebug::KDEBUG_ERROR:
default:
aKey = "ErrorFilename";
break;
}
QString aOutputFileName = boDebug_data->config->readEntry(aKey, "bodebug.dbg");
const int BUFSIZE = 4096;
char buf[BUFSIZE] = "";
int nSize;
if ( !boDebug_data->aAreaName.isEmpty() )
{
nSize = snprintf( buf, BUFSIZE, "%s: %s", boDebug_data->aAreaName.ascii(), data);
}
else
{
nSize = snprintf( buf, BUFSIZE, "%s", data);
}
QFile aOutputFile( aOutputFileName );
aOutputFile.open( IO_WriteOnly | IO_Append );
if ( ( nSize == -1 ) || ( nSize >= BUFSIZE ) )
{
aOutputFile.writeBlock( buf, BUFSIZE-1 );
}
else
{
aOutputFile.writeBlock( buf, nSize );
}
aOutputFile.close();
break;
}
case 1: // Message Box
{
// Since we are in kdecore here, we cannot use KMsgBox and use
// QMessageBox instead
if ( !boDebug_data->aAreaName.isEmpty() )
{
aCaption += QString("(") + boDebug_data->aAreaName + ")";
}
QMessageBox::warning( 0L, aCaption, data, i18n("&OK") );
break;
}
case 2: // Shell
{
FILE *output;
/* we used to use stdout for debug
if (nPriority == LOG_INFO)
output = stderr;
else */
output = stderr;
// Uncomment this to get the pid of the app in the output (useful for e.g. kioslaves)
// if ( !boDebug_data->aAreaName.isEmpty() ) fprintf( output, "%d %s: ", (int)getpid(), boDebug_data->aAreaName.ascii() );
if ( !boDebug_data->aAreaName.isEmpty() )
{
fprintf( output, "%s: ", boDebug_data->aAreaName.ascii() );
}
fputs(data, output);
break;
}
case 3: // syslog
{
syslog( nPriority, "%s", data);
break;
}
case 4: // nothing
{
break;
}
case 5: // emit signal
{
// we won't emit anything unless BoDebug::mDebug is non-NULL.
// the user will need to call BoDebug::self() in order to connect
// any signal to BoDebug. So if it is NULL, the signal won't be
// used anyway.
if (BoDebug::selfNonCreate())
{
emit BoDebug::self()->emitSignal(boDebug_data->aAreaName, data, (BoDebug::DebugLevels)nLevel);
}
break;
}
}
// check if we should abort
if( ( nLevel == BoDebug::KDEBUG_FATAL )
&& ( !boDebug_data->config || boDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) )
{
abort();
}
}
bodbgstream &perror( bodbgstream &s) { return s << QString::fromLocal8Bit(strerror(errno)); }
bodbgstream boDebug(int area) { return bodbgstream(area, BoDebug::KDEBUG_INFO); }
bodbgstream boDebug(bool cond, int area) { if (cond) return bodbgstream(area, BoDebug::KDEBUG_INFO); else return bodbgstream(0, 0, false); }
bodbgstream boError(int area) { return bodbgstream(area, BoDebug::KDEBUG_ERROR); }
bodbgstream boError(bool cond, int area) { if (cond) return bodbgstream(area, BoDebug::KDEBUG_ERROR); else return bodbgstream(0,0,false); }
bodbgstream boWarning(int area) { return bodbgstream(area, BoDebug::KDEBUG_WARN); }
bodbgstream boWarning(bool cond, int area) { if (cond) return bodbgstream(area, BoDebug::KDEBUG_WARN); else return bodbgstream(0,0,false); }
bodbgstream boFatal(int area) { return bodbgstream(area, BoDebug::KDEBUG_FATAL); }
bodbgstream boFatal(bool cond, int area) { if (cond) return bodbgstream(area, BoDebug::KDEBUG_FATAL); else return bodbgstream(0,0,false); }
void bodbgstream::flush()
{
if (output.isEmpty() || !print)
{
return;
}
kDebugBackend( level, area, output );
output = QString::null;
}
bodbgstream &bodbgstream::form(const char *format, ...)
{
char buf[4096];
va_list arguments;
va_start( arguments, format );
vsnprintf( buf, sizeof(buf), format, arguments );
va_end(arguments);
*this << buf;
return *this;
}
bodbgstream::~bodbgstream()
{
if (!output.isEmpty())
{
fprintf(stderr, "ASSERT: debug output not ended with \\n\n");
*this << "\n";
}
}
bodbgstream& bodbgstream::operator << (QWidget* widget)
{
QString string, temp;
// -----
if(widget==0)
{
string=(QString)"[Null pointer]";
}
else
{
temp.setNum((ulong)widget, 16);
string=(QString)"["+widget->className()+" pointer "
+ "(0x" + temp + ")";
if(widget->name(0)==0)
{
string += " to unnamed widget, ";
}
else
{
string += (QString)" to widget " + widget->name() + ", ";
}
string += "geometry="
+ QString().setNum(widget->width())
+ "x"+QString().setNum(widget->height())
+ "+"+QString().setNum(widget->x())
+ "+"+QString().setNum(widget->y())
+ "]";
}
if (!print)
{
return *this;
}
output += string;
if (output.at(output.length() -1 ) == '\n')
{
flush();
}
return *this;
}
QString boBacktrace(int levels)
{
QString s;
#ifdef HAVE_BACKTRACE
void* trace[256];
int n = backtrace(trace, 256);
char** strings = backtrace_symbols (trace, n);
if ( levels != -1 )
n = QMIN( n, levels );
s = "[\n";
for (int i = 0; i < n; ++i)
s += QString::number(i) +
QString::fromLatin1(": ") +
QString::fromLatin1(strings[i]) + QString::fromLatin1("\n");
s += "]\n";
free (strings);
#endif
return s;
}
QString boBacktrace()
{
return boBacktrace(-1 /*all*/);
}
void boClearDebugConfig()
{
delete boDebug_data->config;
boDebug_data->config = 0;
}
static KStaticDeleter<BoDebug> sd;
BoDebug* BoDebug::mDebug = 0;
BoDebug::BoDebug() : QObject(0)
{
}
BoDebug::~BoDebug()
{
}
BoDebug* BoDebug::self()
{
if (!mDebug)
{
mDebug = new BoDebug();
sd.setObject(mDebug);
}
return mDebug;
}
BoDebug* BoDebug::selfNonCreate()
{
return mDebug;
}
// Needed for --enable-final
#ifdef NDEBUG
#define boDebug kndDebug
#endif
/*
* vim: et sw=2
*/
|