cmCPackLog.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, const char* msg, size_t length)
  68. {
  69. // By default no logging
  70. bool display = false;
  71. // Display file and line number if debug
  72. bool useFileAndLine = m_Debug;
  73. bool output = false;
  74. bool debug = false;
  75. bool warning = false;
  76. bool error = false;
  77. bool verbose = false;
  78. // When writing in file, add list of tags whenever tag changes.
  79. std::string tagString;
  80. bool needTagString = false;
  81. if ( m_LogOutput && m_LastTag != tag )
  82. {
  83. needTagString = true;
  84. }
  85. if ( tag & LOG_OUTPUT )
  86. {
  87. output = true;
  88. display = true;
  89. if ( needTagString )
  90. {
  91. if ( tagString.size() > 0 ) { tagString += ","; }
  92. tagString = "VERBOSE";
  93. }
  94. }
  95. if ( tag & LOG_WARNING )
  96. {
  97. warning = true;
  98. display = true;
  99. if ( needTagString )
  100. {
  101. if ( tagString.size() > 0 ) { tagString += ","; }
  102. tagString = "WARNING";
  103. }
  104. }
  105. if ( tag & LOG_ERROR )
  106. {
  107. error = true;
  108. display = true;
  109. if ( needTagString )
  110. {
  111. if ( tagString.size() > 0 ) { tagString += ","; }
  112. tagString = "ERROR";
  113. }
  114. }
  115. if ( tag & LOG_DEBUG && m_Debug )
  116. {
  117. debug = true;
  118. display = true;
  119. if ( needTagString )
  120. {
  121. if ( tagString.size() > 0 ) { tagString += ","; }
  122. tagString = "DEBUG";
  123. }
  124. useFileAndLine = true;
  125. }
  126. if ( tag & LOG_VERBOSE && m_Verbose )
  127. {
  128. verbose = true;
  129. display = true;
  130. if ( needTagString )
  131. {
  132. if ( tagString.size() > 0 ) { tagString += ","; }
  133. tagString = "VERBOSE";
  134. }
  135. }
  136. if ( m_Quiet )
  137. {
  138. display = false;
  139. }
  140. if ( m_LogOutput )
  141. {
  142. if ( needTagString )
  143. {
  144. *m_LogOutput << "[" << file << ":" << line << " " << tagString << "] ";
  145. }
  146. m_LogOutput->write(msg, length);
  147. }
  148. m_LastTag = tag;
  149. if ( !display )
  150. {
  151. return;
  152. }
  153. if ( m_NewLine )
  154. {
  155. if ( error && !m_ErrorPrefix.empty() )
  156. {
  157. *m_DefaultError << m_ErrorPrefix.c_str();
  158. }
  159. else if ( warning && !m_WarningPrefix.empty() )
  160. {
  161. *m_DefaultError << m_WarningPrefix.c_str();
  162. }
  163. else if ( output && !m_OutputPrefix.empty() )
  164. {
  165. *m_DefaultOutput << m_OutputPrefix.c_str();
  166. }
  167. else if ( verbose && !m_VerbosePrefix.empty() )
  168. {
  169. *m_DefaultOutput << m_VerbosePrefix.c_str();
  170. }
  171. else if ( debug && !m_DebugPrefix.empty() )
  172. {
  173. *m_DefaultOutput << m_DebugPrefix.c_str();
  174. }
  175. else if ( !m_Prefix.empty() )
  176. {
  177. *m_DefaultOutput << m_Prefix.c_str();
  178. }
  179. if ( useFileAndLine )
  180. {
  181. if ( error || warning )
  182. {
  183. *m_DefaultError << file << ":" << line << " ";
  184. }
  185. else
  186. {
  187. *m_DefaultOutput << file << ":" << line << " ";
  188. }
  189. }
  190. }
  191. if ( error || warning )
  192. {
  193. m_DefaultError->write(msg, length);
  194. m_DefaultError->flush();
  195. }
  196. else
  197. {
  198. m_DefaultOutput->write(msg, length);
  199. m_DefaultOutput->flush();
  200. }
  201. if ( msg[length-1] == '\n' || length > 2 )
  202. {
  203. m_NewLine = true;;
  204. }
  205. }