cmCPackLog.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackLog.h"
  14. #include "cmGeneratedFileStream.h"
  15. //----------------------------------------------------------------------
  16. cmCPackLog::cmCPackLog()
  17. {
  18. this->Verbose = false;
  19. this->Debug = false;
  20. this->Quiet = false;
  21. this->NewLine = true;
  22. this->LastTag = cmCPackLog::NOTAG;
  23. #undef cerr
  24. #undef cout
  25. this->DefaultOutput = &std::cout;
  26. this->DefaultError = &std::cerr;
  27. this->LogOutput = 0;
  28. this->LogOutputCleanup = false;
  29. }
  30. //----------------------------------------------------------------------
  31. cmCPackLog::~cmCPackLog()
  32. {
  33. this->SetLogOutputStream(0);
  34. }
  35. //----------------------------------------------------------------------
  36. void cmCPackLog::SetLogOutputStream(std::ostream* os)
  37. {
  38. if ( this->LogOutputCleanup && this->LogOutput )
  39. {
  40. delete this->LogOutput;
  41. }
  42. this->LogOutputCleanup = false;
  43. this->LogOutput = os;
  44. }
  45. //----------------------------------------------------------------------
  46. bool cmCPackLog::SetLogOutputFile(const char* fname)
  47. {
  48. cmGeneratedFileStream *cg = 0;
  49. if ( fname )
  50. {
  51. cg = new cmGeneratedFileStream(fname);
  52. }
  53. if ( cg && !*cg )
  54. {
  55. delete cg;
  56. cg = 0;
  57. }
  58. this->SetLogOutputStream(cg);
  59. if ( !cg )
  60. {
  61. return false;
  62. }
  63. this->LogOutputCleanup = true;
  64. return true;
  65. }
  66. //----------------------------------------------------------------------
  67. void cmCPackLog::Log(int tag, const char* file, int line,
  68. const char* msg, size_t length)
  69. {
  70. // By default no logging
  71. bool display = false;
  72. // Display file and line number if debug
  73. bool useFileAndLine = this->Debug;
  74. bool output = false;
  75. bool debug = false;
  76. bool warning = false;
  77. bool error = false;
  78. bool verbose = false;
  79. // When writing in file, add list of tags whenever tag changes.
  80. std::string tagString;
  81. bool needTagString = false;
  82. if ( this->LogOutput && this->LastTag != tag )
  83. {
  84. needTagString = true;
  85. }
  86. if ( tag & LOG_OUTPUT )
  87. {
  88. output = true;
  89. display = true;
  90. if ( needTagString )
  91. {
  92. if ( tagString.size() > 0 ) { tagString += ","; }
  93. tagString = "VERBOSE";
  94. }
  95. }
  96. if ( tag & LOG_WARNING )
  97. {
  98. warning = true;
  99. display = true;
  100. if ( needTagString )
  101. {
  102. if ( tagString.size() > 0 ) { tagString += ","; }
  103. tagString = "WARNING";
  104. }
  105. }
  106. if ( tag & LOG_ERROR )
  107. {
  108. error = true;
  109. display = true;
  110. if ( needTagString )
  111. {
  112. if ( tagString.size() > 0 ) { tagString += ","; }
  113. tagString = "ERROR";
  114. }
  115. }
  116. if ( tag & LOG_DEBUG && this->Debug )
  117. {
  118. debug = true;
  119. display = true;
  120. if ( needTagString )
  121. {
  122. if ( tagString.size() > 0 ) { tagString += ","; }
  123. tagString = "DEBUG";
  124. }
  125. useFileAndLine = true;
  126. }
  127. if ( tag & LOG_VERBOSE && this->Verbose )
  128. {
  129. verbose = true;
  130. display = true;
  131. if ( needTagString )
  132. {
  133. if ( tagString.size() > 0 ) { tagString += ","; }
  134. tagString = "VERBOSE";
  135. }
  136. }
  137. if ( this->Quiet )
  138. {
  139. display = false;
  140. }
  141. if ( this->LogOutput )
  142. {
  143. if ( needTagString )
  144. {
  145. *this->LogOutput << "[" << file << ":" << line << " "
  146. << tagString << "] ";
  147. }
  148. this->LogOutput->write(msg, length);
  149. }
  150. this->LastTag = tag;
  151. if ( !display )
  152. {
  153. return;
  154. }
  155. if ( this->NewLine )
  156. {
  157. if ( error && !this->ErrorPrefix.empty() )
  158. {
  159. *this->DefaultError << this->ErrorPrefix.c_str();
  160. }
  161. else if ( warning && !this->WarningPrefix.empty() )
  162. {
  163. *this->DefaultError << this->WarningPrefix.c_str();
  164. }
  165. else if ( output && !this->OutputPrefix.empty() )
  166. {
  167. *this->DefaultOutput << this->OutputPrefix.c_str();
  168. }
  169. else if ( verbose && !this->VerbosePrefix.empty() )
  170. {
  171. *this->DefaultOutput << this->VerbosePrefix.c_str();
  172. }
  173. else if ( debug && !this->DebugPrefix.empty() )
  174. {
  175. *this->DefaultOutput << this->DebugPrefix.c_str();
  176. }
  177. else if ( !this->Prefix.empty() )
  178. {
  179. *this->DefaultOutput << this->Prefix.c_str();
  180. }
  181. if ( useFileAndLine )
  182. {
  183. if ( error || warning )
  184. {
  185. *this->DefaultError << file << ":" << line << " ";
  186. }
  187. else
  188. {
  189. *this->DefaultOutput << file << ":" << line << " ";
  190. }
  191. }
  192. }
  193. if ( error || warning )
  194. {
  195. this->DefaultError->write(msg, length);
  196. this->DefaultError->flush();
  197. }
  198. else
  199. {
  200. this->DefaultOutput->write(msg, length);
  201. this->DefaultOutput->flush();
  202. }
  203. if ( msg[length-1] == '\n' || length > 2 )
  204. {
  205. this->NewLine = true;
  206. }
  207. }