Firebug tutorial-logging, profiling and CommandLine (part I)

Source: Internet
Author: User

Firebug tutorial

Section 1: Console tab: logging, profiling and CommandLine (Part II)

Overview of Console Tab

This tab is mainly used for logging. it also can be used as CommandLine window (like immediate window in Microsoft Visual Studio) while you are debugging the JavaScript. it can be used for monitoring the execution of JavaScript code by using Profiling Service.

The following topic will be covered in this section.

  • Logging in Firebug (with String Substitution pattern)
  • Grouping the logs or messages
  • Console. dir and console. dirxml
  • Assertion (console. assert ())
  • Tracing (console. Trace ())
  • Timing (measuring the time of your code)
  • Javascript profiler (an introduction in this tutorial, the details will be covered in next tutorial .)

I think it is better to read the original text in terms of translation. After all, it will not distort the original intention or improper translation. A little understanding of English can be understood, as a front-end you can do. Come on, Google Translate.

 

#1. Logging in firebug

Firebug supports logging in console tab. So, you don't need to use alert ('something') or document. Write ('something') anymore.

There are five types of logging in firebug.

  • Console. Log: write a message without icon.
  • Console. Debug: writes a message to the console, including a hyperlink to the line where it was called
  • Console. Error (): writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.
  • Console.info (): writes a message to the console with the visual "Info" icon and color coding and a hyperlink to the line where it was called.
  • Console. Warn (): writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.

Example code:

  • Open the HTM file called "plain HTML" or create one HTML file.
  • Paste the following code with <body> tag.

 

<script language="javascript" type="text/javascript">console.log('This is log message');console.debug('This is debug message');console.error('This is error message');console.info('This is info message');console.warn('This is warning message');</script>

 

You will get the following output. If you click on Hyperlink (‑test.htm "in this case), it will take you to script tab and will highlight the line that wrote this message.

String substitution patterns

String substitution parterns can be used in console. log, lele.info, console. debug, console. warn and console. error. you can use the same way that we used in C/C ++.

% S
String

% D, % I
Integer (numeric formatting is not yet supported)

% F
Floating point number (numeric formatting is not yet supported)

% O
Object hyperlink

Example:

Note: I will use console. log in the example below even all console objects (console. log, console.info, console. debug, console. warn and console. error) support string substitution.

  • Remove "script" tag that we pasted for the previous example.
  • Paste the code below within <body> tag.

 

<script language="javascript" type="text/javascript"> //This is for normal string substitution " %s, %d, %i, %f".console.log("My Name is <strong>%s</strong>. My Date of Birth is <strong>%dth %s, %i</strong>. My height is <strong>%f</strong> m.", "Nicolas Cage", 7, 'January', 1964, 1.8542); function Foo(){this.LeftHand = function(){return "Left Hand";}this.RightHand = function(){return "Right Hand";}} //This is for object "%o".var objFoo = new Foo();console.log('This is <strong>%o</strong> of Foo class.', objFoo); </script>

If you are using% OIn your log, the object will be shown as a hyperlink in green color. this hyperlink is linked to the DOM tab. so, If you click "object" in second line, you will see the list of properties of that object (LeftHand RightHand in this case .)

 

#2. Grouping

Firebug allows you to group the message or log in Console tab. If you have some logs in your code, you can probably divide your log into small group or subgroup

Example ~

 

 

<script language="javascript" type="text/javascript"> var groupname = 'group1';console.group("message group : %s " , groupname);console.log("log message 1 from %s", groupname);console.log("log message 2 from %s", groupname);console.log("log message 3 from %s", groupname);console.groupEnd(); groupname = 'group2';console.group("message group : %s " , groupname);console.log("log message 1 from %s", groupname); var subgroupname = 'subgroup1';console.group("message group : %s " , subgroupname);console.log("log message 1 from %s", subgroupname);console.log("log message 2 from %s", subgroupname);console.log("log message 3 from %s", subgroupname);console.groupEnd(); console.log("log message 3 from %s", groupname);console.groupEnd(); </script>

#3. Console. dir and console. dirxml

  • Console. dir: It can be used for getting all properties and methods of a particle object. according the example below, we can get the model (property) and getmanufactor (method) of car object by using console. dir (); you can also pass the object of HTML element (eg: console. dir (document. getelementbyid ('tbl1');) instead of objcar and let's see the result. (you will get all properties and methods of the HTML table called "tbl1 ″).
  • Console. dirxml: Print the XML source tree of HTML element.

 

<table id="tbl1" cellpadding="0" cellspacing="0" border="0"><tr><td>A</td><td>B</td><td>C</td></tr></table><script language="javascript" type="text/javascript">//Create a classfunction Car(){this.Model = "Old Model"; this.getManufactor = new function(){return "Toyota";}} //Create a objectvar objCar = new Car(); //Firebugconsole.dir(objCar);console.dirxml(document.getElementById('tbl1')); </script>

#4. assertion (console. Assert ())

You can use console. assert () to test whether an expression is true or not. If the expression is false, it will write a message to the console and throw an exception.

Example:

 

<script language="javascript" type="text/javascript">function whatIsMyAge(year){var currYear = 2007;return currYear - year;} var yearOfBirth1 = 1982;var age1 = 25;console.assert(whatIsMyAge(yearOfBirth1) == age1); var yearOfBirth2 = 1982;var age2 = 11;console.assert(whatIsMyAge(yearOfBirth2) == age2); //You should get the error here.</script>

#5. Tracing (console. Trace ())

This function is very interesting. Before I tell you the way that I understand, let's take a look what console. trace does exactly in official website.

Console. trace ()

Prints an interactive stack trace of javascript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. you can click each function to take you to its source in the script tab, and click each argument value to inspect it in the Dom or HTML tabs.

This function will tell you about the route information from start point to end point. If you are not clear what I mean, let's take a look at the sample code and the result.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Suppose: we wanna know how "method3" function is invoked. so, we put this code "console. trace () "in that method. then, we run the program and we got the result as picture above. if we read the result from bottom to top, we will see "onclick (click clientX = 34, clientY = 26 )". that means the execution of Javascript started at on click event of button. then, we got "startTrace (" Result ")" in second line. that means startTrace function is invoked after firing onclick event and the parameter is "Result ". if we keep on checking from bottom to top, we will figure out the completed route from onclick event to method3.

If you wanna test more, you can move this code "console. trace () "to method2 (). then, firebug will give the new route from onclick event which is a started point to method2 () which is the end point.

I think that it's pretty useful if you are debugging the other developer's source code and you have no idea why this function is invoked.

Let me know if you are not clear what I'm trying to explain about console. trace ();.

#6. Timing (Measuring the time of your code)

You can use console. time (timeName) function to measure how long a participant code or function take. This feature is very helpful if you are trying to improve the performance of your Javascript code.

Example:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Result: measuringTime: 16 ms

#7. Javascript Profiler

You can start the profiler thought code (console. profile ('filename ') or by clicking "Profile" button from "Console" tag. it can be used for improving the performance of Javascript. it is similiar to the console. time () function but profiler can give your more advanced and detailed information.

I will tell you about this more details in next tutorial (Part2 ). I hope you all are clear about this tutorial. if you have any comment or suggestion, please drop a comment .. thanks. C ya tomorrow.

Reference ~

Console

Lele_api

[Reprinted address] http://michaelsync.net/2007/09/09/firebug-tutorial-logging-profiling-and-commandline-part-i

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.