/usr/include/happycoders/dbg/dbg.h is in happycoders-libdbg-dev 1.2-2ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 | /*
* File: dbg.h
* Author: Pete Goodliffe
* Version: 1.10
* Created: 7 June 2001
*
* Purpose: C++ debugging support library
*
* Copyright (c) Pete Goodliffe 2001-2002 (pete@cthree.org)
*
* This file is modifiable/redistributable under the terms of the GNU
* Lesser General Public License.
*
* You should have recieved a copy of the GNU General Public License along
* with this program; see the file COPYING. If not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211-1307, USA.
*/
#ifndef DBG_DBG_H
#define DBG_DBG_H
#include <iosfwd>
#include <exception>
#ifndef _MSC_VER
#include <ctime>
#else
// The start of a MSVC compatibility disaster area.
// See the documentation for the dbgclock_t type.
#include <time.h>
#endif
#if defined(DBG_ENABLED) && defined(NDEBUG)
//#warning DBG_ENABLED defined with NDEBUG which do you want?
#endif
/**
* @libdoc dbg library
*
* The dbg library is a set of C++ utilities to facilitate modern debugging
* idioms.
*
* It has been designed to support defensive programming techniques in modern
* C++ code. It integrates well with standard library usage and has been
* carefully designed to be easy to write, easy to read and very easy to use.
*
* It provides various constraint checking utilities together with an
* integrated error logging facility. These utilities are flexible and
* customisable. They can be enabled and disabled at runtime, and in release
* builds, dbg library use can be compiled away to nothing.
*
* Rich debugging can only be implemented in large code bases from the outset,
* it is hard to retrofit full defensive programming techniques onto existant
* code. For this reason it is good practice to use a library like dbg when
* you start a new project. By using dbg extensively you will find bugs
* quicker, and prevent more insideous problems rearing their head later in
* the project's life.
*
* For instructions on the dbg library's use see the @ref dbg namespace
* documentation.
*/
/**
* The dbg namespace holds a number of C++ debugging utilities.
*
* They allow you to include constraint checking in your code, and provide
* an integrated advanced stream-based logging facility.
*
* The characteristics of this library are:
* @li Easy to use (not overly complex)
* (easy to write, easy to read, easy to use)
* @li Powerful
* @li Configurable
* @li No run time overhead when "compiled out"
* @li Minimises use of the proprocessor
* @li Can throw exceptions if required
* @li Can separate different "sources" of diagnostic output (these
* sources are differentated by name)
* @li Designed to be a "standard" library
* (integrates with the style of the C++ standard library and works
* well with it)
*
* @sect Enabling debugging
*
* To use dbg in your program you must <code> #include <dbg.h> </code>
* and compile with the <code>DBG_ENABLED</code> flag set.
*
* If you build without DBG_ENABLED you will have no debugging support (neither
* constraints nor logging). There is no overhead building a program using
* these utilities when DBG_ENABLED is not set. Well, actually there might be
* minimal overhead: there is no overhead when using gcc with a little
* optimisation (<code>-O3</code>). There is a few bytes overhead with
* optimisation disabled. (The <code>-O1</code> level leaves almost no
* overhead.)
*
* Either way, the rich debugging support is probably worth a few bytes.
*
* Once your program is running, you will want to enable diagnostic
* levels with @ref dbg::enable, and probably attach an ostream (perhaps
* <code>cerr</code>) to the diagnostic outputs. See the default states section
* below for information on the initial state of dbg.
*
* Aside:
* The standard <code>assert</code> macro is an insideous little devil, a lower
* case macro. This library replaces it and builds much richer constraints
* in its place.
* However, because of it, we have to use an API name dbg::assertion,
* not dbg::assert - this makes me really cross, but I can't assume that the
* user does not <code> #include <assert.h> </code> when using
* <code> <dbg.h> </code>.
*
* @sect Using constraints
*
* The dbg library constraints are very easy to use. Each debugging utility is
* documented fully to help you understand how they work. Here are some simple
* examples of library use for run-time constraint checking:
* <pre>
* void test_dbg()
* {
* dbg::trace trace(DBG_HERE);
*
* int i = 5;
* int *ptr = &i;
*
* dbg::assertion(DBG_ASSERTION(i != 6));
* dbg::check_ptr(ptr, DBG_HERE);
*
* if (i == 5)
* {
* return;
* }
*
* // Shouldn't get here
* dbg::sentinel(DBG_HERE);
* }
* </pre>
*
* The constraints provided by dbg are:
* @li @ref dbg::assertion - General purpose assertion
* (a better <code>assert</code>)
* @li @ref dbg::sentinel - Marker for "shouldn't get here" points
* @li @ref dbg::unimplemented - Marks unimplemented code
* @li @ref dbg::check_ptr - Zero pointer check
* @li @ref dbg::check_bounds - Array bounds checking
* @li @ref dbg::post_mem_fun - Member function post condition
* @li @ref dbg::post - General function post condition
* @li @ref dbg::compile_assertion - Compile time assertion
*
* You can modify constriant behaviour with:
* @li @ref dbg::set_assertion_behaviour - Set how contraints behave
* @li @ref dbg::set_assertion_period - Set up trigger periods
*
* See their individual documentation for further details on usage.
*
* You can specify whether constraints merely report a warning, cause
* an exception to be thrown, or immediately abort the program (see
* @ref dbg::assertion_behaviour).
*
* For assertions that may fire many times in a tight loop, there is the
* facility to time-restrict output (see @ref dbg::set_assertion_period)
*
* @sect Using logging
*
* All the constraint checking shown above integrates with the dbg library
* stream logging mechanisms. These logging facilities are open for your use as
* well.
*
* Here is a simlpe example of this:
* <pre>
* dbg::attach_ostream(dbg::info, cout);
* // now all 'info' messages go to cout
*
* dbg::out(dbg::info) << "This is some info I want to print out\n";
*
* dbg::out(dbg::tracing) << dbg::indent()
* << "This is output at 'tracing' level, indented "
* << "to the same level as the current tracing "
* << "indent.\n";
* </pre>
*
* When you build without the DBG_ENABLED flag specified, these logging
* messages will compile out to nothing.
*
* The logging is a very flexible system. You can attach multiple ostreams
* to any dbg output, so you can easily log to a file and log to the console,
* for example. The output can be formatted in a number of different ways to
* suit your needs.
*
* The logging mechanisms provide you with the ability to prepend to all
* diagnostic output a standard prefix (see @ref dbg::set_prefix), and
* also to add the diagnostic level and current time to the prefix (see
* @ref dbg::enable_level_prefix and @ref dbg::enable_time_prefix).
*
* The logging facilities provide by dbg include:
* @li @ref dbg::enable - Enable/disable activity
* @li @ref dbg::out - Returns a diagnostic ostream
* @li @ref dbg::attach_ostream - Attach an ostream to diagnostic output
* @li @ref dbg::detach_ostream - Detach an ostream to diagnostic output
* @li @ref dbg::trace - Trace entry/exit points
*
* The output formatting utilities include:
* @li @ref dbg::set_prefix - Sets the diagnostic output "margin"
* @li @ref dbg::enable_level_prefix - More information in messages
* @li @ref dbg::enable_time_prefix - Prints time in messages
*
* @sect Diagnostic sources
*
* The dbg library allows you to differentiate different "sources" of logging.
*
* Each of the debug utilities has a second form in which you can supply
* a string describing the source of the diagnostic output (see
* @ref dbg::dbg_source). This source may be a different software component, a
* separate file - whatever granularity you like!
*
* If you don't specify a @ref dbg::dbg_source then you are working with the
* ordinary "unnamed" source.
*
* Using these forms you can filter out diagnostics from the different
* parts of your code. Each source can also be attached to a different set of
* streams (logging each component to a separate file, for example). The
* filtering is rich - you can selectively filter each different diagnostic
* @ref dbg::level for each @ref dbg::dbg_source. For example,
*
* <pre>
* dbg::enable(dbg::all, "foo-driver", true);
* dbg::enable(dbg::all, "bar-driver", false);
*
* int i = 5;
* dbg::assertion("foo-driver", DBG_ASSERTION(i != 6));
* dbg::assertion("bar-driver", DBG_ASSERTION(i != 6));
* </pre>
*
* This will trigger an assertion for the "foo-driver" but not the
* "bar-driver".
*
* There is no requirement to "register" a @ref dbg::dbg_source. The first
* time you use it in any of the dbg APIs, it will be registered with the dbg
* library. It comes into an existance as a copy of the "default"
* debugging sourcei, @ref dbg::default_source.
*
* The default source initially has all debug levels disabled.
* You can change that with this call. Note that this function
* only affects sources created <i>after</i> the call is made.
* Existing sources are unaffected.
*
* If you don't know all of the @ref dbg::dbg_source sources currently
* available, you can blanket enable/disable them with @ref dbg::enable_all.
*
* It can be tedious to specify the @ref dbg_source in every dbg call in
* a source file. For this reason, you can specify the DBG_SOURCE compile
* time macro (wherever you specify DBG_ENABLED). When set, the calls
* automatically recieve the source name via the DBG_HERE macro (see
* @ref dbg::source_pos for details). If DBG_SOURCE is supplied but you call
* a dbg API with a specific named @ref dbg_source, this name will override
* the underlying DBG_SOURCE name.
*
* @sect Overloads
*
* Each constraint utility has a number of overloaded forms. This is to make
* using them more convenient. The most rich overload allows you to specify
* a diagnostic @ref dbg::level and a @ref dbg::dbg_source. There are other
* versions that omit one of these parameters, assuming a relevant default.
*
* @sect Default states
*
* When your program first starts up the dbg library has all debugging levels
* switched off. You can enable debugging with @ref dbg::enable. All of the
* possible @ref dbg::dbg_source enables are also all off for all
* levels. You can enable these with @ref dbg::enable, or @ref dbg::enable_all.
*
* Initially, the <code>std::cerr</code> stream is attached to the
* @ref dbg::error and @ref dbg::fatal diagnostic levels. You can
* attach ostreams to the other diagnostic levels with @ref
* dbg::attach_ostream.
*
* You can modify the "default state" of newly created debug sources. To do
* this use the special @ref dbg::default_source source name in calls to
* @ref dbg::enable, @ref dbg::attach_ostream, and and @ref detach_ostream.
* New sources take the setup from this template source.
*
* All assertion levels are set to @ref dbg::assertions_abort at first, like
* the standard library's assert macro. You can change this behaviour with
* @ref dbg::set_assertion_behaviour. There are no timeout periods set - you
* can change this with @ref dbg::set_assertion_period.
*
* @short Debugging utilities
* @author Pete Goodliffe
* @version 1.0
*/
namespace dbg
{
/**
* This is the version number of the dbg library.
*
* The value is encoded as version * 100. This means that 100 represents
* version 1.00, for example.
*/
const int version = 110;
/**************************************************************************
* Debugging declarations
*************************************************************************/
/**
* The various predefined debugging levels. The dbg API calls use these
* levels as parameters, and allow the user to sift the less interesting
* debugging levels out through @ref dbg::enable.
*
* These levels (and their intended uses) are:
* @li info - Informational, just for interest
* @li warning - For warnings, bad things but recoverable
* @li error - For errors that can't be recovered from
* @li fatal - Errors at this level will cause the dbg library to abort
* program execution, no matter what the
* @ref assertion_behaviour is set to
* @li tracing - Program execution tracing messages
* @li debug - Messages about the state of dbg library, you cannot
* generate messages at this level
* @li none - For APIs that use 'no level specified'
* @li all - Used in @ref enable and @ref attach_ostream to
* specify all levels
*/
enum level
{
info,
warning,
error,
fatal,
tracing,
debug,
none,
all
};
/**
* This enum type describes what happens when a debugging assertion
* fails. The behaviour can be:
* @li assertions_abort - Assertions cause a program abort
* @li assertions_throw - Assertions cause a @ref dbg_exception to
* be thrown
* @li assertions_continue - Assertions cause the standard diagnostic
* printout to occur (the same as the above
* behaviours) but execution continues
* regardless
*
* The dbg library defaults to assertions_abort behaviour, like the
* standard C <code>assert</code>.
*
* @see dbg::set_assertion_behaviour
*/
enum assertion_behaviour
{
assertions_abort,
assertions_throw,
assertions_continue
};
/**
* typedef for a string that describes the "source" of a diagnostic. If
* you are working on a large project with many small code modules you may
* only want to enable debugging from particular source modules. This
* typedef facilitiates this.
*
* Depending on the desired granularity of your dbg sources you will use
* different naming conventions. For example, your dbg_sources might
* be filenames, that way you can switch off all debugging output from
* a particular file quite easily. It might be device driver names,
* component names, library names, or even function names. It's up to you.
*
* If you provide the DBG_SOURCE macro definition at compile time, then
* the DBG_HERE macro includes this source name, differentiating the
* sources for you automatically.
*
* @see dbg::enable(level,dbg_source,bool)
* @see dbg::enable_all
*/
typedef const char * dbg_source;
/**************************************************************************
* source_pos
*************************************************************************/
/**
* Typedef used in the @ref source_pos data structure.
*
* Describes a line number in a source file.
*
* @see dbg::source_pos
*/
typedef const unsigned int line_no_t;
/**
* Typedef used in the @ref source_pos data structure.
*
* Describes a function name in a source file. (Can be zero to
* indicate the function name cannot be assertained on this compiler).
*
* @see dbg::source_pos
*/
typedef const char * func_name_t;
/**
* Typedef used in the @ref source_pos data structure.
*
* Describes a filename.
*
* @see dbg::source_pos
*/
typedef const char * file_name_t;
/**
* Data structure describing a position in the source file. That is,
* @li The line number
* @li The function name (if the compiler supports this)
* @li The filename
* @li The @ref dbg_soruce specified by DBG_SOURCE compilation
* parameter, if any (otherwise zero)
*
* To create a source_pos for the current position, you can use
* the DBG_HERE convenience macro.
*
* There is an empty constructor that allows you to create a source_pos
* that represents 'no position specified'.
*
* This structure should only be used in dbg library API calls.
*
* You can print a source_pos using the usual stream manipulator syntax.
*/
struct source_pos
{
line_no_t line;
func_name_t func;
file_name_t file;
dbg_source src;
/**
* Creates a source_pos struct. Use the DBG_HERE macro to
* call this constructor conveniently.
*/
source_pos(line_no_t ln, func_name_t fn, file_name_t fl, dbg_source s)
: line(ln), func(fn), file(fl), src(s) {}
/**
* A 'null' source_pos for 'no position specified'
*/
source_pos()
: line(0), func(0), file(0), src(0) {}
};
#ifndef _MSC_VER
/**
* The dbgclock_t typedef is an unfortunate workaround for comptability
* purposes. One (unnamed) popular compiler platform supplies a
* <ctime> header file, but this header does NOT place the contents
* into the std namespace.
*
* This typedef is the most elegant work around for that problem. It is
* conditionally set to the appropriate clock_t definition.
*
* In an ideal world this would not exist.
*
* This is the version for sane, standards-compliant platforms.
*/
typedef std::clock_t dbgclock_t;
#else
/**
* See dbgclock_t documentation above. This is the version for broken
* compiler platforms.
*/
typedef clock_t dbgclock_t;
#endif
/**************************************************************************
* Exceptions
*************************************************************************/
/**
* The base type of exception thrown by dbg assertions (and other dbg
* library constraint checks) if the @ref assertion_behaviour is set to
* assertions_throw.
*
* The exception keeps a record of the source position of the trigger
* for this exception.
*/
struct dbg_exception : public std::exception
{
dbg_exception(const source_pos &p) : pos(p) {}
const source_pos pos;
};
/**
* The type of exception thrown by @ref assertion.
*
* @see assertion
*/
struct assertion_exception : public dbg_exception
{
assertion_exception(const source_pos &p) : dbg_exception(p) {}
};
/**
* The type of exception thrown by @ref sentinel.
*
* @see sentinel
*/
struct sentinel_exception : public dbg_exception
{
sentinel_exception(const source_pos &p) : dbg_exception(p) {}
};
/**
* The type of exception thrown by @ref unimplemented.
*
* @see unimplemented
*/
struct unimplemented_exception : public dbg_exception
{
unimplemented_exception(const source_pos &p) : dbg_exception(p) {}
};
/**
* The type of exception thrown by @ref check_ptr.
*
* @see check_ptr
*/
struct check_ptr_exception : public dbg_exception
{
check_ptr_exception(const source_pos &p) : dbg_exception(p) {}
};
#ifdef DBG_ENABLED
/**************************************************************************
* default_source
*************************************************************************/
/**
* The name of a "template" debugging source that provides the default
* state for newly created sources. You can attach and detach logging
* streams here, and enable/disable logging levels.
*
* All source state is copied from the default_source to a new dbg_source.
*
* Whilst you can also use this source for diagnostic purposes this isn't
* it's intention, and it would be confusing to do so.
*
* See @ref dbg_source for discussion on the use of debugging sources in
* dbg.
*
* @see dbg_source
*/
extern dbg_source default_source;
/**************************************************************************
* Debug version of the DBG_HERE macro
*************************************************************************/
/*
* DBG_FUNCTION is defined to be a macro that expands to the name of
* the current function, or zero if the compiler is unable to supply that
* information. It's sad that this wasn't included in the C++ standard
* from the very beginning.
*/
#if defined(__GNUC__)
#define DBG_FUNCTION __FUNCTION__
#else
#define DBG_FUNCTION 0
#endif
#if !defined(DBG_SOURCE)
#define DBG_SOURCE 0
#endif
/*
* Handy macro to generate a @ref source_pos object containing the
* information of the current source line.
*
* @see dbg::source_pos
*/
#define DBG_HERE \
(::dbg::source_pos(__LINE__, DBG_FUNCTION, __FILE__, DBG_SOURCE))
/**************************************************************************
* Enable/disable dbg facilities
*************************************************************************/
/**
* Enables or disables a particular debugging level. The affects dbg
* library calls which don't specify a @ref dbg_source, i.e. from the
* unnamed source.
*
* Enabling affects both constraint checking and diagnostic log output.
*
* If you enable a debugging level twice you only need to disable it once.
*
* All diagnostic output is initially disabled. You can easily enable
* output in your main() thus:
* <pre>
* dbg::enable(dbg::all, true);
* </pre>
*
* Note that if dbg library calls do specify a @ref dbg_source, or you
* provide a definition for the DBG_SOURCE macro on compilation, then you
* will instead need to enable output for that particular source. Use the
* overloaded version of enable. This version of enable doesn't affect
* these other @ref dbg_source calls.
*
* @param lvl Diagnostic level to enable/disable
* @param enabled true to enable this diagnostic level, false to disable it
* @see dbg::enable_all
* @see dbg::out
* @see dbg::attach_ostream
*/
void enable(level lvl, bool enabled);
/**
* In addition to the above enable function, this overloaded version is
* used when you use dbg APIs with a @ref dbg_source specified. For these
* versions of the APIs no debugging will be performed unless you
* enable it with this API.
*
* To enable debugging for the "foobar" diagnostic source at the info
* level you need to do the following:
* <pre>
* dbg::enable(dbg::info, "foobar", true);
* </pre>
*
* If you enable a level for a particular @ref dbg_source twice you only
* need to disable it once.
*
* @param lvl Diagnostic level to enable/disable for the @ref dbg_source
* @param src String describing the diagnostic source
* @param enabled true to enable this diagnostic level, false to disable it
* @see dbg::out
*/
void enable(level lvl, dbg_source src, bool enabled);
/**
* You may not know every single @ref dbg_source that is generating
* debugging in a particular code base. However, using this function
* you can enable a diagnostic level for all currently registered sources
* in one fell swoop.
*
* For example,
* <pre>
* dbg::enable_all(dbg::all, true);
* </pre>
*/
void enable_all(level lvl, bool enabled);
/**************************************************************************
* Logging
*************************************************************************/
/**
* Returns an ostream suitable for sending diagnostic messages to.
* Each diagnostic level has a different logging ostream which can be
* enabled/disabled independantly. In addition, each @ref dbg_source
* has separate enables/disables for each diagnostic level.
*
* This overloaded version of out is used when you are creating diagnostics
* that are tied to a particular @ref dbg_source.
*
* It allows you to write code like this:
* <pre>
* dbg::out(dbg::info, "foobar") << "The foobar is flaky\n";
* </pre>
*
* If you want to prefix your diagnostics with the standard dbg library
* prefix (see @ref set_prefix) then use the @ref prefix or @ref indent
* stream manipulators.
*
* @param lvl Diagnostic level get get ostream for
* @param src String describing the diagnostic source
*/
std::ostream &out(level lvl, dbg_source src);
/**
* Returns an ostream suitable for sending diagnostic messages to.
* Each diagnostic level has a different logging ostream which can be
* enabled/disabled independantly.
*
* You use this version of out when you are creating diagnostics
* that aren't tidied to a particular @ref dbg_source.
*
* Each diagnostic @ref dbg_source has a separate set of streams.
* This function returns the stream for the "unnamed" source. Use the
* overload below to obtain the stream for a named source.
*
* It allows you to write code like this:
* <pre>
* dbg::out(dbg::info) << "The code is flaky\n";
* </pre>
*
* If you want to prefix your diagnostics with the standard dbg library
* prefix (see @ref set_prefix) then use the @ref prefix or @ref indent
* stream manipulators.
*
* @param lvl Diagnostic level get get ostream for
*/
inline std::ostream &out(level lvl)
{
return out(lvl, 0);
}
/**
* Attaches the specified ostream to the given diagnostic level
* for the "unnamed" debug source. Now when diagnostics are produced
* at that level, this ostream will recieve a copy.
*
* You can attach multiple ostreams to a diagnostic level. Be careful
* that they don't go to the same place (e.g. cout and cerr both going
* to your console) - this might confuse you!
*
* If you attach a ostream mutiple times it will only receive one
* copy of the diagnostics, and you will only need to call
* @ref detach_ostream once.
*
* Remember, don't destroy the ostream without first removing it from
* dbg libary, or Bad Things will happen.
*
* @param lvl Diagnostic level
* @param o ostream to attach
* @see dbg::detach_ostream
* @see dbg::detach_all_ostreams
*/
void attach_ostream(level lvl, std::ostream &o);
/**
* Attaches the specified ostream to the given diagnostic level
* for the specified debug source. Otherwise, similar to
* @ref dbg::attach_ostream above.
*
* @param lvl Diagnostic level
* @param src Debug source
* @param o ostream to attach
* @see dbg::detach_ostream
* @see dbg::detach_all_ostreams
*/
void attach_ostream(level lvl, dbg_source src, std::ostream &o);
/**
* Detaches the specified ostream from the given diagnostic level.
*
* If the ostream was not attached then no error is generated.
*
* If you attached the ostream twice, one call to detach_ostream will
* remove it completely.
*
* @param lvl Diagnostic level
* @param o ostream to detach
* @see dbg::attach_ostream
* @see dbg::detach_all_ostreams
*/
void detach_ostream(level lvl, std::ostream &o);
/**
* Detaches the specified ostream from the given diagnostic level
* for the specified debug source. Otherwise, similar to
* @ref dbg::detach_ostream above.
*
* @param lvl Diagnostic level
* @param src Debug source
* @param o ostream to detach
* @see dbg::attach_ostream
* @see dbg::detach_all_ostreams
*/
void detach_ostream(level lvl, dbg_source src, std::ostream &o);
/**
* Detaches all attached ostreams from the specified diagnostic level
* for the "unnamed" diagnostic source.
*
* @param lvl Diagnostic level
* @see dbg::attach_ostream
* @see dbg::detach_ostream
*/
void detach_all_ostreams(level lvl);
/**
* Detaches all attached ostreams from the specified diagnostic level
* for the specified debug source. Otherwise, similar to
* @ref dbg::detach_all_ostreams above.
*
* @param lvl Diagnostic level
* @see dbg::attach_ostream
* @see dbg::detach_ostream
*/
void detach_all_ostreams(level lvl, dbg_source src);
/**
* Convenience function that returns the ostream for the info
* @ref dbg::level for the "unnamed" source.
*
* @see dbg::out
*/
inline std::ostream &info_out()
{
return out(dbg::info);
}
/**
* Convenience function that returns the ostream for the warning
* @ref dbg::level for the "unnamed" source.
*
* @see dbg::out
*/
inline std::ostream &warning_out()
{
return out(dbg::warning);
}
/**
* Convenience function that returns the ostream for the error
* @ref dbg::level for the "unnamed" source.
*
* @see dbg::out
*/
inline std::ostream &error_out()
{
return out(dbg::error);
}
/**
* Convenience function that returns the ostream for the fatal
* @ref dbg::level for the "unnamed" source.
*
* @see dbg::out
*/
inline std::ostream &fatal_out()
{
return out(dbg::fatal);
}
/**
* Convenience function that returns the ostream for the tracing
* @ref dbg::level for the "unnamed" source.
*
* @see dbg::out
*/
inline std::ostream &trace_out()
{
return out(dbg::tracing);
}
/**************************************************************************
* Output formatting
*************************************************************************/
/**
* Sets the debugging prefix - the characters printed before any
* diagnostic output. Defaults to "*** ".
*
* @param prefix New prefix string
* @see dbg::prefix
* @see dbg::enable_level_prefix
* @see dbg::enable_time_prefix
*/
void set_prefix(const char *prefix);
/**
* The dbg library can add to the @ref prefix the name of the used
* diagnostic level (e.g. info, fatal, etc).
*
* By default, this facility is disabled. This function allows you to
* enable the facility.
*
* @param enabled true to enable level prefixing, false to disable
* @see dbg::set_prefix
* @see dbg::enable_time_prefix
*/
void enable_level_prefix(bool enabled);
/**
* The dbg library can add to the @ref prefix the current time. This
* can be useful when debugging systems which remain active for long
* periods of time.
*
* By default, this facility is disabled. This function allows you to
* enable the facility.
*
* The time is produced in the format of the standard library ctime
* function.
*
* @param enabled true to enable time prefixing, false to disable
* @see dbg::set_prefix
* @see dbg::enable_level_prefix
*/
void enable_time_prefix(bool enabled);
/**
* Used so that you can produce a prefix in your diagnostic output in the
* same way that the debugging library does.
*
* You can use it in one of two ways: with or without a diagnostic
* @ref level. For the latter, if level prefixing is enabled (see
* @ref enable_level_prefix) then produces a prefix including the
* specified diagnostic level text.
*
* Examples of use:
*
* <pre>
* dbg::out(dbg::info) << dbg::prefix()
* << "A Bad Thing happened\n";
*
* dbg::out(dbg::info) << dbg::prefix(dbg::info)
* << "A Bad Thing happened\n";
* </pre>
*
* @see dbg::indent
* @see dbg::set_prefix
* @see dbg::enable_level_prefix
* @see dbg::enable_time_prefix
*/
struct prefix
{
/**
* Creates a prefix with no specified diagnostic @ref level.
* No diagnostic level text will be included in the prefix.
*/
prefix() : l(none) {}
/**
* @param lvl Diagnostic @ref level to include in prefix
*/
prefix(level lvl) : l(lvl) {}
level l;
};
/**
* This is called when you use the @ref prefix stream manipulator.
*
* @internal
* @see dbg::prefix
*/
std::ostream &operator<<(std::ostream &s, const prefix &p);
/**
* Used so that you can indent your diagnostic output to the same level
* as the debugging library. This also produces the @ref prefix output.
*
* Examples of use:
*
* <pre>
* dbg::out(dbg::info) << dbg::indent()
* << "A Bad Thing happened\n";
*
* dbg::out(dbg::info) << dbg::indent(dbg::info)
* << "A Bad Thing happened\n";
* </pre>
*
* @see dbg::prefix
* @see dbg::set_prefix
* @see dbg::enable_level_prefix
* @see dbg::enable_time_prefix
*/
struct indent
{
/**
* Creates a indent with no specified diagnostic @ref level.
* No diagnostic level text will be included in the @ref prefix part.
*/
indent() : l(none) {}
/**
* @param lvl Diagnostic level to include in prefix
*/
indent(level lvl) : l(lvl) {}
level l;
};
/**
* This is called when you use the @ref indent stream manipulator.
*
* @internal
* @see dbg::indent
*/
std::ostream &operator<<(std::ostream &s, const indent &i);
/**
* This is called when you send a @ref source_pos to a diagnostic output.
* You can use this to easily check the flow of execcution in your
* program.
*
* For example,
* <pre>
* dbg::out(dbg::tracing) << DBG_HERE << std::endl;
* </pre>
*
* Take care that you only send DBG_HERE to the diagnostic outputs
* (obtained with @ref dbg::out) and not "ordinary" streams like
* <code>std::cout</code>.
*
* In non debug builds, DBG_HERE is a "no-op" doing nothing, and so no
* useful output will be produced on cout.
*
* @internal
* @see dbg::indent
*/
std::ostream &operator<<(std::ostream &s, const source_pos &pos);
/**************************************************************************
* Behaviour
*************************************************************************/
/**
* Sets what happens when assertions (or other constraints) trigger. There
* will always be diagnostic ouput. Assertions have 'abort' behaviour by
* default - like the ISO C standard, they cause an abort.
*
* If an assertion is encountered at the fatal level, the debugging library
* will abort the program regardless of this behaviour setting.
*
* If a diagnostic level is not enabled (see @ref enable) then the
* @ref assertion_behaviour is not enacted, and no output is produced.
*
* @param lvl Diagnostic level to set behaviour for
* @param behaviour Assertion behaviour
* @see dbg::set_assertion_period
* @see dbg::enable
* @see dbg::assertion
* @see dbg::sentinel
* @see dbg::unimplemented
* @see dbg::check_ptr
*/
void set_assertion_behaviour(level lvl, assertion_behaviour behaviour);
/**
* You may want an assertion to trigger once only and then for subsequent
* calls to remain inactive. For example, if there is an @ref assertion in
* a loop you may not want diagnostics produced for each loop iteration.
*
* To do this, you do the following:
* <pre>
* // Prevent several thousand diagnostic print outs
* dbg::set_assertion_period(CLOCKS_PER_SEC);
*
* // Example loop
* int array[LARGE_VALUE];
* put_stuff_in_array(array);
* for(unsigned int n = 0; n < LARGE_VALUE; n++)
* {
* dbg::assertion(DBG_ASSERT(array[n] != 0));
* do_something(array[n]);
* }
* </pre>
*
* set_assertion_period forces a certain time period between triggers of a
* particular constraint. The @ref assertion in the example above will only
* be triggered once a second (despite the fact that the constraint
* condition will be broken thousands of times a second). This will not
* affect any other @ref assertion - they will each have their own timeout
* periods.
*
* Setting a period of zero disables any constraint period.
*
* The default behaviour is to have no period.
*
* If a period is set then diagnostic printouts will include the number
* of times each constraint has been triggered (since the period was set).
* Using this, even if diagnostics don't always appear on the attached
* ostreams you have some indication of how often each constraint is
* triggered.
*
* This call only really makes sense if the @ref assertion_behaviour is
* set to @ref assertions_continue.
*
* @param period Time between triggerings of each assertion, or zero to
* disable
* @see dbg::set_assertion_behaviour
* @see dbg::assertion
* @see dbg::sentinel
* @see dbg::unimplemented
* @see dbg::check_ptr
*/
void set_assertion_period(dbgclock_t period);
/**************************************************************************
* Assertion
*************************************************************************/
/**
* Describes an @ref assertion.
*
* This is an internal data structure, you do not need to create it
* directly. Use the DBG_ASSERTION macro to create it.
*
* @internal
* @see dbg::assertion
*/
struct assert_info : public source_pos
{
bool asserted;
const char *text;
/**
* Do not call this directly. Use the DBG_ASSERTION macro.
*
* @internal
*/
assert_info(bool a, const char *t,
line_no_t line, func_name_t func,
file_name_t file, dbg_source spos)
: source_pos(line, func, file, spos), asserted(a), text(t) {}
/**
* Do not call this directly. Use the DBG_ASSERTION macro.
*
* @internal
*/
assert_info(bool a, const char *b, const source_pos &sp)
: source_pos(sp), asserted(a), text(b) {}
};
/*
* Utility macro used by the DBG_ASSERTION macro - it converts a
* macro parameter into a character string.
*/
#define DBG_STRING(a) #a
/*
* Handy macro used by clients of the @ref dbg::assertion function.
* It use is described in the @ref assertion documentation.
*
* @see dbg::assertion
*/
#define DBG_ASSERTION(a) \
::dbg::assert_info(a, DBG_STRING(a), DBG_HERE)
/**
* Used to assert a constraint in your code. Use the DBG_ASSERTION macro
* to generate the third parameter.
*
* This version creates an assertion bound to a particular @ref dbg_source.
*
* The assertion is the most general constraint utility - there are others
* which have more specific purposes (like @ref check_ptr to ensure a
* pointer is non-null). assertion allows you to test any boolean
* expression.
*
* To use assertion for a @ref dbg_source "foobar" you write code like:
* <pre>
* int i = 0;
* dbg::assertion(info, "foobar", DBG_ASSERTION(i != 0));
* </pre>
*
* If you build with debugging enabled (see @ref dbg) the program will
* produce diagnostic output to the relevant output stream if the
* constraint fails, and the appropriate @ref assertion_behaviour
* is enacted.
*
* Since in non-debug builds the expression in the DBG_ASSERTION macro
* will not be evaluated, it is important that the expression has no
* side effects.
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param ai assert_info structure created with DBG_ASSERTION
*/
void assertion(level lvl, dbg_source src, const assert_info &ai);
/**
* Overloaded version of @ref assertion that is not bound to a particular
* @ref dbg_source.
*
* @param lvl Diagnostic level to assert at
* @param ai assert_info structure created with DBG_ASSERTION
*/
inline void assertion(level lvl, const assert_info &ai)
{
assertion(lvl, 0, ai);
}
/**
* Overloaded version of @ref assertion that defaults to the
* warning @ref level.
*
* @param src String describing the diagnostic source
* @param ai assert_info structure created with DBG_ASSERTION
*/
inline void assertion(dbg_source src, const assert_info &ai)
{
assertion(warning, src, ai);
}
/**
* Overloaded version of @ref assertion that defaults to the
* warning @ref level and is not bound to a particular @ref dbg_source.
*
* @param ai assert_info structure created with DBG_ASSERTION
*/
inline void assertion(const assert_info &ai)
{
assertion(warning, 0, ai);
}
/**************************************************************************
* Sentinel
*************************************************************************/
/**
* You should put this directly after a "should never get here" comment.
*
* <pre>
* int i = 5;
* if (i == 5)
* {
* std::cout << "Correct program behaviour\n";
* }
* else
* {
* dbg::sentinel(dbg::error, "foobar", DBG_HERE);
* }
* </pre>
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param here Supply DBG_HERE
*/
void sentinel(level lvl, dbg_source src, const source_pos &here);
/**
* Overloaded version of @ref sentinel that is not bound to a particular
* @ref dbg_source.
*
* @param lvl Diagnostic level to assert at
* @param here Supply DBG_HERE
*/
inline void sentinel(level lvl, const source_pos &here)
{
sentinel(lvl, 0, here);
}
/**
* Overloaded version of @ref sentinel that defaults to the warning
* @ref level and is not bound to a particular @ref dbg_source.
*
* @param src String describing the diagnostic source
* @param here Supply DBG_HERE
*/
inline void sentinel(dbg_source src, const source_pos &here)
{
sentinel(warning, src, here);
}
/**
* Overloaded version of @ref sentinel that defaults to the warning
* @ref level and is not bound to a particular @ref dbg_source.
*
* @param here Supply DBG_HERE
*/
inline void sentinel(const source_pos &here)
{
sentinel(warning, 0, here);
}
/**************************************************************************
* Unimplemented
*************************************************************************/
/**
* You should put this directly after a "this has not been implemented
* (yet)" comment.
*
* <pre>
* switch (variable)
* {
* ...
* case SOMETHING:
* {
* dbg::unimplemented(dbg::warning, "foobar", DBG_HERE);
* break;
* }
* ...
* }
* </pre>
*
* Note the "break;" above - if the @ref assertion_behaviour is non-fatal
* then execution will continue. You wouldn't want unintentional
* fall-through.
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param here Supply DBG_HERE
*
*/
void unimplemented(level lvl, dbg_source src, const source_pos &here);
/**
* Overloaded version of @ref unimplemented that is not bound to a
* particular @ref dbg_source.
*
* @param lvl Diagnostic level to assert at
* @param here Supply DBG_HERE
*/
inline void unimplemented(level lvl, const source_pos &here)
{
unimplemented(lvl, 0, here);
}
/**
* Overloaded version of @ref unimplemented that defaults to the
* warning @ref level.
*
* @param src String describing the diagnostic source
* @param here Supply DBG_HERE
*/
inline void unimplemented(dbg_source src, const source_pos &here)
{
unimplemented(warning, src, here);
}
/**
* Overloaded version of @ref unimplemented that defaults to the
* warning @ref level and is not bound to a particular @ref dbg_source.
*
* @param here Supply DBG_HERE
*/
inline void unimplemented(const source_pos &here)
{
unimplemented(warning, 0, here);
}
/**************************************************************************
* Pointer checking
*************************************************************************/
/**
* A diagnostic function to assert that a pointer is not zero.
*
* To use it you write code like:
* <pre>
* void *p = 0;
* dbg::check_ptr(dbg::info, "foobar", p, DBG_HERE);
* </pre>
*
* It's better to use this than a general purpose @ref assertion. It
* reads far more intuitively in your code.
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param p Pointer to check
* @param here Supply DBG_HERE
*/
void check_ptr(level lvl, dbg_source src, const void *p, const source_pos &here);
/**
* Overloaded version of @ref check_ptr that is not bound to a particular
* @ref dbg_source.
*
* @param lvl Diagnostic level to assert at
* @param p Pointer to check
* @param here Supply DBG_HERE
*/
inline void check_ptr(level lvl, const void *p, const source_pos &here)
{
check_ptr(lvl, 0, p, here);
}
/**
* Overloaded version of @ref check_ptr that defaults to the
* warning @ref level.
*
* @param src String describing the diagnostic source
* @param p Pointer to check
* @param here Supply DBG_HERE
*/
inline void check_ptr(dbg_source src, const void *p, const source_pos &here)
{
check_ptr(warning, src, p, here);
}
/**
* Overloaded version of @ref check_ptr that defaults to the
* warning @ref level and is not bound to a particular @ref dbg_source.
*
* @param p Pointer to check
* @param here Supply DBG_HERE
*/
inline void check_ptr(const void *p, const source_pos &here)
{
check_ptr(warning, 0, p, here);
}
/**************************************************************************
* Bounds checking
*************************************************************************/
/**
* Utility that determines the number of elements in an array. Used
* by the @ref check_bounds constraint utility function.
*
* This is not available in non-debug versions, so do not use it
* directly.
*
* @param array Array to determine size of
* @return The number of elements in the array
* @internal
*/
template <class T>
inline unsigned int array_size(T &array)
{
return sizeof(array)/sizeof(array[0]);
}
/**
* A diagnostic function to assert that an array access is not out
* of bounds.
*
* You probably want to use the more convenient check_bounds versions
* below if you are accessing an array whose definition is in scope -
* the compiler will then safely detrmine the size of the array for you.
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param index Test index
* @param bound Boundary value (index must be < bound, and >= 0)
* @param here Supply DBG_HERE
*/
void check_bounds(level lvl, dbg_source src,
int index, int bound, const source_pos &here);
/**
* A diagnostic function to assert that an array access is not out
* of bounds. With this version you can specify the minimum and maximum
* bound value.
*
* You probably want to use the more convenient check_bounds version
* below if you are accessing an array whose definition is in scope -
* the compiler will then safely detrmine the size of the array for you.
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param index Test index
* @param minbound Minimum bound (index must be >= minbound
* @param maxbound Minimum bound (index must be < maxbound)
* @param here Supply DBG_HERE
*/
inline void check_bounds(level lvl, dbg_source src,
int index, int minbound, int maxbound,
const source_pos &here)
{
check_bounds(lvl, src, index-minbound, maxbound, here);
}
/**
* Overloaded version of check_bounds that can automatically determine the
* size of an array if it within the current scope.
*
* You use it like this:
* <pre>
* int a[10];
* int index = 10;
* dbg::check_bounds(dbg::error, index, a, DBG_HERE);
* a[index] = 5;
* </pre>
*
* @param lvl Diagnostic level to assert at
* @param src String describing the diagnostic source
* @param index Test index
* @param array Array index is applied to
* @param here Supply DBG_HERE
*/
template <class T>
void check_bounds(level lvl, dbg_source src,
int index, T &array, const source_pos &here)
{
check_bounds(lvl, src, index, array_size(array), here);
}
/**
* Overloaded version of @ref check_bounds that is not bound to a
* particular @ref dbg_source.
*
* @param lvl Diagnostic level to assert at
* @param index Test index
* @param array Array index is applied to
* @param here Supply DBG_HERE
*/
template <class T>
void check_bounds(level lvl, int index, T &array, const source_pos &here)
{
check_bounds(lvl, 0, index, array_size(array), here);
}
/**
* Overloaded version of @ref check_bounds that defaults to the
* warning @ref level.
*
* @param src String describing the diagnostic source
* @param index Test index
* @param array Array index is applied to
* @param here Supply DBG_HERE
*/
template <class T>
void check_bounds(dbg_source src, int index, T &array,
const source_pos &here)
{
check_bounds(warning, src, index, array_size(array), here);
}
/**
* Overloaded version of @ref check_bounds that defaults to the
* warning @ref level and is not bound to a particular @ref dbg_source.
*
* @param index Test index
* @param array Array index is applied to
* @param here Supply DBG_HERE
*/
template <class T>
void check_bounds(int index, T &array, const source_pos &here)
{
check_bounds(warning, 0, index, array_size(array), here);
}
/**************************************************************************
* Tracing
*************************************************************************/
/**
* The trace class allows you to easily produce tracing diagnostics.
*
* When the ctor is called, it prints "->" and the name of the
* function, increasing the indent level. When the object is deleted
* it prints "<-" followed again by the name of the function.
*
* You can use the name of the current function gathered via the
* DBG_HERE macro, or some other tracing string you supply.
*
* Diagnostics are produced at the tracing @ref level.
*
* For example, if you write the following code:
*
* <pre>
* void foo()
* {
* dbg::trace t1(DBG_HERE);
* // do some stuff
* {
* dbg::trace t2("sub block");
* // do some stuff
* dbg::out(tracing) << dbg::prefix() << "Hello!\n";
* }
* dbg::out(tracing) << dbg::prefix() << "Hello again!\n";
* // more stuff
* }
* </pre>
*
* You will get the following tracing information:
*
* <pre>
* *** ->foo (0 in foo.cpp)
* *** ->sub block
* *** Hello!
* *** <-sub block
* *** Hello again!
* *** <-foo (0 in foo.cpp)
* </pre>
*
* Don't forget to create named dbg::trace objects. If you create
* anonymous objects (i.e. you just wrote "dbg::trace(DBG_HERE);")
* then the destructor will be called immediately, rather than at the
* end of the block scope, causing invalid trace output.
*
* Tracing does not cause assertions to trigger, therefore you will
* never generate an abort or exception using this object.
*
* If you disable the tracing diagnostic @ref level before the trace
* object's destructor is called you will still get the closing trace
* output. This is important, otherwise the indentation level of the
* library would get out of sync. In this case, the closing diagnostic
* output will have a "note" attached to indicate what has happened.
*
* Similarly, if tracing diagnostics are off when the trace object is
* created, yet subsequencently enabled before the destructor there will
* be no closing tracing ouput.
*/
class trace
{
public:
/**
* Provide the function name, or some other tracing string.
*
* This will not tie the trace object to a particular
* @ref dbg_source.
*
* @param name Tracing block name
*/
trace(func_name_t name);
/**
* @param src String describing the diagnostic source
* @param name Tracing block name
*/
trace(dbg_source src, func_name_t name);
/**
* This will not tie the trace object to a particular
* @ref dbg_source.
*
* @param here Supply DBG_HERE
*/
trace(const source_pos &here);
/**
* @param src String describing the diagnostic source
* @param here Supply DBG_HERE
*/
trace(dbg_source src, const source_pos &here);
~trace();
private:
trace(const trace &);
trace &operator=(const trace &);
void trace_begin();
void trace_end();
dbg_source m_src;
const char *m_name;
const source_pos m_pos;
bool m_triggered;
};
/**************************************************************************
* Post conditions
*************************************************************************/
/**
* A post condition class. This utility automates the checking of
* post conditions using @ref assertion. It requires a member function
* with the signature:
* <pre>
* bool some_class::invariant() const;
* </pre>
*
* When you create a post_mem_fun object you specify a post condition
* member function. When the post_mem_fun object is destroyed the
* postconsition is asserted.
*
* This is useful for methods where there are a number of exit points
* which would make it tedious to put the same @ref dbg::assertion
* in multiple places.
*
* It is also handy when an exception might be thrown and propagated by a
* funciton, ensuring that a postcondition is first checked. Bear in mind
* that Bad Things can happen if the @ref assertion_behaviour is
* assertions_throw and this is triggered via a propagating exception.
*
* An example of usage, the do_test method below uses the post_mem_fun
* object:
* <pre>
* class test
* {
* public:
* test() : a(10) {}
* do_test()
* {
* dbg::post_mem_fun<test>
* post(dbg::info, this, &test::invariant, DBG_HERE);
* a = 9;
* if (SOME_CONDITION)
* {
* return; // (*)
* }
* else if (SOME_OTHER_CONDITION)
* {
* throw std::exception(); // (*)
* }
* // (*)
* }
* private:
* bool invariant()
* {
* return a == 10;
* }
* int a;
* };
* </pre>
* The post condition will be asserted at each point marked (*).
*
* @see dbg::post
*/
template <class obj_t>
class post_mem_fun
{
public:
/**
* The type of the contraint function. It returns a bool and
* takes no parameters.
*/
typedef bool (obj_t::*fn_t)();
/**
* @param lvl Diagnostic level
* @param obj Object to invoke @p fn on (usually "this")
* @param fn Post condition member function
* @param here Supply DBG_HERE
*/
post_mem_fun(level lvl, obj_t *obj, fn_t fn, const source_pos &pos)
: m_lvl(lvl), m_src(0), m_obj(obj), m_fn(fn), m_pos(pos) {}
/**
* @param lvl Diagnostic level
* @param src String describing the diagnostic source
* @param obj Object to invoke @p fn on (usually "this")
* @param fn Post condition member function
* @param here Supply DBG_HERE
*/
post_mem_fun(level lvl, dbg_source src,
obj_t *obj, fn_t fn, const source_pos &pos)
: m_lvl(lvl), m_src(src), m_obj(obj), m_fn(fn), m_pos(pos) {}
/**
* Overloaded version of constructor which defaults to the
* @ref warning diagnostic level.
*
* @param obj Object to invoke @p fn on (usually "this")
* @param fn Post condition member function
* @param here Supply DBG_HERE
*/
post_mem_fun(obj_t *obj, fn_t fn, const source_pos &pos)
: m_lvl(dbg::warning), m_src(0),
m_obj(obj), m_fn(fn), m_pos(pos) {}
/**
* Overloaded version of constructor which defaults to the
* @ref warning diagnostic level.
*
* @param src String describing the diagnostic source
* @param obj Object to invoke @p fn on (usually "this")
* @param fn Post condition member function
* @param here Supply DBG_HERE
*/
post_mem_fun(dbg_source src, obj_t *obj, fn_t fn,
const source_pos &pos)
: m_lvl(dbg::warning), m_src(src),
m_obj(obj), m_fn(fn), m_pos(pos) {}
/**
* The destructor asserts the post condition.
*/
~post_mem_fun()
{
assertion(m_lvl, m_src,
assert_info((m_obj->*m_fn)(), "post condition",
m_pos.line, m_pos.func, m_pos.file, m_pos.src));
}
private:
const level m_lvl;
const dbg_source m_src;
obj_t *m_obj;
fn_t m_fn;
const source_pos m_pos;
};
/**
* A post condition class. Unlike @ref post_mem_fun, this class
* calls a non-member function with signature:
* <pre>
* bool some_function();
* </pre>
*
* Otherwise, use it identically to the @ref post_mem_fun.
*
* @see dbg::post_mem_fun
*/
class post
{
public:
/**
* The type of the contraint function. It returns a bool and
* takes no parameters.
*/
typedef bool (*fn_t)();
/**
* @param lvl Diagnostic level
* @param fn Post condition function
* @param here Supply DBG_HERE
*/
post(level lvl, fn_t fn, const source_pos &pos)
: m_lvl(lvl), m_src(0), m_fn(fn), m_pos(pos) {}
/**
* @param lvl Diagnostic level
* @param src String describing the diagnostic source
* @param fn Post condition function
* @param here Supply DBG_HERE
*/
post(level lvl, dbg_source src, fn_t fn, const source_pos &pos)
: m_lvl(lvl), m_src(src), m_fn(fn), m_pos(pos) {}
/**
* Overloaded version of constructor which defaults to the
* @ref warning diagnostic level.
*
* @param fn Post condition function
* @param here Supply DBG_HERE
*/
post(fn_t fn, const source_pos &pos)
: m_lvl(dbg::warning), m_src(0), m_fn(fn), m_pos(pos) {}
/**
* Overloaded version of constructor which defaults to the
* @ref warning diagnostic level.
*
* @param src String describing the diagnostic source
* @param fn Post condition function
* @param here Supply DBG_HERE
*/
post(dbg_source src, fn_t fn, const source_pos &pos)
: m_lvl(dbg::warning), m_src(src), m_fn(fn), m_pos(pos) {}
/**
* The destructor asserts the post condition.
*/
~post()
{
assertion(m_lvl, m_src,
assert_info(m_fn(), "post condition",
m_pos.line, m_pos.func, m_pos.file, m_pos.src));
}
private:
level m_lvl;
const dbg_source m_src;
fn_t m_fn;
const source_pos m_pos;
};
/**************************************************************************
* Compile time assertions
*************************************************************************/
/**
* If we need to assert a constraint that can be calculated at compile
* time, then it would be advantageous to do so - moving error detection
* to an earlier phase in development is always a Good Thing.
*
* This utility allows you to do this. You use it like this:
*
* <pre>
* enum { foo = 4, bar = 6 };
* compile_assertion<(foo > bar)>();
* </pre>
*
* There is a particular point to observe here. Although the
* expression is now a template parameter, it is important to contain it
* in parentheses. This is simply because the expression contains a ">"
* which otherwise would be taken by the compiler to be the closing of
* the template parameter. Although not all expressions require this,
* it is good practice to do it at all times.
*/
template <bool expression>
class compile_assertion;
template <>
class compile_assertion<true> {};
#else
/**************************************************************************
* Non-debug stub versions
*************************************************************************/
/*
* With debugging switched off we generate null versions of the above
* definitions.
*
* Given a good compiler and a strong prevailing headwind, these will
* optimise away to nothing.
*/
#define DBG_HERE ((void*)0)
#define DBG_ASSERTION(a) ((void*)0)
//enum { default_source = 0xdead };
const dbg_source default_source = 0;
/**
* In non-debug versions, this class is used to replace an ostream
* so that code will compile away. Do not use it directly.
*
* @internal
*/
class null_stream
{
public:
#ifdef _MSC_VER
null_stream &operator<<(void *) { return *this; }
null_stream &operator<<(const void *) { return *this; }
null_stream &operator<<(long) { return *this; }
#else
template <class otype>
null_stream &operator<<(const otype &) { return *this; }
#endif
template <class otype>
null_stream &operator<<(otype &) { return *this; }
null_stream &operator<<(std::ostream& (*)(std::ostream&))
{
return *this;
}
};
struct prefix { prefix() {} prefix(level) {} };
struct indent { indent() {} indent(level) {} };
inline void enable(level, bool) {}
inline void enable(level, dbg_source, bool) {}
inline void enable_all(level, bool) {}
inline null_stream out(level, dbg_source) {return null_stream();}
inline null_stream out(level) {return null_stream();}
inline void attach_ostream(level, std::ostream &) {}
inline void attach_ostream(level, dbg_source, std::ostream &) {}
inline void detach_ostream(level, std::ostream &) {}
inline void detach_ostream(level, dbg_source, std::ostream &) {}
inline void detach_all_ostreams(level) {}
inline void detach_all_ostreams(level, dbg_source) {}
inline null_stream info_out() {return null_stream();}
inline null_stream warning_out() {return null_stream();}
inline null_stream error_out() {return null_stream();}
inline null_stream fatal_out() {return null_stream();}
inline null_stream trace_out() {return null_stream();}
inline void set_prefix(const char *) {}
inline void enable_level_prefix(bool) {}
inline void enable_time_prefix(bool) {}
inline void set_assertion_behaviour(level, assertion_behaviour) {}
inline void set_assertion_period(dbgclock_t) {}
inline void assertion(level, dbg_source, void *) {}
inline void assertion(level, void *) {}
inline void assertion(dbg_source, void *) {}
inline void assertion(void *) {}
inline void sentinel(level, dbg_source, void *) {}
inline void sentinel(level, void *) {}
inline void sentinel(dbg_source, void *) {}
inline void sentinel(void *) {}
inline void unimplemented(level, dbg_source, void *) {}
inline void unimplemented(level, void *) {}
inline void unimplemented(dbg_source, void *) {}
inline void unimplemented(void *) {}
inline void check_ptr(level, dbg_source, const void *, void *) {}
inline void check_ptr(level, const void *, void *) {}
inline void check_ptr(dbg_source, const void *, void *) {}
inline void check_ptr(const void *, void *) {}
inline void check_bounds(level, void *, int, int, void *) {}
inline void check_bounds(level, dbg_source, int, void*, void*) {}
inline void check_bounds(level, dbg_source, int, int,
void *, void *) {}
inline void check_bounds(level, int, void *, void*) {}
inline void check_bounds(void *, int, void *, void *) {}
inline void check_bounds(int, void *, void *) {}
class trace
{
public:
trace(const char *fn_name) {}
trace(dbg_source, const char *fn_name) {}
trace(void *here) {}
trace(dbg_source, void *here) {}
~trace() {}
};
template <class obj_t>
class post_mem_fun
{
public:
typedef bool (obj_t::*fn_t)();
post_mem_fun(level, void *, fn_t, void *) {}
post_mem_fun(level, dbg_source, void *, fn_t, void *) {}
post_mem_fun(void *, fn_t, void *) {}
post_mem_fun(dbg_source, void *, fn_t, void *) {}
~post_mem_fun() {}
};
class post
{
public:
typedef bool(*fn_t)();
post(level, fn_t, void *) {}
post(level, dbg_source, fn_t, void *) {}
post(fn_t, void *) {}
post(dbg_source, fn_t, void *) {}
~post() {}
};
template <bool expression>
class compile_assertion {};
#endif
}
#endif
|