aboutsummaryrefslogtreecommitdiff
path: root/build/diagnostics.cxx
blob: 0dfae1a9182b0e337ef88e01116cc13af3c2b8c8 (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
// file      : build/diagnostics.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/diagnostics>

#include <sstream>
#include <iostream>

#include <build/scope>
#include <build/target>
#include <build/operation>
#include <build/context>
#include <build/utility>

using namespace std;

namespace build
{
  string
  diag_relative (const path& p)
  {
    const path& b (*relative_base);

    if (p.absolute ())
    {
      if (p == b)
        return ".";

#ifndef _WIN32
      if (p == home)
        return "~";
#endif

      path rb (relative (p));

#ifndef _WIN32
      if (rb.relative ())
      {
        // See if the original path with the ~/ shortcut is better
        // that the relative to base.
        //
        if (p.sub (home))
        {
          path rh (p.leaf (home));
          if (rb.string ().size () > rh.string ().size () + 2) // 2 for '~/'
            return "~/" + rh.string ();
        }
      }
      else if (rb.sub (home))
        return "~/" + rb.leaf (home).string ();
#endif

      return rb.string ();
    }

    return p.string ();
  }

  string
  diag_relative (const dir_path& d, bool cur)
  {
    string r (diag_relative (static_cast<const path&> (d)));

    // Translate "." to empty.
    //
    if (!cur && d.absolute () && r == ".")
      r.clear ();

    // Add trailing '/'.
    //
    if (!r.empty () && !dir_path::traits::is_separator (r.back ()))
      r += '/';

    return r;
  }

  // Relative stream.
  //
  const int relative_index = ostream::xalloc ();

  // diag_do(), etc.
  //
  string
  diag_do (const action&, const target& t)
  {
    const meta_operation_info& mi (*current_mif);
    const operation_info& oi (*current_oif);

    ostringstream os;

    // perform(update(x))   -> "update x"
    // configure(update(x)) -> "configure updating x"
    //
    if (mi.name_do.empty ())
      os << oi.name_do << ' ';
    else
    {
      os << mi.name_do << ' ';

      if (!oi.name_doing.empty ())
        os << oi.name_doing << ' ';
    }

    os << t;
    return os.str ();
  }

  string
  diag_doing (const action&, const target& t)
  {
    const meta_operation_info& mi (*current_mif);
    const operation_info& oi (*current_oif);

    ostringstream os;

    // perform(update(x))   -> "updating x"
    // configure(update(x)) -> "configuring updating x"
    //
    if (!mi.name_doing.empty ())
      os << mi.name_doing << ' ';

    if (!oi.name_doing.empty ())
      os << oi.name_doing << ' ';

    os << t;
    return os.str ();
  }

  string
  diag_already_done (const action&, const target& t)
  {
    const meta_operation_info& mi (*current_mif);
    const operation_info& oi (*current_oif);

    ostringstream os;

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

      if (!oi.name_already_done.empty ())
        os << " is already " << oi.name_already_done;
    }
    else
    {
      if (!oi.name_doing.empty ())
        os << oi.name_doing << ' ';

      os << t << " is already " << mi.name_already_done;
    }

    return os.str ();
  }

  void
  print_process (const char* const* args)
  {
    diag_record r (text);

    for (const char* const* p (args); *p != nullptr; p++)
      r << (p != args ? " " : "") << *p;
  }

  // Trace verbosity level.
  //
  uint16_t verb;

  // Diagnostic facility, base infrastructure.
  //
  ostream* diag_stream = &cerr;

  diag_record::
  ~diag_record () noexcept(false)
  {
    // Don't flush the record if this destructor was called as part of
    // the stack unwinding. Right now this means we cannot use this
    // mechanism in destructors, which is not a big deal, except for
    // one place: exception_guard. So for now we are going to have
    // this ugly special check which we will be able to get rid of
    // once C++17 uncaught_exceptions() becomes available.
    //
    if (!empty_ && (!std::uncaught_exception () || exception_unwinding_dtor))
    {
      *diag_stream << os_.str () << std::endl;

      if (epilogue_ != nullptr)
        epilogue_ (*this); // Can throw.
    }
  }

  // Diagnostic facility, project specifics.
  //

  void simple_prologue_base::
  operator() (const diag_record& r) const
  {
    relative (r.os_, relative_);

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

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

  void location_prologue_base::
  operator() (const diag_record& r) const
  {
    relative (r.os_, relative_);

    r << loc_.file << ':' << loc_.line << ':' << loc_.column << ": ";

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

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

  const basic_mark error ("error");
  const basic_mark warn ("warning");
  const basic_mark info ("info");
  const text_mark text;
  const fail_mark<failed> fail;
}