Log handler example

Ano script supports hooking own handler functions to log events, this is demonstration about that. This example uses also function prototype, which enables Ano compiler to check function parameters more closely.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh log_handler

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; log_handler.ano
;
; To compile:
;
; $ ./build/ano ./examples/log_handler/log_handler.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		log_handler
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Simple error handler
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; Ano script supports hooking own handler functions to log events, this is
; demonstration about that. This example uses also function prototype, which
; enables Ano compiler to check function parameters more closely.
;

; Enable function prototype checking.
;
proto print_error(class, code, level, errno, cmd, node)

main [exit: 0] {
	; Log handlers need no initialization, just try to call nonexistent
	; function to cause an error.
	;
	nonexistent()
}

loghandler info {
	print "There is something worth mention, message details:" . "\n\n"

	_code = get_last_error_code
	_level = get_last_error_level
	_errno = get_last_error_errno

	_ano_cmd = get_last_error_command
	_ano_node = get_last_error_node

	&print_error (\
		class:	"Info", \
		code:	_code, \
		level:	_level, \
		errno:	_errno, \
		cmd:	_ano_cmd, \
		node:	_ano_node)
}

loghandler warn {
	print "Something did not work as expected, warning details:" . "\n\n"

	_code = get_last_error_code
	_level = get_last_error_level
	_errno = get_last_error_errno

	_ano_cmd = get_last_error_command
	_ano_node = get_last_error_node

	&print_error (\
		class:	"Warning", \
		code:	_code, \
		level:	_level, \
		errno:	_errno, \
		cmd:	_ano_cmd, \
		node:	_ano_node)
}

loghandler error {
	print "Something went totally wrong, error details:" . "\n\n"

	_code = get_last_error_code
	_level = get_last_error_level
	_errno = get_last_error_errno

	&print_error (\
		class:	"Error", \
		code:	_code, \
		level:	_level, \
		errno:	_errno, \
		cmd:	"N/A", \
		node:	"N/A")
}

function print_error (_class, _code, _level, _errno, _cmd, _node) {
	print "Error class: " . _class . "\n"
	print "Error code: " . _code . "\n"
	print "Error level: " . _level . "\n"
	print "Error errno: " . _errno . "\n"

	print "Command: " . _cmd . "\n"
	print "Node: " . _node . "\n"
}
; ------------------------------------------------------------------------------
; log_handler.w2c
;
; To compile:
;
; $ ./build/widget ./examples/log_handler/log_handler.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; log_handler.m2c
;
; To compile:
;
; $ ./build/menu ./examples/log_handler/log_handler.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.