Coverage Report

Created: 2026-02-09 06:05

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