Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Libraries

Three libraries cover jobs most programs end up doing. std/logs records what a program did, with levels, typed fields and several destinations. std/tui draws full-screen terminal interfaces. vendor/sqlite3 puts the SQLite database an #import away. This first example is the shortest useful log.

#import "std/logs"

'' Logs normally go to stderr with a timestamp. This sends them to
'' stdout with no timestamp, so the output is the same on every run.
dim tr as logs.TextRenderer
tr.stamp = logs.TS_NONE
dim con as logs.ConsoleSink = logs.ConsoleSink( logs.CT_STDOUT, @tr )
logs.LogInit( @con, logs.LOG_INFO )

logs.LogInfo( "starting" )
logs.LogDebug( "not shown: debug is below the INFO level" )
logs.LogWarn( "disk 91% full" )

logs.LogShutdown( )

Output

INFO   starting
WARN   disk 91% full

logs.LogInit connects the program's default logger to a sink and sets its level; after that, logs.LogInfo and its siblings can be called from anywhere. The debug message is dropped because LOG_DEBUG is below the LOG_INFO level the logger was given. logs.LogShutdown( ) flushes and closes the sinks at the end.

A ConsoleSink normally writes to stderr, because logs are diagnostics and must not mix into output that is piped to another program, and each line normally starts with the time. The examples in this section send their logs to stdout without a timestamp, so that the output is the same on every run.

The pages that follow:

Activities

  1. Change the level passed to LogInit to logs.LOG_DEBUG.
  2. Remove tr.stamp = logs.TS_NONE and run the program twice.