28 #include "kdebugdcopiface.h"
30 #include "tdeapplication.h"
31 #include "tdeglobal.h"
32 #include "kinstance.h"
33 #include "kstandarddirs.h"
35 #include <tqmessagebox.h>
38 #include <tqintdict.h>
40 #include <tqdatetime.h>
44 #include <tqstringlist.h>
58 #include <tdeconfig.h>
59 #include "kstaticdeleter.h"
71 KDebugEntry (
int n,
const TQCString& d) {number=n; descr=d;}
76 static TQIntDict<KDebugEntry> *KDebugCache;
80 static TQCString getDescrFromNum(
unsigned int _num)
83 kdd.
setObject(KDebugCache,
new TQIntDict<KDebugEntry>( 601 ));
86 KDebugCache->setAutoDelete(
true);
89 KDebugEntry *ent = KDebugCache->find( _num );
93 if ( !KDebugCache->isEmpty() )
96 TQString filename(
locate(
"config",
"kdebug.areas"));
97 if (filename.isEmpty())
100 TQFile file(filename);
101 if (!file.open(IO_ReadOnly)) {
102 tqWarning(
"Couldn't open %s", filename.local8Bit().data());
108 TQCString line(1024);
111 while (( len = file.readLine(line.data(),line.size()-1) ) > 0) {
115 while (line[i] && line[i] <=
' ')
118 unsigned char ch=line[i];
120 if ( !ch || ch ==
'#' || ch ==
'\n')
123 if (ch < '0' && ch >
'9') {
124 tqWarning(
"Syntax error: no number (line %u)",lineNumber);
128 const int numStart=i;
131 }
while ( ch >=
'0' && ch <=
'9');
133 const TQ_ULONG number =line.mid(numStart,i).toULong();
135 while (line[i] && line[i] <=
' ')
138 KDebugCache->insert(number,
new KDebugEntry(number, line.mid(i, len-i-1)));
142 ent = KDebugCache->find( _num );
157 struct kDebugPrivate {
159 oldarea(0), config(0) { }
161 ~kDebugPrivate() {
delete config; }
164 unsigned int oldarea;
168 static kDebugPrivate *kDebug_data = 0;
173 static void kDebugBackend(
unsigned short nLevel,
unsigned int nArea,
const char *data)
177 pcd.
setObject(kDebug_data,
new kDebugPrivate());
182 if (!kDebugDCOPIface)
188 if (!kDebug_data->config && TDEGlobal::_instance )
190 kDebug_data->config =
new TDEConfig(
"kdebugrc",
false,
false);
191 kDebug_data->config->setGroup(
"0");
195 if ( TDEGlobal::_instance )
199 if (kDebug_data->config && kDebug_data->oldarea != nArea) {
200 kDebug_data->config->setGroup( TQString::number(static_cast<int>(nArea)) );
201 kDebug_data->oldarea = nArea;
202 if ( nArea > 0 && TDEGlobal::_instance )
203 kDebug_data->aAreaName = getDescrFromNum(nArea);
204 if ((nArea == 0) || kDebug_data->aAreaName.isEmpty())
205 if ( TDEGlobal::_instance )
220 nPriority = LOG_INFO;
224 aCaption =
"Warning";
225 nPriority = LOG_WARNING;
229 aCaption =
"Fatal Error";
230 nPriority = LOG_CRIT;
242 short nOutput = kDebug_data->config ? kDebug_data->config->readNumEntry(key, 4) : 4;
246 if (!kapp && (nOutput == 1))
248 else if ( nOutput == 4 && nLevel != KDEBUG_FATAL )
251 const int BUFSIZE = 4096;
253 if ( !kDebug_data->aAreaName.isEmpty() ) {
254 strlcpy( buf, kDebug_data->aAreaName.data(), BUFSIZE );
255 strlcat( buf,
": ", BUFSIZE );
256 strlcat( buf, data, BUFSIZE );
259 strlcpy( buf, data, BUFSIZE );
271 aKey =
"InfoFilename";
274 aKey =
"WarnFilename";
277 aKey =
"FatalFilename";
281 aKey =
"ErrorFilename";
284 TQFile aOutputFile( kDebug_data->config->readPathEntry(aKey,
"kdebug.dbg") );
285 aOutputFile.open( (TQIODevice_OpenModeFlag)((
int)IO_WriteOnly | (
int)IO_Append | (
int)IO_Raw) );
286 aOutputFile.writeBlock( buf, strlen( buf ) );
294 if ( !kDebug_data->aAreaName.isEmpty() )
295 aCaption += TQString(
"(%1)").arg( QString(kDebug_data->aAreaName) );
296 TQMessageBox::warning( 0L, aCaption, data,
i18n(
"&OK") );
301 if (write( 2, buf, strlen( buf ) ) < 0) {
308 syslog( nPriority,
"%s", buf);
314 if( ( nLevel == KDEBUG_FATAL )
315 && ( !kDebug_data->config || kDebug_data->config->readNumEntry(
"AbortFatal", 1 ) ) )
319 kdbgstream &perror(
kdbgstream &s) {
return s << TQString(TQString::fromLocal8Bit(strerror(errno))); }
331 : output(str.output), area(str.area), level(str.level), print(str.print)
333 str.output.truncate(0);
337 if (output.isEmpty() || !print)
339 kDebugBackend( level, area, output.local8Bit().data() );
340 output = TQString::null;
347 va_start( arguments, format );
348 vsnprintf( buf,
sizeof(buf), format, arguments );
354 kdbgstream::~kdbgstream() {
355 if (!output.isEmpty()) {
356 fprintf(stderr,
"ASSERT: debug output not ended with \\n\n");
357 TQString backtrace = kdBacktrace();
358 if (backtrace.ascii() != NULL) {
359 fprintf(stderr,
"%s", backtrace.latin1());
367 if (!print)
return *
this;
369 output +=
"\\x" + TQString::number( static_cast<uint>( ch ), 16 ).rightJustify(2,
'0');
372 if (ch ==
'\n')
flush();
379 if (!print)
return *
this;
381 output +=
"\\x" + TQString::number( ch.unicode(), 16 ).rightJustify(2,
'0');
384 if (ch == QChar(
'\n'))
flush();
391 return *this << const_cast< const TQWidget* >( widget );
396 TQString string, temp;
400 string=(TQString)
"[Null pointer]";
402 temp.setNum((ulong)widget, 16);
403 string=(TQString)
"["+widget->className()+
" pointer "
404 +
"(0x" + temp +
")";
405 if(widget->name(0)==0)
407 string +=
" to unnamed widget, ";
409 string += (TQString)
" to widget " + widget->name() +
", ";
411 string +=
"geometry="
412 + TQString().setNum(widget->width())
413 +
"x"+TQString().setNum(widget->height())
414 +
"+"+TQString().setNum(widget->x())
415 +
"+"+TQString().setNum(widget->y())
423 if (output.at(output.length() -1 ) == QChar(
'\n'))
435 *
this << time.toString();
439 *
this << TQString(date.toString());
444 *
this << TQString(time.toString());
448 *
this <<
"(" << p.x() <<
", " << p.y() <<
")";
452 *
this <<
"[" << s.width() <<
"x" << s.height() <<
"]";
456 *
this <<
"[" << r.x() <<
"," << r.y() <<
" - " << r.width() <<
"x" << r.height() <<
"]";
462 TQMemArray<TQRect>rs=reg.rects();
463 for (uint i=0;i<rs.size();++i)
464 *
this << TQString(TQString(
"[%1,%2 - %3x%4] ").arg(rs[i].x()).arg(rs[i].y()).arg(rs[i].width()).arg(rs[i].height() )) ;
475 *
this << l.join(
",");
482 *
this << TQString(c.name());
484 *
this <<
"(invalid/default)";
488 static const char*
const s_penStyles[] = {
489 "NoPen",
"SolidLine",
"DashLine",
"DotLine",
"DashDotLine",
491 static const char*
const s_capStyles[] = {
492 "FlatCap",
"SquareCap",
"RoundCap" };
494 *
this << s_penStyles[ p.style() ];
498 if ( p.color().isValid() )
499 *
this << TQString(p.color().name());
501 *
this <<
"(invalid/default)";
504 *
this <<
" capstyle:";
505 *
this << s_capStyles[ p.capStyle() >> 4 ];
512 static const char*
const s_brushStyles[] = {
513 "NoBrush",
"SolidPattern",
"Dense1Pattern",
"Dense2Pattern",
"Dense3Pattern",
514 "Dense4Pattern",
"Dense5Pattern",
"Dense6Pattern",
"Dense7Pattern",
515 "HorPattern",
"VerPattern",
"CrossPattern",
"BDiagPattern",
"FDiagPattern",
516 "DiagCrossPattern" };
519 *
this <<s_brushStyles[ b.style() ];
522 if ( b.color().isValid() )
523 *
this << TQString(b.color().name()) ;
525 *
this <<
"(invalid/default)";
527 *
this <<
" has a pixmap";
533 *
this <<
"[variant: ";
534 *
this << v.typeName();
537 *
this <<
" toString=";
538 *
this << v.toString();
544 if (!print)
return *
this;
547 unsigned int sz = QMIN( data.size(), 64 );
548 for ( ; i < sz ; ++i ) {
549 output += TQString::number( (
unsigned char) data[i], 16 ).rightJustify(2,
'0');
553 if ( sz < data.size() )
562 #ifdef HAVE_BACKTRACE
564 int n = backtrace(trace, 256);
567 char** strings = backtrace_symbols (trace, n);
570 n = QMIN( n, levels );
573 for (
int i = 0; i < n; ++i)
574 s += TQString::number(i) +
575 TQString::fromLatin1(
": ") +
576 TQString::fromLatin1(strings[i]) + TQString::fromLatin1(
"\n");
592 delete kDebug_data->config;
593 kDebug_data->config = 0;
600 #define kdDebug kndDebug