cmCPackLog.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackLog.h"
  4. #include <iostream>
  5. #include "cmGeneratedFileStream.h"
  6. #include "cmSystemTools.h"
  7. cmCPackLog::cmCPackLog()
  8. {
  9. this->Verbose = false;
  10. this->Debug = false;
  11. this->Quiet = false;
  12. this->NewLine = true;
  13. this->LastTag = cmCPackLog::NOTAG;
  14. this->DefaultOutput = &std::cout;
  15. this->DefaultError = &std::cerr;
  16. this->LogOutput = nullptr;
  17. this->LogOutputCleanup = false;
  18. }
  19. cmCPackLog::~cmCPackLog()
  20. {
  21. this->SetLogOutputStream(nullptr);
  22. }
  23. void cmCPackLog::SetLogOutputStream(std::ostream* os)
  24. {
  25. if (this->LogOutputCleanup && this->LogOutput) {
  26. delete this->LogOutput;
  27. }
  28. this->LogOutputCleanup = false;
  29. this->LogOutput = os;
  30. }
  31. bool cmCPackLog::SetLogOutputFile(const char* fname)
  32. {
  33. cmGeneratedFileStream* cg = nullptr;
  34. if (fname) {
  35. cg = new cmGeneratedFileStream(fname);
  36. }
  37. if (cg && !*cg) {
  38. delete cg;
  39. cg = nullptr;
  40. }
  41. this->SetLogOutputStream(cg);
  42. if (!cg) {
  43. return false;
  44. }
  45. this->LogOutputCleanup = true;
  46. return true;
  47. }
  48. void cmCPackLog::Log(int tag, const char* file, int line, const char* msg,
  49. size_t length)
  50. {
  51. // By default no logging
  52. bool display = false;
  53. // Display file and line number if debug
  54. bool useFileAndLine = this->Debug;
  55. bool output = false;
  56. bool debug = false;
  57. bool warning = false;
  58. bool error = false;
  59. bool verbose = false;
  60. // When writing in file, add list of tags whenever tag changes.
  61. std::string tagString;
  62. bool needTagString = false;
  63. if (this->LogOutput && this->LastTag != tag) {
  64. needTagString = true;
  65. }
  66. if (tag & LOG_OUTPUT) {
  67. output = true;
  68. display = true;
  69. if (needTagString) {
  70. if (!tagString.empty()) {
  71. tagString += ",";
  72. }
  73. tagString = "VERBOSE";
  74. }
  75. }
  76. if (tag & LOG_WARNING) {
  77. warning = true;
  78. display = true;
  79. if (needTagString) {
  80. if (!tagString.empty()) {
  81. tagString += ",";
  82. }
  83. tagString = "WARNING";
  84. }
  85. }
  86. if (tag & LOG_ERROR) {
  87. error = true;
  88. display = true;
  89. if (needTagString) {
  90. if (!tagString.empty()) {
  91. tagString += ",";
  92. }
  93. tagString = "ERROR";
  94. }
  95. }
  96. if (tag & LOG_DEBUG && this->Debug) {
  97. debug = true;
  98. display = true;
  99. if (needTagString) {
  100. if (!tagString.empty()) {
  101. tagString += ",";
  102. }
  103. tagString = "DEBUG";
  104. }
  105. useFileAndLine = true;
  106. }
  107. if (tag & LOG_VERBOSE && this->Verbose) {
  108. verbose = true;
  109. display = true;
  110. if (needTagString) {
  111. if (!tagString.empty()) {
  112. tagString += ",";
  113. }
  114. tagString = "VERBOSE";
  115. }
  116. }
  117. if (this->Quiet) {
  118. display = false;
  119. }
  120. if (this->LogOutput) {
  121. if (needTagString) {
  122. *this->LogOutput << "[" << file << ":" << line << " " << tagString
  123. << "] ";
  124. }
  125. this->LogOutput->write(msg, length);
  126. }
  127. this->LastTag = tag;
  128. if (!display) {
  129. return;
  130. }
  131. if (this->NewLine) {
  132. if (error && !this->ErrorPrefix.empty()) {
  133. *this->DefaultError << this->ErrorPrefix;
  134. } else if (warning && !this->WarningPrefix.empty()) {
  135. *this->DefaultError << this->WarningPrefix;
  136. } else if (output && !this->OutputPrefix.empty()) {
  137. *this->DefaultOutput << this->OutputPrefix;
  138. } else if (verbose && !this->VerbosePrefix.empty()) {
  139. *this->DefaultOutput << this->VerbosePrefix;
  140. } else if (debug && !this->DebugPrefix.empty()) {
  141. *this->DefaultOutput << this->DebugPrefix;
  142. } else if (!this->Prefix.empty()) {
  143. *this->DefaultOutput << this->Prefix;
  144. }
  145. if (useFileAndLine) {
  146. if (error || warning) {
  147. *this->DefaultError << file << ":" << line << " ";
  148. } else {
  149. *this->DefaultOutput << file << ":" << line << " ";
  150. }
  151. }
  152. }
  153. if (error || warning) {
  154. this->DefaultError->write(msg, length);
  155. this->DefaultError->flush();
  156. } else {
  157. this->DefaultOutput->write(msg, length);
  158. this->DefaultOutput->flush();
  159. }
  160. if (msg[length - 1] == '\n' || length > 2) {
  161. this->NewLine = true;
  162. }
  163. if (error) {
  164. cmSystemTools::SetErrorOccured();
  165. }
  166. }