|
1
|
## -*- tcl -*- |
|
2
|
# # ## ### ##### ######## ############# ##################### |
|
3
|
## Copyright (c) 2007-2008 Andreas Kupries. |
|
4
|
# |
|
5
|
# This software is licensed as described in the file LICENSE, which |
|
6
|
# you should have received as part of this distribution. |
|
7
|
# |
|
8
|
# This software consists of voluntary contributions made by many |
|
9
|
# individuals. For exact contribution history, see the revision |
|
10
|
# history and logs, available at http://fossil-scm.hwaci.com/fossil |
|
11
|
# # ## ### ##### ######## ############# ##################### |
|
12
|
|
|
13
|
## Utility package, basic user feedback |
|
14
|
|
|
15
|
# # ## ### ##### ######## ############# ##################### |
|
16
|
## Requirements |
|
17
|
|
|
18
|
package require Tcl 8.4 ; # Required runtime |
|
19
|
package require snit ; # OO system. |
|
20
|
package require vc::tools::mem ; # Memory tracking. |
|
21
|
|
|
22
|
# # ## ### ##### ######## ############# ##################### |
|
23
|
## |
|
24
|
|
|
25
|
snit::type ::vc::tools::log { |
|
26
|
# # ## ### ##### ######## ############# |
|
27
|
## Public API, Methods |
|
28
|
|
|
29
|
# Write the message 'text' to log, for the named 'system'. The |
|
30
|
# message is written if and only if the message verbosity is less |
|
31
|
# or equal the chosen verbosity. A message of verbosity 0 cannot |
|
32
|
# be blocked. |
|
33
|
|
|
34
|
typemethod write {verbosity system text} { |
|
35
|
if {$verbosity > $myloglevel} return |
|
36
|
uplevel #0 [linsert $mylogcmd end write [System $system] \ |
|
37
|
[uplevel 1 [list ::subst $text]]] |
|
38
|
return |
|
39
|
} |
|
40
|
|
|
41
|
# Similar to write, especially in the handling of the verbosity, |
|
42
|
# to drive progress displays. It signals that for some long |
|
43
|
# running operation we are at tick 'n' of at most 'max' ticks. An |
|
44
|
# empty 'max' indicates an infinite progress display. |
|
45
|
|
|
46
|
typemethod progress {verbosity system n max} { |
|
47
|
if {!$myprogress} return |
|
48
|
if {$verbosity > $myloglevel} return |
|
49
|
uplevel #0 [linsert $mylogcmd end progress [System $system] $n $max] |
|
50
|
return |
|
51
|
} |
|
52
|
|
|
53
|
typemethod visible? {verbosity} { |
|
54
|
return [expr {$verbosity <= $myloglevel}] |
|
55
|
} |
|
56
|
|
|
57
|
# # ## ### ##### ######## ############# |
|
58
|
# Public API, Administrative methods |
|
59
|
|
|
60
|
# Set verbosity to the chosen 'level'. Only messages with a level |
|
61
|
# less or equal to this one will be shown. |
|
62
|
|
|
63
|
typemethod verbosity {level} { |
|
64
|
if {$level < 1} {set level 0} |
|
65
|
set myloglevel $level |
|
66
|
return |
|
67
|
} |
|
68
|
|
|
69
|
typemethod verbose {} { |
|
70
|
incr myloglevel |
|
71
|
return |
|
72
|
} |
|
73
|
|
|
74
|
typemethod noprogress {} { |
|
75
|
set myprogress 0 |
|
76
|
return |
|
77
|
} |
|
78
|
|
|
79
|
typemethod quiet {} { |
|
80
|
if {$myloglevel < 1} return |
|
81
|
incr myloglevel -1 |
|
82
|
return |
|
83
|
} |
|
84
|
|
|
85
|
# Query the currently set verbosity. |
|
86
|
|
|
87
|
typemethod verbosity? {} { |
|
88
|
return $myloglevel |
|
89
|
} |
|
90
|
|
|
91
|
# Set the log callback handling the actual output of messages going |
|
92
|
# through the package. |
|
93
|
|
|
94
|
typemethod command {cmdprefix} { |
|
95
|
variable mylogcmd $cmdprefix |
|
96
|
return |
|
97
|
} |
|
98
|
|
|
99
|
# Register a system name, to enable tabular formatting. This is |
|
100
|
# done by setting up a format specifier with a proper width. This |
|
101
|
# is handled in the generation command, before the output callback |
|
102
|
# is invoked. |
|
103
|
|
|
104
|
typemethod register {name} { |
|
105
|
set nlen [string length $name] |
|
106
|
if {$nlen < $mysyslen} return |
|
107
|
set mysyslen $nlen |
|
108
|
set mysysfmt %-${mysyslen}s |
|
109
|
return |
|
110
|
} |
|
111
|
|
|
112
|
# # ## ### ##### ######## ############# |
|
113
|
## Internal, state |
|
114
|
|
|
115
|
typevariable myloglevel 2 ; # Some verbosity, not too much |
|
116
|
typevariable mylogcmd ::vc::tools::log::OUT ; # Standard output to stdout. |
|
117
|
typevariable mysysfmt %s ; # Non-tabular formatting. |
|
118
|
typevariable mysyslen 0 ; # Ditto. |
|
119
|
typevariable myprogress 1 ; # Progress output is standard. |
|
120
|
|
|
121
|
# # ## ### ##### ######## ############# |
|
122
|
## Internal, helper methods (formatting, dispatch) |
|
123
|
|
|
124
|
proc System {s} { |
|
125
|
::variable mysysfmt |
|
126
|
return [format $mysysfmt $s] |
|
127
|
} |
|
128
|
|
|
129
|
# # ## ### ##### ######## ############# |
|
130
|
## Standard output callback, module internal |
|
131
|
|
|
132
|
# Dispatch to the handlers of the possible operations. |
|
133
|
|
|
134
|
proc OUT {op args} { |
|
135
|
eval [linsert $args 0 ::vc::tools::log::OUT/$op] |
|
136
|
return |
|
137
|
} |
|
138
|
|
|
139
|
# Write handler. Each message is a line. |
|
140
|
|
|
141
|
proc OUT/write {system text} { |
|
142
|
set m [mlog] |
|
143
|
regsub -all {[^ ]} $m { } b |
|
144
|
puts "$m$system [join [split $text \n] "\n$b$system "]" |
|
145
|
mlimit |
|
146
|
return |
|
147
|
} |
|
148
|
|
|
149
|
# Progress handler. Uses \r to return to the beginning of the |
|
150
|
# current line without advancing. |
|
151
|
|
|
152
|
proc OUT/progress {system n max} { |
|
153
|
if {$max eq {}} { |
|
154
|
puts -nonewline "$system $n\r" |
|
155
|
} else { |
|
156
|
puts -nonewline "$system [format %[string length $max]s $n]/$max\r" |
|
157
|
} |
|
158
|
flush stdout |
|
159
|
return |
|
160
|
} |
|
161
|
|
|
162
|
# # ## ### ##### ######## ############# |
|
163
|
## Configuration |
|
164
|
|
|
165
|
pragma -hasinstances no ; # singleton |
|
166
|
pragma -hastypeinfo no ; # no introspection |
|
167
|
pragma -hastypedestroy no ; # immortal |
|
168
|
|
|
169
|
# # ## ### ##### ######## ############# |
|
170
|
} |
|
171
|
|
|
172
|
namespace eval ::vc::tools { |
|
173
|
namespace export log |
|
174
|
namespace eval log { |
|
175
|
namespace import ::vc::tools::mem::mlog |
|
176
|
namespace import ::vc::tools::mem::mlimit |
|
177
|
} |
|
178
|
} |
|
179
|
|
|
180
|
# ----------------------------------------------------------------------------- |
|
181
|
# Ready |
|
182
|
|
|
183
|
package provide vc::tools::log 1.0 |
|
184
|
return |
|
185
|
|