One of the first things you're likely to want to know in any programming language is how to send a message to the console.
Xcode: NSLog and the Console
In Xcode sending a message to the console is achieved using NSLog. For example, a call to NSLog(@"Hello World") will appear in the debug area at the bottom of your Xcode project window, in the right section called the Console. Normally the console will appear automatically when there is a message, but there is also a button that is an upwards pointing triangle in a box at the foot of the project window, which will open the debug area for you.To run the code as a newcomer, place NSLog(@"Hello World"); inside the viewDidLoad: method of your initial view controller.
Eclipse: Log and LogCat
In Eclipse, Log messages do not appear in the Console window at the foot of the project window, they appear in the LogCat window, which you can open from Window>Show View>Other...>Android>LogCat. You create a log message using Log.v(), Log.d(), Log.i(), Log.w(), and Log.e(). The reason for the multiple Log methods is one of verbosity. The Log.v() being the most verbose and Log.e() being the least. The letters that follow Log mean in turn VERBOSE, DEBUG, INFO, WARN, ERROR, and you can filter messages in LogCat based on these.As a test, place your log code inside the onCreate() method of your ActivityMain.java file in the src>com>[YOUR-NAME]>[APPLICATION-NAME] folder. Like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Hello", "World");
}
Making sure that you have imported the log class using
import android.util.Log;
at the top of the file.
Note: Log methods in Android must have two strings passed to them, the first typically being a static TAG string used to identify the class or some subsection of it, but for brevity I've done just enough to make it work here.
In addition to Log you can also use System.out.println() to display a message in LogCat.
Visual Studio: Debug and Output/Immediate Window
In Visual Studio when programming Windows 8 (WinRT/Windows Store) apps, you have have at your disposal a Debug class with the method Debug.WriteLine. There are plenty of other useful methods in the Debug class, but begin by using Debug.WriteLine("Hello World!").
For this to work, as with all Windows namespaces, either type at the head of the page:
using System.Diagnostics;
or precede the method with the namespace like this:
System.Diagnostics.Debug.WriteLine(Hello World!");.
If you're new to Windows development (like me!) place this code inside the OnNavigatedTo method of the MainPage file to see it working. But equally you could place it in the constructor:
public MainPage() {
}
Both these locations will allow your code to run without any user input when the page is created. But there is some particular behaviour you need to be aware of:
Trace methods and listeners exist for desktop apps, but don't work in WinRT (Windows Store) apps according to the accepted response to this StackOverflow post.
For this to work, as with all Windows namespaces, either type at the head of the page:
using System.Diagnostics;
or precede the method with the namespace like this:
System.Diagnostics.Debug.WriteLine(Hello World!");.
If you're new to Windows development (like me!) place this code inside the OnNavigatedTo method of the MainPage file to see it working. But equally you could place it in the constructor:
public MainPage() {
}
Both these locations will allow your code to run without any user input when the page is created. But there is some particular behaviour you need to be aware of:
- you need to be in Debug mode not Release mode for the messages to register (there's a drop down menu in the Standard toolbar to set this)
- the Output window, which you see at the bottom of the project window during programming disappears while the app is running, so you'll only see the messages after you stop running/debugging the app
- if you wish to see the log messages while the app is running you need to do two things (i) go to Debug>Options and Settings... and tick 'Redirect All Output Window text to the Immediate Window', which you'll find under Debugging>General (ii) when the app is running tap on the tab of the bottom right window that reads 'Immediate Window'. (Note: when you follow these instructions you will no longer see the log messages in the Output window when the app stops running.)
Trace methods and listeners exist for desktop apps, but don't work in WinRT (Windows Store) apps according to the accepted response to this StackOverflow post.
Comments
Post a Comment