Console
Most JavaScript environments have some sort of console that allows running
programs to print output, log messages and the like. In web browsers, this
capability is provided by the console
object, and in server-side
environments the program may write output to the terminal. Console
provides
a single interface to all these facilities, using the most appropriate device
wherever it is being used.
// In the browser JS.require('JS.Console', function(Console) { ... }); // In CommonJS var Console = require('jsclass/src/console').Console;
When writing output to a terminal, or the WebKit developer console, Console
also supports many formatting commands for changing the color and other
attributes of printed text.
The main two output methods are as follows:
Console.puts(string)
– printsstring
to the console with a line break afterward, so that subsequent messages appear on the next line.Console.print(string)
– printsstring
without applying line breaks, allowing multipleprint()
calls to write output to the same line if the console supports this. In environments that don’t support this, the output is buffered until the nextputs()
call.
The output devices used are as follows:
- In web browsers that support
window.console
, the development console is used to log output. - In web browsers with no
console
object, thealert()
function is used. - In Adobe AIR applications running under
adl
, theruntime.trace()
function is used to write to the terminal. - In programs running in a terminal, output is sent to
stdout
.
Formatting
The following formatting commands are available for formatting output. Not all
environments support formatting, and on those environments these commands will
be ignored. All commands are methods of Console
, for example you can switch
to bold text by calling Console.bold()
. Text printed after the command has
been issued will have the formatting applied to it. In environments that do
not support formatting these methods have no effect.
reset()
resets all formatting attributesbold()
,normal()
set the weight of the textitalic()
,noitalic()
switch between italic and roman textunderline()
,noline()
apply and remove underliningblink()
,noblink()
apply and remove blinking textblack()
,red()
,green()
,yellow()
,blue()
,magenta()
,cyan()
,white()
,nocolor()
set the current text color- Color methods can be prefixed with
bg
, for examplebgyellow()
, to set the current background color
Multiple formatting instructions can be combined using this method:
Console.consoleFormat('bold', 'red');
The consoleFormat()
method takes a list of formatting instructions as input.
It calls reset()
and then applies the given formats.
On some platforms, Console
also supports a set of commands for moving the
cursor and clearing parts of the screen, for creating interfaces in the
terminal. Those commands are:
cursorUp(n)
,cursorDown(n)
,cursorForward(n)
,cursorBack(n)
move the cursorn
places in the given directioncursorNextLine(n)
,cursorPrevLine(n)
move the cursor byn
linescursorColumn(x)
moves the cursor to columnx
cursorPosition(x,y)
moves the cursor to columnx
, rowy
cursorHide()
,cursorShow()
hides and shows the cursoreraseScreen()
clears the entire terminal screeneraseScreenForward()
,eraseScreenBack()
clears everything either after or before the cursor positioneraseLine()
clears the whole of the current lineeraseLineForward()
,eraseLineBack()
clears the current line either to the right or the left of the cursor
Console
as a mixin
Finally note that Console
can be used as a mixin so all the above methods
appear in the current class:
Logger = new Class({ include: Console, info: function(message) { this.bgblack(); this.white(); this.print('INFO'); this.reset(); this.puts(' ' + message); } });