5abfc785 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
7f911892 |
2 | ;; Copyright 2005-2006 Espen S. Johnsen <espen@users.sf.net> |
5abfc785 |
3 | ;; |
55212af1 |
4 | ;; Permission is hereby granted, free of charge, to any person obtaining |
5 | ;; a copy of this software and associated documentation files (the |
6 | ;; "Software"), to deal in the Software without restriction, including |
7 | ;; without limitation the rights to use, copy, modify, merge, publish, |
8 | ;; distribute, sublicense, and/or sell copies of the Software, and to |
9 | ;; permit persons to whom the Software is furnished to do so, subject to |
10 | ;; the following conditions: |
5abfc785 |
11 | ;; |
55212af1 |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
5abfc785 |
14 | ;; |
55212af1 |
15 | ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
5abfc785 |
22 | |
24ff86cc |
23 | ;; $Id: gerror.lisp,v 1.7 2006/04/28 08:27:22 espen Exp $ |
5abfc785 |
24 | |
25 | |
26 | (in-package "GLIB") |
27 | |
28 | (defclass gerror (struct) |
29 | ((domain :allocation :alien :type quark :reader gerror-domain) |
30 | (code :allocation :alien :type int :reader gerror-code) |
31 | (message :allocation :alien :type string :reader gerror-message)) |
7f911892 |
32 | (:metaclass struct-class) |
33 | (:ref gerror-copy) |
34 | (:unref gerror-free)) |
5abfc785 |
35 | |
36 | (defbinding (%gerror-copy "g_error_copy") () pointer |
37 | (location pointer)) |
38 | |
39 | (defbinding (%gerror-free "g_error_free") () nil |
40 | (location pointer)) |
41 | |
5abfc785 |
42 | (define-condition glib-error (error) |
43 | ((code :initarg :domain :reader gerror-code) |
44 | (message :initarg :message :reader gerror-message)) |
45 | (:report (lambda (condition stream) |
46 | (write-string (gerror-message condition) stream)))) |
47 | |
48 | (define-condition glib-file-error (glib-error) |
49 | ()) |
50 | |
51 | (defbinding file-error-quark () quark) |
52 | |
53 | (defun signal-gerror (gerror) |
54 | (let ((condition |
55 | (cond |
56 | ((= (gerror-domain gerror) (file-error-quark)) 'glib-file-error) |
57 | (t 'glib-error)))) |
58 | (error condition :code (gerror-code gerror) :message (gerror-message gerror)))) |
59 | |
60 | |
7803a730 |
61 | (deftype gerror-signal () 'gerror) |
62 | |
7f911892 |
63 | (define-type-method from-alien-form ((type gerror-signal) gerror &key (ref :free)) |
4d1fea77 |
64 | (declare (ignore type)) |
7f911892 |
65 | `(let ((gerror ,(from-alien-form 'gerror gerror :ref ref))) |
7803a730 |
66 | (when gerror |
67 | (signal-gerror gerror)))) |
68 | |
69 | |
5abfc785 |
70 | ;;; Message logging |
71 | |
72 | (eval-when (:compile-toplevel :load-toplevel :execute) |
73 | (deftype log-levels () |
74 | '(flags |
75 | :recursion :fatal ;; These are not real log-levels, but flags |
76 | ;; which may be set |
77 | error-log-level |
78 | critical-log-level |
79 | warning-log-level |
80 | message-log-level |
81 | info-log-level |
82 | debug-log-level))) |
83 | |
84 | (define-condition log-level (warning) |
85 | ((domain :initarg :domain :reader log-domain) |
86 | (message :initarg :message :reader log-message)) |
87 | (:report (lambda (condition stream) |
88 | (format stream "~A: ~A" |
89 | (log-domain condition) (log-message condition))))) |
90 | |
91 | (define-condition unknown-log-level (log-level) |
92 | ()) |
93 | |
94 | (define-condition error-log-level (log-level) |
95 | ()) |
96 | |
97 | (define-condition critical-log-level (log-level) |
98 | ()) |
99 | |
100 | (define-condition warning-log-level (log-level) |
101 | ()) |
102 | |
24ff86cc |
103 | (define-condition message-log-level (log-level) |
104 | ()) |
105 | |
5abfc785 |
106 | (define-condition info-log-level (log-level) |
107 | ()) |
108 | |
109 | (define-condition debug-log-level (log-level) |
110 | ()) |
111 | |
112 | (defparameter *fatal-log-levels* '(error-log-level critical-log-level)) |
113 | |
a92553bd |
114 | (define-callback log-handler nil |
115 | ((domain string) (log-level log-levels) (message string)) |
5abfc785 |
116 | (let ((fatal-p (or |
117 | (find :fatal log-level) |
118 | (some |
119 | #'(lambda (level) (find level *fatal-log-levels*)) |
120 | log-level))) |
121 | (condition (or |
122 | (find-if |
123 | #'(lambda (level) (subtypep level 'condition)) |
124 | log-level) |
125 | 'unknown-log-level))) |
126 | (funcall (if fatal-p #'error #'warn) condition |
127 | :domain domain :message message))) |
128 | |
7f911892 |
129 | #-clisp |
130 | (setf |
131 | #+cmu(alien:extern-alien "log_handler" alien:system-area-pointer) |
132 | #+sbcl(sb-alien:extern-alien "log_handler" sb-alien:system-area-pointer) |
a92553bd |
133 | (callback-address log-handler)) |
5abfc785 |
134 | |
135 | |
7f911892 |
136 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.6.0") |
5abfc785 |
137 | (progn |
138 | ;; Unfortunately this will only work as long as we don't abort to |
139 | ;; toplevel from within the log handler. If we do that, the next |
140 | ;; invocation of g_log will be handled as a recursion and cause an |
141 | ;; abort (SIGABORT being signaled). To make things even worse, SBCL |
142 | ;; doesn't handle SIGABRT at all. |
7f911892 |
143 | (defbinding %log-set-default-handler (nil) pointer |
144 | (log-handler callback) |
5abfc785 |
145 | (nil null)) |
146 | (%log-set-default-handler)) |