Home | History | Annotate | Download | only in net_internals
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * This class stores information of one single entry of log
      7  */
      8 
      9 var CrosLogEntry = function() {
     10 
     11   /**
     12     * @constructor
     13     */
     14   function CrosLogEntry() {
     15     // The entry is visible by default
     16     this.visibility = true;
     17   }
     18 
     19   CrosLogEntry.prototype = {
     20     //------------------------------------------------------------------------
     21     // Log input text parser
     22     // Parses network log into tokens like time, name, pid
     23     // and description.
     24     //--------------------------------------------------------------------------
     25     tokenizeNetworkLog: function(NetworkLogEntry) {
     26       var tokens = NetworkLogEntry.split(' ');
     27       var timeTokens = tokens[0].split(/[\s|\:|\-|T|\.]/);
     28 
     29       // List of all parameters for Date Object
     30       var year = timeTokens[0];
     31       var month = timeTokens[1];
     32       var day = timeTokens[2];
     33       var hour = timeTokens[3];
     34       var minute = timeTokens[4];
     35       var second = timeTokens[5];
     36       var millisecond = (parseInt(timeTokens[6]) / 1000).toFixed(0);
     37       this.time = new Date(year, month, day, hour, minute,
     38                            second, millisecond);
     39 
     40       // Parses for process name and ID.
     41       var process = tokens[2];
     42       if (hasProcessID(process)) {
     43         var processTokens = process.split(/[\[|\]]/);
     44         this.processName = processTokens[0];
     45         this.processID = processTokens[1];
     46       } else {
     47         this.processName = process.split(/\:/)[0];
     48         this.processID = 'Unknown';
     49       }
     50 
     51       // Gets level of the log: error|warning|info|unknown if failed.
     52       this.level = hasLevelInfo(tokens[3]);
     53 
     54       // Treats the rest of the entry as description.
     55       var descriptionStartPoint = NetworkLogEntry.indexOf(tokens[2]) +
     56           tokens[2].length;
     57       this.description = NetworkLogEntry.substr(descriptionStartPoint);
     58     },
     59 
     60     // Represents the Date object as a string.
     61     getTime: function() {
     62       return this.time.getMonth() + '/' + this.time.getDate() +
     63           ' ' + this.time.getHours() + ':' + this.time.getMinutes() +
     64           ':' + this.time.getSeconds() + ':' + this.time.getMilliseconds();
     65     }
     66   };
     67 
     68   /**
     69    * Helper function
     70    * Takes a token as input and searches for '['.
     71    * We assume if the token contains '[' it contains a process ID.
     72    *
     73    * @param {string} token A token from log
     74    * @return {boolean} true if '[' is found
     75    */
     76   var hasProcessID = function(token) {
     77     return token != undefined && token.indexOf('[') != -1;
     78   }
     79 
     80   /**
     81    * Helper function
     82    * Checks if the input token contains level information.
     83    *
     84    * @param {string} token A token from log
     85    * @return {string} Level found in the token
     86    */
     87   var hasLevelInfo = function(token) {
     88     if (token == undefined)
     89       return 'Unknown';
     90     if (token.toLowerCase().indexOf('err') != -1) {
     91       return 'Error';
     92     } else if (token.toLowerCase().indexOf('warn') != -1) {
     93       return 'Warning';
     94     } else if (token.toLowerCase().indexOf('info') != -1) {
     95       return 'Info';
     96     } else {
     97       return 'Unknown';
     98     }
     99   }
    100 
    101   return CrosLogEntry;
    102 }();
    103