Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Macros from above...
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Patrick Stein  
View profile  
 More options Apr 22 2011, 8:21 pm
Newsgroups: comp.lang.lisp
From: Patrick Stein <p...@nklein.com>
Date: Fri, 22 Apr 2011 17:21:51 -0700 (PDT)
Local: Fri, Apr 22 2011 8:21 pm
Subject: Macros from above...

I'm working on a network-layer library.  I'd like to build this
library with ubiquitous logging that applications can turn on at
will.  I don't want to tie the application to a particular choice of
logging libraries.

I can easily set up something like:

    (defvar *logger* nil)

    (defun set-logger (fn) (setf *logger* fn))

    (defmacro log (category &rest args)
      `(when *logger* (funcall *logger* ,category ,@args))

The problem with this approach is that most logging libraries have
their main logging routine set up as a macro like this:

    (defmacro log (category &rest args)
      `(when (category-is-turned-on-p ,category)
         (really-log ,category ,@args)))

In this way, they avoid having to evaluate the arguments when no-one
cares about the particular category:

    (log :excruciating-detail (stringify-all-of-memory *gc* :pretty
t))

I can't think of a way to have the application load my network
library, then call something in my library to activate logging, and
not have my library unconditionally evaluating the arguments.

I pretty much want the user to be able to provide a function for me to
call to log things.  I want this function to take two arguments, a
category and a form.  I want the form to be evaluated in my library's
context if it gets evaluated at all, but I don't want it to get
evaluated before I pass it into the lambda.  I don't know how to do
this.

Some trick with `&environment` and `eval`?  What am I missing?

Thanks,
Patrick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patrick Stein  
View profile  
 More options Apr 22 2011, 8:37 pm
Newsgroups: comp.lang.lisp
From: Patrick Stein <p...@nklein.com>
Date: Fri, 22 Apr 2011 17:37:46 -0700 (PDT)
Local: Fri, Apr 22 2011 8:37 pm
Subject: Re: Macros from above...
I should mention... this was my best hope:

    (in-package :lib)

    (defvar *logger* nil)

    (defmacro log-it (category form &environment env)
      `(when *logger* (funcall *logger* ,category ',form ,env)))

    (in-package :app)

    (defun app-cheesy-logger (category form env)
      (when category (princ (eval form env))))

    (setf *logger* #'app-cheesy-logger)

    (defun try-it (a)
      (lib:log-it t (incf a))
      (lib:log-it nil (incf a))
      a)

But... SBCL at least is complaining trying to compile the log-it macro
that "There is no MAKE-LOAD-FORM function for bootstrap type SB-
KERNEL:LEXENV".  And, I have no idea how to interpret that.

Thanks,
Patrick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patrick Stein  
View profile  
 More options Apr 23 2011, 12:10 am
Newsgroups: comp.lang.lisp
From: Patrick Stein <p...@nklein.com>
Date: Fri, 22 Apr 2011 21:10:49 -0700 (PDT)
Local: Sat, Apr 23 2011 12:10 am
Subject: Re: Macros from above...
Never mind... answered my own question... closures... I tried them
before but had closed more than I should have or was trying eval's
inside of them.

Anyway, for posterity:

(defpackage :lib (:use :cl) (:export :*logger* :try-it))
(in-package :lib)

(defvar *logger* nil)

(defmacro log-it (category form)
  `(when *logger*
     (funcall *logger* ,category #'(lambda () ,form))))

(defun try-it ()
  (let ((a 0))
    (log-it t   (format t "log ~S" (incf a)))
    (log-it nil (format t "log ~S" (incf a)))
    a))

(defpackage :app (:use :cl :lib))
(in-package :app)

(defun app-log (category form-func)
  (when category (funcall form-fun)))

(setf *logger* #'app-log)

(try-it)

;;; and of course, to use cl-log, the app-log function would look more
like:
(defun app-log (category form-func)
   (cl-log:log-message category (funcall form-func)))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »