cmCPackLog.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // Should we go to the error stream
  72. bool errorStream = false;
  73. // Display file and line number if debug
  74. bool useFileAndLine = m_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 ( m_LogOutput && m_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. errorStream = true;
  102. if ( needTagString )
  103. {
  104. if ( tagString.size() > 0 ) { tagString += ","; }
  105. tagString = "WARNING";
  106. }
  107. }
  108. if ( tag & LOG_ERROR )
  109. {
  110. error = true;
  111. display = true;
  112. errorStream = true;
  113. if ( needTagString )
  114. {
  115. if ( tagString.size() > 0 ) { tagString += ","; }
  116. tagString = "ERROR";
  117. }
  118. }
  119. if ( tag & LOG_DEBUG && m_Debug )
  120. {
  121. debug = true;
  122. display = true;
  123. if ( needTagString )
  124. {
  125. if ( tagString.size() > 0 ) { tagString += ","; }
  126. tagString = "DEBUG";
  127. }
  128. useFileAndLine = true;
  129. }
  130. if ( tag & LOG_VERBOSE && m_Verbose )
  131. {
  132. verbose = true;
  133. display = true;
  134. if ( needTagString )
  135. {
  136. if ( tagString.size() > 0 ) { tagString += ","; }
  137. tagString = "VERBOSE";
  138. }
  139. }
  140. if ( m_Quiet )
  141. {
  142. display = false;
  143. }
  144. if ( m_LogOutput )
  145. {
  146. if ( needTagString )
  147. {
  148. *m_LogOutput << "[" << file << ":" << line << " " << tagString << "] ";
  149. }
  150. m_LogOutput->write(msg, length);
  151. }
  152. m_LastTag = tag;
  153. if ( !display )
  154. {
  155. return;
  156. }
  157. if ( m_NewLine )
  158. {
  159. if ( error && !m_ErrorPrefix.empty() )
  160. {
  161. *m_DefaultError << m_ErrorPrefix.c_str();
  162. }
  163. else if ( warning && !m_WarningPrefix.empty() )
  164. {
  165. *m_DefaultError << m_WarningPrefix.c_str();
  166. }
  167. else if ( output && !m_OutputPrefix.empty() )
  168. {
  169. *m_DefaultOutput << m_OutputPrefix.c_str();
  170. }
  171. else if ( verbose && !m_VerbosePrefix.empty() )
  172. {
  173. *m_DefaultOutput << m_VerbosePrefix.c_str();
  174. }
  175. else if ( debug && !m_DebugPrefix.empty() )
  176. {
  177. *m_DefaultOutput << m_DebugPrefix.c_str();
  178. }
  179. else if ( !m_Prefix.empty() )
  180. {
  181. *m_DefaultOutput << m_Prefix.c_str();
  182. }
  183. if ( useFileAndLine )
  184. {
  185. *m_DefaultOutput << __FILE__ << ":" << __LINE__ << " ";
  186. }
  187. }
  188. if ( error || warning )
  189. {
  190. m_DefaultError->write(msg, length);
  191. }
  192. else
  193. {
  194. m_DefaultOutput->write(msg, length);
  195. }
  196. if ( msg[length-1] == '\n' || length > 2 )
  197. {
  198. m_NewLine = true;;
  199. }
  200. }