cmCPackLog.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. m_Verbose = false;
  19. m_Debug = false;
  20. m_Quiet = false;
  21. m_NewLine = true;
  22. m_LastTag = cmCPackLog::NOTAG;
  23. #undef cerr
  24. #undef cout
  25. m_DefaultOutput = &std::cout;
  26. m_DefaultError = &std::cerr;
  27. m_LogOutput = 0;
  28. m_LogOutputCleanup = false;
  29. }
  30. //----------------------------------------------------------------------
  31. cmCPackLog::~cmCPackLog()
  32. {
  33. this->SetLogOutputStream(0);
  34. }
  35. //----------------------------------------------------------------------
  36. void cmCPackLog::SetLogOutputStream(std::ostream* os)
  37. {
  38. if ( m_LogOutputCleanup && m_LogOutput )
  39. {
  40. delete m_LogOutput;
  41. }
  42. m_LogOutputCleanup = false;
  43. m_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. m_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 = m_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 ( m_LogOutput && m_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 && m_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 && m_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 ( m_Quiet )
  138. {
  139. display = false;
  140. }
  141. if ( m_LogOutput )
  142. {
  143. if ( needTagString )
  144. {
  145. *m_LogOutput << "[" << file << ":" << line << " " << tagString << "] ";
  146. }
  147. m_LogOutput->write(msg, length);
  148. }
  149. m_LastTag = tag;
  150. if ( !display )
  151. {
  152. return;
  153. }
  154. if ( m_NewLine )
  155. {
  156. if ( error && !m_ErrorPrefix.empty() )
  157. {
  158. *m_DefaultError << m_ErrorPrefix.c_str();
  159. }
  160. else if ( warning && !m_WarningPrefix.empty() )
  161. {
  162. *m_DefaultError << m_WarningPrefix.c_str();
  163. }
  164. else if ( output && !m_OutputPrefix.empty() )
  165. {
  166. *m_DefaultOutput << m_OutputPrefix.c_str();
  167. }
  168. else if ( verbose && !m_VerbosePrefix.empty() )
  169. {
  170. *m_DefaultOutput << m_VerbosePrefix.c_str();
  171. }
  172. else if ( debug && !m_DebugPrefix.empty() )
  173. {
  174. *m_DefaultOutput << m_DebugPrefix.c_str();
  175. }
  176. else if ( !m_Prefix.empty() )
  177. {
  178. *m_DefaultOutput << m_Prefix.c_str();
  179. }
  180. if ( useFileAndLine )
  181. {
  182. if ( error || warning )
  183. {
  184. *m_DefaultError << file << ":" << line << " ";
  185. }
  186. else
  187. {
  188. *m_DefaultOutput << file << ":" << line << " ";
  189. }
  190. }
  191. }
  192. if ( error || warning )
  193. {
  194. m_DefaultError->write(msg, length);
  195. m_DefaultError->flush();
  196. }
  197. else
  198. {
  199. m_DefaultOutput->write(msg, length);
  200. m_DefaultOutput->flush();
  201. }
  202. if ( msg[length-1] == '\n' || length > 2 )
  203. {
  204. m_NewLine = true;
  205. }
  206. }