chiark / gitweb /
Bug fix (function name included in declarations)
[clg] / glib / gerror.lisp
CommitLineData
65f5a6e1 1;; Common Lisp bindings for GTK+ v2.0
31ce821b 2;; Copyright 2005-2006 Espen S. Johnsen <espen@users.sf.net>
65f5a6e1 3;;
112ac1d3 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:
65f5a6e1 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
65f5a6e1 14;;
112ac1d3 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.
65f5a6e1 22
82f3b33c 23;; $Id: gerror.lisp,v 1.10 2008-04-11 20:35:48 espen Exp $
65f5a6e1 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))
31ce821b 32 (:metaclass struct-class)
e72f7035 33 (:ref %gerror-copy)
34 (:unref %gerror-free))
65f5a6e1 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
65f5a6e1 42(define-condition glib-error (error)
f595e05c 43 ((code :initarg :code :reader gerror-code)
65f5a6e1 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
b4a94e20 61(deftype gerror-signal () 'gerror)
62
82f3b33c 63(define-type-method return-type ((type gerror-signal))
64 (declare (ignore type))
65 '(or null gerror))
66
31ce821b 67(define-type-method from-alien-form ((type gerror-signal) gerror &key (ref :free))
75689fea 68 (declare (ignore type))
31ce821b 69 `(let ((gerror ,(from-alien-form 'gerror gerror :ref ref)))
b4a94e20 70 (when gerror
71 (signal-gerror gerror))))
72
73
65f5a6e1 74;;; Message logging
75
76(eval-when (:compile-toplevel :load-toplevel :execute)
77 (deftype log-levels ()
78 '(flags
79 :recursion :fatal ;; These are not real log-levels, but flags
80 ;; which may be set
81 error-log-level
82 critical-log-level
83 warning-log-level
84 message-log-level
85 info-log-level
86 debug-log-level)))
87
88(define-condition log-level (warning)
89 ((domain :initarg :domain :reader log-domain)
90 (message :initarg :message :reader log-message))
91 (:report (lambda (condition stream)
92 (format stream "~A: ~A"
93 (log-domain condition) (log-message condition)))))
94
95(define-condition unknown-log-level (log-level)
96 ())
97
98(define-condition error-log-level (log-level)
99 ())
100
101(define-condition critical-log-level (log-level)
102 ())
103
104(define-condition warning-log-level (log-level)
105 ())
106
43656337 107(define-condition message-log-level (log-level)
108 ())
109
65f5a6e1 110(define-condition info-log-level (log-level)
111 ())
112
113(define-condition debug-log-level (log-level)
114 ())
115
116(defparameter *fatal-log-levels* '(error-log-level critical-log-level))
117
56ccd5b7 118(define-callback log-handler nil
119 ((domain string) (log-level log-levels) (message string))
65f5a6e1 120 (let ((fatal-p (or
121 (find :fatal log-level)
122 (some
123 #'(lambda (level) (find level *fatal-log-levels*))
124 log-level)))
125 (condition (or
126 (find-if
127 #'(lambda (level) (subtypep level 'condition))
128 log-level)
129 'unknown-log-level)))
130 (funcall (if fatal-p #'error #'warn) condition
131 :domain domain :message message)))
132
31ce821b 133#-clisp
134(setf
135 #+cmu(alien:extern-alien "log_handler" alien:system-area-pointer)
136 #+sbcl(sb-alien:extern-alien "log_handler" sb-alien:system-area-pointer)
56ccd5b7 137 (callback-address log-handler))
65f5a6e1 138
139
31ce821b 140#?(pkg-exists-p "glib-2.0" :atleast-version "2.6.0")
65f5a6e1 141(progn
142 ;; Unfortunately this will only work as long as we don't abort to
143 ;; toplevel from within the log handler. If we do that, the next
144 ;; invocation of g_log will be handled as a recursion and cause an
145 ;; abort (SIGABORT being signaled). To make things even worse, SBCL
146 ;; doesn't handle SIGABRT at all.
31ce821b 147 (defbinding %log-set-default-handler (nil) pointer
148 (log-handler callback)
65f5a6e1 149 (nil null))
150 (%log-set-default-handler))