Disable date/process ID in xcode output - ios

How can I remove date and process ID from output window in XCode? They only take up a lot of space without real value.

Use this macro, this will override the default behavior of NSLog,
#define NSLog(STRING, ...) printf("%s\n", [[NSString stringWithFormat:STRING, ##__VA_ARGS__] UTF8String]);

Related

iOS app debug without cable

Is it possible to log to a file on the iPhone (or get at the console output somehow) to read it later, or perhaps directly from the phone?
I need to debug an app that is using a cable-connected accessory device, so it cannot be connected to XCode at the same time.
You can make use of PonyDebugger.
I've been using it a lot to debug networking and CoreData resources, but it also allows logging to the console with PDLog.
PonyDebugger is a remote debugging toolset. It is a client library and gateway server combination that uses Chrome Developer Tools on your browser to debug your application's network traffic and managed object contexts.
Some recommendations:
It works better in Safari than Chrome.
Look into repo issues for making it work in your OSX version if needed. I have it working on El Capitan.
Automatic connection didn't work for me, try using local IP gateway address instead:
e.g. [debugger connectToURL:[NSURL URLWithString:#"ws://192.168.0.12:9000/device"]];
Kevin Xi's approach is OK. You can also redirect NSLog output to the file using following code in AppDelegate's didFinishLaunchingWithOptions method:
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:#"log.txt"];
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+", stderr);
It is really a pity that Apple doesn't provide WiFi debugging for Xcode, but for your question, the answer is YES, you can definitely redirect NSLog to a log file, you can try this approach:
// this will redirect all NSLog in your project, add it to PCH file.
#define NSLog(args...) writeLog(__PRETTY_FUNCTION__, __LINE__, args)
void writeLog(const char *function, int lineNumber, NSString *format, ...) {
// basic log content.
va_list ap;
va_start (ap, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:ap];
va_end (ap);
// add function name, and line number.
NSString *log = [NSString stringWithFormat:#"%s line %d $ %#", function, lineNumber, msg];
// add your own code to write `log` into a text file.
....
}
Note: if you are using some libraries which also write logs to Console, this macro won't redirect those logs to your log file, it will only work with NSLog in your own source files.
As far as I know, this is not possible.
You can eventually use a free and excellent solution like bugfender which will allow you to get the data logged by NSLog remotely. This could help you.

Why are NSLogs in Xcode 8 cut off?

I just installed Xcode 8 and I use NSLogs to see my server response and for other verifications.However, my NSLogs with my server response (in JSON) are getting cut off.Any Ideas?I am using objective-c and running my app on a real device.
I was able to figure this out with the help of This question
You have to create a global header (YourProjectName.pch) Then post this line:
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
Then you can call NSLog like normal from any view like NSLog(#"My array data: %#", anArray);

Unity / Objective-C: Trouble converting char* to NSString

I have what should be a dead-simple piece of code that is failing for me in strange ways.
void MediaShare(char* text, char* furl) {
NSString *status = [NSString stringWithUTF8String: text];
NSString *media = [NSString stringWithUTF8String: furl];
[[SocialShare sharedInstance] mediaShare:status media:media];
text is just a line of text for Twitter sharing, and furl is just a file location string. I am getting a crash down past this function that comes down to bad data getting passed. Putting a breakpoint at the head of this function yields the following-
Image of Xcode variable monitor
The two values look fine, although not sure if the * values that only contain the first char are a problem.
Anyway, jumping to the end, and status and media appear to be converted to hex values.
Converted to hex values?
Any ideas? To give the full story, this is a Unity plug-in. I am simply passing in two strings from Unity C# to this function.
Thanks so much.
The code looks fine so far, if the input values are well formed C char* strings that can be interpreted as an UTF-8 encoded string.
status and media being hex values at the end of the function hint that they are. Both are pointers to Objective-C objects, so this is expected. Print them to the debug console or use po status at the debug console to check their contents (it will print the result of the -description method of status which is the string content in that case).
The subsequent crash might be caused elsewhere in the code.
What's the crash's log output?

Remove NSLog header

Below is a typical NSLog output from the console. Can I get rid of the bold text?
2013-06-09 22:17:02.351 ProjectName[33584:907] MyWantedText
I want to cut out the console text, and compare it (by diff), to a similar log. I don't want time data etc that only will produce false positives.
Is it possible to make my own console write method, MyNsLog, if I can't alter de behavior NSLog?
for your app put this into your Prefix header:
#undef NSLog
#define NSLog(fmt, ...) printf("%s", [[NSString stringWithFormat:fmt, ##__VA_ARGS__] UTF8String])
but id actually rather leave nslog and just use another logging mechanism like ddlog or so
Simple,
NSString *text = #"Some text here";
printf("%s", [text UTF8String]);
Result,
Some text here
You can't rid of the bold text, NSLog() adds the capability to print out objects variables. Also as you can see, adds the program name, the date, the time.
You can change for another logging function like printf

Using SQLite with iOS... very beginner program

I am attempting to write an application in which I have two text fields that I will be using for input. The input taken from the text fields are being initialized as a string. So what I want to accomplish is:
Create an SQLite database (file) that will be saved within the Xcode project (I'm not sure where it can be saved to, but I would need to be able to read it and write to it).
I then want to place the strings (from the text field inputs) into the SQLite table, which will only have two fields (for simplicity). I'm using a button to accept the input from the text fields, place the input into strings, and then put the value of the strings into a label(s).
I ultimately will want to place the strings from the text fields into the table, and then read the table to "grab" the strings and display them into the labels. All of this would be done by the click of a button.
I realize this is very specific, but I have been having a difficult time finding anything simple to accomplish this. I am very new to iOS development and I appreciate if anyone could be as specific and detailed as possible, or at least point me to some resources that would teach me how to accomplish this.
I am using Xcode v4.3.2 and I do not use Storyboard or ARC. I tried to be as detailed as possible, but as you can, my goal is really simple.
I'm tapped out and I appreciate all the help I can get.
Thanks!
-Matt
creating a database file: simply drop your data.sqlite file in your xcode project, as you drop any other class or image resource. Please note, that you will have to copy the file to writeable directory after app installation. take a look at createeditablecopyofdatabaseifneeded.
open your database with sqlite3_open with respect to your new database location.
query the database as you like with sqlite3_prepare_v2
a simple query snippet looks like:
NSString *querystring;
// create your statement
querystring= [NSString stringWithFormat:#"SELECT text1, text2 FROM datatable;"];
const char *sql = [querystring UTF8String];
NSString *text1 = nil;
NSString *text2 = nil;
if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK){
NSLog(#"sql problem occured with: %s", sql);
NSLog(#"%s", sqlite3_errmsg(db));
}
else
{
// you could handle multiple rows here
while (sqlite3_step(statement) == SQLITE_ROW) {
text1 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
text2 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
} // while
}
sqlite3_finalize(statement);
// go on with putting data where you want
There is an example project for using SQLite here you can refer to: https://github.com/AaronBratcher/ABSQLite
It has classes for accessing SQLite in a more traditional database way. The sample project uses SQL to create the tables and shows how you can change the schema over different versions of your app.

Resources