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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
/**
* SECTION:warnings
* @Title: Message Output and Debugging Functions
* @Short_description: functions to output messages and help debug applications
*
* These functions provide support for outputting messages.
*
* The g_return family of macros (g_return_if_fail(),
* g_return_val_if_fail(), g_return_if_reached(),
* g_return_val_if_reached()) should only be used for programming
* errors, a typical use case is checking for invalid parameters at
* the beginning of a public function. They should not be used if
* you just mean "if (error) return", they should only be used if
* you mean "if (bug in program) return". The program behavior is
* generally considered undefined after one of these checks fails.
* They are not intended for normal control flow, only to give a
* perhaps-helpful warning before giving up.
*/
#include "config.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <locale.h>
#include <errno.h>
#include "glib-init.h"
#include "gbacktrace.h"
#include "gcharset.h"
#include "gconvert.h"
#include "genviron.h"
#include "gmem.h"
#include "gprintfint.h"
#include "gtestutils.h"
#include "gthread.h"
#include "gstrfuncs.h"
#include "gstring.h"
#include "gpattern.h"
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
#ifdef G_OS_WIN32
#include <process.h> /* For getpid() */
#include <io.h>
# define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
# include <windows.h>
#endif
/**
* SECTION:messages
* @title: Message Logging
* @short_description: versatile support for logging messages
* with different levels of importance
*
* These functions provide support for logging error messages
* or messages used for debugging.
*
* There are several built-in levels of messages, defined in
* #GLogLevelFlags. These can be extended with user-defined levels.
*/
/**
* G_LOG_DOMAIN:
*
* Defines the log domain.
*
* For applications, this is typically left as the default %NULL
* (or "") domain. Libraries should define this so that any messages
* which they log can be differentiated from messages from other
* libraries and application code. But be careful not to define
* it in any public header files.
*
* For example, GTK+ uses this in its Makefile.am:
* |[
* INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
* ]|
*/
/**
* G_LOG_FATAL_MASK:
*
* GLib log levels that are considered fatal by default.
*/
/**
* GLogFunc:
* @log_domain: the log domain of the message
* @log_level: the log level of the message (including the
* fatal and recursion flags)
* @message: the message to process
* @user_data: user data, set in g_log_set_handler()
*
* Specifies the prototype of log handler functions.
*
* The default log handler, g_log_default_handler(), automatically appends a
* new-line character to @message when printing it. It is advised that any
* custom log handler functions behave similarly, so that logging calls in user
* code do not need modifying to add a new-line character to the message if the
* log handler is changed.
*/
/**
* GLogLevelFlags:
* @G_LOG_FLAG_RECURSION: internal flag
* @G_LOG_FLAG_FATAL: internal flag
* @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
* This level is also used for messages produced by g_assert().
* @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
* This level is also used for messages produced by g_return_if_fail()
* and g_return_val_if_fail().
* @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
* @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
* @G_LOG_LEVEL_INFO: log level for informational messages, see g_info()
* @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
* @G_LOG_LEVEL_MASK: a mask including all log levels
*
* Flags specifying the level of log messages.
*
* It is possible to change how GLib treats messages of the various
* levels using g_log_set_handler() and g_log_set_fatal_mask().
*/
/**
* G_LOG_LEVEL_USER_SHIFT:
*
* Log levels below 1<<G_LOG_LEVEL_USER_SHIFT are used by GLib.
* Higher bits can be used for user-defined log levels.
*/
/**
* g_message:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* A convenience function/macro to log a normal message.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*/
/**
* g_warning:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* A convenience function/macro to log a warning message.
*
* You can make warnings fatal at runtime by setting the `G_DEBUG`
* environment variable (see
* [Running GLib Applications](glib-running.html)).
*
* If g_log_default_handler() is used as the log handler function,
* a newline character will automatically be appended to @..., and
* need not be entered manually.
*/
/**
* g_critical:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
* It's more or less application-defined what constitutes
* a critical vs. a regular warning. You could call
* g_log_set_always_fatal() to make critical warnings exit
* the program, then use g_critical() for fatal errors, for
* example.
*
* You can also make critical warnings fatal at runtime by
* setting the `G_DEBUG` environment variable (see
* [Running GLib Applications](glib-running.html)).
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*/
/**
* g_error:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* A convenience function/macro to log an error message.
*
* Error messages are always fatal, resulting in a call to
* abort() to terminate the application. This function will
* result in a core dump; don't use it for errors you expect.
* Using this function indicates a bug in your program, i.e.
* an assertion failure.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*
*/
/**
* g_info:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* A convenience function/macro to log an informational message. Seldom used.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*
* Such messages are suppressed by the g_log_default_handler() unless
* the G_MESSAGES_DEBUG environment variable is set appropriately.
*
* Since: 2.40
*/
/**
* g_debug:
* @...: format string, followed by parameters to insert
* into the format string (as with printf())
*
* A convenience function/macro to log a debug message.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*
* Such messages are suppressed by the g_log_default_handler() unless
* the G_MESSAGES_DEBUG environment variable is set appropriately.
*
* Since: 2.6
*/
/* --- structures --- */
typedef struct _GLogDomain GLogDomain;
typedef struct _GLogHandler GLogHandler;
struct _GLogDomain
{
gchar *log_domain;
GLogLevelFlags fatal_mask;
GLogHandler *handlers;
GLogDomain *next;
};
struct _GLogHandler
{
guint id;
GLogLevelFlags log_level;
GLogFunc log_func;
gpointer data;
GLogHandler *next;
};
/* --- variables --- */
static GMutex g_messages_lock;
static GLogDomain *g_log_domains = NULL;
static GPrintFunc glib_print_func = NULL;
static GPrintFunc glib_printerr_func = NULL;
static GPrivate g_log_depth;
static GLogFunc default_log_func = g_log_default_handler;
static gpointer default_log_data = NULL;
static GTestLogFatalFunc fatal_log_func = NULL;
static gpointer fatal_log_data;
/* --- functions --- */
static void _g_log_abort (gboolean breakpoint);
static void
_g_log_abort (gboolean breakpoint)
{
if (g_test_subprocess ())
{
/* If this is a test case subprocess then it probably caused
* this error message on purpose, so just exit() rather than
* abort()ing, to avoid triggering any system crash-reporting
* daemon.
*/
_exit (1);
}
if (breakpoint)
G_BREAKPOINT ();
else
abort ();
}
#ifdef G_OS_WIN32
# include <windows.h>
static gboolean win32_keep_fatal_message = FALSE;
/* This default message will usually be overwritten. */
/* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
* called with huge strings, is it?
*/
static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
static gchar *fatal_msg_ptr = fatal_msg_buf;
#undef write
static inline int
dowrite (int fd,
const void *buf,
unsigned int len)
{
if (win32_keep_fatal_message)
{
memcpy (fatal_msg_ptr, buf, len);
fatal_msg_ptr += len;
*fatal_msg_ptr = 0;
return len;
}
write (fd, buf, len);
return len;
}
#define write(fd, buf, len) dowrite(fd, buf, len)
#endif
static void
write_string (int fd,
const gchar *string)
{
int res;
do
res = write (fd, string, strlen (string));
while (G_UNLIKELY (res == -1 && errno == EINTR));
}
static GLogDomain*
g_log_find_domain_L (const gchar *log_domain)
{
GLogDomain *domain;
domain = g_log_domains;
while (domain)
{
if (strcmp (domain->log_domain, log_domain) == 0)
return domain;
domain = domain->next;
}
return NULL;
}
static GLogDomain*
g_log_domain_new_L (const gchar *log_domain)
{
GLogDomain *domain;
domain = g_new (GLogDomain, 1);
domain->log_domain = g_strdup (log_domain);
domain->fatal_mask = G_LOG_FATAL_MASK;
domain->handlers = NULL;
domain->next = g_log_domains;
g_log_domains = domain;
return domain;
}
static void
g_log_domain_check_free_L (GLogDomain *domain)
{
if (domain->fatal_mask == G_LOG_FATAL_MASK &&
domain->handlers == NULL)
{
GLogDomain *last, *work;
last = NULL;
work = g_log_domains;
while (work)
{
if (work == domain)
{
if (last)
last->next = domain->next;
else
g_log_domains = domain->next;
g_free (domain->log_domain);
g_free (domain);
break;
}
last = work;
work = last->next;
}
}
}
static GLogFunc
g_log_domain_get_handler_L (GLogDomain *domain,
GLogLevelFlags log_level,
gpointer *data)
{
if (domain && log_level)
{
GLogHandler *handler;
handler = domain->handlers;
while (handler)
{
if ((handler->log_level & log_level) == log_level)
{
*data = handler->data;
return handler->log_func;
}
handler = handler->next;
}
}
*data = default_log_data;
return default_log_func;
}
/**
* g_log_set_always_fatal:
* @fatal_mask: the mask containing bits set for each level
* of error which is to be fatal
*
* Sets the message levels which are always fatal, in any log domain.
* When a message with any of these levels is logged the program terminates.
* You can only set the levels defined by GLib to be fatal.
* %G_LOG_LEVEL_ERROR is always fatal.
*
* You can also make some message levels fatal at runtime by setting
* the `G_DEBUG` environment variable (see
* [Running GLib Applications](glib-running.html)).
*
* Returns: the old fatal mask
*/
GLogLevelFlags
g_log_set_always_fatal (GLogLevelFlags fatal_mask)
{
GLogLevelFlags old_mask;
/* restrict the global mask to levels that are known to glib
* since this setting applies to all domains
*/
fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
/* force errors to be fatal */
fatal_mask |= G_LOG_LEVEL_ERROR;
/* remove bogus flag */
fatal_mask &= ~G_LOG_FLAG_FATAL;
g_mutex_lock (&g_messages_lock);
old_mask = g_log_always_fatal;
g_log_always_fatal = fatal_mask;
g_mutex_unlock (&g_messages_lock);
return old_mask;
}
/**
* g_log_set_fatal_mask:
* @log_domain: the log domain
* @fatal_mask: the new fatal mask
*
* Sets the log levels which are fatal in the given domain.
* %G_LOG_LEVEL_ERROR is always fatal.
*
* Returns: the old fatal mask for the log domain
*/
GLogLevelFlags
g_log_set_fatal_mask (const gchar *log_domain,
GLogLevelFlags fatal_mask)
{
GLogLevelFlags old_flags;
GLogDomain *domain;
if (!log_domain)
log_domain = "";
/* force errors to be fatal */
fatal_mask |= G_LOG_LEVEL_ERROR;
/* remove bogus flag */
fatal_mask &= ~G_LOG_FLAG_FATAL;
g_mutex_lock (&g_messages_lock);
domain = g_log_find_domain_L (log_domain);
if (!domain)
domain = g_log_domain_new_L (log_domain);
old_flags = domain->fatal_mask;
domain->fatal_mask = fatal_mask;
g_log_domain_check_free_L (domain);
g_mutex_unlock (&g_messages_lock);
return old_flags;
}
/**
* g_log_set_handler:
* @log_domain: (allow-none): the log domain, or %NULL for the default ""
* application domain
* @log_levels: the log levels to apply the log handler for.
* To handle fatal and recursive messages as well, combine
* the log levels with the #G_LOG_FLAG_FATAL and
* #G_LOG_FLAG_RECURSION bit flags.
* @log_func: the log handler function
* @user_data: data passed to the log handler
*
* Sets the log handler for a domain and a set of log levels.
* To handle fatal and recursive messages the @log_levels parameter
* must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
* bit flags.
*
* Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
* you want to set a handler for this log level you must combine it with
* #G_LOG_FLAG_FATAL.
*
* Here is an example for adding a log handler for all warning messages
* in the default domain:
* |[<!-- language="C" -->
* g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
* | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
* ]|
*
* This example adds a log handler for all critical messages from GTK+:
* |[<!-- language="C" -->
* g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
* | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
* ]|
*
* This example adds a log handler for all messages from GLib:
* |[<!-- language="C" -->
* g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
* | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
* ]|
*
* Returns: the id of the new handler
*/
guint
g_log_set_handler (const gchar *log_domain,
GLogLevelFlags log_levels,
GLogFunc log_func,
gpointer user_data)
{
static guint handler_id = 0;
GLogDomain *domain;
GLogHandler *handler;
g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
g_return_val_if_fail (log_func != NULL, 0);
if (!log_domain)
log_domain = "";
handler = g_new (GLogHandler, 1);
g_mutex_lock (&g_messages_lock);
domain = g_log_find_domain_L (log_domain);
if (!domain)
domain = g_log_domain_new_L (log_domain);
handler->id = ++handler_id;
handler->log_level = log_levels;
handler->log_func = log_func;
handler->data = user_data;
handler->next = domain->handlers;
domain->handlers = handler;
g_mutex_unlock (&g_messages_lock);
return handler_id;
}
/**
* g_log_set_default_handler:
* @log_func: the log handler function
* @user_data: data passed to the log handler
*
* Installs a default log handler which is used if no
* log handler has been set for the particular log domain
* and log level combination. By default, GLib uses
* g_log_default_handler() as default log handler.
*
* Returns: the previous default log handler
*
* Since: 2.6
*/
GLogFunc
g_log_set_default_handler (GLogFunc log_func,
gpointer user_data)
{
GLogFunc old_log_func;
g_mutex_lock (&g_messages_lock);
old_log_func = default_log_func;
default_log_func = log_func;
default_log_data = user_data;
g_mutex_unlock (&g_messages_lock);
return old_log_func;
}
/**
* g_test_log_set_fatal_handler:
* @log_func: the log handler function.
* @user_data: data passed to the log handler.
*
* Installs a non-error fatal log handler which can be
* used to decide whether log messages which are counted
* as fatal abort the program.
*
* The use case here is that you are running a test case
* that depends on particular libraries or circumstances
* and cannot prevent certain known critical or warning
* messages. So you install a handler that compares the
* domain and message to precisely not abort in such a case.
*
* Note that the handler is reset at the beginning of
* any test case, so you have to set it inside each test
* function which needs the special behavior.
*
* This handler has no effect on g_error messages.
*
* Since: 2.22
**/
void
g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
gpointer user_data)
{
g_mutex_lock (&g_messages_lock);
fatal_log_func = log_func;
fatal_log_data = user_data;
g_mutex_unlock (&g_messages_lock);
}
/**
* g_log_remove_handler:
* @log_domain: the log domain
* @handler_id: the id of the handler, which was returned
* in g_log_set_handler()
*
* Removes the log handler.
*/
void
g_log_remove_handler (const gchar *log_domain,
guint handler_id)
{
GLogDomain *domain;
g_return_if_fail (handler_id > 0);
if (!log_domain)
log_domain = "";
g_mutex_lock (&g_messages_lock);
domain = g_log_find_domain_L (log_domain);
if (domain)
{
GLogHandler *work, *last;
last = NULL;
work = domain->handlers;
while (work)
{
if (work->id == handler_id)
{
if (last)
last->next = work->next;
else
domain->handlers = work->next;
g_log_domain_check_free_L (domain);
g_mutex_unlock (&g_messages_lock);
g_free (work);
return;
}
last = work;
work = last->next;
}
}
g_mutex_unlock (&g_messages_lock);
g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
G_STRLOC, handler_id, log_domain);
}
#define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
(wc == 0x7f) || \
(wc >= 0x80 && wc < 0xa0)))
static gchar*
strdup_convert (const gchar *string,
const gchar *charset)
{
if (!g_utf8_validate (string, -1, NULL))
{
GString *gstring = g_string_new ("[Invalid UTF-8] ");
guchar *p;
for (p = (guchar *)string; *p; p++)
{
if (CHAR_IS_SAFE(*p) &&
!(*p == '\r' && *(p + 1) != '\n') &&
*p < 0x80)
g_string_append_c (gstring, *p);
else
g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
}
return g_string_free (gstring, FALSE);
}
else
{
GError *err = NULL;
gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
if (result)
return result;
else
{
/* Not thread-safe, but doesn't matter if we print the warning twice
*/
static gboolean warned = FALSE;
if (!warned)
{
warned = TRUE;
_g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
}
g_error_free (err);
return g_strdup (string);
}
}
}
/* For a radix of 8 we need at most 3 output bytes for 1 input
* byte. Additionally we might need up to 2 output bytes for the
* readix prefix and 1 byte for the trailing NULL.
*/
#define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
static void
format_unsigned (gchar *buf,
gulong num,
guint radix)
{
gulong tmp;
gchar c;
gint i, n;
/* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
if (radix != 8 && radix != 10 && radix != 16)
{
*buf = '\000';
return;
}
if (!num)
{
*buf++ = '0';
*buf = '\000';
return;
}
if (radix == 16)
{
*buf++ = '0';
*buf++ = 'x';
}
else if (radix == 8)
{
*buf++ = '0';
}
n = 0;
tmp = num;
while (tmp)
{
tmp /= radix;
n++;
}
i = n;
/* Again we can't use g_assert; actually this check should _never_ fail. */
if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
{
*buf = '\000';
return;
}
while (num)
{
i--;
c = (num % radix);
if (c < 10)
buf[i] = c + '0';
else
buf[i] = c + 'a' - 10;
num /= radix;
}
buf[n] = '\000';
}
/* string size big enough to hold level prefix */
#define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
#define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
/* these are emitted by the default log handler */
#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
/* these are filtered by G_MESSAGES_DEBUG by the default log handler */
#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
static int
mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
GLogLevelFlags log_level)
{
gboolean to_stdout = TRUE;
/* we may not call _any_ GLib functions here */
switch (log_level & G_LOG_LEVEL_MASK)
{
case G_LOG_LEVEL_ERROR:
strcpy (level_prefix, "ERROR");
to_stdout = FALSE;
break;
case G_LOG_LEVEL_CRITICAL:
strcpy (level_prefix, "CRITICAL");
to_stdout = FALSE;
break;
case G_LOG_LEVEL_WARNING:
strcpy (level_prefix, "WARNING");
to_stdout = FALSE;
break;
case G_LOG_LEVEL_MESSAGE:
strcpy (level_prefix, "Message");
to_stdout = FALSE;
break;
case G_LOG_LEVEL_INFO:
strcpy (level_prefix, "INFO");
break;
case G_LOG_LEVEL_DEBUG:
strcpy (level_prefix, "DEBUG");
break;
default:
if (log_level)
{
strcpy (level_prefix, "LOG-");
format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
}
else
strcpy (level_prefix, "LOG");
break;
}
if (log_level & G_LOG_FLAG_RECURSION)
strcat (level_prefix, " (recursed)");
if (log_level & ALERT_LEVELS)
strcat (level_prefix, " **");
#ifdef G_OS_WIN32
if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
win32_keep_fatal_message = TRUE;
#endif
return to_stdout ? 1 : 2;
}
typedef struct {
gchar *log_domain;
GLogLevelFlags log_level;
gchar *pattern;
} GTestExpectedMessage;
static GSList *expected_messages = NULL;
/**
* g_logv:
* @log_domain: the log domain
* @log_level: the log level
* @format: the message format. See the printf() documentation
* @args: the parameters to insert into the format string
*
* Logs an error or debugging message.
*
* If the log level has been set as fatal, the abort()
* function is called to terminate the program.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*/
void
g_logv (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *format,
va_list args)
{
gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
gchar buffer[1025], *msg, *msg_alloc = NULL;
gint i;
log_level &= G_LOG_LEVEL_MASK;
if (!log_level)
return;
if (log_level & G_LOG_FLAG_RECURSION)
{
/* we use a stack buffer of fixed size, since we're likely
* in an out-of-memory situation
*/
gsize size G_GNUC_UNUSED;
size = _g_vsnprintf (buffer, 1024, format, args);
msg = buffer;
}
else
msg = msg_alloc = g_strdup_vprintf (format, args);
if (expected_messages)
{
GTestExpectedMessage *expected = expected_messages->data;
if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
((log_level & expected->log_level) == expected->log_level) &&
g_pattern_match_simple (expected->pattern, msg))
{
expected_messages = g_slist_delete_link (expected_messages,
expected_messages);
g_free (expected->log_domain);
g_free (expected->pattern);
g_free (expected);
g_free (msg_alloc);
return;
}
else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
{
gchar level_prefix[STRING_BUFFER_SIZE];
gchar *expected_message;
mklevel_prefix (level_prefix, expected->log_level);
expected_message = g_strdup_printf ("Did not see expected message %s-%s: %s",
expected->log_domain ? expected->log_domain : "**",
level_prefix, expected->pattern);
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, expected_message, NULL);
g_free (expected_message);
log_level |= G_LOG_FLAG_FATAL;
}
}
for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
{
GLogLevelFlags test_level;
test_level = 1 << i;
if (log_level & test_level)
{
GLogDomain *domain;
GLogFunc log_func;
GLogLevelFlags domain_fatal_mask;
gpointer data = NULL;
gboolean masquerade_fatal = FALSE;
guint depth;
if (was_fatal)
test_level |= G_LOG_FLAG_FATAL;
if (was_recursion)
test_level |= G_LOG_FLAG_RECURSION;
/* check recursion and lookup handler */
g_mutex_lock (&g_messages_lock);
depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
domain = g_log_find_domain_L (log_domain ? log_domain : "");
if (depth)
test_level |= G_LOG_FLAG_RECURSION;
depth++;
domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
if ((domain_fatal_mask | g_log_always_fatal) & test_level)
test_level |= G_LOG_FLAG_FATAL;
if (test_level & G_LOG_FLAG_RECURSION)
log_func = _g_log_fallback_handler;
else
log_func = g_log_domain_get_handler_L (domain, test_level, &data);
domain = NULL;
g_mutex_unlock (&g_messages_lock);
g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
log_func (log_domain, test_level, msg, data);
if ((test_level & G_LOG_FLAG_FATAL)
&& !(test_level & G_LOG_LEVEL_ERROR))
{
masquerade_fatal = fatal_log_func
&& !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
}
if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
{
#ifdef G_OS_WIN32
if (win32_keep_fatal_message)
{
gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
MessageBox (NULL, locale_msg, NULL,
MB_ICONERROR|MB_SETFOREGROUND);
}
_g_log_abort (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION));
#else
_g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
#endif /* !G_OS_WIN32 */
}
depth--;
g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
}
}
g_free (msg_alloc);
}
/**
* g_log:
* @log_domain: the log domain, usually #G_LOG_DOMAIN
* @log_level: the log level, either from #GLogLevelFlags
* or a user-defined level
* @format: the message format. See the printf() documentation
* @...: the parameters to insert into the format string
*
* Logs an error or debugging message.
*
* If the log level has been set as fatal, the abort()
* function is called to terminate the program.
*
* If g_log_default_handler() is used as the log handler function, a new-line
* character will automatically be appended to @..., and need not be entered
* manually.
*/
void
g_log (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *format,
...)
{
va_list args;
va_start (args, format);
g_logv (log_domain, log_level, format, args);
va_end (args);
}
void
g_return_if_fail_warning (const char *log_domain,
const char *pretty_function,
const char *expression)
{
g_log (log_domain,
G_LOG_LEVEL_CRITICAL,
"%s: assertion '%s' failed",
pretty_function,
expression);
}
void
g_warn_message (const char *domain,
const char *file,
int line,
const char *func,
const char *warnexpr)
{
char *s, lstr[32];
g_snprintf (lstr, 32, "%d", line);
if (warnexpr)
s = g_strconcat ("(", file, ":", lstr, "):",
func, func[0] ? ":" : "",
" runtime check failed: (", warnexpr, ")", NULL);
else
s = g_strconcat ("(", file, ":", lstr, "):",
func, func[0] ? ":" : "",
" ", "code should not be reached", NULL);
g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
g_free (s);
}
void
g_assert_warning (const char *log_domain,
const char *file,
const int line,
const char *pretty_function,
const char *expression)
{
if (expression)
g_log (log_domain,
G_LOG_LEVEL_ERROR,
"file %s: line %d (%s): assertion failed: (%s)",
file,
line,
pretty_function,
expression);
else
g_log (log_domain,
G_LOG_LEVEL_ERROR,
"file %s: line %d (%s): should not be reached",
file,
line,
pretty_function);
_g_log_abort (FALSE);
abort ();
}
/**
* g_test_expect_message:
* @log_domain: (allow-none): the log domain of the message
* @log_level: the log level of the message
* @pattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
*
* Indicates that a message with the given @log_domain and @log_level,
* with text matching @pattern, is expected to be logged. When this
* message is logged, it will not be printed, and the test case will
* not abort.
*
* Use g_test_assert_expected_messages() to assert that all
* previously-expected messages have been seen and suppressed.
*
* You can call this multiple times in a row, if multiple messages are
* expected as a result of a single call. (The messages must appear in
* the same order as the calls to g_test_expect_message().)
*
* For example:
*
* |[<!-- language="C" -->
* // g_main_context_push_thread_default() should fail if the
* // context is already owned by another thread.
* g_test_expect_message (G_LOG_DOMAIN,
* G_LOG_LEVEL_CRITICAL,
* "assertion*acquired_context*failed");
* g_main_context_push_thread_default (bad_context);
* g_test_assert_expected_messages ();
* ]|
*
* Note that you cannot use this to test g_error() messages, since
* g_error() intentionally never returns even if the program doesn't
* abort; use g_test_trap_subprocess() in this case.
*
* If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
* expected via g_test_expect_message() then they will be ignored.
*
* Since: 2.34
*/
void
g_test_expect_message (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *pattern)
{
GTestExpectedMessage *expected;
g_return_if_fail (log_level != 0);
g_return_if_fail (pattern != NULL);
g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR);
expected = g_new (GTestExpectedMessage, 1);
expected->log_domain = g_strdup (log_domain);
expected->log_level = log_level;
expected->pattern = g_strdup (pattern);
expected_messages = g_slist_append (expected_messages, expected);
}
void
g_test_assert_expected_messages_internal (const char *domain,
const char *file,
int line,
const char *func)
{
if (expected_messages)
{
GTestExpectedMessage *expected;
gchar level_prefix[STRING_BUFFER_SIZE];
gchar *message;
expected = expected_messages->data;
mklevel_prefix (level_prefix, expected->log_level);
message = g_strdup_printf ("Did not see expected message %s-%s: %s",
expected->log_domain ? expected->log_domain : "**",
level_prefix, expected->pattern);
g_assertion_message (G_LOG_DOMAIN, file, line, func, message);
g_free (message);
}
}
/**
* g_test_assert_expected_messages:
*
* Asserts that all messages previously indicated via
* g_test_expect_message() have been seen and suppressed.
*
* If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
* expected via g_test_expect_message() then they will be ignored.
*
* Since: 2.34
*/
void
_g_log_fallback_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer unused_data)
{
gchar level_prefix[STRING_BUFFER_SIZE];
#ifndef G_OS_WIN32
gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
#endif
int fd;
/* we cannot call _any_ GLib functions in this fallback handler,
* which is why we skip UTF-8 conversion, etc.
* since we either recursed or ran out of memory, we're in a pretty
* pathologic situation anyways, what we can do is giving the
* the process ID unconditionally however.
*/
fd = mklevel_prefix (level_prefix, log_level);
if (!message)
message = "(NULL) message";
#ifndef G_OS_WIN32
format_unsigned (pid_string, getpid (), 10);
#endif
if (log_domain)
write_string (fd, "\n");
else
write_string (fd, "\n** ");
#ifndef G_OS_WIN32
write_string (fd, "(process:");
write_string (fd, pid_string);
write_string (fd, "): ");
#endif
if (log_domain)
{
write_string (fd, log_domain);
write_string (fd, "-");
}
write_string (fd, level_prefix);
write_string (fd, ": ");
write_string (fd, message);
}
static void
escape_string (GString *string)
{
const char *p = string->str;
gunichar wc;
while (p < string->str + string->len)
{
gboolean safe;
wc = g_utf8_get_char_validated (p, -1);
if (wc == (gunichar)-1 || wc == (gunichar)-2)
{
gchar *tmp;
guint pos;
pos = p - string->str;
/* Emit invalid UTF-8 as hex escapes
*/
tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
g_string_erase (string, pos, 1);
g_string_insert (string, pos, tmp);
p = string->str + (pos + 4); /* Skip over escape sequence */
g_free (tmp);
continue;
}
if (wc == '\r')
{
safe = *(p + 1) == '\n';
}
else
{
safe = CHAR_IS_SAFE (wc);
}
if (!safe)
{
gchar *tmp;
guint pos;
pos = p - string->str;
/* Largest char we escape is 0x0a, so we don't have to worry
* about 8-digit \Uxxxxyyyy
*/
tmp = g_strdup_printf ("\\u%04x", wc);
g_string_erase (string, pos, g_utf8_next_char (p) - p);
g_string_insert (string, pos, tmp);
g_free (tmp);
p = string->str + (pos + 6); /* Skip over escape sequence */
}
else
p = g_utf8_next_char (p);
}
}
/**
* g_log_default_handler:
* @log_domain: the log domain of the message
* @log_level: the level of the message
* @message: the message
* @unused_data: data passed from g_log() which is unused
*
* The default log handler set up by GLib; g_log_set_default_handler()
* allows to install an alternate default log handler.
* This is used if no log handler has been set for the particular log
* domain and log level combination. It outputs the message to stderr
* or stdout and if the log level is fatal it calls abort(). It automatically
* prints a new-line character after the message, so one does not need to be
* manually included in @message.
*
* The behavior of this log handler can be influenced by a number of
* environment variables:
*
* - `G_MESSAGES_PREFIXED`: A :-separated list of log levels for which
* messages should be prefixed by the program name and PID of the
* aplication.
*
* - `G_MESSAGES_DEBUG`: A space-separated list of log domains for
* which debug and informational messages are printed. By default
* these messages are not printed.
*
* stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
* %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
* the rest.
*/
void
g_log_default_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer unused_data)
{
gchar level_prefix[STRING_BUFFER_SIZE], *string;
GString *gstring;
int fd;
const gchar *domains;
if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
goto emit;
domains = g_getenv ("G_MESSAGES_DEBUG");
if (((log_level & INFO_LEVELS) == 0) ||
domains == NULL ||
(strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
return;
emit:
/* we can be called externally with recursion for whatever reason */
if (log_level & G_LOG_FLAG_RECURSION)
{
_g_log_fallback_handler (log_domain, log_level, message, unused_data);
return;
}
fd = mklevel_prefix (level_prefix, log_level);
gstring = g_string_new (NULL);
if (log_level & ALERT_LEVELS)
g_string_append (gstring, "\n");
if (!log_domain)
g_string_append (gstring, "** ");
if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
{
const gchar *prg_name = g_get_prgname ();
if (!prg_name)
g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
else
g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
}
if (log_domain)
{
g_string_append (gstring, log_domain);
g_string_append_c (gstring, '-');
}
g_string_append (gstring, level_prefix);
g_string_append (gstring, ": ");
if (!message)
g_string_append (gstring, "(NULL) message");
else
{
GString *msg;
const gchar *charset;
msg = g_string_new (message);
escape_string (msg);
if (g_get_charset (&charset))
g_string_append (gstring, msg->str); /* charset is UTF-8 already */
else
{
string = strdup_convert (msg->str, charset);
g_string_append (gstring, string);
g_free (string);
}
g_string_free (msg, TRUE);
}
g_string_append (gstring, "\n");
string = g_string_free (gstring, FALSE);
write_string (fd, string);
g_free (string);
}
/**
* g_set_print_handler:
* @func: the new print handler
*
* Sets the print handler.
*
* Any messages passed to g_print() will be output via
* the new handler. The default handler simply outputs
* the message to stdout. By providing your own handler
* you can redirect the output, to a GTK+ widget or a
* log file for example.
*
* Returns: the old print handler
*/
GPrintFunc
g_set_print_handler (GPrintFunc func)
{
GPrintFunc old_print_func;
g_mutex_lock (&g_messages_lock);
old_print_func = glib_print_func;
glib_print_func = func;
g_mutex_unlock (&g_messages_lock);
return old_print_func;
}
/**
* g_print:
* @format: the message format. See the printf() documentation
* @...: the parameters to insert into the format string
*
* Outputs a formatted message via the print handler.
* The default print handler simply outputs the message to stdout, without
* appending a trailing new-line character. Typically, @format should end with
* its own new-line character.
*
* g_print() should not be used from within libraries for debugging
* messages, since it may be redirected by applications to special
* purpose message windows or even files. Instead, libraries should
* use g_log(), or the convenience functions g_message(), g_warning()
* and g_error().
*/
void
g_print (const gchar *format,
...)
{
va_list args;
gchar *string;
GPrintFunc local_glib_print_func;
g_return_if_fail (format != NULL);
va_start (args, format);
string = g_strdup_vprintf (format, args);
va_end (args);
g_mutex_lock (&g_messages_lock);
local_glib_print_func = glib_print_func;
g_mutex_unlock (&g_messages_lock);
if (local_glib_print_func)
local_glib_print_func (string);
else
{
const gchar *charset;
if (g_get_charset (&charset))
fputs (string, stdout); /* charset is UTF-8 already */
else
{
gchar *lstring = strdup_convert (string, charset);
fputs (lstring, stdout);
g_free (lstring);
}
fflush (stdout);
}
g_free (string);
}
/**
* g_set_printerr_handler:
* @func: the new error message handler
*
* Sets the handler for printing error messages.
*
* Any messages passed to g_printerr() will be output via
* the new handler. The default handler simply outputs the
* message to stderr. By providing your own handler you can
* redirect the output, to a GTK+ widget or a log file for
* example.
*
* Returns: the old error message handler
*/
GPrintFunc
g_set_printerr_handler (GPrintFunc func)
{
GPrintFunc old_printerr_func;
g_mutex_lock (&g_messages_lock);
old_printerr_func = glib_printerr_func;
glib_printerr_func = func;
g_mutex_unlock (&g_messages_lock);
return old_printerr_func;
}
/**
* g_printerr:
* @format: the message format. See the printf() documentation
* @...: the parameters to insert into the format string
*
* Outputs a formatted message via the error message handler.
* The default handler simply outputs the message to stderr, without appending
* a trailing new-line character. Typically, @format should end with its own
* new-line character.
*
* g_printerr() should not be used from within libraries.
* Instead g_log() should be used, or the convenience functions
* g_message(), g_warning() and g_error().
*/
void
g_printerr (const gchar *format,
...)
{
va_list args;
gchar *string;
GPrintFunc local_glib_printerr_func;
g_return_if_fail (format != NULL);
va_start (args, format);
string = g_strdup_vprintf (format, args);
va_end (args);
g_mutex_lock (&g_messages_lock);
local_glib_printerr_func = glib_printerr_func;
g_mutex_unlock (&g_messages_lock);
if (local_glib_printerr_func)
local_glib_printerr_func (string);
else
{
const gchar *charset;
if (g_get_charset (&charset))
fputs (string, stderr); /* charset is UTF-8 already */
else
{
gchar *lstring = strdup_convert (string, charset);
fputs (lstring, stderr);
g_free (lstring);
}
fflush (stderr);
}
g_free (string);
}
/**
* g_printf_string_upper_bound:
* @format: the format string. See the printf() documentation
* @args: the parameters to be inserted into the format string
*
* Calculates the maximum space needed to store the output
* of the sprintf() function.
*
* Returns: the maximum space needed to store the formatted string
*/
gsize
g_printf_string_upper_bound (const gchar *format,
va_list args)
{
gchar c;
return _g_vsnprintf (&c, 1, format, args) + 1;
}
|