aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/diagnostics.cxx
blob: 3246caef8da4bdf9d297b162b3c9496ba0f35438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
// file      : libbuild2/diagnostics.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/diagnostics.hxx>

#include <cstring> // strchr(), memcpy()

#include <libbutl/process-io.hxx>

#include <libbuild2/scope.hxx>
#include <libbuild2/action.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/context.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  // Diagnostics state (verbosity level, progress, etc). Keep disabled until
  // set from options.
  //
  uint16_t verb = 0;
  bool silent = true;

  optional<bool> diag_progress_option;

  bool diag_no_line = false;
  bool diag_no_column = false;

  bool stderr_term = false;

  void
  init_diag (uint16_t v, bool s, optional<bool> p, bool nl, bool nc, bool st)
  {
    assert (!s || v == 0);

    verb = v;
    silent = s;
    diag_progress_option = p;
    diag_no_line = nl;
    diag_no_column = nc;
    stderr_term = st;
  }

  // Stream verbosity.
  //
  const int stream_verb_index = ostream::xalloc ();

  // print_diag()
  //
  void
  print_diag_impl (const char* p, target_key* l, target_key&& r, const char* c)
  {
    // @@ Print directly to diag_stream (and below)? Won't we be holding
    //    the lock longer?

    diag_record dr (text);

    dr << p << ' ';

    if (l != nullptr)
    {
      // Omit the @.../ qualification in either lhs or rhs if it's implied by
      // the other.
      //
      // @@ Shouldn't we, strictly speaking, also check that they belong to
      //    the same project? Though it would be far-fetched to use another
      //    project's target from src. Or maybe not.
      //
      if (!l->out->empty ())
      {
        if (r.out->empty ())
          l->out = &empty_dir_path;
      }
      else if (!r.out->empty ())
        r.out = &empty_dir_path;

      dr << *l << ' ' << (c == nullptr ? "->" : c) << ' ';
    }

    dr << r;
  }

  template <typename L> // L can be target_key, path, or string.
  static void
  print_diag_impl (const char* p,
                   const L* l, bool lempty,
                   vector<target_key>&& rs,
                   const char* c)
  {
    assert (rs.size () > 1);

    // The overall plan is as follows:
    //
    // 1. Collect the printed names for all the group members.
    //
    //    Note if the printed representation is irregular (see
    //    to_stream(target_key) for details). We will print such members each
    //    on a separate line.
    //
    // 2. Move the names so that we end up with contiguous partitions of
    //    targets with the same name.
    //
    // 3. Print the partitions, one per line.
    //
    vector<pair<optional<string>, const target_key*>> ns;
    ns.reserve (rs.size ());

    // Use the diag_record's ostringstream so that we get the appropriate
    // stream verbosity, etc.
    //
    diag_record dr (text);
    ostringstream& os (dr.os);
    stream_verbosity sv (stream_verb (os));

    for (const target_key& k: rs)
    {
      bool r;
      if (auto p = k.type->print)
        r = p (os, k, true /* name_only */);
      else
        r = to_stream (os, k, sv, true /* name_only */);

      ns.push_back (make_pair (r ? optional<string> (os.str ()) : nullopt, &k));

      os.clear ();
      os.str (string ()); // Note: just seekp(0) is not enough.
    }

    // Partition.
    //
    auto cmp = [] (const pair<optional<string>, const target_key*>& x,
                   const pair<optional<string>, const target_key*>& y)
    {
      return (x.second->dir->compare (*y.second->dir) == 0 &&
              x.first->compare (*y.first) == 0);
    };

    // While at it also determine whether we have multiple partitions.
    //
    optional<string> ml;
    for (auto b (ns.begin ()), e (ns.end ()); b != e; )
    {
      const pair<optional<string>, const target_key*>& x (*b++);

      // Move all the elements that are equal to x to the front, preserving
      // order.
      //
      b = stable_partition (
        b, e,
        [&cmp, &x] (const pair<optional<string>, const target_key*>& y)
        {
          return (x.first && y.first && cmp (x, y));
        });

      if (!ml && b != e)
        ml = string ();
    }

    // Print.
    //
    os << p << ' ';

    if (l != nullptr)
      os << *l << (lempty ? "" : " ") << (c == nullptr ? "->" : c) << ' ';

    if (ml)
      ml = string (os.str ().size (), ' '); // Indentation.

    for (auto b (ns.begin ()), i (b), e (ns.end ()); i != e; )
    {
      if (i != b)
        os << '\n' << *ml;

      const pair<optional<string>, const target_key*>& p (*i);

      if (!p.first) // Irregular.
      {
        os << *p.second;
        ++i;
        continue;
      }

      // Calculate the number of members in this partition.
      //
      size_t n (1);
      for (auto j (i + 1); j != e && j->first && cmp (*i, *j); ++j)
        ++n;

      // Similar code to to_stream(target_key).
      //

      // Print the directory.
      //
      {
        const target_key& k (*p.second);

        uint16_t dv (sv.path);

        // Note: relative() returns empty for './'.
        //
        const dir_path& rd (dv < 1 ? relative (*k.dir) : *k.dir);

        if (!rd.empty ())
        {
          if (dv < 1)
            os << diag_relative (rd);
          else
            to_stream (os, rd, true /* representation */);
        }
      }

      // Print target types.
      //
      {
        if (n != 1)
          os << '{';

        for (auto j (i), e (i + n); j != e; ++j)
          os << (j != i ? " " : "") << j->second->type->name;

        if (n != 1)
          os << '}';
      }

      // Print the target name (the same for all members of this partition).
      //
      os << '{' << *i->first << '}';

      i += n;
    }
  }

  void
  print_diag_impl (const char* p,
                   target_key* l, vector<target_key>&& rs,
                   const char* c)
  {
    // Note: keep this implementation separate from the above for performance.
    //
    assert (!rs.empty ());

    if (rs.size () == 1)
    {
      print_diag_impl (p, l, move (rs.front ()), c);
      return;
    }

    // At the outset handle out-qualification as above. Here we assume that
    // all the targets in the group have the same out.
    //
    if (l != nullptr)
    {
      if (!l->out->empty ())
      {
        if (rs.front ().out->empty ())
          l->out = &empty_dir_path;
      }
      else if (!rs.front ().out->empty ())
      {
        for (target_key& r: rs)
          r.out = &empty_dir_path;
      }
    }

    print_diag_impl<target_key> (p, l, false /* empty */, move (rs), c);
  }

  // Note: these can't be inline since need the target class definition.
  //
  void
  print_diag (const char* p, const target& l, const target& r, const char* c)
  {
    target_key lk (l.key ());
    print_diag_impl (p, &lk, r.key (), c);
  }

  void
  print_diag (const char* p, target_key&& l, const target& r, const char* c)
  {
    print_diag_impl (p, &l, r.key (), c);
  }

  void
  print_diag (const char* p, const target& l, target_key&& r, const char* c)
  {
    target_key lk (l.key ());
    print_diag_impl (p, &lk, move (r), c);
  }

  void
  print_diag (const char* p, const path& l, const target& r, const char* c)
  {
    return print_diag (p, l, r.key (), c);
  }

  void
  print_diag (const char* p, const path& l, target_key&& r, const char* c)
  {
    text << p << ' ' << l << ' ' << (c == nullptr ? "->" : c) << ' ' << r;
  }

  void
  print_diag (const char* p,
              const path& l, vector<target_key>&& rs,
              const char* c)
  {
    assert (!rs.empty ());

    if (rs.size () == 1)
      print_diag (p, l, move (rs.front ()), c);
    else
      print_diag_impl<path> (p, &l, false /* empty */, move (rs), c);
  }

  void
  print_diag (const char* p, const string& l, const target& r, const char* c)
  {
    return print_diag (p, l, r.key (), c);
  }

  void
  print_diag (const char* p, const string& l, target_key&& r, const char* c)
  {
    text << p << ' '
         << l << (l.empty () ? "" : " ")
         << (c == nullptr ? "->" : c) << ' '
         << r;
  }

  void
  print_diag (const char* p,
              const string& l, vector<target_key>&& rs,
              const char* c)
  {
    assert (!rs.empty ());

    if (rs.size () == 1)
      print_diag (p, l, move (rs.front ()), c);
    else
      print_diag_impl<string> (p, &l, l.empty (), move (rs), c);
  }

  void
  print_diag (const char* p, const target& r)
  {
    print_diag_impl (p, nullptr, r.key (), nullptr);
  }

  void
  print_diag (const char* p, const dir_path& r)
  {
    text << p << ' ' << r;
  }

  void
  print_diag (const char* p, const path_name_view& r)
  {
    text << p << ' ' << r;
  }

  void
  print_diag (const char* p, const target& l, const dir_path& r, const char* c)
  {
    // @@ TODO: out qualification stripping: only do if p.out is subdir of t
    //          (also below)?

    text << p << ' ' << l << ' ' << (c == nullptr ? "->" : c) << ' ' << r;
  }

  void
  print_diag (const char* p,
              const target& l, const path_name_view& r,
              const char* c)
  {
    text << p << ' ' << l << ' ' << (c == nullptr ? "->" : c) << ' ' << r;
  }

  void
  print_diag (const char* p, const path& l, const dir_path& r, const char* c)
  {
    text << p << ' ' << l << ' ' << (c == nullptr ? "->" : c) << ' ' << r;
  }

  void
  print_diag (const char* p,
              const path& l, const path_name_view& r,
              const char* c)
  {
    text << p << ' ' << l << ' ' << (c == nullptr ? "->" : c) << ' ' << r;
  }

  void
  print_diag (const char* p,
              const string& l, const path_name_view& r,
              const char* c)
  {
    text << p << ' '
         << l << (l.empty () ? "" : " ")
         << (c == nullptr ? "->" : c) << ' '
         << r;
  }

  // print_process()
  //
  void
  print_process (const char* const* args, size_t n)
  {
    diag_record dr (text);
    print_process (dr, args, n);
  }

  void
  print_process (diag_record& dr,
                 const char* const* args, size_t n)
  {
    dr << butl::process_args {args, n};
  }

  void
  print_process (const process_env& pe, const char* const* args, size_t n)
  {
    diag_record dr (text);
    print_process (dr, pe, args, n);
  }

  void
  print_process (diag_record& dr,
                 const process_env& pe, const char* const* args, size_t n)
  {
    if (pe.env ())
      dr << pe << ' ';

    dr << butl::process_args {args, n};
  }

  // Diagnostic facility, project specifics.
  //

  void simple_prologue_base::
  operator() (const diag_record& r) const
  {
    stream_verb (r.os, sverb_);

    if (type_ != nullptr)
      r << type_ << ": ";

    if (mod_ != nullptr)
      r << mod_ << "::";

    if (name_ != nullptr)
      r << name_ << ": ";
  }

  void location_prologue_base::
  operator() (const diag_record& r) const
  {
    stream_verb (r.os, sverb_);

    if (!loc_.empty ())
    {
      r << loc_.file << ':';

      if (!diag_no_line)
      {
        if (loc_.line != 0)
        {
          r << loc_.line << ':';

          if (!diag_no_column)
          {
            if (loc_.column != 0)
              r << loc_.column << ':';
          }
        }
      }

      r << ' ';
    }

    if (type_ != nullptr)
      r << type_ << ": ";

    if (mod_ != nullptr)
      r << mod_ << "::";

    if (name_ != nullptr)
      r << name_ << ": ";
  }

  const basic_mark error ("error");
  const basic_mark warn  ("warning");
  const basic_mark info  ("info");
  const basic_mark text  (nullptr, nullptr, nullptr); // No type/data/frame.
  const fail_mark  fail  ("error");
  const fail_end   endf;

  // diag_buffer
  //
  process::pipe diag_buffer::
  open (const char* args0, bool force, fdstream_mode m)
  {
    assert (state_ == state::closed && args0 != nullptr);

    serial = ctx_.sched.serial ();
    nobuf = !serial && ctx_.no_diag_buffer;

    process::pipe r;
    if (!(serial || nobuf) || force)
    {
      try
      {
        fdpipe p (fdopen_pipe ());

        // Note that we must return non-owning fd to our end of the pipe (see
        // the process class for details).
        //
        r = process::pipe (p.in.get (), move (p.out));

        m |= fdstream_mode::text;

        is.open (move (p.in), m);
      }
      catch (const io_error& e)
      {
        fail << "unable to read from " << args0 << " stderr: " << e;
      }
    }
    else
      r = process::pipe (-1, 2);

    this->args0 = args0;
    state_ = state::opened;
    return r;
  }

  void diag_buffer::
  open_eof (const char* args0)
  {
    assert (state_ == state::closed && args0 != nullptr);

    serial = ctx_.sched.serial ();
    nobuf = !serial && ctx_.no_diag_buffer;
    this->args0 = args0;
    state_ = state::eof;
  }

  bool diag_buffer::
  read (bool force)
  {
    assert (state_ == state::opened);

    bool r;
    if (is.is_open ())
    {
      try
      {
        // Copy buffers directly.
        //
        auto copy = [this] (fdstreambuf& sb)
        {
          const char* p (sb.gptr ());
          size_t n (sb.egptr () - p);

          // Allocate at least fdstreambuf::buffer_size to reduce
          // reallocations and memory fragmentation.
          //
          size_t i (buf.size ());
          if (i == 0 && n < fdstreambuf::buffer_size)
            buf.reserve (fdstreambuf::buffer_size);

          buf.resize (i + n);
          memcpy (buf.data () + i, p, n);

          sb.gbump (static_cast<int> (n));
        };

        if (is.blocking ())
        {
          if ((serial || nobuf) && !force)
          {
            // This is the case where we are called after custom processing.
            //
            assert (buf.empty ());

            // Note that the eof check is important: if the stream is at eof,
            // this and all subsequent writes to the diagnostics stream will
            // fail (and you won't see a thing).
            //
            if (is.peek () != ifdstream::traits_type::eof ())
            {
              if (serial)
              {
                // Holding the diag lock while waiting for diagnostics from
                // the child process would be a bad idea in the parallel
                // build. But it should be harmless in serial.
                //
                // @@ TODO: do direct buffer copy.
                //
                diag_stream_lock dl;
                *diag_stream << is.rdbuf ();
              }
              else
              {
                // Read/write one line at a time not to hold the lock for too
                // long.
                //
                for (string l; !eof (std::getline (is, l)); )
                {
                  diag_stream_lock dl;
                  *diag_stream << l << '\n';
                }
              }
            }
          }
          else
          {
            fdstreambuf& sb (*static_cast<fdstreambuf*> (is.rdbuf ()));

            while (is.peek () != istream::traits_type::eof ())
              copy (sb);
          }

          r = false;
        }
        else
        {
          // We do not support finishing off after the custom processing in
          // the non-blocking mode unless forced to buffer (but could probably
          // do if necessary).
          //
          assert (!(serial || nobuf) || force);

          fdstreambuf& sb (*static_cast<fdstreambuf*> (is.rdbuf ()));

          // Try not to allocate the buffer if there is no diagnostics (the
          // common case).
          //
          // Note that we must read until blocked (0) or EOF (-1).
          //
          streamsize n;
          while ((n = sb.in_avail ()) > 0)
            copy (sb);

          r = (n != -1);
        }

        if (!r)
          is.close ();
      }
      catch (const io_error& e)
      {
        // For now we assume (here and pretty much everywhere else) that the
        // output can't fail.
        //
        fail << "unable to read from " << args0 << " stderr: " << e;
      }
    }
    else
      r = false;

    if (!r)
      state_ = state::eof;

    return r;
  }

  void diag_buffer::
  write (const string& s, bool nl, bool force)
  {
    assert (state_ != state::closed);

    // Similar logic to read() above.
    //
    if ((serial || nobuf) && !force)
    {
      assert (buf.empty ());

      diag_stream_lock dl;
      *diag_stream << s;
      if (nl)
        *diag_stream << '\n';
    }
    else
    {
      size_t n (s.size () + (nl ? 1 : 0));

      size_t i (buf.size ());
      if (i == 0 && n < fdstreambuf::buffer_size)
        buf.reserve (fdstreambuf::buffer_size);

      buf.resize (i + n);
      memcpy (buf.data () + i, s.c_str (), s.size ());

      if (nl)
        buf.back () = '\n';
    }
  }

  void diag_buffer::
  close (const char* const* args,
         const process_exit& pe,
         uint16_t v,
         bool omit_normal,
         const location& loc)
  {
    tracer trace ("diag_buffer::close");

    assert (state_ != state::closed);

    // We need to make sure the command line we print on the unsuccessful exit
    // is inseparable from any buffered diagnostics. So we prepare the record
    // first and then write both while holding the diagnostics stream lock.
    //
    diag_record dr;
    if (!pe)
    {
      // Note: see similar code in run_finish_impl().
      //
      if (omit_normal && pe.normal ())
      {
        l4 ([&]{trace << "process " << args[0] << " " << pe;});
      }
      else
      {
        dr << error (loc) << "process " << args[0] << " " << pe;

        if (verb >= 1 && verb <= v)
        {
          dr << info << "command line: ";
          print_process (dr, args);
        }
      }
    }

    close (move (dr));
  }

  void diag_buffer::
  close (diag_record&& dr)
  {
    assert (state_ != state::closed);

    // We may still be in the open state in case of custom processing.
    //
    if (state_ == state::opened)
    {
      if (is.is_open ())
      {
        try
        {
          if (is.good ())
          {
            if (is.blocking ())
            {
              assert (is.peek () == ifdstream::traits_type::eof ());
            }
            else
            {
              assert (is.rdbuf ()->in_avail () == -1);
            }
          }

          is.close ();
        }
        catch (const io_error& e)
        {
          fail << "unable to read from " << args0 << " stderr: " << e;
        }
      }

      state_ = state::eof;
    }

    // Note: flushing of the diag record may throw.
    //
    args0 = nullptr;
    state_ = state::closed;

    if (!buf.empty () || !dr.empty ())
    {
      diag_stream_lock l;

      if (!buf.empty ())
      {
        diag_stream->write (buf.data (), static_cast<streamsize> (buf.size ()));
        buf.clear ();
      }

      if (!dr.empty ())
        dr.flush ([] (const butl::diag_record& r)
                  {
                    // Similar to default_writer().
                    //
                    *diag_stream << r.os.str () << '\n';
                    diag_stream->flush ();
                  });
      else
        diag_stream->flush ();
    }
  }

  // diag_do(), etc.
  //
  string
  diag_do (context& ctx, const action&)
  {
    const meta_operation_info& m (*ctx.current_mif);
    const operation_info& io (*ctx.current_inner_oif);
    const operation_info* oo (ctx.current_outer_oif);

    string r;

    // perform(update(x))   -> "update x"
    // configure(update(x)) -> "configure updating x"
    //
    if (m.name_do.empty ())
      r = io.name_do;
    else
    {
      r = m.name_do;

      if (io.name_doing[0] != '\0')
      {
        r += ' ';
        r += io.name_doing;
      }
    }

    if (oo != nullptr)
    {
      r += " (for ";
      r += oo->name;
      r += ')';
    }

    return r;
  }

  void
  diag_do (ostream& os, const action& a, const target& t)
  {
    os << diag_do (t.ctx, a) << ' ' << t;
  }

  string
  diag_doing (context& ctx, const action&)
  {
    const meta_operation_info& m (*ctx.current_mif);
    const operation_info& io (*ctx.current_inner_oif);
    const operation_info* oo (ctx.current_outer_oif);

    string r;

    // perform(update(x))   -> "updating x"
    // configure(update(x)) -> "configuring updating x"
    //
    if (!m.name_doing.empty ())
      r = m.name_doing;

    if (io.name_doing[0] != '\0')
    {
      if (!r.empty ()) r += ' ';
      r += io.name_doing;
    }

    if (oo != nullptr)
    {
      r += " (for ";
      r += oo->name;
      r += ')';
    }

    return r;
  }

  void
  diag_doing (ostream& os, const action& a, const target& t)
  {
    os << diag_doing (t.ctx, a) << ' ' << t;
  }

  string
  diag_did (context& ctx, const action&)
  {
    const meta_operation_info& m (*ctx.current_mif);
    const operation_info& io (*ctx.current_inner_oif);
    const operation_info* oo (ctx.current_outer_oif);

    string r;

    // perform(update(x))   -> "updated x"
    // configure(update(x)) -> "configured updating x"
    //
    if (!m.name_did.empty ())
    {
      r = m.name_did;

      if (io.name_doing[0] != '\0')
      {
        r += ' ';
        r += io.name_doing;
      }
    }
    else
      r += io.name_did;

    if (oo != nullptr)
    {
      r += " (for ";
      r += oo->name;
      r += ')';
    }

    return r;
  }

  void
  diag_did (ostream& os, const action& a, const target& t)
  {
    os << diag_did (t.ctx, a) << ' ' << t;
  }

  void
  diag_done (ostream& os, const action&, const target& t)
  {
    const meta_operation_info& m (*t.ctx.current_mif);
    const operation_info& io (*t.ctx.current_inner_oif);
    const operation_info* oo (t.ctx.current_outer_oif);

    // perform(update(x))   -> "x is up to date"
    // configure(update(x)) -> "updating x is configured"
    //
    if (m.name_done.empty ())
    {
      os << t;

      if (io.name_done[0] != '\0')
        os << ' ' << io.name_done;

      if (oo != nullptr)
        os << " (for " << oo->name << ')';
    }
    else
    {
      if (io.name_doing[0] != '\0')
        os << io.name_doing << ' ';

      if (oo != nullptr)
        os << "(for " << oo->name << ") ";

      os << t << ' ' << m.name_done;
    }
  }
}