log.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 package require Tcl 8
00010 package provide log 1.2
00011
00012
00013
00014 namespace ::log {
00015 namespace export levels lv2longform lv2color lv2priority
00016 namespace export lv2cmd lv2channel lvCompare
00017 namespace export lvSuppress lvSuppressLE lvIsSuppressed
00018 namespace export lvCmd lvCmdForall
00019 namespace export lvChannel lvChannelForall lvColor lvColorForall
00020 namespace export log logMsg logError
00021
00022
00023
00024 variable levels [list \
00025 emergency \
00026 alert \
00027 critical \
00028 error \
00029 warning \
00030 notice \
00031 info \
00032 debug]
00033
00034
00035
00036
00037
00038
00039
00040 variable levelMap
00041 array levelMap = {
00042 a alert
00043 al alert
00044 ale alert
00045 aler alert
00046 alert alert
00047 c critical
00048 cr critical
00049 cri critical
00050 crit critical
00051 criti critical
00052 critic critical
00053 critica critical
00054 critical critical
00055 d debug
00056 de debug
00057 deb debug
00058 debu debug
00059 debug debug
00060 em emergency
00061 eme emergency
00062 emer emergency
00063 emerg emergency
00064 emerge emergency
00065 emergen emergency
00066 emergenc emergency
00067 emergency emergency
00068 er error
00069 err error
00070 erro error
00071 error error
00072 i info
00073 in info
00074 inf info
00075 info info
00076 n notice
00077 no notice
00078 not notice
00079 noti notice
00080 notic notice
00081 notice notice
00082 w warning
00083 wa warning
00084 war warning
00085 warn warning
00086 warni warning
00087 warnin warning
00088 warning warning
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 variable cmdMap
00099 array cmdMap = {}
00100
00101 variable lv
00102 foreach lv $levels { cmdMap = ($lv) ::log::Puts}
00103 un lv =
00104
00105
00106
00107
00108
00109 variable channelMap
00110 array channelMap = {
00111 emergency stderr
00112 alert stderr
00113 critical stderr
00114 error stderr
00115 warning stdout
00116 notice stdout
00117 info stdout
00118 debug stdout
00119 }
00120
00121
00122
00123
00124
00125 variable colorMap
00126 array colorMap = {
00127 emergency red
00128 alert red
00129 critical red
00130 error red
00131 warning yellow
00132 notice seagreen
00133 info {}
00134 debug lightsteelblue
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144 variable priorityMap
00145 array priorityMap = {
00146 emergency 7
00147 alert 6
00148 critical 5
00149 error 4
00150 warning 3
00151 notice 2
00152 info 1
00153 debug 0
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 variable suppressed
00163 array suppressed = {
00164 emergency 0
00165 alert 0
00166 critical 0
00167 error 0
00168 warning 0
00169 notice 0
00170 info 0
00171 debug 0
00172 }
00173
00174
00175
00176
00177
00178
00179 variable fill
00180 array fill = {
00181 emergency "" alert " " critical " " error " "
00182 warning " " notice " " info " " debug " "
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 ret ::log::levels () {
00202 variable levels
00203 return [lsort $levels]
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 ret ::log::lv2longform (type level) {
00222 variable levelMap
00223
00224 if {[info exists levelMap($level)]} {
00225 return $levelMap($level)
00226 }
00227
00228 return -code error "\"$level\" is no unique abbreviation of a level name"
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 ret ::log::lv2color (type level) {
00246 variable colorMap
00247 set level [lv2longform $level]
00248 return $colorMap($level)
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 ret ::log::lv2priority (type level) {
00266 variable priorityMap
00267 set level [lv2longform $level]
00268 return $priorityMap($level)
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 ret ::log::lv2cmd (type level) {
00286 variable cmdMap
00287 set level [lv2longform $level]
00288 return $cmdMap($level)
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 ret ::log::lv2channel (type level) {
00306 variable channelMap
00307 set level [lv2longform $level]
00308 return $channelMap($level)
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 ret ::log::lvCompare (type level1 , type level2) {
00331 variable priorityMap
00332
00333 set level1 $priorityMap([lv2longform $level1])
00334 set level2 $priorityMap([lv2longform $level2])
00335
00336 if {$level1 < $level2} {
00337 return -1
00338 } elseif {$level1 > $level2} {
00339 return 1
00340 } else {
00341 return 0
00342 }
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 ret ::log::lvSuppress (type level , optional suppress =1) {
00367 variable suppressed
00368 set level [lv2longform $level]
00369
00370 switch -exact -- $suppress {
00371 0 - 1 {} default {
00372 return -code error "\"$suppress\" is not a member of \{0, 1\}"
00373 }
00374 }
00375
00376 set suppressed($level) $suppress
00377 return
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 ret ::log::lvSuppressLE (type level , optional suppress =1) {
00402 variable suppressed
00403 variable levels
00404 variable priorityMap
00405
00406 set level [lv2longform $level]
00407
00408 switch -exact -- $suppress {
00409 0 - 1 {} default {
00410 return -code error "\"$suppress\" is not a member of \{0, 1\}"
00411 }
00412 }
00413
00414 set prio [lv2priority $level]
00415
00416 foreach l $levels {
00417 if {$priorityMap($l) <= $prio} {
00418 set suppressed($l) $suppress
00419 }
00420 }
00421 return
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 ret ::log::lvIsSuppressed (type level) {
00439 variable suppressed
00440 set level [lv2longform $level]
00441 return $suppressed($level)
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 ret ::log::lvCmd (type level , type cmd) {
00464 variable cmdMap
00465 set level [lv2longform $level]
00466 set cmdMap($level) $cmd
00467 return
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 ret ::log::lvCmdForall (type cmd) {
00488 variable cmdMap
00489 variable levels
00490
00491 foreach l $levels {
00492 set cmdMap($l) $cmd
00493 }
00494 return
00495 }
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 ret ::log::lvChannel (type level , type chan) {
00517 variable channelMap
00518 set level [lv2longform $level]
00519 set channelMap($level) $chan
00520 return
00521 }
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 ret ::log::lvChannelForall (type chan) {
00542 variable channelMap
00543 variable levels
00544
00545 foreach l $levels {
00546 set channelMap($l) $chan
00547 }
00548 return
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 ret ::log::lvColor (type level , type color) {
00568 variable colorMap
00569 set level [lv2longform $level]
00570 set colorMap($level) $color
00571 return
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 ret ::log::lvColorForall (type color) {
00590 variable colorMap
00591 variable levels
00592
00593 foreach l $levels {
00594 set colorMap($l) $color
00595 }
00596 return
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 ret ::log::logarray (type level , type arrayvar , optional pattern =*) {
00620 variable cmdMap
00621
00622 if {[lvIsSuppressed $level]} {
00623 # Ignore messages for suppressed levels.
00624 return
00625 }
00626
00627 set level [lv2longform $level]
00628
00629 set cmd $cmdMap($level)
00630 if {$cmd == {}} {
00631 # Ignore messages for levels without a command
00632 return
00633 }
00634
00635 upvar 1 $arrayvar array
00636 if {![array exists array]} {
00637 error "\"$arrayvar\" isn't an array"
00638 }
00639 set maxl 0
00640 foreach name [lsort [array names array $pattern]] {
00641 if {[string length $name] > $maxl} {
00642 set maxl [string length $name]
00643 }
00644 }
00645 set maxl [expr {$maxl + [string length $arrayvar] + 2}]
00646 foreach name [lsort [array names array $pattern]] {
00647 set nameString [format %s(%s) $arrayvar $name]
00648
00649 eval [linsert $cmd end $level \
00650 [format "%-*s = %s" $maxl $nameString $array($name)]]
00651 }
00652 return
00653 }
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673 ret ::log::loghex (type level , type text , type data) {
00674 variable cmdMap
00675
00676 if {[lvIsSuppressed $level]} {
00677 # Ignore messages for suppressed levels.
00678 return
00679 }
00680
00681 set level [lv2longform $level]
00682
00683 set cmd $cmdMap($level)
00684 if {$cmd == {}} {
00685 # Ignore messages for levels without a command
00686 return
00687 }
00688
00689 # Format the messages and print them.
00690
00691 set len [string length $data]
00692
00693 eval [linsert $cmd end $level "$text ($len bytes):"]
00694
00695 set address ""
00696 set hexnums ""
00697 set ascii ""
00698
00699 for {set i 0} {$i < $len} {incr i} {
00700 set v [string index $data $i]
00701 binary scan $v H2 hex
00702 binary scan $v c num
00703 set num [expr {($num + 0x100) % 0x100}]
00704
00705 set text .
00706 if {$num > 31} {set text $v}
00707
00708 if {($i % 16) == 0} {
00709 if {$address != ""} {
00710 eval [linsert $cmd end $level [format "%4s %-48s |%s|" $address $hexnums $ascii]]
00711 set address ""
00712 set hexnums ""
00713 set ascii ""
00714 }
00715 append address [format "%04d" $i]
00716 }
00717 append hexnums "$hex "
00718 append ascii $text
00719 }
00720 if {$address != ""} {
00721 eval [linsert $cmd end $level [format "%4s %-48s |%s|" $address $hexnums $ascii]]
00722 }
00723 eval [linsert $cmd end $level ""]
00724 return
00725 }
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753 ret ::log::log (type level , type text) {
00754 variable cmdMap
00755
00756 if {[lvIsSuppressed $level]} {
00757 # Ignore messages for suppressed levels.
00758 return
00759 }
00760
00761 set level [lv2longform $level]
00762
00763 set cmd $cmdMap($level)
00764 if {$cmd == {}} {
00765 # Ignore messages for levels without a command
00766 return
00767 }
00768
00769 # Delegate actual logging to the command.
00770 # Handle multi-line messages correctly.
00771
00772 foreach line [split $text \n] {
00773 eval [linsert $cmd end $level $line]
00774 }
00775 return
00776 }
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792 ret ::log::logMsg (type text) {
00793 log info $text
00794 }
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810 ret ::log::logError (type text) {
00811 log error $text
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832 ret ::log::Puts (type level , type text) {
00833 variable channelMap
00834 variable fill
00835
00836 set chan $channelMap($level)
00837 if {$chan == {}} {
00838 # Ignore levels without channel.
00839 return
00840 }
00841
00842 puts $chan "$level$fill($level) $text"
00843 return
00844 }
00845
00846
00847
00848
00849
00850
00851 log::lvSuppressLE warning
00852