Ronald Oussoren avatar Ronald Oussoren committed 3547770

Small tweaks

* Add translations from string to integer log levels (and back)

* Start of the command-line interface (python3 -m asl), with
documentation.

This interface is incomplete, there will also be a command
to create arbitrary log lines and a command to query the database.

Comments (0)

Files changed (4)

 
 def ASL_FILTER_MASK_UPTO(level):
     return (1 << (level+1)) - 1
+
+LEVEL2STRING = {
+    ASL_LEVEL_EMERG: ASL_STRING_EMERG,
+    ASL_LEVEL_ALERT: ASL_STRING_ALERT,
+    ASL_LEVEL_CRIT: ASL_STRING_CRIT,
+    ASL_LEVEL_ERR: ASL_STRING_ERR,
+    ASL_LEVEL_WARNING: ASL_STRING_WARNING,
+    ASL_LEVEL_NOTICE: ASL_STRING_NOTICE,
+    ASL_LEVEL_INFO: ASL_STRING_INFO,
+    ASL_LEVEL_DEBUG: ASL_STRING_DEBUG,
+}
+
+STRING2LEVEL = {
+    ASL_STRING_EMERG: ASL_LEVEL_EMERG,
+    ASL_STRING_ALERT: ASL_LEVEL_ALERT,
+    ASL_STRING_CRIT: ASL_LEVEL_CRIT,
+    ASL_STRING_ERR: ASL_LEVEL_ERR,
+    ASL_STRING_WARNING: ASL_LEVEL_WARNING,
+    ASL_STRING_NOTICE: ASL_LEVEL_NOTICE,
+    ASL_STRING_INFO: ASL_LEVEL_INFO,
+    ASL_STRING_DEBUG: ASL_LEVEL_DEBUG,
+}
+import argparse
+
+import asl
+import os
+
+
+parser = argparse.ArgumentParser(description="ASL command-line interface", prog=__package__)
+parser.add_argument('--version', action='version', version='%(prog)s ' + asl.__version__)
+
+sub = parser.add_subparsers(help="Sub-commands")
+
+parser_consolelog = sub.add_parser('consolelog', help='Log to Console.app')
+parser_consolelog.set_defaults(action='consolelog')
+parser_consolelog.add_argument(
+    '-i', '--ident',
+    action='store', metavar='IDENT',
+    default=os.getlogin(), help='Source identifier (default: %(default)s)'
+)
+parser_consolelog.add_argument(
+    '-l', '--level',
+    action='store', metavar='LEVEL',
+    default=asl.ASL_STRING_NOTICE, choices=list(asl.STRING2LEVEL.keys()),
+    help='Logging level (default: %(default)s)'
+)
+parser_consolelog.add_argument('message', help='The message', metavar='MESSAGE', nargs='+')
+
+
+def do_consolelog(options):
+    ident = options.ident
+    message = ' '.join(options.message)
+    level = options.level
+
+    cli = asl.aslclient(ident=ident, facility='com.apple.console', options=0)
+    msg = asl.aslmsg(asl.ASL_TYPE_MSG)
+    msg[asl.ASL_KEY_FACILITY] = "com.apple.console"
+    msg[asl.ASL_KEY_LEVEL] = level
+    msg[asl.ASL_KEY_READ_UID] = str(os.getuid())
+
+    cli.log(msg, asl.STRING2LEVEL[level], message)
+
+def main():
+    opts = parser.parse_args()
+    if opts.action == 'consolelog':
+        do_consolelog(opts)
+
+if __name__ == "__main__":
+    main()
 Utility functions
 -----------------
 
+
 .. function:: ASL_FILTER_MASK(level)
 
    :param level: A message priority level
 .. data::  ASL_STRING_DEBUG
 
 
+Priority translations
+.....................
+
+.. data:: LEVEL2STRING
+
+   A directionary mapping numeric levels to the equivalent string value
+
+.. data:: STRING2LEVEL
+
+   A directionary mapping string levels to the equivalent integer value
+
+
 Attribute matching operations
 .............................
 
   $ python3 -m asl --help
 
 This is primarily intended as an example
-on how to use the module, and more or less
-implements the interface of the syslog(1) command
-on Mac OS X 10.8.
+on how to use the module, but can be usefull
+on its own.
 
 Usage
 -----
 
-...
+Writing messages to the Console.app default view
+................................................
 
+::
 
+  $ python3 -m asl consolelog [-i IDENT] [-l LEVEL] message ...
+
+This logs the message arguments as a single line (separated
+by spaces) with the attributes needed to end up in the default
+view of Console.app.
+
+The *IDENT* is the source identifier and is usually an application
+name. It defaults to the name of the user.
+
+The *LEVEL* is the log level, and is one of "Emergency", "Alert",
+"Critical", "Error", "Warning", "Notice", "Info" and "Debug" (from
+high to low priority). The default is "Notice", which is the lowest
+priority that will end up in the default view of Console.app.
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.