Logging

This module provides logging to HALMD scripts and modules.

Script Logger

The script logger may be used in HALMD scripts.

A message is logged with one of the following severity levels.

Severity Description
error An error has occurred, and HALMD will abort
warning Warn user, e.g., when using single precision
message Normal logging level, e.g., for simulation progress and parameters
info More verbose logging level, e.g., for internal parameters
debug Low frequency debugging messages
trace High frequency debugging messages

This example shows use of the script logger:

local halmd = require("halmd")

local log = halmd.io.log

function my_simulation(args)
    log.info("box edge lengths: %s", table.concat(args.length, " "))

    log.message("equilibrate system for %d steps", args.equilibrate)

    log.message("measure mean-square displacement for %d steps", args.steps)
end
halmd.io.log.error(format, ...)

Log message with severity error.

Parameters:format (string) – see string.format
halmd.io.log.warning(format, ...)

Log message with severity warning.

Parameters:format (string) – see string.format
halmd.io.log.message(format, ...)

Log message with severity message.

Parameters:format (string) – see string.format
halmd.io.log.info(format, ...)

Log message with severity info.

Parameters:format (string) – see string.format
halmd.io.log.debug(format, ...)

Log message with severity debug.

Parameters:format (string) – see string.format
halmd.io.log.trace(format, ...)

Log message with severity trace.

Parameters:format (string) – see string.format

Module Logger

This class provides module loggers for Lua and C++ modules.

A logger may optionally have a label, which is prepended to messages to distinguish output of a module from that of other modules.

This example shows use of a logger in a HALMD module:

local log    = require("halmd.io.log")
local module = require("halmd.utility.module")

-- C++ class
local my_potential = assert(libhalmd.mdsim.potentials.my_potential)

-- use the same logger for all instances
local logger = log.logger({label = "my_potential"})

local M = module(function(args)
    -- logs message with prefix "my_potential: "
    logger:message("parameters: %g %g %g", 1.0, 0.88, 0.8)

    -- pass module logger to C++ constructor
    local self = my_potential(..., logger)

    return self
end)

return M
class halmd.io.log.logger(args)

Construct a logger instance, optionally with given label.

Parameters:
  • args (table) – keyword arguments (optional)
  • args.label (string) – logging prefix (optional)
error(format, ...)

Log message with severity error.

Parameters:format (string) – see string.format
warning(format, ...)

Log message with severity warning.

Parameters:format (string) – see string.format
message(format, ...)

Log message with severity message.

Parameters:format (string) – see string.format
info(format, ...)

Log message with severity info.

Parameters:format (string) – see string.format
debug(format, ...)

Log message with severity debug.

Parameters:format (string) – see string.format
trace(format, ...)

Log message with severity trace.

Parameters:format (string) – see string.format

Logging Setup

The following functions setup available logging sinks.

By default, messages are logged to console with severity warning.

This example shows logging setup in a HALMD script:

local halmd = require("halmd")

-- log warning and error messages to console
halmd.io.log.open_console({severity = "warning"})
-- log anything except trace messages to file
halmd.io.log.open_file("kob_andersen.log", {severity = "debug"})
halmd.io.log.open_console(args)

Log messages with equal or higher severity to console.

If severity is not specified, it is set to message.

Parameters:
  • args (table) – keyword arguments (optional)
  • args.severity (string) – log severity level (optional)
halmd.io.log.close_console()

Disable logging to console.

halmd.io.log.open_file(filename, args)

Log messages with equal or higher severity to file.

If severity is not specified, it is set to message.

If a file with filename exists, it is truncated.

Parameters:
  • filename (string) – log filename
  • args (table) – keyword arguments (optional)
  • args.severity (string) – log severity level (optional)
halmd.io.log.close_file()

Close log file.