source: branches/home/psmith/restructure/src/utils/utils.lisp

Last change on this file was 94, checked in by psmith, 18 years ago

moved threadsafe queue and added more tests.

File size: 3.1 KB
Line 
1#|
2Copyright (c) 2007
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
133. The name of the author may not be used to endorse or promote products
14   derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26|#
27
28(in-package :nio-utils)
29
30(declaim (optimize (debug 3) (speed 3) (space 0)))
31
32(defun get-readable (format-string &optional (time (get-universal-time)))
33  (multiple-value-bind (second minute hour date month year) (decode-universal-time time)
34    (with-output-to-string (out)
35      (format out format-string year month date hour minute second))))
36
37(defun get-readable-time ( &optional (time (get-universal-time)))
38  (get-readable "~A/~2,'0d/~2,'0d ~2,'0d:~2,'0d:~2,'0d" time))
39
40
41;;High res timer
42
43(let ((internal-base (get-internal-real-time))
44      (universal-base (get-universal-time)))
45;
46; Gets the time including milliseconds by using a base time from universal time and
47; tracking high res passing of time using the get-internal-real-time
48; Probably not that accurate in absolute terms i.e. may drift from the base,
49; but good enough for performance timings
50;
51  (defun get-universal-high-res()
52    (let ((current-internal (get-internal-real-time)))
53      (+ universal-base (/ (- current-internal internal-base) internal-time-units-per-second) )))
54
55  (defun get-readable-high-res-time()
56    (let ((estimated-universal-float (get-universal-high-res)))
57      (multiple-value-bind (estimated-universal estimated-universal-rem) (floor estimated-universal-float)
58        (format nil "~A.~3,'0d"(get-readable-time estimated-universal) (* 1000 estimated-universal-rem)))))
59 
60  )
61
62(defparameter *format-mutex* (nio-compat:make-mutex "format lock"))
63
64;Format the message to destination but prepend a high res time to the message, useful for logging
65(defmacro format-log (destination control-string &rest format-arguments)
66  `(nio-compat:with-mutex (*format-mutex*)
67     (format ,destination (concatenate 'string "~A - " ,control-string) (get-readable-high-res-time) ,@format-arguments)))
Note: See TracBrowser for help on using the repository browser.