+ return s.encode (sys.stderr.encoding or 'utf-8', 'replace')
+
+# Lilylib globals.
+program_version = '@TOPLEVEL_VERSION@'
+program_name = os.path.basename (sys.argv[0])
+
+
+# Check if program_version contains @ characters. This will be the case if
+# the .py file is called directly while building the lilypond documentation.
+# If so, try to check for the env var LILYPOND_VERSION, which is set by our
+# makefiles and use its value.
+at_re = re.compile (r'@')
+if at_re.match (program_version):
+ if os.environ.has_key('LILYPOND_VERSION'):
+ program_version = os.environ['LILYPOND_VERSION']
+ else:
+ program_version = "unknown"
+
+
+# Logging framework: We have the following output functions:
+# error
+# warning
+# progress
+# debug
+
+loglevels = {"NONE":0, "ERROR":1, "WARN":2, "BASIC":3, "PROGRESS":4, "INFO":5, "DEBUG":6}
+
+loglevel = loglevels["PROGRESS"]
+
+def set_loglevel (l):
+ global loglevel
+ newlevel = loglevels.get (l, -1)
+ if newlevel > 0:
+ debug_output (_ ("Setting loglevel to %s") % l)
+ loglevel = newlevel
+ else:
+ error (_ ("Unknown or invalid loglevel '%s'") % l)
+
+
+def handle_loglevel_option (option, opt_str, value, parser, *args):
+ if value:
+ set_loglevel (value);
+ elif args:
+ set_loglevel (args[0]);
+
+def is_loglevel (l):
+ global loglevel
+ return loglevel >= loglevels[l];
+
+def is_verbose ():
+ return is_loglevel ("DEBUG")