import { Component , OnDestroy , OnInit , Input } from "@angular/core" ; import { Subscription } from "rxjs" ; import { Logger , LoggingService , LogLevelConverter , LogMessage } from "ionic-logging-service" ; import { LoggingViewerFilterService } from "../logging-viewer-filter.service" ; /**
 * Component for displaying the current logs.
 *
 * The component can be embedded in any web page using:
 *
 * &lt;ionic-logging-viewer>&lt;/ionic-logging-viewer>
 */ @ Component ( { selector : "ionic-logging-viewer" , templateUrl : "./logging-viewer.component.html" , styleUrls : [ "./logging-viewer.component.scss" ] } ) export class LoggingViewerComponent implements OnInit , OnDestroy { /**
     * Comma-separated list of localStorageKeys. If set, the logs get loaded from localStorage instead of memory.
     */ @ Input ( ) public localStorageKeys : string ; /**
     * Log messages which fulfill the filter condition.
     */ public logMessagesForDisplay : LogMessage [ ] ; private logger : Logger ; private logMessages : LogMessage [ ] ; private logMessagesChangedSubscription : Subscription ; private filterChangedSubscription : Subscription ; /**
     * Creates a new instance of the component.
     */ constructor ( private loggingService , private loggingViewerFilterService ) { this . logger = loggingService . getLogger ( "Ionic.Logging.Viewer.Component" ) ; const methodName = "ctor" ; this . logger . entry ( methodName ) ; this . logger . exit ( methodName ) ; } /**
     * Initialize the component.
     *
     * This is done by reading the filter data from [LoggingViewerFilterService](LoggingViewerFilterService.html)
     * and the log messages from [LoggingService](../../../ionic-logging-service/typedoc/index.html).
     * If the localStorageKeys property is set, the messages are read from local storage.
     */ public ngOnInit ( ) { const methodName = "ngOnInit" ; this . logger . entry ( methodName ) ; this . loadLogMessages ( ) ; this . filterLogMessages ( ) ; this . logMessagesChangedSubscription = this . loggingService . logMessagesChanged . subscribe ( async ( ) => { this . loadLogMessages ( ) ; this . filterLogMessages ( ) ; } ) ; this . filterChangedSubscription = this . loggingViewerFilterService . filterChanged . subscribe ( ( ) => { this . filterLogMessages ( ) ; } ) ; this . logger . exit ( methodName ) ; } /**
     * Clean up.
     */ public ngOnDestroy ( ) { const methodName = "ngOnDestroy" ; this . logger . entry ( methodName ) ; this . logMessagesChangedSubscription . unsubscribe ( ) ; this . filterChangedSubscription . unsubscribe ( ) ; this . logger . exit ( methodName ) ; } /**
     * Filter the log messages.
     */ public filterLogMessages ( ) { this . logMessagesForDisplay = this . logMessages . filter ( ( message ) => this . filterLogMessagesByLevel ( message ) && this . filterLogMessagesBySearch ( message ) ) ; } param message returns public filterLogMessagesByLevel ( message ) { const levelValue = this . loggingViewerFilterService . level ; return LogLevelConverter . levelFromString ( message . level ) >= LogLevelConverter . levelFromString ( levelValue ) ; } param message returns public filterLogMessagesBySearch ( message ) { const searchValue = new RegExp ( this . loggingViewerFilterService . search , "i" ) ; return message . logger . search ( searchValue ) >= 0 || message . methodName . search ( searchValue ) >= 0 || message . message . join ( "|" ) . search ( searchValue ) >= 0 ; } /**
     * Load the current log messages.
     * For unit test purposes mainly.
     */ public loadLogMessages ( ) { if ( this . localStorageKeys ) { this . logMessages = [ ] ; for ( const localStorageKey of this . localStorageKeys . split ( "," ) ) { this . logMessages = this . logMessages . concat ( this . loggingService . getLogMessagesFromLocalStorage ( localStorageKey ) ) ; } this . logMessages = this . logMessages . sort ( ( a , b ) => a . timeStamp . getTime ( ) - b . timeStamp . getTime ( ) ) ; } else { this . logMessages = this . loggingService . getLogMessages ( ) ; } } }