Jump to content
RAGE Multiplayer Community

Snippet: client-side implementaion of console.log


Recommended Posts

The print.ts snippet

  1. Information
  2. Problem
  3. Disadvantages
  4. Examples
  5. Sources

 

1. Information:

Since 1.0 we have an object mp.console which helps us to send any information to the console (F11)

There it's an interface

interface ConsoleMp {
	logInfo(message: string, save?: boolean, saveAsync?: boolean): void;
	logWarning(message: string, save?: boolean, saveAsync?: boolean): void;
	logError(message: string, save?: boolean, saveAsync?: boolean): void;
	logFatal(message: string, save?: boolean, saveAsync?: boolean): void;
	clear(): void;
	reset(): void;
	verbosity: RageEnums.ConsoleVerbosity | string;
}

2. Problem:

We couldn't pass two or more params into the log functions and the first argument is waiting the string type.

Snippet is extending usage of that interface by passing params like the function console.log.

3. Disadvantages:

Disadvantage 1: this snippet is skipping two params: [save, saveAsync] which allows you to save logdata to ragemp folder.

Disadvantage 2: snippet requires npm package: [util]

$ npm i --save util
// or
$ yarn add util

 

4. Examples:

Lets look to usage of the snippet print.ts

index.ts

import { print } from './print'

// print helps you to send message for log information in way like this
// and also you can pass any type to this function instead of logInfo which is waiting string argument
print.info(mp.players.local, "any", "parameter", "you", ...["want", "just", "simple", { foo: 'Bar' }])

// instead of usaging the mp.console
mp.console.logInfo(JSON.stringify(mp.players.local))
mp.console.logInfo("any")
mp.console.logInfo("parameter")
mp.console.logInfo("you")
mp.console.logInfo(["want", "just", "simple"].join("\n"))

5. Sources:

Snippet print.ts (typescript)

Скрытый текст
import { format } from 'util'

const enum LOG_TYPE {
  INFO  = "logInfo",
  WARN  = "logWarning",
  ERROR = "logError",
  FATAL = "logFatal",
  CLEAR = "clear",
  RESET = "reset",
}

function message(TYPE: LOG_TYPE, ...args: any[]) {
  if (mp.console) {
    try {
      return mp.console[TYPE](format('', ...args) + "\n")
    } catch(err) {
      return mp.console.logError(err.stack + "\n")
    }
  } else {
    return console.log(...args)
  }
}

export const print = {
  info:  (...params: any[]) => message(LOG_TYPE.INFO, ...params),
  warn:  (...params: any[]) => message(LOG_TYPE.WARN, ...params),
  error: (...params: any[]) => message(LOG_TYPE.ERROR, ...params),
  fatal: (...params: any[]) => message(LOG_TYPE.FATAL, ...params),
  clear: (...params: any[]) => message(LOG_TYPE.CLEAR, ...params),
  reset: (...params: any[]) => message(LOG_TYPE.RESET, ...params),
}

 

Snippet print.js (javascript)

Скрытый текст
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
exports.__esModule = true;
exports.print = void 0;
var util_1 = require("util");
function message(TYPE) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    if (mp.console) {
        try {
            return mp.console[TYPE](util_1.format.apply(void 0, __spreadArrays([''], args)) + "\n");
        }
        catch (err) {
            return mp.console.logError(err.stack + "\n");
        }
    }
    else {
        return console.log.apply(console, args);
    }
}
exports.print = {
    info: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["logInfo" /* INFO */], params));
    },
    warn: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["logWarning" /* WARN */], params));
    },
    error: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["logError" /* ERROR */], params));
    },
    fatal: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["logFatal" /* FATAL */], params));
    },
    clear: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["clear" /* CLEAR */], params));
    },
    reset: function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        return message.apply(void 0, __spreadArrays(["reset" /* RESET */], params));
    }
};

 

In-game screenshot:

d9lIiqk.png

Edited by cacao
Link to post
Share on other sites
Posted (edited)

Instead of these multiple arguments, why can't I just use ES6 template literals if I have multiple things to send and end up doing mp.console.logInfo(`${firstParam} ${secondParam}`) etc. Is there a usecase where I won't have variables but somehow have multiple things to send?

Edited by MrPancakers
Link to post
Share on other sites
3 часа назад, MrPancakers сказал:

why can't I just use ES6 template literals if I have multiple things to send and end up doing mp.console.logInfo(`${firstParam} ${secondParam}`) etc. Is there a usecase where I won't have variables but somehow have multiple things to send?

You can. Also you can pass them via JSON.stringify to make log of an object. The point of usecase is that to pass any type to the function instead of native type string usage.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...