Apple system uptime in milliseconds?

NSLog () uses millisecond resolution timestamps, but is a dumb tool because all of its log messages are at the warning level.

Apple System Log is a much finer-grained system with 8 different levels. BUT ... his timestamps have only 1 second resolution. I read the man page about temporary formats, but they all seem to be only second.

Obviously, this information is available at least by NSLog. A lot can go on in a second; is there any way to get better resolution with ASL?

+6
source share
1 answer

You can add ".#" the time format in ASL (and syslog) to indicate accuracy. Thus, "utc.3" will be formatted in UTC with milliseconds. You can add this to the msg_fmt or time_fmt .

The accuracy of the time format seems to be documented only in syslogd (1) . I am not sure why he did not do this asl (3) .

For example, using asl_add_output_file and pointing to time_fmt might look like this:

 // setup, do once! aslclient log = asl_open(NULL, NULL, 0); asl_add_output_file(log, STDERR_FILENO, "$Time $((Level)(str)) - $Message", // $Time here uses time_fmt arg on next line ASL_TIME_FMT_UTC ".3", // time_fmt: append ".3" for milliseconds here ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE); // example log asl_log(log, NULL, ASL_LEVEL_INFO, "Hello"); // Note in the above ASL_TIME_FMT_UTC is #defined to "utc", so the we're // using compile-time string concatenation of "utc" ".3" to "utc.3". // Alternately you can just specify "utc.3" directly. 

and conclusion:

 2016-02-01 19:16:39.561Z Info - Hello 

An indication in the msg_fmt asl_add_output_file argument may look like this:

 // setup, do once! aslclient log = asl_open(NULL, NULL, 0); asl_add_output_file(log, STDERR_FILENO, // in msg_fmt below, use ISO8601 + ".6" for microseconds "$((Time)(ISO8601.6)) $((Level)(str)) - $Message", ASL_TIME_FMT_UTC, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE); // example log asl_log(log, NULL, ASL_LEVEL_INFO, "Hello"); 

and conclusion:

 2016-02-01T14:16:39.562030-05 Info - Hello 

(I would caution that the above code snippets are intended only to demonstrate the accuracy of the time format in ASL. Actual use will most likely include dispatch_once for configuration, use the sequential send queue for logging.)

0
source

Source: https://habr.com/ru/post/975159/


All Articles