aboutsummaryrefslogtreecommitdiff
path: root/build/trace
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-01-08 13:27:15 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-01-08 13:27:15 +0200
commitab4421747146aa7995f0cfb1a639c9121c82c915 (patch)
treedeb08893c02ed0238f73becbbe80ede5568b946e /build/trace
parentd3e624ef7c0fb274e62b81c4c7bd59640770520a (diff)
Implement tracing support
Also use to-relative path translation in diagnostics.
Diffstat (limited to 'build/trace')
-rw-r--r--build/trace71
1 files changed, 71 insertions, 0 deletions
diff --git a/build/trace b/build/trace
new file mode 100644
index 0000000..f1c0567
--- /dev/null
+++ b/build/trace
@@ -0,0 +1,71 @@
+// file : build/trace -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#ifndef BUILD_TRACE
+#define BUILD_TRACE
+
+#include <cstdint>
+#include <iostream>
+
+namespace build
+{
+ // 1 - command lines to update explicit targets (e.g., .o)
+ // 2 - command lines to update implicit targets (e.g., .d)
+ // 3 - things didn't work out (e.g., rule did not match)
+ // 4 - additional information
+ // 5 - more additional information
+ //
+ extern std::uint8_t verb;
+
+ struct tracer
+ {
+ explicit
+ tracer (const char* name): name_ (name) {}
+
+ struct record
+ {
+ ~record () {if (!empty_) std::cerr << std::endl;}
+
+ template <typename T>
+ std::ostream&
+ operator<< (const T& x) const
+ {
+ return std::cerr << x;
+ }
+
+ // Movable-only type.
+ //
+ explicit record (bool e = true): empty_ (e) {}
+ record (record&& r) {empty_ = r.empty_; r.empty_ = true;}
+ record& operator= (record&& r) {empty_ = r.empty_; r.empty_ = true;}
+
+ record (const record&) = delete;
+ record& operator= (const record&) = delete;
+
+ private:
+ mutable bool empty_;
+ };
+
+ template <typename T>
+ record
+ operator<< (const T& x) const
+ {
+ std::cerr << "trace: " << name_ << ": " << x;
+ return record (false);
+ }
+
+ private:
+ const char* name_;
+ };
+
+ template <typename F>
+ inline void
+ trace (std::uint8_t level, const F& f)
+ {
+ if (verb >= level)
+ f ();
+ }
+}
+
+#endif // BUILD_TRACE