• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kdebug.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
3  2002 Holger Freyther (freyther@kde.org)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kdebug.h"
22 
23 #ifdef NDEBUG
24 #undef kdDebug
25 #undef kdBacktrace
26 #endif
27 
28 #include "kdebugdcopiface.h"
29 
30 #include "tdeapplication.h"
31 #include "tdeglobal.h"
32 #include "kinstance.h"
33 #include "kstandarddirs.h"
34 
35 #include <tqmessagebox.h>
36 #include <tdelocale.h>
37 #include <tqfile.h>
38 #include <tqintdict.h>
39 #include <tqstring.h>
40 #include <tqdatetime.h>
41 #include <tqpoint.h>
42 #include <tqrect.h>
43 #include <tqregion.h>
44 #include <tqstringlist.h>
45 #include <tqpen.h>
46 #include <tqbrush.h>
47 #include <tqsize.h>
48 
49 #include <kurl.h>
50 
51 #include <stdlib.h> // abort
52 #include <unistd.h> // getpid
53 #include <stdarg.h> // vararg stuff
54 #include <ctype.h> // isprint
55 #include <syslog.h>
56 #include <errno.h>
57 #include <string.h>
58 #include <tdeconfig.h>
59 #include "kstaticdeleter.h"
60 #include <config.h>
61 
62 #ifdef HAVE_BACKTRACE
63 #include <execinfo.h>
64 #endif
65 
66 class KDebugEntry;
67 
68 class KDebugEntry
69 {
70 public:
71  KDebugEntry (int n, const TQCString& d) {number=n; descr=d;}
72  unsigned int number;
73  TQCString descr;
74 };
75 
76 static TQIntDict<KDebugEntry> *KDebugCache;
77 
78 static KStaticDeleter< TQIntDict<KDebugEntry> > kdd;
79 
80 static TQCString getDescrFromNum(unsigned int _num)
81 {
82  if (!KDebugCache) {
83  kdd.setObject(KDebugCache, new TQIntDict<KDebugEntry>( 601 ));
84  // Do not call this deleter from ~TDEApplication
85  TDEGlobal::unregisterStaticDeleter(&kdd);
86  KDebugCache->setAutoDelete(true);
87  }
88 
89  KDebugEntry *ent = KDebugCache->find( _num );
90  if ( ent )
91  return ent->descr;
92 
93  if ( !KDebugCache->isEmpty() ) // areas already loaded
94  return TQCString();
95 
96  TQString filename(locate("config","kdebug.areas"));
97  if (filename.isEmpty())
98  return TQCString();
99 
100  TQFile file(filename);
101  if (!file.open(IO_ReadOnly)) {
102  tqWarning("Couldn't open %s", filename.local8Bit().data());
103  file.close();
104  return TQCString();
105  }
106 
107  uint lineNumber=0;
108  TQCString line(1024);
109  int len;
110 
111  while (( len = file.readLine(line.data(),line.size()-1) ) > 0) {
112  int i=0;
113  ++lineNumber;
114 
115  while (line[i] && line[i] <= ' ')
116  i++;
117 
118  unsigned char ch=line[i];
119 
120  if ( !ch || ch =='#' || ch =='\n')
121  continue; // We have an eof, a comment or an empty line
122 
123  if (ch < '0' && ch > '9') {
124  tqWarning("Syntax error: no number (line %u)",lineNumber);
125  continue;
126  }
127 
128  const int numStart=i;
129  do {
130  ch=line[++i];
131  } while ( ch >= '0' && ch <= '9');
132 
133  const TQ_ULONG number =line.mid(numStart,i).toULong();
134 
135  while (line[i] && line[i] <= ' ')
136  i++;
137 
138  KDebugCache->insert(number, new KDebugEntry(number, line.mid(i, len-i-1)));
139  }
140  file.close();
141 
142  ent = KDebugCache->find( _num );
143  if ( ent )
144  return ent->descr;
145 
146  return TQCString();
147 }
148 
149 enum DebugLevels {
150  KDEBUG_INFO= 0,
151  KDEBUG_WARN= 1,
152  KDEBUG_ERROR= 2,
153  KDEBUG_FATAL= 3
154 };
155 
156 
157 struct kDebugPrivate {
158  kDebugPrivate() :
159  oldarea(0), config(0) { }
160 
161  ~kDebugPrivate() { delete config; }
162 
163  TQCString aAreaName;
164  unsigned int oldarea;
165  TDEConfig *config;
166 };
167 
168 static kDebugPrivate *kDebug_data = 0;
169 static KStaticDeleter<kDebugPrivate> pcd;
170 static KStaticDeleter<KDebugDCOPIface> dcopsd;
171 static KDebugDCOPIface* kDebugDCOPIface = 0;
172 
173 static void kDebugBackend( unsigned short nLevel, unsigned int nArea, const char *data)
174 {
175  if ( !kDebug_data )
176  {
177  pcd.setObject(kDebug_data, new kDebugPrivate());
178  // Do not call this deleter from ~TDEApplication
179  TDEGlobal::unregisterStaticDeleter(&pcd);
180 
181  // create the dcop interface if it has not been created yet
182  if (!kDebugDCOPIface)
183  {
184  kDebugDCOPIface = dcopsd.setObject(kDebugDCOPIface, new KDebugDCOPIface);
185  }
186  }
187 
188  if (!kDebug_data->config && TDEGlobal::_instance )
189  {
190  kDebug_data->config = new TDEConfig("kdebugrc", false, false);
191  kDebug_data->config->setGroup("0");
192 
193  //AB: this is necessary here, otherwise all output with area 0 won't be
194  //prefixed with anything, unless something with area != 0 is called before
195  if ( TDEGlobal::_instance )
196  kDebug_data->aAreaName = TDEGlobal::instance()->instanceName();
197  }
198 
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 )
206  kDebug_data->aAreaName = TDEGlobal::instance()->instanceName();
207  }
208 
209  int nPriority = 0;
210  TQString aCaption;
211 
212  /* Determine output */
213 
214  TQString key;
215  switch( nLevel )
216  {
217  case KDEBUG_INFO:
218  key = "InfoOutput";
219  aCaption = "Info";
220  nPriority = LOG_INFO;
221  break;
222  case KDEBUG_WARN:
223  key = "WarnOutput";
224  aCaption = "Warning";
225  nPriority = LOG_WARNING;
226  break;
227  case KDEBUG_FATAL:
228  key = "FatalOutput";
229  aCaption = "Fatal Error";
230  nPriority = LOG_CRIT;
231  break;
232  case KDEBUG_ERROR:
233  default:
234  /* Programmer error, use "Error" as default */
235  key = "ErrorOutput";
236  aCaption = "Error";
237  nPriority = LOG_ERR;
238  break;
239  }
240 
241  // if no output mode is specified default to no debug output
242  short nOutput = kDebug_data->config ? kDebug_data->config->readNumEntry(key, 4) : 4;
243 
244  // If the application doesn't have a TQApplication object it can't use
245  // a messagebox.
246  if (!kapp && (nOutput == 1))
247  nOutput = 2;
248  else if ( nOutput == 4 && nLevel != KDEBUG_FATAL )
249  return;
250 
251  const int BUFSIZE = 4096;
252  char buf[BUFSIZE];
253  if ( !kDebug_data->aAreaName.isEmpty() ) {
254  strlcpy( buf, kDebug_data->aAreaName.data(), BUFSIZE );
255  strlcat( buf, ": ", BUFSIZE );
256  strlcat( buf, data, BUFSIZE );
257  }
258  else
259  strlcpy( buf, data, BUFSIZE );
260 
261 
262  // Output
263  switch( nOutput )
264  {
265  case 0: // File
266  {
267  const char* aKey;
268  switch( nLevel )
269  {
270  case KDEBUG_INFO:
271  aKey = "InfoFilename";
272  break;
273  case KDEBUG_WARN:
274  aKey = "WarnFilename";
275  break;
276  case KDEBUG_FATAL:
277  aKey = "FatalFilename";
278  break;
279  case KDEBUG_ERROR:
280  default:
281  aKey = "ErrorFilename";
282  break;
283  }
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 ) );
287  aOutputFile.close();
288  break;
289  }
290  case 1: // Message Box
291  {
292  // Since we are in tdecore here, we cannot use KMsgBox and use
293  // TQMessageBox instead
294  if ( !kDebug_data->aAreaName.isEmpty() )
295  aCaption += TQString("(%1)").arg( QString(kDebug_data->aAreaName) );
296  TQMessageBox::warning( 0L, aCaption, data, i18n("&OK") );
297  break;
298  }
299  case 2: // Shell
300  {
301  if (write( 2, buf, strlen( buf ) ) < 0) { //fputs( buf, stderr );
302  // ERROR
303  }
304  break;
305  }
306  case 3: // syslog
307  {
308  syslog( nPriority, "%s", buf);
309  break;
310  }
311  }
312 
313  // check if we should abort
314  if( ( nLevel == KDEBUG_FATAL )
315  && ( !kDebug_data->config || kDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) )
316  abort();
317 }
318 
319 kdbgstream &perror( kdbgstream &s) { return s << TQString(TQString::fromLocal8Bit(strerror(errno))); }
320 kdbgstream kdDebug(int area) { return kdbgstream(area, KDEBUG_INFO); }
321 kdbgstream kdDebug(bool cond, int area) { if (cond) return kdbgstream(area, KDEBUG_INFO); else return kdbgstream(0, 0, false); }
322 
323 kdbgstream kdError(int area) { return kdbgstream("ERROR: ", area, KDEBUG_ERROR); }
324 kdbgstream kdError(bool cond, int area) { if (cond) return kdbgstream("ERROR: ", area, KDEBUG_ERROR); else return kdbgstream(0,0,false); }
325 kdbgstream kdWarning(int area) { return kdbgstream("WARNING: ", area, KDEBUG_WARN); }
326 kdbgstream kdWarning(bool cond, int area) { if (cond) return kdbgstream("WARNING: ", area, KDEBUG_WARN); else return kdbgstream(0,0,false); }
327 kdbgstream kdFatal(int area) { return kdbgstream("FATAL: ", area, KDEBUG_FATAL); }
328 kdbgstream kdFatal(bool cond, int area) { if (cond) return kdbgstream("FATAL: ", area, KDEBUG_FATAL); else return kdbgstream(0,0,false); }
329 
330 kdbgstream::kdbgstream(kdbgstream &str)
331  : output(str.output), area(str.area), level(str.level), print(str.print)
332 {
333  str.output.truncate(0);
334 }
335 
336 void kdbgstream::flush() {
337  if (output.isEmpty() || !print)
338  return;
339  kDebugBackend( level, area, output.local8Bit().data() );
340  output = TQString::null;
341 }
342 
343 kdbgstream &kdbgstream::form(const char *format, ...)
344 {
345  char buf[4096];
346  va_list arguments;
347  va_start( arguments, format );
348  vsnprintf( buf, sizeof(buf), format, arguments );
349  va_end(arguments);
350  *this << buf;
351  return *this;
352 }
353 
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());
360  }
361  *this << "\n";
362  }
363 }
364 
365 kdbgstream& kdbgstream::operator << (char ch)
366 {
367  if (!print) return *this;
368  if (!isprint(ch))
369  output += "\\x" + TQString::number( static_cast<uint>( ch ), 16 ).rightJustify(2, '0');
370  else {
371  output += ch;
372  if (ch == '\n') flush();
373  }
374  return *this;
375 }
376 
377 kdbgstream& kdbgstream::operator << (TQChar ch)
378 {
379  if (!print) return *this;
380  if (!ch.isPrint())
381  output += "\\x" + TQString::number( ch.unicode(), 16 ).rightJustify(2, '0');
382  else {
383  output += ch;
384  if (ch == QChar('\n')) flush();
385  }
386  return *this;
387 }
388 
389 kdbgstream& kdbgstream::operator << (TQWidget* widget)
390 {
391  return *this << const_cast< const TQWidget* >( widget );
392 }
393 
394 kdbgstream& kdbgstream::operator << (const TQWidget* widget)
395 {
396  TQString string, temp;
397  // -----
398  if(widget==0)
399  {
400  string=(TQString)"[Null pointer]";
401  } else {
402  temp.setNum((ulong)widget, 16);
403  string=(TQString)"["+widget->className()+" pointer "
404  + "(0x" + temp + ")";
405  if(widget->name(0)==0)
406  {
407  string += " to unnamed widget, ";
408  } else {
409  string += (TQString)" to widget " + widget->name() + ", ";
410  }
411  string += "geometry="
412  + TQString().setNum(widget->width())
413  + "x"+TQString().setNum(widget->height())
414  + "+"+TQString().setNum(widget->x())
415  + "+"+TQString().setNum(widget->y())
416  + "]";
417  }
418  if (!print)
419  {
420  return *this;
421  }
422  output += string;
423  if (output.at(output.length() -1 ) == QChar('\n'))
424  {
425  flush();
426  }
427  return *this;
428 }
429 /*
430  * either use 'output' directly and do the flush if needed
431  * or use the TQString operator which calls the char* operator
432  *
433  */
434 kdbgstream& kdbgstream::operator<<( const TQDateTime& time) {
435  *this << time.toString();
436  return *this;
437 }
438 kdbgstream& kdbgstream::operator<<( const TQDate& date) {
439  *this << TQString(date.toString());
440 
441  return *this;
442 }
443 kdbgstream& kdbgstream::operator<<( const TQTime& time ) {
444  *this << TQString(time.toString());
445  return *this;
446 }
447 kdbgstream& kdbgstream::operator<<( const TQPoint& p ) {
448  *this << "(" << p.x() << ", " << p.y() << ")";
449  return *this;
450 }
451 kdbgstream& kdbgstream::operator<<( const TQSize& s ) {
452  *this << "[" << s.width() << "x" << s.height() << "]";
453  return *this;
454 }
455 kdbgstream& kdbgstream::operator<<( const TQRect& r ) {
456  *this << "[" << r.x() << "," << r.y() << " - " << r.width() << "x" << r.height() << "]";
457  return *this;
458 }
459 kdbgstream& kdbgstream::operator<<( const TQRegion& reg ) {
460  *this<< "[ ";
461 
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() )) ;
465 
466  *this <<"]";
467  return *this;
468 }
469 kdbgstream& kdbgstream::operator<<( const KURL& u ) {
470  *this << u.prettyURL();
471  return *this;
472 }
473 kdbgstream& kdbgstream::operator<<( const TQStringList& l ) {
474  *this << "(";
475  *this << l.join(",");
476  *this << ")";
477 
478  return *this;
479 }
480 kdbgstream& kdbgstream::operator<<( const TQColor& c ) {
481  if ( c.isValid() )
482  *this << TQString(c.name());
483  else
484  *this << "(invalid/default)";
485  return *this;
486 }
487 kdbgstream& kdbgstream::operator<<( const TQPen& p ) {
488  static const char* const s_penStyles[] = {
489  "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine",
490  "DashDotDotLine" };
491  static const char* const s_capStyles[] = {
492  "FlatCap", "SquareCap", "RoundCap" };
493  *this << "[ style:";
494  *this << s_penStyles[ p.style() ];
495  *this << " width:";
496  *this << p.width();
497  *this << " color:";
498  if ( p.color().isValid() )
499  *this << TQString(p.color().name());
500  else
501  *this <<"(invalid/default)";
502  if ( p.width() > 0 ) // cap style doesn't matter, otherwise
503  {
504  *this << " capstyle:";
505  *this << s_capStyles[ p.capStyle() >> 4 ];
506  // join style omitted
507  }
508  *this <<" ]";
509  return *this;
510 }
511 kdbgstream& kdbgstream::operator<<( const TQBrush& b) {
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" };
517 
518  *this <<"[ style: ";
519  *this <<s_brushStyles[ b.style() ];
520  *this <<" color: ";
521  // can't use operator<<(str, b.color()) because that terminates a kdbgstream (flushes)
522  if ( b.color().isValid() )
523  *this << TQString(b.color().name()) ;
524  else
525  *this <<"(invalid/default)";
526  if ( b.pixmap() )
527  *this <<" has a pixmap";
528  *this <<" ]";
529  return *this;
530 }
531 
532 kdbgstream& kdbgstream::operator<<( const TQVariant& v) {
533  *this << "[variant: ";
534  *this << v.typeName();
535  // For now we just attempt a conversion to string.
536  // Feel free to switch(v.type()) and improve the output.
537  *this << " toString=";
538  *this << v.toString();
539  *this << "]";
540  return *this;
541 }
542 
543 kdbgstream& kdbgstream::operator<<( const TQByteArray& data) {
544  if (!print) return *this;
545  output += '[';
546  unsigned int i = 0;
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');
550  if ( i < sz )
551  output += ' ';
552  }
553  if ( sz < data.size() )
554  output += "...";
555  output += ']';
556  return *this;
557 }
558 
559 TQString kdBacktrace(int levels)
560 {
561  TQString s;
562 #ifdef HAVE_BACKTRACE
563  void* trace[256];
564  int n = backtrace(trace, 256);
565  if (!n)
566  return s;
567  char** strings = backtrace_symbols (trace, n);
568 
569  if ( levels != -1 )
570  n = QMIN( n, levels );
571  s = "[\n";
572 
573  for (int i = 0; i < n; ++i)
574  s += TQString::number(i) +
575  TQString::fromLatin1(": ") +
576  TQString::fromLatin1(strings[i]) + TQString::fromLatin1("\n");
577  s += "]\n";
578  if (strings)
579  free (strings);
580 #endif
581  return s;
582 }
583 
584 TQString kdBacktrace()
585 {
586  return kdBacktrace(-1 /*all*/);
587 }
588 
589 void kdClearDebugConfig()
590 {
591  if (kDebug_data) {
592  delete kDebug_data->config;
593  kDebug_data->config = 0;
594  }
595 }
596 
597 
598 // Needed for --enable-final
599 #ifdef NDEBUG
600 #define kdDebug kndDebug
601 #endif

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.