Skip to content

Instantly share code, notes, and snippets.

@allenhumphreys
Created July 13, 2023 20:34
Show Gist options
  • Save allenhumphreys/8becffa2de02a4da3941b9957b46caa8 to your computer and use it in GitHub Desktop.
Save allenhumphreys/8becffa2de02a4da3941b9957b46caa8 to your computer and use it in GitHub Desktop.
Converting os_log to Logger via RegEx
os_log\(\s*".*"\s*,\nlog: ((self\.)?log), type: \.((fault|error|debug|info))[^\)]*\)
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.((fault|error|debug|info))[^\)](?!=\n)*\) // figuring out the look ahead. Need to match all non ) that
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.((fault|error|debug|info))([^\)]?!\)|(.|\n)*
([^\)]?!\)|(.|\n)* // match everything except close parethesis unless the close paren is not followed by a close paren
// paren not preceded by a paren
(?<!\))\)
// Getting close
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.(fault|error|debug|info)((\)(?=\)))|[^)])*
doesn't match:
os_log("Setting live flag to %{public}@ for %{public}d instances",
log: log,
type: .info,
String(describing: value), instances.count)
Explanation, matches all close parens followed by other parens, but not close parens followed by a comma
Needs to match all close parens that aren't followed by another close paren. Negative look ahead should be good for this
// paren
\)(?!\)) not follwed by a paren
// I think this is it
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.(fault|error|debug|info)((\)(?=\)))|\)(?=\))|.|\n)*?\)(?!\))
Nope, doesn't match this:
os_log("Looking for live instances with startTime <= %{public}@ and endTime >= %{public}@", log: log, type: .debug, String(describing: startTime), String(describing: endTime))
// This made progress, note that it captures the end white space:
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: (\.(fault|error|debug|info))((\)(?=\)))|\)(?=\))|.|\n)*?(\)(?!\))\s)
group 1 group 3
// Wonder if you could capture the argument patterns, really starting to get into needing a dynamic code around the regex if you wanted to automat back filling
Figuring out capture groups:
os_log(\(\s*".*")\s*,\s*log: ((self\.)?log),\s*type: \.(fault|error|debug|info)(((\)(?=\)))|\)(?=\))|.|\n)*?)(\)(?!\))\s)
$2 $4
$2.$4$1
Yields: log.debug("%{public}@"
Need to insert everything after `type: .thing`
os_log(\(\s*".*")\s*,\s*log: ((self\.)?log),\s*type: \.(fault|error|debug|info)(((\)(?=\)))|\)(?=\))|.|\n)*?)(\)(?!\))\s)
$2.$4$1$5$8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment