Debian Codegraph @

comlib/message.lisp

raw
1 ;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :chaos)
30 #|==============================================================================
31 System: CHAOS
32 Module: comlib
33 File: message.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 (defun flush-all ()
41 (finish-output *standard-output*)
42 (finish-output *error-output*)
43 )
44
45 (defun fresh-all ()
46 (fresh-line *standard-output*)
47 (fresh-line *error-output*))
48
49 ;;;
50 ;;; Message DB
51 ;;;
52 (defvar *Message-DB* (make-hash-table))
53
54 ;;;
55 (defun get-msg-type (id)
56 (first (gethash id *Message-DB*)))
57 (defun get-msg-level (id)
58 (second (gethash id *Message-DB*)))
59 (defun get-msg-fmt (id)
60 (third (gethash id *Message-DB*)))
61 (defun get-msg-description (id)
62 (fourth (gethash id *Message-DB*)))
63
64 (defun register-message (type msg)
65 (setf (gethash (car msg) *Message-DB*) (cons type (cdr msg))))
66
67 #+:Allegro
68 (defun read-message-db (path)
69 (clrhash *Message-DB*)
70 (flet ((regme (type msgs)
71 (dolist (msg msgs)
72 (register-message type msg))))
73 (with-open-file (strm path :if-does-not-exist :error
74 :external-format :utf-8)
75 (loop for type = (read strm nil :eof)
76 while (not (eq type :eof))
77 do (case type
78 ((:panic :panics) (regme :panic (read strm nil)))
79 ((:error :errors) (regme :error (read strm nil)))
80 ((:warning :warnings) (regme :warning (read strm nil)))
81 ((:message :messages) (regme :message (read strm nil)))
82 ((:smessage :smessages) (regme :smessage (read strm nil)))
83 (otherwise
84 (error "Internal error, invalid message type ~s" type)))))))
85
86 #+:Allegro
87 (defun setup-message-db ()
88 (let ((fname (chaos-probe-file "messagesDB" *chaos-libpath* '(".msg"))))
89 (unless fname
90 (error "Internal error, can't find messagesDB."))
91 (read-message-db fname)))
92
93 ;;;
94 ;;; OUTPUT-MSG
95 ;;;
96
97 ;;; message level
98 ;;; 0 : always
99 ;;; 1 : standard
100 ;;; 2 : only under verbose mode
101 ;;;
102 (defvar *msg-lvl* 1)
103 (defvar *old-msg-lvl* *msg-lvl*)
104
105 (defun set-verbose-lvl (lvl)
106 (if (<= lvl 3)
107 (setf *msg-lvl* lvl)
108 (error "Internal error, invalid verbose level ~d" lvl)))
109
110 (defun set-verbose-on ()
111 (setf *old-msg-lvl* *msg-lvl*)
112 (set-verbose-lvl 0)
113 (setq *chaos-verbose* t))
114
115 (defun set-verbose-off ()
116 (when (zerop *msg-lvl*)
117 (set-verbose-lvl *old-msg-lvl*))
118 (setq *chaos-verbose* nil))
119
120 (defun set-expert-on ()
121 (setf *old-msg-lvl* *msg-lvl*)
122 (set-verbose-lvl 2))
123
124 (defun set-export-off ()
125 (when (= *msg-lvl* 2)
126 (set-verbose-lvl *old-msg-lvl*)))
127
128 (defun set-quiet-on ()
129 (setf *old-msg-lvl* *msg-lvl*)
130 (set-verbose-lvl 3))
131
132 (defun set-quiet-off ()
133 (when (= *msg-lvl* 3)
134 (set-verbose-lvl *old-msg-lvl*)))
135
136 (defun output-msg (id prefix args)
137 (when (>= (get-msg-level id) *msg-lvl*)
138 (apply #'format t (concatenate 'string
139 prefix
140 "(:"
141 (string id)
142 ") "
143 (get-msg-fmt id))
144 args)))
145
146 (defmacro with-output-chaos-error-n ((msg-id args &optional (tag 'to-top tag-p)) &body body)
147 ` (progn
148 (let ((*standard-output* *error-output*)
149 (*print-indent* 4))
150 (output-msg ',msg-id "~%[Error]" ,args)
151 ,@body)
152 ,(if (and tag-p (eq tag 'to-top))
153 `(chaos-to-top)
154 `(chaos-error ,tag)
155 )))
156
157 (defmacro with-output-chaos-warning-n ((msg-id args) &body body)
158 ` (unless *chaos-quiet*
159 (let ((*standard-output* *error-output*)
160 (*print-indent* 4))
161 (output-msg ',msg-id "~%[Warning]" ,args)
162 ,@body)
163 (flush-all)))
164
165 (defmacro with-output-panic-message-n ((msg-id args) &body body)
166 ` (progn
167 (let ((*standard-output* *error-output*))
168 (output-msg ',msg-id "~%[!! PANIC !!]" ,args)
169 ,@body)
170 (chaos-to-top)))
171
172 (defmacro with-output-msg-n ((msg-id args &optional (stream '*error-output*)) &body body)
173 ` (unless *chaos-quiet*
174 (let ((*standard-output* ,stream)
175 (*print-indent* 3))
176 (output-msg ',msg-id "~%-- " ,args)
177 ,@body)
178 (flush-all)))
179
180 (defmacro with-output-simple-msg-n ((msg-id args &optional (stream '*error-output*)) &body body)
181 ` (unless *chaos-quiet*
182 (let ((*standard-output* ,stream)
183 (*print-indent* 2))
184 (output-msg ',msg-id "~%" ,args)
185 ,@body)
186 (flush-all)))
187
188 ;;; older versions
189
190 (defmacro with-output-chaos-error ((&optional (tag 'to-top)) &body body)
191 ` (progn
192 ;; (flush-all)
193 ;; (fresh-all)
194 (let ((*standard-output* *error-output*)
195 (*print-indent* 4))
196 (format t "~%[Error]: ")
197 ,@body)
198 ,(if (eq tag 'to-top)
199 `(chaos-to-top)
200 `(chaos-error ,tag)
201 )))
202
203 (defmacro with-output-chaos-warning ((&optional (stream '*error-output*)) &body body)
204 ` (unless *chaos-quiet*
205 (let ((*standard-output* ,stream)
206 (*print-indent* 4))
207 (format t "~%[Warning]: ")
208 ,@body)
209 (flush-all)))
210
211 (defmacro with-output-panic-message ((&optional (stream '*error-output*)) &body body)
212 ` (progn
213 ;; (fresh-all)
214 ;; (flush-all)
215 (let ((*standard-output* ,stream))
216 (print-next)
217 (princ "!! PANIC !!: ")
218 ,@body)
219 (chaos-to-top)))
220
221 ;;;
222 (defmacro with-output-msg ((&optional (stream '*standard-output*)) &body body)
223 ` (unless *chaos-quiet*
224 (let ((*standard-output* ,stream)
225 (*print-indent* 3))
226 (format t "~%-- ")
227 ,@body)
228 (flush-all)))
229
230 (defmacro with-output-simple-msg ((&optional (stream '*standard-output*)) &body body)
231 ` (unless *chaos-quiet*
232 ;; (fresh-all)
233 ;; (flush-all)
234 (let ((*standard-output* ,stream)
235 (*print-indent* 2))
236 (format t "~%")
237 ,@body)
238 (flush-all)))
239
240 ;;;
241 #||
242 (defun print-in-progress (str)
243 (unless *chaos-quiet*
244 (princ str *error-output*)
245 (finish-output *error-output*)))
246 ||#
247
248 (defun print-in-progress (str)
249 (declare (ignore str))
250 nil)
251
252 ;;; I-miss-current-module me
253 ;;; Checks if the *current-module* is bound, returns nil with an error mesage if
254 ;;; *current-mdoule* is not bound to non-nil value.
255 ;;; - me must a name (symbol) of a block.
256 ;;;
257 (defmacro I-miss-current-module (me)
258 ` (unless *current-module*
259 (fresh-all)
260 (flush-all)
261 (with-output-panic-message ()
262 (format t "in ~a : no current module is specified!" ',me)
263 (force-output)
264 (finish-output)
265 (return-from ,me nil))))
266
267 ;;; EOF