Skip to content

Instantly share code, notes, and snippets.

@badpenguin
Created February 14, 2018 11:18
Show Gist options
  • Save badpenguin/d0da3a9c0be0c4dc372332819c8f771a to your computer and use it in GitHub Desktop.
Save badpenguin/d0da3a9c0be0c4dc372332819c8f771a to your computer and use it in GitHub Desktop.
simple replacement for angular1 $log service - include an rxjs observable debugger and some helper for android's web apps
/*
* Antonio Gallo - https://www.antoniogallo.it - http://www.badpenguin.org/
* simple replacement for angular1 $log service
*/
// makes TS compiler happy
import {jStringify} from "./helpers";
import {Observable} from "rxjs";
declare let console: any;
export class $log {
static has_debug: boolean = true;
static is_android: boolean = false;
static is_android_emulator: boolean = false;
static error( ...args: any[] ) : void {
//( console && console.error ) && console.error( ...args );
if ( console && console.error ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.error(args[0]+': '+jStringify(args[1]));
} else {
console.error( ...args );
}
}
}
static warn( ...args: any[] ) : void {
//( console && console.warn ) && console.warn( ...args );
if ( console && console.warn ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.warn(args[0]+': '+jStringify(args[1]));
} else {
console.warn( ...args );
}
}
}
static info( ...args: any[] ) : void {
//( console && console.info ) && console.info( ...args );
if ( console && console.info ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.info(args[0]+': '+jStringify(args[1]));
} else {
console.info( ...args );
}
}
}
// NOTE: not using .debug() because of the colours in google chrome
static debug( ...args: any[] ) : void {
// SKIP debugging
if (!$log.has_debug) {
return;
}
//( $log.has_debug && console && console.debug ) && console.debug( ...args );
if ( console && console.log ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.log(args[0]+': '+jStringify(args[1]));
} else {
console.log( ...args );
}
}
}
// TODO bugged?
static log( ...args: any[] ) : void {
//( console && console.log ) && console.log( ...args );
if ( !console ) return;
if ( console.debug ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.debug(args[0]+': '+jStringify(args[1]));
} else {
console.debug( ...args );
}
}
else
if ( console.log ) {
if ($log.is_android && Array.prototype.slice.call(args).length>1) {
console.log(args[0]+': '+jStringify(args[1]));
} else {
console.log( ...args );
}
}
}
static boolean(arg1:string, arg2:boolean) : void {
$log.debug(arg1+': '+(arg2?'true':'false'));
}
static object(arg1:string, arg2:any) : void {
$log.debug(arg1+': '+ jStringify(arg2) );
}
}
/*
* http://blog.angular-university.io/debug-rxjs/
*/
declare module 'rxjs/Observable' {
interface Observable<T> {
debug: (message:string, oncomplete?:boolean, showData?:boolean) => Observable<T>;
}
}
Observable.prototype.debug = function (message:string, oncomplete:boolean=false, showData:boolean=false ) {
return this.do(
function (next:any) {
if ($log.has_debug) {
if (showData) {
$log.debug('OK '+message,next);
} else {
$log.debug('OK',message);
}
}
},
function (err:any) {
if ($log.has_debug) {
$log.error('ERROR: '+message,err);
}
},
function () {
if (oncomplete && $log.has_debug) {
$log.debug('COMPLETED',message);
}
}
);
};
// initialize variable when loading
$log.is_android = window.navigator.userAgent.match(/android/i) != null;
$log.is_android_emulator = $log.is_android && window.navigator.userAgent.match(/sdk/i) != null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment