qglobal.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 **
4 ** Global functions
5 **
6 ** Created : 920604
7 **
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9 **
10 ** This file is part of the tools module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37 
38 #include "qglobal.h"
39 #include "qasciidict.h"
40 #include "qstring.h"
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 
45 // NOT REVISED
46 
47 /*!
48  \relates QApplication
49  Returns the Qt version number for the library, typically "1.30"
50  or "2.1.0".
51 */
52 
53 const char *qVersion()
54 {
55  return QT_VERSION_STR;
56 }
57 
58 
59 /*****************************************************************************
60  System detection routines
61  *****************************************************************************/
62 
63 static bool si_alreadyDone = FALSE;
64 static int si_wordSize;
65 static bool si_bigEndian;
66 
67 /*!
68  \relates QApplication
69  Obtains information about the system.
70 
71  The system's word size in bits (typically 32) is returned in \e *wordSize.
72  The \e *bigEndian is set to TRUE if this is a big-endian machine,
73  or to FALSE if this is a little-endian machine.
74 
75  This function calls qFatal() with a message if the computer is truly weird
76  (i.e. different endianness for 16 bit and 32 bit integers).
77 */
78 
79 bool qSysInfo( int *wordSize, bool *bigEndian )
80 {
81 #if defined(CHECK_NULL)
82  ASSERT( wordSize != 0 );
83  ASSERT( bigEndian != 0 );
84 #endif
85 
86  if ( si_alreadyDone ) { // run it only once
87  *wordSize = si_wordSize;
88  *bigEndian = si_bigEndian;
89  return TRUE;
90  }
92 
93  si_wordSize = 0;
94  uint n = (uint)(~0);
95  while ( n ) { // detect word size
96  si_wordSize++;
97  n /= 2;
98  }
99  *wordSize = si_wordSize;
100 
101  if ( *wordSize != 64 &&
102  *wordSize != 32 &&
103  *wordSize != 16 ) { // word size: 16, 32 or 64
104 #if defined(CHECK_RANGE)
105  qFatal( "qSysInfo: Unsupported system word size %d", *wordSize );
106 #endif
107  return FALSE;
108  }
109  if ( sizeof(Q_INT8) != 1 || sizeof(Q_INT16) != 2 || sizeof(Q_INT32) != 4 ||
110  sizeof(float) != 4 || sizeof(double) != 8 ) {
111 #if defined(CHECK_RANGE)
112  qFatal( "qSysInfo: Unsupported system data type size" );
113 #endif
114  return FALSE;
115  }
116 
117  bool be16, be32; // determine byte ordering
118  short ns = 0x1234;
119  int nl = 0x12345678;
120 
121  unsigned char *p = (unsigned char *)(&ns); // 16-bit integer
122  be16 = *p == 0x12;
123 
124  p = (unsigned char *)(&nl); // 32-bit integer
125  if ( p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78 )
126  be32 = TRUE;
127  else
128  if ( p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12 )
129  be32 = FALSE;
130  else
131  be32 = !be16;
132 
133  if ( be16 != be32 ) { // strange machine!
134 #if defined(CHECK_RANGE)
135  qFatal( "qSysInfo: Inconsistent system byte order" );
136 #endif
137  return FALSE;
138  }
139 
140  *bigEndian = si_bigEndian = be32;
141  return TRUE;
142 }
143 
144 
145 /*****************************************************************************
146  Debug output routines
147  *****************************************************************************/
148 
149 /*!
150  \fn void qDebug( const char *msg, ... )
151 
152  \relates QApplication
153  Prints a debug message, or calls the message handler (if it has been
154  installed).
155 
156  This function takes a format string and a list of arguments, similar to
157  the C printf() function.
158 
159  Example:
160  \code
161  qDebug( "my window handle = %x", myWidget->id() );
162  \endcode
163 
164  Under X11, the text is printed to stderr. Under Windows, the text is
165  sent to the debugger.
166 
167  \warning The internal buffer is limited to 8196 bytes (including the
168  0-terminator).
169 
170  \sa qWarning(), qFatal(), qInstallMsgHandler(),
171  \link debug.html Debugging\endlink
172 */
173 
174 /*!
175  \fn void qWarning( const char *msg, ... )
176 
177  \relates QApplication
178  Prints a warning message, or calls the message handler (if it has been
179  installed).
180 
181  This function takes a format string and a list of arguments, similar to
182  the C printf() function.
183 
184  Example:
185  \code
186  void f( int c )
187  {
188  if ( c > 200 )
189  qWarning( "f: bad argument, c == %d", c );
190  }
191  \endcode
192 
193  Under X11, the text is printed to stderr. Under Windows, the text is
194  sent to the debugger.
195 
196  \warning The internal buffer is limited to 8196 bytes (including the
197  0-terminator).
198 
199  \sa qDebug(), qFatal(), qInstallMsgHandler(),
200  \link debug.html Debugging\endlink
201 */
202 
203 /*!
204  \fn void qFatal( const char *msg, ... )
205 
206  \relates QApplication
207  Prints a fatal error message and exits, or calls the message handler (if it
208  has been installed).
209 
210  This function takes a format string and a list of arguments, similar to
211  the C printf() function.
212 
213  Example:
214  \code
215  int divide( int a, int b )
216  {
217  if ( b == 0 ) // program error
218  qFatal( "divide: cannot divide by zero" );
219  return a/b;
220  }
221  \endcode
222 
223  Under X11, the text is printed to stderr. Under Windows, the text is
224  sent to the debugger.
225 
226  \warning The internal buffer is limited to 8196 bytes (including the
227  0-terminator).
228 
229  \sa qDebug(), qWarning(), qInstallMsgHandler(),
230  \link debug.html Debugging\endlink
231 */
232 
233 
234 static msg_handler handler = 0; // pointer to debug handler
235 
236 
237 #ifdef _OS_MAC_
238 
239 static FILE * mac_debug=0;
240 
241 void qDebug( const char *msg, ... )
242 {
243  mac_debug=fopen( "debug.txt", "a+" );
244  if(mac_debug) {
245  char buf[8196];
246  va_list ap;
247  va_start( ap, msg ); // use variable arg list
248  if ( handler ) {
249  vsprintf( buf, msg, ap );
250  va_end( ap );
251  (*handler)( QtDebugMsg, buf );
252  } else {
253  vfprintf( mac_debug, msg, ap );
254  va_end( ap );
255  fprintf( mac_debug, "\n" ); // add newline
256  fflush( mac_debug );
257  }
258  fclose(mac_debug);
259  } else {
260  exit(0);
261  }
262 }
263 
264 // copied... this looks really bad.
265 void debug( const char *msg, ... )
266 {
267  mac_debug=fopen( "debug.txt", "a+" );
268  if(mac_debug) {
269  char buf[8196];
270  va_list ap;
271  va_start( ap, msg ); // use variable arg list
272  if ( handler ) {
273  vsprintf( buf, msg, ap );
274  va_end( ap );
275  (*handler)( QtDebugMsg, buf );
276  } else {
277  vfprintf( mac_debug, msg, ap );
278  va_end( ap );
279  fprintf( mac_debug, "\n" ); // add newline
280  fflush( mac_debug );
281  }
282  fclose(mac_debug);
283  }
284 }
285 
286 void qWarning( const char *msg, ... )
287 {
288  mac_debug=fopen( "debug.txt", "a+" );
289  if(mac_debug) {
290  char buf[8196];
291  va_list ap;
292  va_start( ap, msg ); // use variable arg list
293  if ( handler ) {
294  vsprintf( buf, msg, ap );
295  va_end( ap );
296  (*handler)( QtDebugMsg, buf );
297  } else {
298  vfprintf( mac_debug, msg, ap );
299  va_end( ap );
300  fprintf( mac_debug, "\n" ); // add newline
301  fflush( mac_debug );
302  }
303  fclose(mac_debug);
304  }
305 }
306 
307 // copied... this looks really bad.
308 void warning( const char *msg, ... )
309 {
310  mac_debug=fopen( "debug.txt", "a+" );
311  if(mac_debug) {
312  char buf[8196];
313  va_list ap;
314  va_start( ap, msg ); // use variable arg list
315  if ( handler ) {
316  vsprintf( buf, msg, ap );
317  va_end( ap );
318  (*handler)( QtDebugMsg, buf );
319  } else {
320  vfprintf( mac_debug, msg, ap );
321  va_end( ap );
322  fprintf( mac_debug, "\n" ); // add newline
323  fflush( mac_debug );
324  }
325  fclose(mac_debug);
326  }
327 }
328 
329 void qFatal( const char *msg, ... )
330 {
331  mac_debug=fopen( "debug.txt", "a+");
332  if(mac_debug) {
333  char buf[8196];
334  va_list ap;
335  va_start( ap, msg ); // use variable arg list
336  if ( handler ) {
337  vsprintf( buf, msg, ap );
338  va_end( ap );
339  (*handler)( QtDebugMsg, buf );
340  } else {
341  vfprintf( mac_debug, msg, ap );
342  va_end( ap );
343  fprintf( mac_debug, "\n" ); // add newline
344  fflush( mac_debug );
345  }
346  fclose(mac_debug);
347  }
348  exit(0);
349 }
350 
351 // copied... this looks really bad.
352 void fatal( const char *msg, ... )
353 {
354  mac_debug=fopen( "debug.txt", "a+" );
355  if(mac_debug) {
356  char buf[8196];
357  va_list ap;
358  va_start( ap, msg ); // use variable arg list
359  if ( handler ) {
360  vsprintf( buf, msg, ap );
361  va_end( ap );
362  (*handler)( QtDebugMsg, buf );
363  } else {
364  vfprintf( mac_debug, msg, ap );
365  va_end( ap );
366  fprintf( mac_debug, "\n" ); // add newline
367  fflush( mac_debug );
368  }
369  fclose(mac_debug);
370  }
371  exit(0);
372 }
373 
374 #else
375 
376 void qDebug( const char *msg, ... )
377 {
378  char buf[8196];
379  va_list ap;
380  va_start( ap, msg ); // use variable arg list
381  if ( handler ) {
382  vsprintf( buf, msg, ap ); // ### vsnprintf would be great here
383  va_end( ap );
384  (*handler)( QtDebugMsg, buf );
385  } else {
386  vfprintf( stderr, msg, ap );
387  va_end( ap );
388  fprintf( stderr, "\n" ); // add newline
389  }
390 }
391 
392 // copied... this looks really bad.
393 void debug( const char *msg, ... )
394 {
395  char buf[8196];
396  va_list ap;
397  va_start( ap, msg ); // use variable arg list
398  if ( handler ) {
399  vsprintf( buf, msg, ap );
400  va_end( ap );
401  (*handler)( QtDebugMsg, buf );
402  } else {
403  vfprintf( stderr, msg, ap );
404  va_end( ap );
405  fprintf( stderr, "\n" ); // add newline
406  }
407 }
408 
409 void qWarning( const char *msg, ... )
410 {
411  char buf[8196];
412  va_list ap;
413  va_start( ap, msg ); // use variable arg list
414  if ( handler ) {
415  vsprintf( buf, msg, ap );
416  va_end( ap );
417  (*handler)( QtWarningMsg, buf );
418  } else {
419  vfprintf( stderr, msg, ap );
420  va_end( ap );
421  fprintf( stderr, "\n" ); // add newline
422  }
423 }
424 
425 
426 // again, copied
427 void warning( const char *msg, ... )
428 {
429  char buf[8196];
430  va_list ap;
431  va_start( ap, msg ); // use variable arg list
432  if ( handler ) {
433  vsprintf( buf, msg, ap );
434  va_end( ap );
435  (*handler)( QtWarningMsg, buf );
436  } else {
437  vfprintf( stderr, msg, ap );
438  va_end( ap );
439  fprintf( stderr, "\n" ); // add newline
440  }
441 }
442 
443 void qFatal( const char *msg, ... )
444 {
445  char buf[8196];
446  va_list ap;
447  va_start( ap, msg ); // use variable arg list
448  if ( handler ) {
449  vsprintf( buf, msg, ap );
450  va_end( ap );
451  (*handler)( QtFatalMsg, buf );
452  } else {
453  vfprintf( stderr, msg, ap );
454  va_end( ap );
455  fprintf( stderr, "\n" ); // add newline
456 #if defined(_OS_UNIX_) && defined(DEBUG)
457  abort(); // trap; generates core dump
458 #else
459  exit( 1 ); // goodbye cruel world
460 #endif
461  }
462 }
463 
464 // yet again, copied
465 void fatal( const char *msg, ... )
466 {
467  char buf[8196];
468  va_list ap;
469  va_start( ap, msg ); // use variable arg list
470  if ( handler ) {
471  vsprintf( buf, msg, ap );
472  va_end( ap );
473  (*handler)( QtFatalMsg, buf );
474  } else {
475  vfprintf( stderr, msg, ap );
476  va_end( ap );
477  fprintf( stderr, "\n" ); // add newline
478 #if defined(_OS_UNIX_) && defined(DEBUG)
479  abort(); // trap; generates core dump
480 #else
481  exit( 1 ); // goodbye cruel world
482 #endif
483  }
484 }
485 
486 #endif
487 
488 
489 /*!
490  \fn void ASSERT( bool test )
491  \relates QApplication
492  Prints a warning message containing the source code file name and line number
493  if \e test is FALSE.
494 
495  This is really a macro defined in qglobal.h.
496 
497  ASSERT is useful for testing required conditions in your program.
498 
499  Example:
500  \code
501  //
502  // File: div.cpp
503  //
504 
505  #include <qglobal.h>
506 
507  int divide( int a, int b )
508  {
509  ASSERT( b != 0 ); // this is line 9
510  return a/b;
511  }
512  \endcode
513 
514  If \c b is zero, the ASSERT statement will output the following message
515  using the qWarning() function:
516  \code
517  ASSERT: "b == 0" in div.cpp (9)
518  \endcode
519 
520  \sa qWarning(), \link debug.html Debugging\endlink
521 */
522 
523 
524 /*!
525  \fn void CHECK_PTR( void *p )
526  \relates QApplication
527  If \e p is null, a fatal messages says that the program ran out of memory
528  and exits. If \e p is not null, nothing happens.
529 
530  This is really a macro defined in qglobal.h.
531 
532  Example:
533  \code
534  int *a;
535  CHECK_PTR( a = new int[80] ); // never do this!
536  // do this instead:
537  a = new int[80];
538  CHECK_PTR( a ); // this is fine
539  \endcode
540 
541  \sa qFatal(), \link debug.html Debugging\endlink
542 */
543 
544 
545 //
546 // The CHECK_PTR macro calls this function to check if an allocation went ok.
547 //
548 
549 bool qt_check_pointer( bool c, const char *n, int l )
550 {
551  if ( c )
552  qFatal( "In file %s, line %d: Out of memory", n, l );
553  return TRUE;
554 }
555 
556 
557 static bool firstObsoleteWarning(const char *obj, const char *oldfunc )
558 {
559  static QAsciiDict<int> *obsoleteDict = 0;
560  if ( !obsoleteDict ) { // first time func is called
561  obsoleteDict = new QAsciiDict<int>;
562 #if defined(DEBUG)
563  qDebug(
564  "You are using obsolete functions in the Qt library. Call the function\n"
565  "qSuppressObsoleteWarnings() to suppress obsolete warnings.\n"
566  );
567 #endif
568  }
569  QCString s( obj );
570  s += "::";
571  s += oldfunc;
572  if ( obsoleteDict->find(s.data()) == 0 ) {
573  obsoleteDict->insert( s.data(), (int*)1 ); // anything different from 0
574  return TRUE;
575  }
576  return FALSE;
577 }
578 
579 static bool suppressObsolete = FALSE;
580 
581 void qSuppressObsoleteWarnings( bool suppress )
582 {
583  suppressObsolete = suppress;
584 }
585 
586 void qObsolete( const char *obj, const char *oldfunc, const char *newfunc )
587 {
588  if ( suppressObsolete )
589  return;
590  if ( !firstObsoleteWarning(obj, oldfunc) )
591  return;
592  if ( obj )
593  qDebug( "%s::%s: This function is obsolete, use %s instead.",
594  obj, oldfunc, newfunc );
595  else
596  qDebug( "%s: This function is obsolete, use %s instead.",
597  oldfunc, newfunc );
598 }
599 
600 void qObsolete( const char *obj, const char *oldfunc )
601 {
602  if ( suppressObsolete )
603  return;
604  if ( !firstObsoleteWarning(obj, oldfunc) )
605  return;
606  if ( obj )
607  qDebug( "%s::%s: This function is obsolete.", obj, oldfunc );
608  else
609  qDebug( "%s: This function is obsolete.", oldfunc );
610 }
611 
612 void qObsolete( const char *message )
613 {
614  if ( suppressObsolete )
615  return;
616  if ( !firstObsoleteWarning( "Qt", message) )
617  return;
618  qDebug( "%s", message );
619 }
620 
621 
622 /*!
623  \relates QApplication
624  Installs a Qt message handler. Returns a pointer to the message handler
625  previously defined.
626 
627  The message handler is a function that prints out debug messages,
628  warnings and fatal error messages. The Qt library (debug version)
629  contains hundreds of warning messages that are printed when internal
630  errors (usually invalid function arguments) occur. If you implement
631  your own message handler, you get total control of these messages.
632 
633  The default message handler prints the message to the standard output
634  under X11 or to the debugger under Windows. If it is a fatal message,
635  the application aborts immediately.
636 
637  Only one message handler can be defined, since this is usually done on
638  an application-wide basis to control debug output.
639 
640  To restore the message handler, call \c qInstallMsgHandler(0).
641 
642  Example:
643  \code
644  #include <qapplication.h>
645  #include <stdio.h>
646  #include <stdlib.h>
647 
648  void myMessageOutput( QtMsgType type, const char *msg )
649  {
650  switch ( type ) {
651  case QtDebugMsg:
652  fprintf( stderr, "Debug: %s\n", msg );
653  break;
654  case QtWarningMsg:
655  fprintf( stderr, "Warning: %s\n", msg );
656  break;
657  case QtFatalMsg:
658  fprintf( stderr, "Fatal: %s\n", msg );
659  abort(); // dump core on purpose
660  }
661  }
662 
663  int main( int argc, char **argv )
664  {
665  qInstallMsgHandler( myMessageOutput );
666  QApplication a( argc, argv );
667  ...
668  return a.exec();
669  }
670  \endcode
671 
672  \sa qDebug(), qWarning(), qFatal(), \link debug.html Debugging\endlink
673 */
674 
676 {
677  msg_handler old = handler;
678  handler = h;
679  return old;
680 }
681 
682 
683 #ifdef _WS_WIN_
684 bool qt_winunicode=FALSE;
685 #endif
void qDebug(const char *msg,...)
Definition: qglobal.cpp:376
void qObsolete(const char *obj, const char *oldfunc, const char *newfunc)
Definition: qglobal.cpp:586
void msg(const char *fmt,...)
Definition: message.cpp:107
Q_EXPORT bool qSysInfo(int *wordSize, bool *bigEndian)
static bool firstObsoleteWarning(const char *obj, const char *oldfunc)
Definition: qglobal.cpp:557
Q_EXPORT const char * qVersion()
type * find(const char *k) const
Definition: qasciidict.h:65
void insert(const char *k, const type *d)
Definition: qasciidict.h:59
bool qt_check_pointer(bool c, const char *n, int l)
Definition: qglobal.cpp:549
const bool FALSE
Definition: qglobal.h:370
void warning(const char *msg,...)
Definition: qglobal.cpp:427
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void debug(const char *msg,...)
Definition: qglobal.cpp:393
static QStrList * l
Definition: config.cpp:1044
void(* msg_handler)(QtMsgType, const char *)
Definition: qglobal.h:608
signed char Q_INT8
Definition: qglobal.h:415
static bool si_alreadyDone
Definition: qglobal.cpp:63
int Q_INT32
Definition: qglobal.h:419
std::void_t< T > n
#define QT_VERSION_STR
Definition: qglobal.h:43
p
Definition: test.py:223
const char * data() const
Definition: qcstring.h:207
void fatal(const char *msg,...)
Definition: qglobal.cpp:465
static bool suppressObsolete
Definition: qglobal.cpp:579
Q_EXPORT msg_handler qInstallMsgHandler(msg_handler)
static int si_wordSize
Definition: qglobal.cpp:64
static msg_handler handler
Definition: qglobal.cpp:234
short Q_INT16
Definition: qglobal.h:417
void qSuppressObsoleteWarnings(bool suppress)
Definition: qglobal.cpp:581
void qFatal(const char *msg,...)
Definition: qglobal.cpp:443
unsigned uint
Definition: qglobal.h:351
QAsciiDict< Entry > ns
std::string nl(std::size_t i=1)
static QCString * s
Definition: config.cpp:1042
const bool TRUE
Definition: qglobal.h:371
#define ASSERT(x)
Definition: qglobal.h:590
static bool si_bigEndian
Definition: qglobal.cpp:65