summary refs log tree commit diff
path: root/lib/debug.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2017-06-06 22:42:30 +0200
committerProfpatsch <mail@profpatsch.de>2017-06-22 00:58:59 +0200
commitfeb8cbdc38c6b10b2c66a96151d8d560e960c29f (patch)
treed66cc68fdd4da522112a23f865fa0d67deba54fb /lib/debug.nix
parentb1ffe5e4c029c9f5675bcc42997413fd1b21fbf1 (diff)
downloadnixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar.gz
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar.bz2
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar.lz
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar.xz
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.tar.zst
nixpkgs-feb8cbdc38c6b10b2c66a96151d8d560e960c29f.zip
lib/debug: traceSeqN & traceSeqValN
Strict trace functions that only go down to a specified depth.
Handy to get a better picture and prevent infinite recursions.
Diffstat (limited to 'lib/debug.nix')
-rw-r--r--lib/debug.nix24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/debug.nix b/lib/debug.nix
index 5b3878554c5..925e0d405a7 100644
--- a/lib/debug.nix
+++ b/lib/debug.nix
@@ -20,8 +20,32 @@ rec {
   traceXMLValMarked = str: x: trace (str + builtins.toXML x) x;
 
   # strict trace functions (traced structure is fully evaluated and printed)
+
+  /* `builtins.trace`, but the value is `builtins.deepSeq`ed first. */
   traceSeq = x: y: trace (builtins.deepSeq x x) y;
+
+  /* Like `traceSeq`, but only down to depth n.
+   * This is very useful because lots of `traceSeq` usages
+   * lead to an infinite recursion.
+   */
+  traceSeqN = depth: x: y: with lib;
+    let snip = v: if      isList  v then noQuotes "[…]" v
+                  else if isAttrs v then noQuotes "{…}" v
+                  else v;
+        noQuotes = str: v: { __pretty = const str; val = v; };
+        modify = n: fn: v: if (n == 0) then fn v
+                      else if isList  v then map (modify (n - 1) fn) v
+                      else if isAttrs v then mapAttrs
+                        (const (modify (n - 1) fn)) v
+                      else v;
+    in trace (generators.toPretty { allowPrettyValues = true; }
+               (modify depth snip x)) y;
+
+  /* `traceSeq`, but the same value is traced and returned */
   traceValSeq = v: traceVal (builtins.deepSeq v v);
+  /* `traceValSeq` but with fixed depth */
+  traceValSeqN = depth: v: traceSeqN depth v v;
+
 
   # this can help debug your code as well - designed to not produce thousands of lines
   traceShowVal = x: trace (showVal x) x;