[svn] r5831: nemerle/trunk/macros/Logging.n

nazgul svnadmin at nemerle.org
Wed Oct 19 21:53:56 CEST 2005


Log:
Make logging accept mapping from define constants to print functions. Add assembly level version of macros

Author: nazgul
Date: Wed Oct 19 21:53:53 2005
New Revision: 5831

Modified:
   nemerle/trunk/macros/Logging.n

Modified: nemerle/trunk/macros/Logging.n
==============================================================================
--- nemerle/trunk/macros/Logging.n	(original)
+++ nemerle/trunk/macros/Logging.n	Wed Oct 19 21:53:53 2005
@@ -38,8 +38,10 @@
   internal module Helper
   {
     public Flags : Hashtable [string, bool] = Hashtable ();
-    public mutable PrintExpression : PExpr;
-    public mutable SetPrintExpression : bool;
+    
+    // empty string holds default print function (also set when no VERB=>expr mapping is specified)
+    public FlagsToFunctions : Hashtable [string, PExpr] = Hashtable ();
+    public mutable SetPrintExpression = false;
 
     public GetName (expr : PExpr) : string
     {
@@ -57,37 +59,81 @@
     this ()
     {
       Passes.OnInit += Init;
-      PrintExpression = <[ System.Console.WriteLine ]>;
-      SetPrintExpression = false;
+      FlagsToFunctions [""] = <[ System.Console.WriteLine ]>;
       Init ();
     }
+    
+    public LogFlag (id : PExpr, is_on : bool) : void {
+      def name = GetName (id);
+      when (Flags.Contains (name))
+        Message.Error ($ "redefinition of the flag `$(name)'");
+      Flags [name] = is_on;
+    }
+    
+    public LogFunction (fn : list [PExpr]) : void
+    {
+      when (SetPrintExpression)
+        Message.Error ("the logging expression already set");
+      SetPrintExpression = true;
+      
+      match (fn) {
+        | <[ $_ => $_ ]> :: _ =>
+          foreach (f in fn) {
+            | <[ $(verb : name) => $fn ]> =>
+              FlagsToFunctions [verb.Id] = fn;
+              
+            | _ =>
+              Message.Error ($ "expected mapping of VERB to print function, like `DEBUG => My.DebugFunction' got $f");
+          }
+                  
+        | [fn] =>
+          FlagsToFunctions [""] = fn;
+          
+        | _ => Message.Error ($ "expected single logging function or set of mappings from VERB to print functions");
+      }  
+    }
+  }
+
+  [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
+                       Nemerle.MacroTargets.Assembly)]
+  macro LogFlag (id, is_on : bool)
+  {
+    LogFlag (id, is_on);
   }
 
   [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
                        Nemerle.MacroTargets.Class)]
   macro LogFlag (_ : TypeBuilder, id, is_on : bool)
   {
-    def name = GetName (id);
-    when (Flags.Contains (name))
-      Message.Error ($ "redefinition of the flag `$(name)'");
-    Flags [name] = is_on;
+    LogFlag (id, is_on) 
+  }
+
+  [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
+                       Nemerle.MacroTargets.Assembly)]
+  macro LogFunction (params fn : list [PExpr])
+  {
+    LogFunction (fn);  
   }
 
   [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
                        Nemerle.MacroTargets.Class)]
-  macro LogFunction (_ : TypeBuilder, fn)
+  macro LogFunction (_ : TypeBuilder, params fn : list [PExpr])
   {
-    when (SetPrintExpression)
-      Message.Error ("the logging expression already set");
-    PrintExpression = fn;
+    LogFunction (fn);  
   }
 
   macro log (flag, str)
   {
     def name = GetName (flag);
+    def print_expr = 
+      match (FlagsToFunctions.Get (name)) {
+        | Some (e) => e
+        | _ => FlagsToFunctions [""]
+      }
+      
     if (Flags.Contains (name))
       if (Flags [name])
-        <[ $PrintExpression ($str) ]>
+        <[ $print_expr ($str) ]>
       else <[ {} ]>
     else {
       Message.Error ($ "there is no debug flag named `$(name)'");



More information about the svn mailing list