Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 20x 15x 15x 2x 20x 5x 5x 4x 1x 5x 12x 12x 12x 12x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 80x 67x 4x 4x 2x 2x 1x 1x 4x 13x 78x 78x 78x 78x 78x 78x 78x 78x 13x 37x 37x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x | import { Injectable } from '@angular/core';
import { Subscription } from 'rxjs';
import { GenericUtils } from '@unpispas/upp-defs';
import { clockService } from './clock';
import { adhocService } from './adhoc';
import { viewService } from './view';
const enum LogsLevel {
Error= "Error",
Warn= "Warn",
Info= "Info",
Debug= "Debug",
Trace= "Trace"
}
/**
* @class logsService
* @description A service for managing application logs with levels, queuing, and flushing logs to a server.
* Provides logging capabilities with customizable log levels and integrates with external services.
*/
@Injectable({
providedIn: 'root'
})
export class logsService {
private _loggerUrl = 'logger/writer.php';
private _authtok: string | null = null;
private _started = true;
private _flush_subscription: Subscription | null = null;
private _queued: Array<any> = [];
private _session = GenericUtils.uuidv4();
/******************************/
/* LOGGER CAMPAIGN */
/******************************/
private _loggercampaignkey = 'upp-logger-campaignkey';
private _loggercampaignval: string | null = null;
/**
* @description Determine if the campaign is currently running
* @type {boolean}
*/
get IsActive(){
return this._started;
}
/**
* @description Gets or sets the current user logger campaign name.
* The logger campaign is persitently stored in the localStorage with key 'upp-logger-campaignkey'
* @type {string | null}
*/
get Campaign(): string | null {
if (this._loggercampaignval == null){
const _campaign = localStorage.getItem(this._loggercampaignkey);
if (_campaign){ // found in broser storage
this._loggercampaignval = _campaign;
}
}
return this._loggercampaignval;
}
set Campaign(value: string | null){
if (this._loggercampaignval != value){
if (value){
localStorage.setItem(this._loggercampaignkey, value);
}
else {
localStorage.removeItem(this._loggercampaignkey);
}
this._loggercampaignval = value;
}
}
/******************************/
/* STARTS HERE */
/******************************/
/**
* @constructor
* @param {clockService} clock - A service providing clock-based events.
* @param {adhocService} adhoc - A service for executing ad hoc HTTP requests.
* @param {viewService} view - A service for handling application view states.
*/
constructor(private clock: clockService, private adhoc: adhocService, private view: viewService){
window.console = this.OverrideConsole();
const _active = this.Campaign;
Iif (_active){
this.Start(_active);
}
}
/**
* @method Start
* @description Initializes logging for a given campaign. Subscribes to periodic flushing.
* @param {string} campaign - The campaign name for logging.
* @returns {Promise<void>}
*/
async Start(campaign: string){
Iif (campaign === null){
return; // must be in a campaign
}
this.Campaign = campaign;
this._started = true;
const _data: any = await this.adhoc.DoRequest(this._loggerUrl, null, {
command: 'INI',
session: this._session,
source: this.Campaign
}, false);
if (_data['errorcode'] == 0){
this._authtok = _data['authtok'];
}
Iif(this._flush_subscription != null){
this._flush_subscription.unsubscribe();
this._flush_subscription = null;
}
this._flush_subscription = this.clock.OnOneSecondTick.subscribe(
() => {
this.Flush();
});
}
/**
* @method Stop
* @description Stops logging, unsubscribes from periodic flushing, and ends the current logging session.
* @returns {Promise<void>}
*/
async Stop(){
if(this._flush_subscription != null){
this._flush_subscription.unsubscribe();
this._flush_subscription = null;
}
this._started = false;
Iif (this._authtok === null){
return; // not initialized
}
const _data: any = await this.adhoc.DoRequest(this._loggerUrl, null, {
command: 'END',
session: this._session,
authtok: this._authtok
}, false);
if (_data['errorcode'] == 0){
this._authtok = null;
}
}
/**
* @method Queue
* @description Queues a log message to be sent to the server.
* @param {LogsLevel} level - The level of the log (e.g., Error, Warn, Info, Debug, Trace).
* @param {string} message - The log message.
* @param {any} extras - Additional context for the log.
*/
Queue(level: LogsLevel, message: string, extras: any){
if (this._started){
this._queued.push({
level: level,
message: message,
extras: extras
});
}
}
private async Flush(){
Iif (!this.view.IsOnline){
return; // we are offline
}
if ((this._authtok !== null) && (this._queued.length > 0)){
// first try to send the log with the authtoken
const _first: any = await this.adhoc.DoRequest(this._loggerUrl, null, {
command: 'LOG',
session: this._session,
authtok: this._authtok,
logs: this._queued
}, false);
// if authtoken is expired then refresh and send the log
if ((_first['errorcode'] == 9) || (_first['errorcode'] == 10)) {
const _retry: any = await this.adhoc.DoRequest(this._loggerUrl, null, {
command: 'INI',
session: this._session,
source: this.Campaign,
logs: this._queued
}, false);
Iif (_retry['errorcode'] != 0){
console.error("[LOGGER]: Could not resend log traces (errorcode: " + _retry['errorcode']+ ")");
}
}
}
this._queued = [];
}
private OverrideConsole(): Console {
const _console = console;
function send_report(service: logsService, level: LogsLevel, message: string, extras: any) {
service.Queue(level, message, extras);
}
function process_args(args: any[]) {
const args_named: Record<string, string> = {};
for (let i = 0; i < args.length; i++) {
switch (typeof args[i]) {
case 'object':
for (const key in args[i]) {
args_named[key] = args[i][key]?.toString();
}
break;
default:
args_named[`arg_${i}`] = args[i]?.toString();
}
}
return args_named;
}
return {
..._console,
log: (message: string, ...args: any[]) => {
_console.log(message, ...args);
send_report(this, LogsLevel.Trace, message, process_args(args));
},
error: (message: string, ...args: any[]) => {
_console.error(message, ...args);
send_report(this, LogsLevel.Error, message, process_args(args));
},
warn: (message: string, ...args: any[]) => {
_console.warn(message, ...args);
send_report(this, LogsLevel.Warn, message, process_args(args));
},
info: (message: string, ...args: any[]) => {
_console.info(message, ...args);
send_report(this, LogsLevel.Info, message, process_args(args));
},
debug: (message: string, ...args: any[]) => {
_console.log(message, ...args);
send_report(this, LogsLevel.Debug, message, process_args(args));
},
trace: (message: string, ...args: any[]) => {
_console.log(message, ...args);
send_report(this, LogsLevel.Trace, message, process_args(args));
}
} as Console
}
}
|