Skip to content

console

Provides basic console output, unit testing and time measuring functionality.

Module: none (built-in)

All output functions write to the log file of the INFINITY.JS instance (and to the console, if one is attached). Which messages are written depends on the MinLogLevel setting in the [LogFile] section of the .ini configuration file: 0 writes everything, 1 suppresses console.log(), 2 additionally suppresses info(), debug(), table(), count(), time*() output, and 3 only writes error() messages.


Functions

assert()

Lets the execution continue if the condition results in boolean true. Stops execution and throws a runtime error if the condition results in boolean false. Throws a TypeError if the passed value is not a boolean.

Signature:

assert( condition: boolean ): void

Parameters:

  • condition: boolean

    Variable or an expression resulting in a boolean value.

Example:

let var1 = 3;
let var2 = 4;

console.assert(var1 < var2);
//continues execution
console.assert(var1 > var2);
//throws "Error: Assertion failed: console.assert", stops execution

count()

Counts up an internal counter on every function call and returns the counter value. Counters can be named through the name function parameter. count() also prints the counter name and the counter value to the console, unless disableOutput is set to true.

Signature:

count( name?: string, disableOutput?: boolean ): number

Parameters:

  • name: string, optional

    Can serve as an identifier for differentiating between multiple counters.

  • disableOutput: boolean, optional

    Disables the console output.

Return type: number

Example:

console.count();
console.count();
console.count("counterName");
//prints out:
//default: 1
//default: 2
//counterName: 1

countReset()

Resets a counter used previously with count().

Signature:

countReset( name?: string ): void

Parameters:

  • name: string, optional

    Can serve as an identifier for differentiating between multiple counters.

Example:

console.count();
console.count();
console.count('test');
console.countReset();
console.count();
//prints out:
//default: 1
//default: 2
//test: 1
//default: 1

debug()

Prints out a message (log level 1, "important") if MinLogLevel ≤1 is set in the [LogFile] section of the .ini file. Non-string values are converted to JSON. Same level as info().

Signature:

debug( msg: any ): void

Parameters:

  • msg: any

    Data to print out.

Example:

console.debug("Process finished");

error()

Prints out an error message (log level 3). Error messages are written with every MinLogLevel setting (≤3). Non-string values are converted to JSON.

Signature:

error( msg: any ): void

Parameters:

  • msg: any

    Data to print out.

Example:

console.error("Process failed");

info()

Prints out a message (log level 1, "important") if MinLogLevel ≤1 is set in the [LogFile] section of the .ini file. Non-string values are converted to JSON.

Signature:

info( msg: any ): void

Parameters:

  • msg: any

    Data to print out.

Example:

console.info("Process running");

log()

Prints out a message (log level 0, "message") if MinLogLevel 0 is set in the [LogFile] section of the .ini file. Non-string values are converted to JSON.

Signature:

log( msg: any ): void

Parameters:

  • msg: any

    Data to print out.

Example:

console.log("Process started");

readLn()

Halts the script execution and awaits user input. Returns the user input after the enter button is pressed. If INFINITY.JS is not running as a console application, an empty string is returned immediately.

Signature:

readLn(): string;

Example:

let input = console.readLn();

table()

Prints out an ASCII-formatted table containing the passed data to the console.

Signature:

table( data: Array<any> ): void

Parameters:

  • data: Array<any>

    Data to format. Every 1st-level array value adds a line. Values in multidimensional arrays add corresponding columns; for objects, the property names are used as column names. If data is not an array or is empty, nothing is printed.

Example:

console.table(["Data1", ["Data2", "Data3"], "Data4"]);
//prints out:
// | (index) | 0     | 1     |
// +---------+-------+-------+
// | 0       | Data1 |
// | 1       | Data2 | Data3 |
// | 2       | Data4 |

time()

Starts a timer, measuring execution time since the function call. Timers can be named through the name function parameter (default: 'default'). Calling time() again with the same name restarts the timer. See timeEnd() for further information.

Signature:

time( name?: string ): void

Parameters:

  • name: string, optional

    Can serve as an identifier for differentiating between multiple timers.

Example:

console.time();
infinity.sleep(500);
console.timeEnd();
//default: 0,50797710s

timeSince()

Returns the measured time in seconds (with fractions) since the call of time() without stopping the timer. The timer must have been started with time() before. timeSince() also prints the timer name and the measured time to the console, unless disableOutput is set to true.

Signature:

timeSince( name?: string, disableOutput?: boolean ): number

Parameters:

  • name: string, optional

    Can serve as an identifier for differentiating between multiple timers.

  • disableOutput: boolean, optional

    Disables the console output.

Return type: number

Example:

console.time();
infinity.sleep(500);
console.timeSince();
//default: 0,50797710s

timeEnd()

Terminates the time measuring for the corresponding timer and returns the measured time in seconds (with fractions) since the call of time(). The timer must have been started with time() before. timeEnd() also prints the timer name and the measured time to the console, unless disableOutput is set to true.

Signature:

timeEnd( name?: string, disableOutput?: boolean ): number

Parameters:

  • name: string, optional

    Can serve as an identifier for differentiating between multiple timers.

  • disableOutput: boolean, optional

    Disables the console output.

Return type: number

Example:

console.time();
infinity.sleep(500);
console.timeEnd();
//default: 0,50797710s

warn()

Prints out a warning (log level 2) if MinLogLevel ≤2 is set in the [LogFile] section of the .ini file. Non-string values are converted to JSON.

Signature:

warn( msg: any ): void

Parameters:

  • msg: any

    Data to print out.

Example:

console.warn("Process ending");

write()

Writes the given string directly to the console (standard output), without log prefix and independent of MinLogLevel. Has no effect if INFINITY.JS is not running as a console application. Throws a TypeError if output is not a string.

Signature:

write(output: string): void;

Parameters:

  • output: string

    The string to be written to the console.

Example:

console.write('Test');
console.write('Test');
//TestTest

writeLn()

Writes the given string directly to the console (standard output) and starts a new line, without log prefix and independent of MinLogLevel. Has no effect if INFINITY.JS is not running as a console application. Throws a TypeError if output is not a string.

Signature:

writeLn(output: string): void;

Parameters:

  • output: string

    The string to be written to the console.

Example:

console.writeLn('Test');
console.writeLn('Test');
//Test
//Test