1 | #| |
---|
2 | Copyright (c) 2007 |
---|
3 | 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 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. 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. |
---|
13 | 3. 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 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS 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))) |
---|