cmConfigureLog.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmConfigureLog.h"
  4. #include <cassert>
  5. #include <cstdio>
  6. #include <iterator>
  7. #include <sstream>
  8. #include <utility>
  9. #include <cmext/algorithm>
  10. #include <cmext/string_view>
  11. #include <cm3p/json/writer.h>
  12. #include "cm_utf8.h"
  13. #include "cmListFileCache.h"
  14. #include "cmMakefile.h"
  15. #include "cmRange.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. cmConfigureLog::cmConfigureLog(std::string logDir,
  20. std::vector<unsigned int> logVersions)
  21. : LogDir(std::move(logDir))
  22. , LogVersions(std::move(logVersions))
  23. {
  24. // Always emit events for the latest log version.
  25. static unsigned int const LatestLogVersion = 1;
  26. if (!cm::contains(this->LogVersions, LatestLogVersion)) {
  27. this->LogVersions.emplace_back(LatestLogVersion);
  28. }
  29. Json::StreamWriterBuilder builder;
  30. this->Encoder.reset(builder.newStreamWriter());
  31. }
  32. cmConfigureLog::~cmConfigureLog()
  33. {
  34. if (this->Opened) {
  35. this->EndObject();
  36. this->Stream << "...\n";
  37. }
  38. }
  39. bool cmConfigureLog::IsAnyLogVersionEnabled(
  40. std::vector<unsigned int> const& v) const
  41. {
  42. // Both input lists are sorted. Look for a matching element.
  43. auto i1 = v.cbegin();
  44. auto i2 = this->LogVersions.cbegin();
  45. while (i1 != v.cend() && i2 != this->LogVersions.cend()) {
  46. if (*i1 < *i2) {
  47. ++i1;
  48. } else if (*i2 < *i1) {
  49. ++i2;
  50. } else {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. void cmConfigureLog::WriteBacktrace(cmMakefile const& mf)
  57. {
  58. std::vector<std::string> backtrace;
  59. auto root = mf.GetCMakeInstance()->GetHomeDirectory();
  60. for (auto bt = mf.GetBacktrace(); !bt.Empty(); bt = bt.Pop()) {
  61. auto t = bt.Top();
  62. if (!t.Name.empty() || t.Line == cmListFileContext::DeferPlaceholderLine) {
  63. t.FilePath = cmSystemTools::RelativeIfUnder(root, t.FilePath);
  64. std::ostringstream s;
  65. s << t;
  66. backtrace.emplace_back(s.str());
  67. }
  68. }
  69. this->WriteValue("backtrace"_s, backtrace);
  70. }
  71. void cmConfigureLog::WriteChecks(cmMakefile const& mf)
  72. {
  73. if (!mf.GetCMakeInstance()->HasCheckInProgress()) {
  74. return;
  75. }
  76. this->BeginObject("checks"_s);
  77. for (auto const& value :
  78. cmReverseRange(mf.GetCMakeInstance()->GetCheckInProgressMessages())) {
  79. this->BeginLine() << "- ";
  80. this->Encoder->write(value, &this->Stream);
  81. this->EndLine();
  82. }
  83. this->EndObject();
  84. }
  85. void cmConfigureLog::EnsureInit()
  86. {
  87. if (this->Opened) {
  88. return;
  89. }
  90. assert(!this->Stream.is_open());
  91. std::string name = cmStrCat(this->LogDir, "/CMakeConfigureLog.yaml");
  92. this->Stream.open(name.c_str(), std::ios::out | std::ios::app);
  93. this->Opened = true;
  94. this->Stream << "\n---\n";
  95. this->BeginObject("events"_s);
  96. }
  97. cmsys::ofstream& cmConfigureLog::BeginLine()
  98. {
  99. for (unsigned i = 0; i < this->Indent; ++i) {
  100. this->Stream << " ";
  101. }
  102. return this->Stream;
  103. }
  104. void cmConfigureLog::EndLine()
  105. {
  106. this->Stream << std::endl;
  107. }
  108. void cmConfigureLog::BeginArray()
  109. {
  110. ++this->Indent;
  111. }
  112. void cmConfigureLog::NextArrayElement()
  113. {
  114. assert(this->Indent);
  115. --this->Indent;
  116. this->BeginLine() << '-';
  117. this->EndLine();
  118. ++this->Indent;
  119. }
  120. void cmConfigureLog::EndArray()
  121. {
  122. assert(this->Indent);
  123. --this->Indent;
  124. }
  125. void cmConfigureLog::BeginObject(cm::string_view key)
  126. {
  127. this->BeginLine() << key << ':';
  128. this->EndLine();
  129. ++this->Indent;
  130. }
  131. void cmConfigureLog::EndObject()
  132. {
  133. assert(this->Indent);
  134. --this->Indent;
  135. }
  136. void cmConfigureLog::BeginEvent(std::string const& kind, cmMakefile const& mf)
  137. {
  138. this->EnsureInit();
  139. this->BeginLine() << '-';
  140. this->EndLine();
  141. ++this->Indent;
  142. this->WriteValue("kind"_s, kind);
  143. this->WriteBacktrace(mf);
  144. this->WriteChecks(mf);
  145. }
  146. void cmConfigureLog::EndEvent()
  147. {
  148. assert(this->Indent);
  149. --this->Indent;
  150. }
  151. void cmConfigureLog::WriteValue(cm::string_view key, std::nullptr_t)
  152. {
  153. this->BeginLine() << key << ": null";
  154. this->EndLine();
  155. }
  156. void cmConfigureLog::WriteValue(cm::string_view key, bool value)
  157. {
  158. this->BeginLine() << key << ": " << (value ? "true" : "false");
  159. this->EndLine();
  160. }
  161. void cmConfigureLog::WriteValue(cm::string_view key, int value)
  162. {
  163. this->BeginLine() << key << ": " << value;
  164. this->EndLine();
  165. }
  166. void cmConfigureLog::WriteValue(cm::string_view key, std::string const& value)
  167. {
  168. this->BeginLine() << key << ": ";
  169. this->Encoder->write(value, &this->Stream);
  170. this->EndLine();
  171. }
  172. void cmConfigureLog::WriteValue(cm::string_view key,
  173. std::vector<std::string> const& list)
  174. {
  175. this->BeginObject(key);
  176. for (auto const& value : list) {
  177. this->BeginLine() << "- ";
  178. this->Encoder->write(value, &this->Stream);
  179. this->EndLine();
  180. }
  181. this->EndObject();
  182. }
  183. void cmConfigureLog::WriteValue(cm::string_view key,
  184. std::map<std::string, std::string> const& map)
  185. {
  186. static std::string const rawKeyChars = //
  187. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" //
  188. "abcdefghijklmnopqrstuvwxyz" //
  189. "0123456789" //
  190. "-_" //
  191. ;
  192. this->BeginObject(key);
  193. for (auto const& entry : map) {
  194. if (entry.first.find_first_not_of(rawKeyChars) == std::string::npos) {
  195. this->WriteValue(entry.first, entry.second);
  196. } else {
  197. this->BeginLine();
  198. this->Encoder->write(entry.first, &this->Stream);
  199. this->Stream << ": ";
  200. this->Encoder->write(entry.second, &this->Stream);
  201. this->EndLine();
  202. }
  203. }
  204. this->EndObject();
  205. }
  206. void cmConfigureLog::WriteLiteralTextBlock(cm::string_view key,
  207. cm::string_view text)
  208. {
  209. this->BeginLine() << key << ": |";
  210. this->EndLine();
  211. auto const l = text.length();
  212. if (l) {
  213. ++this->Indent;
  214. this->BeginLine();
  215. auto i = decltype(l){ 0 };
  216. while (i < l) {
  217. // YAML allows ' ', '\t' and "printable characters", but NOT other
  218. // ASCII whitespace; those must be escaped, as must the upper UNICODE
  219. // control characters (U+0080 - U+009F)
  220. static constexpr unsigned int C1_LAST = 0x9F;
  221. auto const c = static_cast<unsigned char>(text[i]);
  222. switch (c) {
  223. case '\r':
  224. // Print a carriage return only if it is not followed by a line feed.
  225. ++i;
  226. if (i == l || text[i] != '\n') {
  227. this->WriteEscape(c);
  228. }
  229. break;
  230. case '\n':
  231. // Print any line feeds except the very last one
  232. if (i + 1 < l) {
  233. this->EndLine();
  234. this->BeginLine();
  235. }
  236. ++i;
  237. break;
  238. case '\t':
  239. // Print horizontal tab verbatim
  240. this->Stream.put('\t');
  241. ++i;
  242. break;
  243. case '\\':
  244. // Escape backslash for disambiguation
  245. this->Stream << "\\\\";
  246. ++i;
  247. break;
  248. default:
  249. if (c >= 32 && c < 127) {
  250. // Print ascii byte.
  251. this->Stream.put(text[i]);
  252. ++i;
  253. break;
  254. } else if (c > 127) {
  255. // Decode a UTF-8 sequence.
  256. unsigned int c32;
  257. auto const* const s = text.data() + i;
  258. auto const* const e = text.data() + l;
  259. auto const* const n = cm_utf8_decode_character(s, e, &c32);
  260. if (n > s && c32 > C1_LAST) {
  261. auto const k = std::distance(s, n);
  262. this->Stream.write(s, static_cast<std::streamsize>(k));
  263. i += static_cast<unsigned>(k);
  264. break;
  265. }
  266. }
  267. // Escape non-printable byte.
  268. this->WriteEscape(c);
  269. ++i;
  270. break;
  271. }
  272. }
  273. this->EndLine();
  274. --this->Indent;
  275. }
  276. }
  277. void cmConfigureLog::WriteEscape(unsigned char c)
  278. {
  279. char buffer[6];
  280. int n = snprintf(buffer, sizeof(buffer), "\\x%02x", c);
  281. if (n > 0) {
  282. this->Stream.write(buffer, n);
  283. }
  284. }