/*
 * call-seq:
 *   conn.set_notice_processor {|message| ... } -> Proc
 *
 * Notice and warning messages generated by the server are not returned
 * by the query execution functions, since they do not imply failure of
 * the query. Instead they are passed to a notice handling function, and
 * execution continues normally after the handler returns. The default
 * notice handling function prints the message on <tt>stderr</tt>, but the
 * application can override this behavior by supplying its own handling
 * function.
 *
 * This function takes a new block to act as the handler, which should
 * accept a single parameter that will be a PGresult object, and returns 
 * the Proc object previously set, or +nil+ if it was previously the default.
 *
 * If you pass no arguments, it will reset the handler to the default.
 */
static VALUE
pgconn_set_notice_processor(VALUE self)
{
        VALUE proc, old_proc;
        PGconn *conn = get_pgconn(self);

        /* If default_notice_processor is unset, assume that the current 
         * notice processor is the default, and save it to a global variable. 
         * This should not be a problem because the default processor is
         * always the same, so won't vary among connections.
         */
        if(default_notice_processor == NULL)
                default_notice_processor = PQsetNoticeProcessor(conn, NULL, NULL);

        old_proc = rb_iv_get(self, "@notice_processor");
        if( rb_block_given_p() ) {
                proc = rb_block_proc();
                PQsetNoticeProcessor(conn, notice_processor_proxy, (void *)self);
        } else {
                /* if no block is given, set back to default */
                proc = Qnil;
                PQsetNoticeProcessor(conn, default_notice_processor, NULL);
        }

        rb_iv_set(self, "@notice_processor", proc);
        return old_proc;
}