乍听之下,不无道理;仔细揣摩,胡说八道

0%

Common Lisp的restart特性

主流的编程语言中,表示出现错误的手段不外乎两种:

  • 函数调用返回错误码
  • 函数调用抛出异常

C语言就属于前者,它的fopen(3)函数在成功打开文件时返回一个FILE指针,失败时返回NULL,并将错误代码写入全局变量errno;Ruby语言属于后者,它的File.open方法在找不到文件时抛出ENOENT的异常,在File.open之外包裹一层begin…rescue…end即可捕捉其抛出的异常。

Common Lisp的错误机制叫做状况系统(Condition System),与异常机制相似,可以实现抛出异常(使用函数error)和捕捉异常(使用宏handler-case)。但与其它语言的异常机制不同的地方在于,状况系统有一种名为RESTART的特性,能够由使用者、或者由代码决定是否要、以及如何从错误中恢复过来。听起来很别致也很诡异,不妨继续往下看。

假设我有一个Web框架,它提供了将日志记录到文件中的功能。这个功能需要先调用名为init-logger的函数进行初始化,该函数实现如下

1
2
3
4
5
(defun init-logger ()
(with-open-file (s "/tmp/log/web.log"
:direction :output
:if-exists :supersede)
(format s "Logger module starting...")))

这个函数被框架的初始化代码所调用,如下

1
2
3
4
5
6
7
(defun init-framework ()
(format t "Framework starting...~%")
(init-plugin))

(defun init-plugin ()
(format t "Plugins starting...~%")
(init-logger))

而框架的初始化代码则由应用的初始化代码所调用,如下

1
2
3
(defun init-app ()
(format t "Application starting...~%")
(init-framework))

如果在目录/tmp/log不存在时调用init-app函数,我将会收到一个错误,说明其无法找到/tmp/log/web.log这个文件。为了避免错误打断了应用的正常启动流程,可以让init-app函数负责创建这个用于存放日志文件的目录。将init-app函数改写为如下形式

1
2
3
4
(defun init-app ()
(format t "Application starting...~%")
(ensure-directories-exist "/tmp/log/")
(init-framework))

尽管这种做法确实可行,但它导致应用层的代码(init-app函数)必须了解位于底层的日志模块的实现细节。直觉告诉我这样子是不对的,框架的事情应该由框架本身提供方案来解决。借助Common Lisp的restart功能,框架确实可以对外提供一种解决方案。

首先需要主动检测用于存放日志文件的目录是否存在。借助UIOP这个包可以写出如下代码

1
2
3
4
5
6
7
8
(defun init-logger ()
(let ((dir "/tmp/log/"))
(unless (uiop:directory-exists-p dir)
(error 'file-error :pathname dir))
(with-open-file (s (format nil "~Aweb.log" dir)
:direction :output
:if-exists :supersede)
(format s "Logger module starting..."))))

接着,init-logger需要主动为调用者提供目录不存在的问题的解决方案,一种办法就是当这个目录不存在时,可以由调用者选择是否创建。使用Common Lisp的restart-case宏,我将init-logger改写为如下形式

1
2
3
4
5
6
7
8
9
10
11
12
13
(defun init-logger ()
(let ((dir "/tmp/log/"))
(restart-case
(unless (uiop:directory-exists-p dir)
(error 'file-error :pathname dir))
(create-log-directory ()
:report (lambda (stream)
(format stream "Create the directory ~A" dir))
(ensure-directories-exist dir)))
(with-open-file (s (format nil "~Aweb.log" dir)
:direction :output
:if-exists :supersede)
(format s "Logger module starting..."))))

此时如果调用第一版的init-app函数,那么init-logger仍将抛出异常(类型为file-error与之前一样)并将我带入到SBCL(我用的是SBCL)的调试器中,但看到的内容会稍有不同

1
2
3
4
5
6
7
8
error on file "/tmp/log"
[Condition of type FILE-ERROR]

Restarts:
0: [CREATE-LOG-DIRECTORY] Create the directory /tmp/log ;; <- 新增了这一行
1: [RETRY] Retry SLIME REPL evaluation request.
2: [*ABORT] Return to SLIME's top level.
3: [ABORT] abort thread (#<THREAD "new-repl-thread" RUNNING {1001F958A3}>)

在Restarts中新增了名为create-log-directory的一项,这正是在init-logger中通过restart-case定义的新的”恢复措施“。我输入0触发这个restart,Common Lisp会回到它被定义的restart-case宏相应的子句中执行其中的表达式,也就是调用CL:ENSURE-DIRECTORY-EXIST函数创建/tmp/log。

如果总是希望执行CREATE-LOG-DIRECTORY这个选项来创建存放日志文件的目录,可以直接在代码中指定,只需要配合使用Common Lisp的handler-bindinvoke-restart函数即可,最终init-app函数的实现如下

1
2
3
4
5
6
7
(defun init-app ()
(format t "Application starting...~%")
(handler-bind
((file-error #'(lambda (c)
(declare (ignorable c))
(invoke-restart 'create-log-directory))))
(init-framework)))
Liutos wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
你的一点心意,我的十分动力。