changeset 7:abe05fb9c2a6

Better debugging. Add "Awful and Skip" option.
author darius
date Mon, 16 Sep 2002 12:19:01 +0000
parents b370e0bbe050
children 81b36e5b725b
files mservtk.tcl
diffstat 1 files changed, 68 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/mservtk.tcl	Thu Oct 12 11:41:22 2000 +0000
+++ b/mservtk.tcl	Mon Sep 16 12:19:01 2002 +0000
@@ -39,6 +39,7 @@
 	set state(windows) 1;
     }
 
+    set state(loglevel) 0;
     set state(port) "4444";
     set state(exit) 0;
     set state(tmpphrase) "";
@@ -52,10 +53,10 @@
     gui_conf;
     
     if {$state(host) == "NONE"} {
-	log "%s" "Login cancelled";
+	log 0 "Login cancelled";
 	exit;
     } else {
-	log "%s" "Login OK'd";
+	log 0 "Login OK'd";
     }
 
     f_writeconf;
@@ -84,7 +85,7 @@
 	    set state(rtlist) "";
 
 	    foreach t $tmp {
-#		log "%s" "Handle $t";
+#		log 0 "Handle $t";
 		n_rthandler [lindex $t 0] [lindex $t 1];
 	    }
 	}
@@ -108,14 +109,14 @@
 	    set state(user) [registry get {HKEY_CURRENT_USER\Software\MServTk} user];
 	    set state(pass) [registry get {HKEY_CURRENT_USER\Software\MServTk} pass];
 	} msg]} {
-	    log "%s" "Failed to read registry keys - $msg";
+	    log 0 "Failed to read registry keys - $msg";
 	}
     } else {
 	if {![catch {
 	    set fh [open $state(conffile)];
 	} msg]} {
 	    if {[gets $fh] != "mservtk-0.1"} {
-		log "%s" "Conf file has the wrong version";
+		log 0 "Conf file has the wrong version";
 	    } else {
 		set state(host) [gets $fh];
 		set state(user) [gets $fh];
@@ -123,7 +124,7 @@
 		close $fh;
 	    }
 	} else {
-	    log "%s" "Failed to open $state(conffile) - $msg";
+	    log 0 "Failed to open $state(conffile) - $msg";
 	}
     }
 }
@@ -137,7 +138,7 @@
 	    registry set {HKEY_CURRENT_USER\Software\MServTk} user $state(user);
 	    registry set {HKEY_CURRENT_USER\Software\MServTk} pass $state(pass);
 	} msg]} {
-	    log "%s" "Failed to set registry keys - $msg";
+	    log 0 "Failed to set registry keys - $msg";
 	}
     } else {
 	if {![catch {
@@ -150,7 +151,7 @@
 
 	    close $fh;
 	} else {
-	    log "%s" "Failed to open $state(conffile) - $msg";
+	    log 0 "Failed to open $state(conffile) - $msg";
 	}
     }
 }
@@ -235,7 +236,7 @@
         }
     }
 
-    log "%s" "Host $state(host)";
+    log 0 "Host $state(host)";
 }
 
 proc quit_now {} {
@@ -273,6 +274,7 @@
     .menubar.rate.m add command -label " Neutral" -command "rate_song NEUTRAL";
     .menubar.rate.m add command -label " Bad" -command "rate_song BAD";
     .menubar.rate.m add command -label " Awful" -command "rate_song AWFUL";
+    .menubar.rate.m add command -label " Awful and Skip" -command "rate_song AWFUL ; control_player NEXT";
     
     pack .menubar.rate -side left;
 
@@ -319,6 +321,13 @@
 	-command "global state; set state(sortmode) Album; gui_updatesongs";
     
     pack .menubar.sort -side left;
+ 
+    # Add the debug menu
+    menubutton .menubar.debug -text "Debug" -menu .menubar.debug.m -underline 0;
+    menu .menubar.debug.m -tearoff 0;
+    .menubar.debug.m add command -label " Debug 0" -command "global state; set state(loglevel) 0";
+    .menubar.debug.m add command -label " Debug 1" -command "global state; set state(loglevel) 1";
+    pack .menubar.debug -side left;
 
     # Add the Help menu
     menubutton .menubar.help -text "Help" -menu .menubar.help.m -underline 0;
@@ -419,7 +428,7 @@
     global songs state;
 
     set tmp [lindex [lindex [array get songs *:listid:$id] 1] 0];
-    log "%s" "Queue - '$songs($tmp:name)' by '$songs($tmp:author)' ($tmp)";
+    log 0 "Queue - '$songs($tmp:name)' by '$songs($tmp:author)' ($tmp)";
 
     set cookie [acquire_lock];
     n_write "QUEUE [split $tmp {:}]";
@@ -430,7 +439,7 @@
 	if {$rtn(code) == 510} {
 	    msg_box "Queue" "You can't have the same\nsong in the queue twice!";
 	} else {
-	    log "%s" "Failed to queue track ($rtn(code) $rtn(data))";
+	    log 0 "Failed to queue track ($rtn(code) $rtn(data))";
 	}
     }
 }
@@ -498,7 +507,7 @@
     release_lock $cookie;
 
     if {$rtn(code) != 255} {
-	log "%s" "Couldn't set volume ($rtn(code) $rtn(data))";
+	log 0 "Couldn't set volume ($rtn(code) $rtn(data))";
     }
 }
 
@@ -511,7 +520,7 @@
     release_lock $cookie;
 
     if {$rtn(code) != 270} {
-	log "%s" "Failed to get rate song ($rtn(code) $rtn(data))";
+	log 0 "Failed to get rate song ($rtn(code) $rtn(data))";
     }
     
 }
@@ -519,13 +528,17 @@
 proc control_player {cmd} {
     global state;
 
+    log 0 "acquiring lock";
     set cookie [acquire_lock];
-    log "%s" "Writing $cmd";
+    log 0 "Writing $cmd";
     n_write "$cmd";
+    log 0 "Wrote $cmd";
     n_getrtn rtn;
+    log 0 "Got rtn";
     release_lock $cookie;
+    log 0 "Lock freed";
 
-#    log "%s" "Control Got $rtn(code) $rtn(data)";
+#    log 0 "Control Got $rtn(code) $rtn(data)";
 }
 
 proc gui_top {} {
@@ -592,7 +605,7 @@
 proc gui_updatequeue {} {
     global state songs queue;
 
-#    log "%s" "Updating queue";
+#    log 0 "Updating queue";
 
     .bot.queue.list delete 0 end;
 
@@ -616,7 +629,7 @@
     release_lock $cookie;
 
     if {$rtn(code) != 254} {
-	log "%s" "Failed to remove $id ($queue($id))";
+	log 0 "Failed to remove $id ($queue($id))";
 	msg_box "Queue" "Failed to dequeue the song";
    }	
 }
@@ -658,7 +671,7 @@
 	set rate2 "N/A";
 	set misc "N/A";
 	if {$rtn(code) != 401} {
-	    log "%s" "Failed to get track info ($rtn(code) $rtn(data))";
+	    log 0 "Failed to get track info ($rtn(code) $rtn(data))";
 	}
     }
 
@@ -707,9 +720,9 @@
 	    incr i;
 	}
     } elseif {$rtn(code) == 404} {
-#	log "%s" "Queue empty";
+#	log 0 "Queue empty";
     } else {
-	log "%s" "Failed to get queue ($rtn(code) $rtn(data))";
+	log 0 "Failed to get queue ($rtn(code) $rtn(data))";
     }
 }
 
@@ -747,7 +760,6 @@
 	    set songs($albid:$num:albumname) $albums($albid:name);
 	}
     }
-
 }
 
 proc con_getalbums {albumsvar} {
@@ -776,7 +788,7 @@
 	set albums($id:author) [lindex $foo 1];
 	set albums($id:name) [lindex $foo 2];
 
-#	log "%s" "Album $id, ID '$albums($id:)' called '$albums($id:name)' by '$albums($id:author)'";
+#	log 0 "Album $id, ID '$albums($id:)' called '$albums($id:name)' by '$albums($id:author)'";
     }
 }
 
@@ -784,7 +796,6 @@
 
 #    gui_updateinfo;
 
-    
     after 900 update_timer;
 }
 
@@ -803,7 +814,7 @@
 
     # Greeting from server
     n_getrtn rtn;
-    log "%s" $rtn(data);
+    log 0 "$rtn(data)";
     if {$rtn(code) != "200"} {
 	error "Server failed to send greeting";
     }
@@ -828,7 +839,7 @@
     set state(lock) "";
 #    trace variable state(lock) rw foobar;
 
-    log "%s" "Logged in";
+    log 0 "Logged in";
 }
 
 proc n_write {text} {
@@ -838,24 +849,24 @@
     flush $state(serv_fd);
 
     if {[eof $state(serv_fd)]} {
-	log "%s" "Server went away on write";
+	log 0 "Server went away on write";
 	exit 1;
     }
-#    log "%s" "Wrote - $text";
+#    log 0 "Wrote - $text";
 }
 
 proc n_rthandler {code data} {
     global songs;
 
-#    log "%s" "Got RT - $code $data";
+#    log 0 "Got RT - $code $data";
 
     switch -- $code {
 	600 {
-	    log "%s" "User '$data' connected";
+	    log 0 "User '$data' connected";
 	}
 	
 	601 {
-	    log "%s" "User '$data' disconnected";
+	    log 0 "User '$data' disconnected";
 	}
 
 	240 -
@@ -869,13 +880,13 @@
 	627 -
 	628 -
 	629 {
-#	    log "%s" "Updating queue on idle";
+#	    log 0 "Updating queue on idle";
 	    gui_updateinfo;
 	    gui_updatequeue;
 	}
 
 	default {
-	    log "%s" "Got unhandled RT event $code $data";
+	    log 0 "Got unhandled RT event $code $data";
 	}
     }
 }
@@ -888,10 +899,10 @@
     while {1} {
 	set line [gets $state(serv_fd)];
 	if {[eof $state(serv_fd)]} {
-	    log "%s" "Server went away on read";
+	    log 0 "Server went away on read";
 	    exit 1;
 	}
-	#    log "%s" "Read - $line";
+	log 0 "Read - $line";
 	if {$line == ""} {
 	    return;
 	}
@@ -899,12 +910,12 @@
 	set foo [split $line "\t"];
 	if {[string index $line 0] == "="} {
 	    lappend state(rtlist) [list [string range [lindex $foo 0] 1 3] [lrange $foo 1 end]];
-	    #	log "%s" "RT event";
+	    log 0 "RT event";
 	} else {
 	    lappend state(tmpphrase) $line
 	    if {$line == "."} {
 		set state(pushbuf) [linsert $state(pushbuf) 0 $state(tmpphrase)];
-		#	    log "%s" "push buffer - '$state(tmpphrase)'";
+		log 0 "push buffer - '$state(tmpphrase)'";
 		set state(tmpphrase) "";
 	    }
 	}
@@ -921,11 +932,11 @@
     catch {unset rtn(lines)}
 
     while {[llength $state(pushbuf)] == 0} {
-#	log "%s" "Sleeping for data";
+	log 0 "Sleeping for data";
 	vwait state(pushbuf);
     }
 
-#    log "%s" "Waking up, got $state(pushbuf)";
+    log 0 "Waking up, got $state(pushbuf)";
 
     set buf [lindex $state(pushbuf) 0];
     set state(pushbuf) [lrange $state(pushbuf) 1 end];
@@ -953,28 +964,27 @@
     }
 
     if {$gotcode == 0} {
-	log "%s" "Failed to parse phrase (got . before server response)";
+	log 0 "Failed to parse phrase (got . before server response)";
     }
 }
 
 ##################################################################
 # Log a message to stderr
 #
-proc log {format args} {
+proc log {level message} {
+    global state;
+
     # Extract the calling function's name
     if {[catch {set fname [lindex [info level -1] 0]}]} {
 	set fname "unknown";
     }
 
-    # Evaluate the supplied format string and arguments
-    if {[catch {set csm [eval format {$format} $args]} msg]} {
-        set csm "bad log message. format='$format' args='$args'";
-    }
-
-    # Emit the message
-    catch {
-	puts stderr "[clock format [clock seconds] -format {%y/%m/%d %H:%M:%S} -gmt yes]:$fname: $csm";
-	flush stderr;
+    if {$state(loglevel) > $level} {
+	# Emit the message
+	catch {
+	    puts stderr "[clock format [clock seconds] -format {%y/%m/%d %H:%M:%S} -gmt yes]:$fname: $message";
+	    flush stderr;
+	}
     }
 }
 
@@ -986,26 +996,26 @@
 	set fname "unknown";
     }
 
-#    log "%s" "Acquiring lock for $fname";
+    log 0 "Acquiring lock for $fname";
 
     set foo 0;
 
     if {[info exists state(lock)]} {
 	while {$state(lock) != ""} {
 	    set foo 1;
-	    log "%s" "$fname waiting for lock (held by [lindex $state(lock) 1])";
+	    log 0 "$fname waiting for lock (held by [lindex $state(lock) 1])";
 	    vwait state(lock);
 	}
 
 	if {$foo == 1} {
-	    log "%s" "Lock released";
+	    log 0 "Lock released";
 	}
 
     }
 
     set cookie [clock clicks];
     set state(lock) [list $cookie $fname];
-#    log "%s" "Lock acquired";
+    log 0 "Lock acquired";
     return $cookie;
 }
 
@@ -1018,28 +1028,28 @@
     }
 
     if {$cookie == ""} {
-	log "%s" "$fname trying to unlock without being locked";
+	log 0 "$fname trying to unlock without being locked";
 	exit 1;
     }
 
     if {$cookie != [lindex $state(lock) 0]} {
-	log "%s" "Lock cookie not matched!";
+	log 0 "Lock cookie not matched!";
 	exit 1;
     }
 
     if {$fname != [lindex $state(lock) 1]} {
-	log "%s" "$fname tried to free [lindex $state(lock) 1]'s lock!";
+	log 0 "$fname tried to free [lindex $state(lock) 1]'s lock!";
 	exit 1;
     }
 
     set state(lock) "";
-#    log "%s" "Lock for $fname now free";
+    log 0 "Lock for $fname now free";
 }
 
 proc foobar {n1 n2 op} {
     global state;
 
-    log "%s" "$op, now $state(lock)";
+    log 0 "$op, now $state(lock)";
 }
 
 if {[catch {main} msg]} {