Hello! My name is Jim and I am a programmer. I love to code and I would like to share with you the benefit of using programming languages.
A: Logging demo
In this section, we will look at how to use logging in order to track the progress of a program. It is important that when a program is being written, it must provide details about what is going on inside the program in real time. In this section, we will see how to create a log file to track the progress of a program. This will help in debugging any errors in the program. It will also help in checking if the program has been working properly or not.
This can be done in two ways:
1) Using a console object in a C++ program.
2) Using System.out in an Android application.
In the first method, we have to do certain things for this to work:
Include in the C++ code. In the code, we have to include :
#include 1 #include
We have to open a file for writing:
ofstream outFile; 1 ofstream outFile ;
We have to open a file called “mylog.txt” for writing:
outFile.open(“mylog.txt”); 1 outFile . open ( “ mylog . txt ” ) ;
We have to write data to the file:
outFile << “Program started”; 1 outFile << “ Program started ” ;
We have to close the file:
outFile.close(); 1 outFile . close ( ) ;
The second method is done by using System.out in an Android application. We can use System.out.print() in place of the above methods. All that needs to be done is that we have to include in the Android app and then we can use Console class instead of ofstream class. The output goes directly to the console window of the emulator or device on which the app is running. Hence, it is easier to debug an application on an Android phone than on an emulator. On an emulator, we need to connect the phone to our PC and run it through an ADB command. Debugging on an emulator is not very practical because it takes more time than debugging on a device. The same code can be used for both methods. However, before using it on an Android app, we need to replace ofstream with System.out and replace open() with open(). Exactly the same steps are needed in order to log information when writing any program with any programming language. The following syntax can be used in Java:
// Create a new log file called “my log file” File myLog = new File(“my log file”); // Open the log file for writing myLog.open(“w”); // Write some stuff to it myLog.println(“Here is some stuff…”); // Close it myLog.close(); 1 2 3 4 5 6 7 8 9 // Create a new log file called “my log file” File myLog = new File ( “ my log file ” ) ; // Open the log file for writing myLog . open ( “ w ” ) ; // Write some stuff to it myLog . println ( “ Here is some stuff… ” ) ; // Close it myLog . close ( ) ;
Write a public review