wdebug.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdlib.h>
  19. #include "wdebug.h"
  20. #include "streamer.h"
  21. #include "odevice.h"
  22. static MsgManager *msg_manager=NULL;
  23. static int debug_enabled=0;
  24. static ostream *debug_ostream=NULL;
  25. static Streamer debug_streamer;
  26. static int info_enabled=0;
  27. static ostream *info_ostream=NULL;
  28. static Streamer info_streamer;
  29. static int warn_enabled=0;
  30. static ostream *warn_ostream=NULL;
  31. static Streamer warn_streamer;
  32. static int error_enabled=0;
  33. static ostream *error_ostream=NULL;
  34. static Streamer error_streamer;
  35. // Don't dare touch this semaphore in application code!
  36. #ifdef USE_DEBUG_SEM
  37. Sem4 DebugLibSemaphore;
  38. #else
  39. CritSec DebugLibSemaphore;
  40. #endif
  41. int MsgManager::setAllStreams(OutputDevice *device)
  42. {
  43. if (device==NULL)
  44. return(1);
  45. DEBUGLOCK;
  46. debug_streamer.setOutputDevice(device);
  47. delete(debug_ostream);
  48. debug_ostream=new ostream(&debug_streamer);
  49. info_streamer.setOutputDevice(device);
  50. delete(info_ostream);
  51. info_ostream=new ostream(&info_streamer);
  52. warn_streamer.setOutputDevice(device);
  53. delete(warn_ostream);
  54. warn_ostream=new ostream(&warn_streamer);
  55. error_streamer.setOutputDevice(device);
  56. delete(error_ostream);
  57. error_ostream=new ostream(&error_streamer);
  58. DEBUGUNLOCK;
  59. return(0);
  60. }
  61. int MsgManager::ReplaceAllStreams(FileD * output_device, IN char *device_filename, IN char *copy_filename)
  62. {
  63. DebugLibSemaphore.Wait();
  64. delete(debug_ostream);
  65. delete(info_ostream);
  66. delete(warn_ostream);
  67. delete(error_ostream);
  68. if (output_device != NULL)
  69. {
  70. delete(output_device);
  71. output_device = NULL;
  72. }
  73. rename(device_filename, copy_filename);
  74. // FileD new_device(device_filename);
  75. output_device = new FileD(device_filename);
  76. debug_streamer.setOutputDevice(output_device);
  77. debug_ostream = new ostream(&debug_streamer);
  78. info_streamer.setOutputDevice(output_device);
  79. info_ostream=new ostream(&info_streamer);
  80. warn_streamer.setOutputDevice(output_device);
  81. warn_ostream = new ostream(&warn_streamer);
  82. error_streamer.setOutputDevice(output_device);
  83. error_ostream = new ostream(&error_streamer);
  84. DebugLibSemaphore.Post();
  85. return(0);
  86. }
  87. int MsgManager::setDebugStream(OutputDevice *device)
  88. {
  89. if (device==NULL)
  90. return(1);
  91. DEBUGLOCK;
  92. debug_streamer.setOutputDevice(device);
  93. delete(debug_ostream);
  94. debug_ostream=new ostream(&debug_streamer);
  95. DEBUGUNLOCK;
  96. return(0);
  97. }
  98. int MsgManager::setInfoStream(OutputDevice *device)
  99. {
  100. if (device==NULL)
  101. return(1);
  102. DEBUGLOCK;
  103. info_streamer.setOutputDevice(device);
  104. delete(info_ostream);
  105. info_ostream=new ostream(&info_streamer);
  106. DEBUGUNLOCK;
  107. return(0);
  108. }
  109. int MsgManager::setWarnStream(OutputDevice *device)
  110. {
  111. if (device==NULL)
  112. return(1);
  113. DEBUGLOCK;
  114. warn_streamer.setOutputDevice(device);
  115. delete(warn_ostream);
  116. warn_ostream=new ostream(&warn_streamer);
  117. DEBUGUNLOCK;
  118. return(0);
  119. }
  120. int MsgManager::setErrorStream(OutputDevice *device)
  121. {
  122. if (device==NULL)
  123. return(1);
  124. DEBUGLOCK;
  125. error_streamer.setOutputDevice(device);
  126. delete(error_ostream);
  127. error_ostream=new ostream(&error_streamer);
  128. DEBUGUNLOCK;
  129. return(0);
  130. }
  131. ostream *MsgManager::debugStream(void)
  132. {
  133. return(debug_ostream);
  134. }
  135. ostream *MsgManager::infoStream(void)
  136. {
  137. return(info_ostream);
  138. }
  139. ostream *MsgManager::warnStream(void)
  140. {
  141. return(warn_ostream);
  142. }
  143. ostream *MsgManager::errorStream(void)
  144. {
  145. return(error_ostream);
  146. }