def test_log_auxiliary_location(self): msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" asl.log_auxiliary_location( msg, "title", "public.text", "http://www.python.org/" ) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" asl.log_auxiliary_location(msg, "title", None, "http://www.python.org/") msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" self.assertRaises( TypeError, asl.log_auxiliary_location, msg, "title", None, None ) self.assertRaises(TypeError, asl.log_auxiliary_location, msg, "title", None, 42) self.assertRaises( TypeError, asl.log_auxiliary_location, msg, "title", 42, "http://www.python.org", ) self.assertRaises( TypeError, asl.log_auxiliary_location, msg, 42, None, "http://www.python.org", )
def test_no_bytes(self): msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" self.assertRaises(TypeError, asl.create_auxiliary_file, None, b"title", None) self.assertRaises( TypeError, asl.create_auxiliary_file, None, "title", b"public.text" ) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" self.assertRaises( TypeError, asl.log_auxiliary_location, msg, b"title", "public.txt", "http://www.python.org/", ) self.assertRaises( TypeError, asl.log_auxiliary_location, msg, "title", b"public.txt", "http://www.python.org/", ) self.assertRaises( TypeError, asl.log_auxiliary_location, msg, "title", "public.txt", b"http://www.python.org/", )
def test_with_unicode(self): msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" fd = asl.create_auxiliary_file(msg, b"title".decode("utf-8"), "public.text") self.assertIsInstance(fd, (int, long)) os.write(fd, b"hello world\n") asl.close_auxiliary_file(fd) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" fd = asl.create_auxiliary_file(msg, "title", b"public.text".decode("utf-8")) self.assertIsInstance(fd, (int, long)) os.write(fd, b"hello world\n") asl.close_auxiliary_file(fd) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" asl.log_auxiliary_location( msg, b"title".decode("utf-8"), "public.text", "http://www.python.org/" ) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" asl.log_auxiliary_location( msg, "title", b"public.text".decode("utf-8"), "http://www.python.org/" ) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" asl.log_auxiliary_location( msg, "title", "public.text", b"http://www.python.org/".decode("utf-8") )
def test_create_auxiliary_file(self): msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" fd = asl.create_auxiliary_file(msg, "title", None) self.assertIsInstance(fd, (int, long)) os.write(fd, b"hello world\n") asl.close_auxiliary_file(fd) self.assertRaises(OSError, os.write, fd, b"Done") msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" fd = asl.create_auxiliary_file(msg, "title", "public.text") self.assertIsInstance(fd, (int, long)) os.write(fd, b"hello world\n") asl.close_auxiliary_file(fd) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "hello world" self.assertRaises(TypeError, asl.create_auxiliary_file, "foo", "title", None) self.assertRaises(TypeError, asl.create_auxiliary_file, None, 42, None) self.assertRaises(TypeError, asl.create_auxiliary_file, None, "title", 42)
def test_creation(self): m = asl.aslmsg(asl.ASL_TYPE_MSG) self.assertIsInstance(m, asl.aslmsg) m = asl.aslmsg(asl.ASL_TYPE_QUERY) self.assertIsInstance(m, asl.aslmsg) self.assertRaises(ValueError, asl.aslmsg, 42) self.assertRaises(TypeError, asl.aslmsg)
def emit(self, record): msg = asl.aslmsg(asl.ASL_TYPE_MSG) # Add all attributes of the logging record # to the ASL log message: for k in dir(record): if k in ( "args", "levelno", "levelname", "msecs", "relativeCreated", "asctime", "created", ): continue if k.startswith("_"): continue # What about exc_info? msg["py." + k] = str(getattr(record, k)) # Then set up the default attributes: msg[asl.ASL_KEY_FACILITY] = "com.apple.console" msg[asl.ASL_KEY_LEVEL] = _logging2asl(record.levelno) msg[asl.ASL_KEY_READ_UID] = str(os.getuid()) msg[asl.ASL_KEY_MSG] = self.format(record) self._asl.send(msg)
def test_attributes(self): m = asl.aslmsg(asl.ASL_TYPE_MSG) self.assertIsInstance(m, asl.aslmsg) self.assertEqual(m.keys(), set()) self.assertEqual(m.asdict(), {}) m["foo"] = "bar" m["baz"] = "hello" self.assertEqual(m.keys(), {"foo", "baz"}) self.assertEqual(m.asdict(), {"foo": "bar", "baz": "hello"}) self.assertEqual(m["foo"], "bar") self.assertEqual(m["baz"], "hello") self.assertRaises(TypeError, operator.setitem, m, "aap", 42) self.assertRaises(TypeError, operator.setitem, m, 42, "aap") self.assertRaises(KeyError, operator.getitem, m, "nokey") del m["baz"] self.assertEqual(m.keys(), {"foo"}) self.assertEqual(m.asdict(), {"foo": "bar"}) del m["nokey"] self.assertEqual(m.keys(), {"foo"}) self.assertEqual(m.asdict(), {"foo": "bar"})
def test_redirection(self): cli = asl.aslclient("ident", "facility", 0) self.assertIsInstance(cli, asl.aslclient) fd = os.open("/dev/null", os.O_WRONLY) cli.log_descriptor(None, asl.ASL_LEVEL_NOTICE, fd, asl.ASL_LOG_DESCRIPTOR_WRITE) self.assertRaises(ValueError, cli.log_descriptor, None, asl.ASL_LEVEL_NOTICE, fd, 44) self.assertRaises( TypeError, cli.log_descriptor, "u", asl.ASL_LEVEL_NOTICE, fd, asl.ASL_LOG_DESCRIPTOR_WRITE, ) # cli = asl.aslclient("ident", "facility", 0) self.assertIsInstance(cli, asl.aslclient) fd = os.open("/dev/null", os.O_WRONLY) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_FACILITY] = "com.apple.console" cli.log_descriptor(msg, asl.ASL_LEVEL_NOTICE, fd, asl.ASL_LOG_DESCRIPTOR_WRITE)
def test_no_bytes_attributes(self): m = asl.aslmsg(asl.ASL_TYPE_MSG) self.assertIsInstance(m, asl.aslmsg) self.assertRaises(TypeError, operator.setitem, m, b"aap", "hello") self.assertRaises(TypeError, operator.setitem, m, "aap", b"hello") self.assertRaises(TypeError, m.set_query, "aap", b"hello", asl.ASL_QUERY_OP_EQUAL) self.assertRaises(TypeError, m.set_query, b"aap", "hello", asl.ASL_QUERY_OP_EQUAL)
def test_send(self): cli = asl.aslclient("ident", "facility", 0) self.assertIsInstance(cli, asl.aslclient) msg = asl.aslmsg(asl.ASL_TYPE_MSG) msg[asl.ASL_KEY_MSG] = "Breaking attempt!" cli.send(msg) self.assertRaises(TypeError, cli.send, None) self.assertRaises(TypeError, cli.send, "hello")
def do_sendlog(opts): if not opts.keys: print("No message arguments specified", file=sys.stderr) sys.exit(1) cli = asl.aslclient(ident=None, facility="user", options=0) msg = asl.aslmsg(asl.ASL_TYPE_MSG) for k, v in opts.keys: msg[k] = v cli.send(msg)
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 test_set_query(self): m = asl.aslmsg(asl.ASL_TYPE_QUERY) self.assertIsInstance(m, asl.aslmsg) m.set_query("foo", "bar", asl.ASL_QUERY_OP_EQUAL) self.assertEqual(m.keys(), {"foo"}) self.assertRaises(TypeError, m.set_query, 42, "foo", asl.ASL_QUERY_OP_EQUAL) self.assertRaises(TypeError, m.set_query, "key", 42, asl.ASL_QUERY_OP_EQUAL) self.assertRaises(TypeError, m.set_query, "key", "key", "hello")
def test_search(self): cli = asl.aslclient("ident", "facility", 0) self.assertIsInstance(cli, asl.aslclient) msg = asl.aslmsg(asl.ASL_TYPE_QUERY) msg.set_query(asl.ASL_KEY_FACILITY, "com.apple.console", asl.ASL_QUERY_OP_EQUAL) info = None for info in cli.search(msg): self.assertIsInstance(info, asl.aslmsg) self.assertIsNot(info, None) msg = asl.aslmsg(asl.ASL_TYPE_QUERY) msg.set_query(asl.ASL_KEY_FACILITY, "com.apple.nosuchapp", asl.ASL_QUERY_OP_EQUAL) self.assertEqual(list(cli.search(msg)), []) msg = asl.aslmsg(asl.ASL_TYPE_QUERY) msg.set_query(asl.ASL_KEY_FACILITY, "com.apple.console", asl.ASL_QUERY_OP_EQUAL) self.assertNotEqual(list(cli.search(msg)), [])
def test_log(self): cli = asl.aslclient("ident", "facility", 0) self.assertIsInstance(cli, asl.aslclient) # Can't easily verify... cli.log(None, asl.ASL_LEVEL_NOTICE, "hello world") self.assertRaises(TypeError, cli.log, "hello", asl.ASL_LEVEL_NOTICE, "hello world") msg = asl.aslmsg(asl.ASL_TYPE_MSG) cli.log(msg, asl.ASL_LEVEL_NOTICE, "hello world") self.assertRaises(TypeError, cli.log, msg, asl.ASL_LEVEL_NOTICE, "hello world %s", "extra") self.assertRaises(TypeError, cli.log, msg, asl.ASL_LEVEL_NOTICE)
def test_unicode_attributes(self): m = asl.aslmsg(asl.ASL_TYPE_MSG) self.assertIsInstance(m, asl.aslmsg) m[b"aap".decode("utf-8")] = "hello" m["noot"] = b"world".decode("utf-8") self.assertEqual(m.asdict(), {"aap": "hello", "noot": "world"}) m.set_query(b"key1".decode("utf-8"), "value1", asl.ASL_QUERY_OP_EQUAL) m.set_query(b"key2".decode("utf-8"), "value2", asl.ASL_QUERY_OP_EQUAL) self.assertEqual( m.asdict(), { "aap": "hello", "noot": "world", "key1": "value1", "key2": "value2" }, )
def do_query(opts): cli = asl.aslclient(ident=None, facility="user", options=0) msg = asl.aslmsg(asl.ASL_TYPE_QUERY) if opts.show_console: msg.set_query(asl.ASL_KEY_FACILITY, "com.apple.console", asl.ASL_QUERY_OP_EQUAL) # Other search parameters if opts.query: valid = True for key, op, value in opts.query: try: msg.set_query(key, value, OP_MAP[op]) except KeyError: valid = False print("Invalid query operation:", op, file=sys.stderr) if not valid: sys.exit(1) if opts.exists: for key in opts.exists: msg.set_query(key, "", asl.ASL_QUERY_OP_TRUE) for record in cli.search(msg): if opts.format is None: for key in sorted(record.keys()): print("{} {}".format(key, record[key])) print() else: fmt_arg: collections.defaultdict = collections.defaultdict(str) fmt_arg.update({k: record[k] for k in record.keys()}) print(opts.format.format_map(fmt_arg))