22 proc setDebugMode {mode} {
27 proc getDebugMode {} {
32 proc printDebugMode {} {
35 Msg Info "DEBUG_MODE is set to $DEBUG_MODE"
37 Msg Info "DEBUG_MODE is not set or is 0"
45 proc dictSafeGet {d args} {
46 if {[dict exists $d {*}$args]} {
47 return [dict get $d {*}$args]
58 set HogImagesPath [
file normalize [
file join [
file dirname [
info script]] .. .. images]]
62 set NotifyInProgress 0
71 proc NotifyEnabled {} {
72 if {![
info exists ::env(HOG_NOTIFY)]} {
75 set value [
string tolower [
string trim $::env(HOG_NOTIFY)]]
76 return [
expr {[lsearch -exact {1 true yes on enabled} $value] >= 0}]
85 proc NotifyUrgency {level} {
86 set level [
string tolower $level]
88 if {$level == "status" || $level == "extra_info" || $level == "info" || $level == "debug"} {
90 }
elseif {$level == "error"} {
113 proc Notify {level message {title ""}} {
114 global NotifyInProgress
121 set title "Hog [
string toupper $level]"
124 set NotifyInProgress 1
126 if {[
auto_execok notify-send] == ""} {
127 Msg Debug "notify-send was not found, skipping notification"
129 set cmd [list notify-send --app-name=Hog --urgency=[
NotifyUrgency $level]]
130 set icon [
file join $::HogImagesPath hog.png]
131 if {[
file exists $icon]} {
132 lappend cmd --icon=$icon
135 lappend cmd -- $title $message
138 if {[
info procs IsTclsh] != "" && ![
IsTclsh]} {
139 set cmd [
linsert $cmd 0 env -u LD_LIBRARY_PATH]
141 exec {*}$cmd < /dev/null > /dev/null 2> /dev/null &
144 set NotifyInProgress 0
148 catch {
Msg Debug "Could not send notification: $err"}
157 proc Msg {level fmsg {title ""}} {
160 set level [
string tolower $level]
161 if {$title == ""} {
set title [
lindex [
info level [
expr {[info level] - 1}]] 0]}
162 if {$level == 0 || $level == "status" || $level == "extra_info"} {
165 }
elseif {$level == 1 || $level == "info"} {
168 }
elseif {$level == 2 || $level == "warning"} {
171 }
elseif {$level == 3 || [
string first "critical" $level] != -1} {
172 set vlevel {CRITICAL WARNING}
173 set qlevel critical_warning
174 }
elseif {$level == 4 || $level == "error"} {
177 }
elseif {$level == 5 || $level == "debug"} {
178 if {([
info exists ::DEBUG_MODE] && $::DEBUG_MODE == 1) || (
179 [
info exists ::env(HOG_DEBUG_MODE)] && $::env(HOG_DEBUG_MODE) == 1
182 set qlevel extra_info
183 set msg "DEBUG: \[Hog:$title\] $msg"
188 puts "Hog Error: level $level not defined"
192 if {$qlevel == "error"} {
193 Notify $level $msg "Hog $vlevel"
197 if {[
string match "-*" $msg]} {
200 set status [
catch {send_msg_id Hog:$title-0 $vlevel "$msg"}]
206 post_message -type $qlevel "Hog:$title $msg"
207 if {$qlevel == "error"} {
212 if {[
info exists ::HOG_API_MODE] && $::HOG_API_MODE} {
213 if {$qlevel == "error"} {
214 puts "\{\"error\": \"$msg\"\}"
219 if {$vlevel != "STATUS"} {
220 puts "$vlevel: \[Hog:$title\] $msg"
226 if {$qlevel == "error"} {
239 proc MsgAndLog {msg {severity "CriticalWarning"} {outFile ""}} {
241 if {$outFile != ""} {
242 set directory [
file dir $outFile]
243 if {![
file exists $directory]} {
244 Msg Info "Creating $directory..."
245 file mkdir $directory
248 set oF [open "$outFile" a+]
258 proc Logo {{repo_path .}} {
260 if {![
info exists ::env(HOG_LOGO_PRINTED)] || $::env(HOG_LOGO_PRINTED) eq "0"} {
262 [
info exists ::env(HOG_COLOR)] && ([
string match "ENABLED" $::env(HOG_COLOR)] || [
string is integer -strict $::env(HOG_COLOR)] && $::env(HOG_COLOR) > 0)
264 set logo_file "$repo_path/Hog/images/hog_logo_color.txt"
266 set logo_file "$repo_path/Hog/images/hog_logo.txt"
270 set ver [
Git {describe --always}]
274 if {[
file exists $logo_file]} {
275 set f [open $logo_file "r"]
278 set lines [
split $data "\n"]
280 if {[regexp {(Version:)[ ]+} $l -> prefix]} {
281 set string_len [
string length $l]
283 set version_string "* Version: $ver"
284 set version_len [
string length $version_string]
285 append version_string [
string repeat " " [
expr {$string_len - $version_len - 1}]] "*"
286 set l $version_string
291 Msg CriticalWarning "Logo file: $logo_file not found"
295 Msg Status " ★ Like Hog? Star us on GitLab: https://gitlab.com/hog-cern/Hog | GitHub: https://github.com/hog-cern/Hog"
308 proc PrintFileContent {filename} {
310 set file [open $filename r]
313 set content [read $file]
329 proc PrintFileTree {{data} {repo_path} {indentation ""}} {
334 if {![regexp {^[\t\s]*$} $line] & ![regexp {^[\t\s]*\#} $line]} {
335 lappend print_list "$line"
340 foreach p $print_list {
342 if {$i == [
llength $print_list]} {
347 set file_name [
lindex [
split $p] 0]
348 if {[
file exists [
file normalize [
lindex [glob -nocomplain $repo_path/$file_name] 0]]]} {
351 set exists " !!!!! NOT FOUND !!!!!"
354 Msg Status "$indentation$pad$p$exists"
355 set last_printed $file_name
364 namespace eval Hog::LoggerLib {
366 variable toml_dict {}
375 proc GetUserFilePath {filename} {
376 set homeDir [
file normalize ~]
377 set fullPath [
file join $homeDir $filename]
378 if {[
file exists $fullPath]} {
392 proc ParseTOML {toml_file} {
399 if {![
file exists $toml_file]} {
400 Msg Warning "TOML file $toml_file does not exist"
403 if {[
catch {open $toml_file r} file_handle]} {
404 Msg Error "Cannot open TOML file $toml_file: $file_handle"
408 set current_section ""
410 set in_multiline_string 0
411 set multiline_buffer ""
413 while {[
gets $file_handle line] >= 0} {
416 if {$in_multiline_string} {
417 if {[
string match "*\"\"\"*" $line]} {
419 set end_pos [
string first "\"\"\"" $line]
420 append multiline_buffer [
string range $line 0 [
expr $end_pos - 1]]
421 if {$current_section eq ""} {
422 dict set toml_dict $multiline_key $multiline_buffer
424 dict set toml_dict $current_section $multiline_key $multiline_buffer
426 set in_multiline_string 0
427 set multiline_buffer ""
430 append multiline_buffer $line "\n"
438 for {
set i 0} {$i < [
string length $line]} {
incr i} {
439 set char [
string index $line $i]
440 if {!$in_quotes && ($char eq "\"" || $char eq "'")} {
443 append clean_line $char
444 }
elseif {$in_quotes && $char eq $quote_char} {
447 append clean_line $char
448 }
elseif {!$in_quotes && $char eq "#"} {
451 append clean_line $char
454 set line [
string trim $clean_line]
460 if {[regexp {^\[([^\]]+)\]$} $line match section_name]} {
461 set current_section $section_name
463 if {![dict exists $toml_dict $current_section]} {
464 dict set toml_dict $current_section [dict create]
469 if {[regexp {^([^=]+)=(.*)$} $line match raw_key raw_value]} {
470 set key [
string trim $raw_key]
471 set value [
string trim $raw_value]
473 if {[
string match "*\"\"\"*" $value] && ![
string match "*\"\"\"*\"\"\"*" $value]} {
474 set start_pos [
string first "\"\"\"" $value]
475 set multiline_key $key
476 set multiline_buffer [
string range $value [
expr $start_pos + 3] end]
477 append multiline_buffer "\n"
478 set in_multiline_string 1
484 if {[
string match "*.*" $key]} {
485 set key_parts [
split $key "."]
486 set dict_ref toml_dict
487 if {$current_section ne ""} {
488 lappend dict_ref $current_section
490 for {
set i 0} {$i < [
expr [
llength $key_parts] - 1]} {
incr i} {
491 set part [
lindex $key_parts $i]
492 lappend dict_ref $part
493 if {![dict exists {*}$dict_ref]} {
494 dict set {*}$dict_ref [dict create]
497 set final_key [
lindex $key_parts end]
498 lappend dict_ref $final_key
499 dict set {*}$dict_ref $parsed_value
502 if {$current_section eq ""} {
503 dict set toml_dict $key $parsed_value
505 dict set toml_dict $current_section $key $parsed_value
520 proc ParseTOMLValue {value} {
521 set value [
string trim $value]
523 if {$value eq "true"} {
525 }
elseif {$value eq "false"} {
529 if {[regexp {^"(.*)"$} $value match string_content]} {
531 set string_content [
string map {\\" \" \\\\ \\ \\n \n \\t \t \\r \r} $string_content]
532 return $string_content
533 }
elseif {[regexp {^'(.*)'$} $value match string_content]} {
535 return $string_content
538 if {[
string match {\[*\]} $value]} {
539 set array_content [
string range $value 1 end-1]
540 set array_content [
string trim $array_content]
541 if {$array_content eq ""} {
545 set current_element ""
549 for {
set i 0} {$i < [
string length $array_content]} {
incr i} {
550 set char [
string index $array_content $i]
551 if {!$in_quotes && ($char eq "\"" || $char eq "'")} {
554 append current_element $char
555 }
elseif {$in_quotes && $char eq $quote_char} {
558 append current_element $char
559 }
elseif {!$in_quotes && $char eq "\["} {
561 append current_element $char
562 }
elseif {!$in_quotes && $char eq "\]"} {
563 incr bracket_depth -1
564 append current_element $char
565 }
elseif {!$in_quotes && $char eq "," && $bracket_depth == 0} {
567 set current_element ""
569 append current_element $char
572 if {$current_element ne ""} {
578 if {[
string is integer $value]} {
579 return [
expr {int($value)}]
580 }
elseif {[
string is double $value]} {
581 return [
expr {double($value)}]
584 if {[regexp {^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}} $value]} {
598 proc GetTOMLValue {toml_dict key_path} {
599 set key_parts [
split $key_path "."]
600 set current_dict $toml_dict
601 foreach part $key_parts {
602 if {[dict exists $current_dict $part]} {
603 set current_dict [dict get $current_dict $part]
616 proc PrintTOMLDict {toml_dict {indent 0}} {
617 set indent_str [
string repeat " " $indent]
618 dict for {key value} $toml_dict {
619 if {[string is list $value] && [llength $value] > 1 && [string is list [lindex $value 0]]} {
620 # This is likely a nested dictionary
621 Msg Debug "${indent_str}${key}:"
622 if {[catch {dict for {subkey subvalue} $value {}} result]} {
623 # Not a dictionary, print as value
624 Msg Debug "${indent_str} $value"
626 PrintTOMLDict $value [expr {$indent + 1}]
628 } elseif {[string is list $value] && [llength $value] > 0} {
630 Msg Debug "${indent_str}${key}: \[list of [llength $value] items\]"
631 foreach item $value {
632 Msg Debug "${indent_str} - $item"
635 Msg Debug "${indent_str}${key}: $value"
643 proc GetTOMLDict {} {
645 if {[
info exists toml_dict]} {