summary refs log tree commit diff
path: root/pkgs/lib/debug.nix
diff options
context:
space:
mode:
authorMarc Weber <marco-oweber@gmx.de>2009-11-22 21:28:36 +0000
committerMarc Weber <marco-oweber@gmx.de>2009-11-22 21:28:36 +0000
commit715d09d836fe9a3d4b7b87300499113c18c710af (patch)
tree730d9700ca3adec95270ca6486f6d93a201e2d37 /pkgs/lib/debug.nix
parentd0cc584711bc5980acd7e2e544ab76dc1b9b2e50 (diff)
downloadnixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar.gz
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar.bz2
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar.lz
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar.xz
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.tar.zst
nixpkgs-715d09d836fe9a3d4b7b87300499113c18c710af.zip
add some debugging functions
traceCallXml is used to trace arguments and result

svn path=/nixpkgs/trunk/; revision=18536
Diffstat (limited to 'pkgs/lib/debug.nix')
-rw-r--r--pkgs/lib/debug.nix51
1 files changed, 49 insertions, 2 deletions
diff --git a/pkgs/lib/debug.nix b/pkgs/lib/debug.nix
index 5d411b864ec..e0f8b414a0d 100644
--- a/pkgs/lib/debug.nix
+++ b/pkgs/lib/debug.nix
@@ -1,6 +1,9 @@
 let lib = import ./default.nix;
 
-inherit (builtins) trace attrNamesToStr isAttrs isFunction isList isInt isString head substring attrNames;
+inherit (builtins) trace attrNamesToStr isAttrs isFunction isList isInt
+        isString isBool head substring attrNames;
+
+inherit (lib) all id mapAttrsFlatten;
 
 in
 
@@ -21,7 +24,7 @@ rec {
   
   traceVal = if builtins ? trace then x: (builtins.trace x x) else x: x;
   traceXMLVal = if builtins ? trace then x: (builtins.trace (builtins.toXML x) x) else x: x;
-
+  traceXMLValMarked = str: if builtins ? trace then x: (builtins.trace ( str + builtins.toXML x) x) else x: x;
   
   # this can help debug your code as well - designed to not produce thousands of lines
   traceShowVal = x : trace (showVal x) x;
@@ -42,6 +45,7 @@ rec {
       else "x is probably a path `${substring 0 50 (toString x)}'";
 
   # trace the arguments passed to function and its result 
+  # maybe rewrite these functions in a traceCallXml like style. Then one function is enough
   traceCall  = n : f : a : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a));
   traceCall2 = n : f : a : b : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b));
   traceCall3 = n : f : a : b : c : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b) (t "arg 3" c));
@@ -61,4 +65,47 @@ rec {
       then [ { inherit name; expected = test.expected; result = test.expr; } ]
       else [] ) tests));
   
+
+
+  # evaluate everything once so that errors will occur earlier
+  # hacky: traverse attrs by adding a dummy
+  # ignores functions (should this behavior change?) See strictf
+  #
+  # Note: This should be a primop! Something like seq of haskell would be nice to
+  # have as well. It's used fore debugging only anyway
+  strict = x :
+    let
+        traverse = x :
+          if isString x then true
+          else if isAttrs x then
+            if x ? outPath then true
+            else all id (mapAttrsFlatten (n: traverse) x)
+          else if isList x then
+            all id (map traverse x)
+          else if isBool x then true
+          else if isFunction x then true
+          else if isInt x then true
+          else if x == null then true
+          else true; # a (store) path?
+    in if (traverse x) then x else throw "else never reached";
+
+  # example: (traceCallXml "myfun" id 3) will output something like
+  # calling myfun arg 1: 3 result: 3
+  # this forces deep evaluation of all arguments and the result!
+  # note: if result doesn't evaluate you'll get no trace at all (FIXME)
+  #       args should be printed in any case
+  traceCallXml = a:
+    if !isInt a then
+      traceCallXml 1 "calling ${a}\n"
+    else
+      let nr = a;
+      in (str: expr:
+          if isFunction expr then
+            (arg: 
+              traceCallXml (builtins.add 1 nr) "${str}\n arg ${builtins.toString nr} is \n ${builtins.toXML (strict arg)}" (expr arg)
+            )
+          else 
+            let r = strict expr;
+            in builtins.trace "${str}\n result:\n${builtins.toXML r}" r
+      );
 }