cmCPackLog.cxx 5.2 KB

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