debug.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "sys.hpp"
  2. #include "debug.hpp"
  3. #include <stdlib.h>
  4. #ifdef CWDEBUG
  5. namespace objsim { // >
  6. namespace debug { // >--> This part must match DEBUGCHANNELS, see debug.h
  7. namespace channels { // >
  8. namespace dc {
  9. // Add new debug channels here.
  10. channel_ct custom ("CUSTOM");
  11. channel_ct layer ("LAYER");
  12. channel_ct simloop("SIMLOOP");
  13. channel_ct element("ELEMENT");
  14. channel_ct con ("CONNECTION");
  15. channel_ct input ("INPUT");
  16. channel_ct smod ("SIMMODULE");
  17. channel_ct anyopt ("ANYOPT");
  18. channel_ct learn ("LEARN");
  19. channel_ct main ("MAIN");
  20. channel_ct para ("PARASCAN");
  21. channel_ct utils ("UTILS");
  22. channel_ct chunk ("CHUNKFILE");
  23. } // namespace dc
  24. } // namespace DEBUGCHANNELS
  25. // Initialize debugging code from new threads.
  26. void init_thread(void)
  27. {
  28. // Everything below needs to be repeated at the start of every
  29. // thread function, because every thread starts in a completely
  30. // reset state with all debug channels off etc.
  31. #if LIBCWD_THREAD_SAFE // For the non-threaded case this is set by the rcfile.
  32. // Turn on all debug channels by default.
  33. // ForAllDebugChannels(while(!debugChannel.is_on()) debugChannel.on());
  34. // Turn off specific debug channels.
  35. Debug(dc::bfd.off());
  36. Debug(dc::malloc.off());
  37. #endif
  38. // Turn on debug output.
  39. // Only turn on debug output when the environment variable SUPPRESS_DEBUG_OUTPUT is not set.
  40. Debug(if (getenv("SUPPRESS_DEBUG_OUTPUT") == NULL) libcw_do.on());
  41. #if LIBCWD_THREAD_SAFE
  42. Debug(libcw_do.set_ostream(&std::cout, &cout_mutex));
  43. // Set the thread id in the margin.
  44. char margin[12];
  45. sprintf(margin, "%-10lu ", pthread_self());
  46. Debug(libcw_do.margin().assign(margin, 11));
  47. #else
  48. Debug(libcw_do.set_ostream(&std::cout));
  49. #endif
  50. // Write a list of all existing debug channels to the default debug device.
  51. Debug(list_channels_on(libcw_do));
  52. }
  53. // Initialize debugging code from main().
  54. void init(void)
  55. {
  56. // You want this, unless you mix streams output with C output.
  57. // Read http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#8 for an explanation.
  58. // We can't use it, because other code uses printf to write to the console.
  59. #if 0
  60. Debug(set_invisible_on());
  61. std::ios::sync_with_stdio(false); // Cause "memory leaks" ([w]cin, [w]cout and [w]cerr filebuf allocations).
  62. Debug(set_invisible_off());
  63. #endif
  64. // This will warn you when you are using header files that do not belong to the
  65. // shared libcwd object that you linked with.
  66. Debug(check_configuration());
  67. #if CWDEBUG_ALLOC
  68. // Remove all current (pre- main) allocations from the Allocated Memory Overview.
  69. libcwd::make_all_allocations_invisible_except(NULL);
  70. #endif
  71. Debug(read_rcfile());
  72. init_thread();
  73. }
  74. } // namespace debug
  75. } // namespace myproject
  76. void turnOnAllDebug()
  77. {
  78. // Turn on debug object `libcw_do'.
  79. Debug( libcw_do.on() );
  80. // Turn on all debug channels that are off.
  81. ForAllDebugChannels(
  82. if (!debugChannel.is_on())
  83. debugChannel.on();
  84. );
  85. Debug( dc::malloc.off() );
  86. Debug( dc::bfd.off() );
  87. // Print a listing of all debug channels to debug object `libcw_do'.
  88. Debug( list_channels_on(libcw_do) );
  89. }
  90. void turnOffAllDebug()
  91. {
  92. // Turn on all debug channels that are off.
  93. ForAllDebugChannels(
  94. if (debugChannel.is_on())
  95. debugChannel.off();
  96. );
  97. // Print a listing of all debug channels to debug object `libcw_do'.
  98. Debug( list_channels_on(libcw_do) );
  99. }
  100. #endif // CWDEBUG