cmCPackLog.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include "cmSystemTools.h"
  16. //----------------------------------------------------------------------
  17. cmCPackLog::cmCPackLog()
  18. {
  19. this->Verbose = false;
  20. this->Debug = false;
  21. this->Quiet = false;
  22. this->NewLine = true;
  23. this->LastTag = cmCPackLog::NOTAG;
  24. #undef cerr
  25. #undef cout
  26. this->DefaultOutput = &std::cout;
  27. this->DefaultError = &std::cerr;
  28. this->LogOutput = 0;
  29. this->LogOutputCleanup = false;
  30. }
  31. //----------------------------------------------------------------------
  32. cmCPackLog::~cmCPackLog()
  33. {
  34. this->SetLogOutputStream(0);
  35. }
  36. //----------------------------------------------------------------------
  37. void cmCPackLog::SetLogOutputStream(std::ostream* os)
  38. {
  39. if ( this->LogOutputCleanup && this->LogOutput )
  40. {
  41. delete this->LogOutput;
  42. }
  43. this->LogOutputCleanup = false;
  44. this->LogOutput = os;
  45. }
  46. //----------------------------------------------------------------------
  47. bool cmCPackLog::SetLogOutputFile(const char* fname)
  48. {
  49. cmGeneratedFileStream *cg = 0;
  50. if ( fname )
  51. {
  52. cg = new cmGeneratedFileStream(fname);
  53. }
  54. if ( cg && !*cg )
  55. {
  56. delete cg;
  57. cg = 0;
  58. }
  59. this->SetLogOutputStream(cg);
  60. if ( !cg )
  61. {
  62. return false;
  63. }
  64. this->LogOutputCleanup = true;
  65. return true;
  66. }
  67. //----------------------------------------------------------------------
  68. void cmCPackLog::Log(int tag, const char* file, int line,
  69. const char* msg, size_t length)
  70. {
  71. // By default no logging
  72. bool display = false;
  73. // Display file and line number if debug
  74. bool useFileAndLine = this->Debug;
  75. bool output = false;
  76. bool debug = false;
  77. bool warning = false;
  78. bool error = false;
  79. bool verbose = false;
  80. // When writing in file, add list of tags whenever tag changes.
  81. std::string tagString;
  82. bool needTagString = false;
  83. if ( this->LogOutput && this->LastTag != tag )
  84. {
  85. needTagString = true;
  86. }
  87. if ( tag & LOG_OUTPUT )
  88. {
  89. output = true;
  90. display = true;
  91. if ( needTagString )
  92. {
  93. if ( tagString.size() > 0 ) { tagString += ","; }
  94. tagString = "VERBOSE";
  95. }
  96. }
  97. if ( tag & LOG_WARNING )
  98. {
  99. warning = true;
  100. display = true;
  101. if ( needTagString )
  102. {
  103. if ( tagString.size() > 0 ) { tagString += ","; }
  104. tagString = "WARNING";
  105. }
  106. }
  107. if ( tag & LOG_ERROR )
  108. {
  109. error = true;
  110. display = true;
  111. if ( needTagString )
  112. {
  113. if ( tagString.size() > 0 ) { tagString += ","; }
  114. tagString = "ERROR";
  115. }
  116. }
  117. if ( tag & LOG_DEBUG && this->Debug )
  118. {
  119. debug = true;
  120. display = true;
  121. if ( needTagString )
  122. {
  123. if ( tagString.size() > 0 ) { tagString += ","; }
  124. tagString = "DEBUG";
  125. }
  126. useFileAndLine = true;
  127. }
  128. if ( tag & LOG_VERBOSE && this->Verbose )
  129. {
  130. verbose = true;
  131. display = true;
  132. if ( needTagString )
  133. {
  134. if ( tagString.size() > 0 ) { tagString += ","; }
  135. tagString = "VERBOSE";
  136. }
  137. }
  138. if ( this->Quiet )
  139. {
  140. display = false;
  141. }
  142. if ( this->LogOutput )
  143. {
  144. if ( needTagString )
  145. {
  146. *this->LogOutput << "[" << file << ":" << line << " "
  147. << tagString << "] ";
  148. }
  149. this->LogOutput->write(msg, length);
  150. }
  151. this->LastTag = tag;
  152. if ( !display )
  153. {
  154. return;
  155. }
  156. if ( this->NewLine )
  157. {
  158. if ( error && !this->ErrorPrefix.empty() )
  159. {
  160. *this->DefaultError << this->ErrorPrefix.c_str();
  161. }
  162. else if ( warning && !this->WarningPrefix.empty() )
  163. {
  164. *this->DefaultError << this->WarningPrefix.c_str();
  165. }
  166. else if ( output && !this->OutputPrefix.empty() )
  167. {
  168. *this->DefaultOutput << this->OutputPrefix.c_str();
  169. }
  170. else if ( verbose && !this->VerbosePrefix.empty() )
  171. {
  172. *this->DefaultOutput << this->VerbosePrefix.c_str();
  173. }
  174. else if ( debug && !this->DebugPrefix.empty() )
  175. {
  176. *this->DefaultOutput << this->DebugPrefix.c_str();
  177. }
  178. else if ( !this->Prefix.empty() )
  179. {
  180. *this->DefaultOutput << this->Prefix.c_str();
  181. }
  182. if ( useFileAndLine )
  183. {
  184. if ( error || warning )
  185. {
  186. *this->DefaultError << file << ":" << line << " ";
  187. }
  188. else
  189. {
  190. *this->DefaultOutput << file << ":" << line << " ";
  191. }
  192. }
  193. }
  194. if ( error || warning )
  195. {
  196. this->DefaultError->write(msg, length);
  197. this->DefaultError->flush();
  198. }
  199. else
  200. {
  201. this->DefaultOutput->write(msg, length);
  202. this->DefaultOutput->flush();
  203. }
  204. if ( msg[length-1] == '\n' || length > 2 )
  205. {
  206. this->NewLine = true;
  207. }
  208. if ( error )
  209. {
  210. cmSystemTools::SetErrorOccured();
  211. }
  212. }