Reputation: 15069
I have a cocoa application core library is C++ which cocoa app uses. I need to put logs in both parts of the app so that I can easily diagnose the issues when the logs are reported from users via crash log reporter (a separate component).
The cocoa part of the app the logs are like
NSLog(@"something..");
In the C++ library which is a separate project in C++ (not .mm but .h and .cpp) I would like to have similar logs. so if I do
cout<<"log from C++";
I don't seem to get the logs in the crash log reporter (an objective C component) if the program crashes. It only seem to be able to report logs from the objective C. I do however see the log messages from C++ on the output screen but it does not seem to do the job of writing the same to the file as well so that if the program crashes the logs are there to be reported.
So what is the best way to consistently write logs which are also reportable if the program crashes. Given the program is cocoa and uses separate C++ components.
EDIT
The crash reporter that I use is https://github.com/tcurdt/feedbackreporter and one of the example log is located at the following path on my machine: /Users/myusername/Library/Logs/DiagnosticReports/MyApp_2013-09-08-220142_mymac.crash
Thanks,
Upvotes: 2
Views: 1654
Reputation: 731
Ref to https://github.com/flyskywhy/react-native-gcanvas/blob/master/core/src/support/Log.h
typedef void (*GCanvasSystemLog)(const char *tag, const char *log);
API_EXPORT extern GCanvasSystemLog gcanvasSystemLog;
and https://github.com/flyskywhy/react-native-gcanvas/blob/master/core/src/support/Log.cpp
if (gcanvasSystemLog) {
gcanvasSystemLog(tag, buffer);
}
thus can let iOS also has the ability to get the log comes from cpp:
void nsLog(const char *tag, const char *log) {
NSLog(@"%s: %s", tag, log);
}
...
// to get the log comes from cpp
gcanvasSystemLog = nsLog;
Upvotes: 0
Reputation: 1172
you can use fprintf, but it only show up from XCode
fprintf(stderr, "Log string: %s", aStringVariable);
While printf will show up if you're debugging from XCode, it won't show up in the Organizer Console. You can run the following command to print only to device's console:
syslog(LOG_WARNING, "log string");
You will also need to #include <sys/syslog.h> for syslog and LOG_WARNING to be explicitly declared
Upvotes: 0
Reputation: 90701
It's not clear how and from where your crash log reporter is collecting the logs. NSLog()
writes the logs to two destinations, stderr and the ASL (Apple System Log) database.
So, one step to getting the C++ logs to go to a similar location as NSLog()
would be to use cerr
instead of cout
. However, the most reliable way to get them to go to the same places would be to use the ASL API from the C++ code. See the man page. Peter Hosey also explored ASL in a series of blog posts (starting here) which you may find useful.
Upvotes: 2