I wanted to try structured logging with os_log
in C++, but I found that it fails to print anything given a format string and a variable:
eg.
void example(std::string& str)
{
os_log_info(OS_LOG_DEFAULT, "%s", str.c_str());
os_log_debug(OS_LOG_DEFAULT, "%s", str.c_str());
os_log_error(OS_LOG_DEFAULT, "%s", str.c_str());
}
This prints a blank row in the console, but with no text.
How is this meant to work with variables? It only works with literals and constants now as far as I can tell.
I'm looking forward to getting this working.
Oh, that’s interesting. Consider this tiny program:
#include <string>
#include <os/log.h>
void example(std::string& str)
{
os_log_info(OS_LOG_DEFAULT, "'%s'", str.c_str());
os_log_info(OS_LOG_DEFAULT, "'%{public}s'", str.c_str());
}
int main(int argc, const char * argv[]) {
std::string s = "Hello Cruel World!";
example(s);
return 0;
}
The first log entry comes out as ''
whereas the second is 'Hello Cruel World!'
.
This relates to a key system log feature: the ability to selective log private info. By default a string is considered private, and thus you need to annotate it with {public}
to have it show up in the log. Console renders a private string as <private>
, but it seems that Xcode missed that memo.
This is very misleading and I encourage you to file a bug against Xcode. Please post your bug number, just for the record.
Oh, and this has nothing to do with C++ per se. The same problem affects C and probably Objective-C as well. It doesn’t affect Swift, probably because Swift interacts with the system log in a very different way than the C-based languages.
ps If you’re interesting in the system log, check out Your Friend the System Log.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"