{"id":24521,"date":"2019-03-06T16:35:38","date_gmt":"2019-03-06T21:35:38","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=24521"},"modified":"2019-04-28T20:20:24","modified_gmt":"2019-04-29T00:20:24","slug":"problems-with-os_log","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/03\/06\/problems-with-os_log\/","title":{"rendered":"Problems With os_log"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/bjhomer\/status\/1057053068221509632\">BJ Homer<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/bjhomer\/status\/1057053068221509632\">\n<p>Is there anyone outside Apple who finds the new logging system useful? Seems like it&rsquo;s annoying and actively intrusive for external developers.<\/p>\n<p>The <code>os_log<\/code> features are useful to Apple, but without anyway to gather logs for our own reporting, I don&rsquo;t see how the features are useful to third party devs.<\/p>\n<\/blockquote>\n<p>Some of the problems: difficulty getting logs from customers, so much spew that the log files are huge and hard to sift through, important log entries that are never persisted to disk, high CPU use when leaving Console open to catch the unpersisted entries, <code>NSLog<\/code> becoming unreliable and not showing anything in some cases, API that needs to be wrapped to be useful but which is designed not to be wrapped. Instead of unifying things, unified logging has resulted in everyone finding their own workarounds.<\/p>\n\n<p><a href=\"https:\/\/pspdfkit.com\/blog\/2019\/logs-for-your-crash-reports\/\">Peter Steinberger<\/a>:<\/p>\n<blockquote cite=\"https:\/\/pspdfkit.com\/blog\/2019\/logs-for-your-crash-reports\/\"><p>Ideally, we&rsquo;d love to fully switch to Apple&rsquo;s os_log API. However, it has one major downside: There&rsquo;s still no way to access the rolling log to add to the reports that get sent back to us. Multiple radars have been submitted to Apple, requesting this feature be added (rdar:\/\/40853863, rdar:\/\/30444429), but in the meantime, developers are forced to implement their own solutions if they wish to include logging in a crash&rsquo;s backtrace.<\/p><p>[&#8230;]<\/p><p>For our specific use case, we&rsquo;ve decided to integrate Crashlytics&rsquo; <code>CLSLogv<\/code> function as a custom logger that we use with <a href=\"http:\/\/cocoadocs.org\/docsets\/CocoaLumberjack\/index.html\">CocoaLumberjack<\/a>, a framework that offers many other useful features, such as adding macOS logging capabilities.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/openradar.appspot.com\/42469365\">Mike Piontek<\/a> (<a href=\"https:\/\/twitter.com\/robotspacer\/status\/1103368801113030656\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/openradar.appspot.com\/42469365\"><p>With <code>NSLog<\/code> I can have a switch in my iOS or macOS app that redirects stderr to a file. The user can turn that on, recreate the problem, and just tap a button to email the file. Super simple.<\/p>\n<p>With <code>os_log<\/code>, this doesn&rsquo;t work. Of course there&rsquo;s sysdiagnose, but that requires a complicated button combination, scrolling through a list of hundreds of other log files, and then&#8230; basically the only option is to try to email me a 200MB file.<\/p>\n<p>[&#8230;]<\/p>\n<p>I was originally planning to create my own wrapper for <code>os_log<\/code>, even though it&rsquo;s recommended against (see <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2016\/721\/?time=2335\">WWDC 2016<\/a>). That way I could have it write to a file as needed, or just pass the data to <code>os_log<\/code> the rest of the time. But it seems there&rsquo;s actually no good way to do that, due to the problems described <a href=\"https:\/\/stackoverflow.com\/questions\/50937765\/why-does-wrapping-os-log-cause-doubles-to-not-be-logged-correctly\">here<\/a>.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/50937765\/why-does-wrapping-os-log-cause-doubles-to-not-be-logged-correctly\">Rob Mayoff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/stackoverflow.com\/questions\/50937765\/why-does-wrapping-os-log-cause-doubles-to-not-be-logged-correctly\"><p>The problem is that <code>os_log<\/code> doesn&rsquo;t take an <code>Array&lt;CVarArg&gt;<\/code> argument; <code>os_log<\/code> is itself variadic over <code>CVarArg<\/code>. So Swift casts <code>args<\/code> (an <code>Array&lt;CVarArg&gt;<\/code>) to <code>CVarArg<\/code>, and sticks that casted <code>CVarArg<\/code> into <strong>another<\/strong> <code>Array&lt;CVarArg&gt;<\/code>.<\/p><p>[&#8230;]<\/p><p>Sadly for you, Swift doesn&rsquo;t yet have any syntax for argument splatting. It&rsquo;s been discussed more than once in Swift-Evolution (<a href=\"https:\/\/forums.swift.org\/t\/variadic-parameters-that-accept-array-inputs\/12711\/11\">in this thread, for example<\/a>), but there&rsquo;s not yet a solution on the horizon.<\/p><p>[&#8230;]<\/p><p>So you might go looking for an <code>os_logv<\/code>. Sadly, you won&rsquo;t find one. There is no documented companion to <code>os_log<\/code> that takes pre-bundled arguments.<\/p><p>[&#8230;]<\/p><p>One last thing. Why doesn&rsquo;t the compiler complain about converting <code>Array&lt;CVarArg&gt;<\/code> to <code>CVarArg<\/code>? And why does Kamran&rsquo;s suggestion (of using <code>%@<\/code>) work?<\/p><p>It turns out these questions have the same answer: it&rsquo;s because <code>Array<\/code> is &ldquo;bridgeable&rdquo; to an Objective-C object.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/04\/14\/macos-10-12-4-locks-console-log-away-from-normal-users\/\">macOS 10.12.4 Locks Console Log Away From Normal Users<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/10\/27\/sierra-log-littering\/\">Sierra Log Littering<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/03\/13\/sierra-logging-spew\/\">Sierra Logging Spew<\/a><\/li>\n<\/ul>\n\n<p id=\"problems-with-os_log-update-2019-03-07\">Update (2019-03-07): <a href=\"https:\/\/twitter.com\/Freerunnering\/status\/1103684848282226689\">Kyle Howells<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Freerunnering\/status\/1103684848282226689\">\n<p>It feels like an internal tool Apple built for themselves and then asked everyone else to use for our use cases, without realising it doesn&rsquo;t actually work for those uses.<\/p>\n<\/blockquote>\n\n<p>See also: <a href=\"https:\/\/forums.swift.org\/t\/custom-string-interpolation-and-compile-time-interpretation-applied-to-logging\/18799\">Custom String Interpolation and Compile-time Interpretation applied to Logging<\/a> (via <a href=\"https:\/\/twitter.com\/tonyarnold\/status\/1103441686091321344\">Tony Arnold<\/a>).<\/p>\n\n<p id=\"problems-with-os_log-update-2019-03-08\">Update (2019-03-08): <a href=\"https:\/\/twitter.com\/MagerValp\/status\/1103764611050950657\">Per Olofsson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/MagerValp\/status\/1103764611050950657\">\n<p>Add to this that <code>os_log<\/code> can&rsquo;t be called from scripting languages such as python<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>BJ Homer: Is there anyone outside Apple who finds the new logging system useful? Seems like it&rsquo;s annoying and actively intrusive for external developers. The os_log features are useful to Apple, but without anyway to gather logs for our own reporting, I don&rsquo;t see how the features are useful to third party devs. Some of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-03-06T21:35:41Z","apple_news_api_id":"30d1a504-c7fb-4f82-889e-dcc404f4b3d7","apple_news_api_modified_at":"2019-04-29T00:20:28Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAABQ==","apple_news_api_share_url":"https:\/\/apple.news\/AMNGlBMf7T4KIntzEBPSz1w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[384,56,31,1610,30,1609,54,1504,71,1424,901],"class_list":["post-24521","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-console","tag-debugging","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-objective-c","tag-os_log","tag-programming","tag-pspdfkit","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=24521"}],"version-history":[{"count":7,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24521\/revisions"}],"predecessor-version":[{"id":25099,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24521\/revisions\/25099"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=24521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=24521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=24521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}