Skip to content

Commit

Permalink
Minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Oct 21, 2024
1 parent 88232ad commit b305e56
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 140 deletions.
2 changes: 1 addition & 1 deletion app/dmbackup.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ integer function backup(app) result(rc)
rc = dm_db_open(db, app%database)

if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to open database')
call dm_error_out(rc, 'failed to open database ' // app%database)
return
end if

Expand Down
2 changes: 1 addition & 1 deletion app/dmbot.f90
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ function message_callback(connection, stanza, user_data) bind(c)
type = xmpp_stanza_get_type(stanza)

if (type == IM_STANZA_TYPE_ERROR) then
call logger%warning('received error message', error=E_IO)
call logger%warning('received error stanza', error=E_IO)
return
end if

Expand Down
20 changes: 10 additions & 10 deletions app/dmdb.f90
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ program dmdb

! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%logger, &
node_id = app%node_id, &
source = app%name, &
debug = app%debug, &
ipc = (len_trim(app%logger) > 0), &
verbose = app%verbose)
call logger%configure(name = app%logger, & ! Name of logger process.
node_id = app%node_id, & ! Node id.
source = app%name, & ! Log source.
debug = app%debug, & ! Forward debug messages via IPC.
ipc = (len_trim(app%logger) > 0), & ! Enable IPC.
verbose = app%verbose) ! Print logs to standard error.

init_block: block
! Open SQLite database.
Expand All @@ -64,10 +64,10 @@ program dmdb
end if

! Open observation message queue for reading.
rc = dm_mqueue_open(mqueue = mqueue, &
type = TYPE_OBSERV, &
name = app%name, &
access = MQUEUE_RDONLY)
rc = dm_mqueue_open(mqueue = mqueue, & ! Message queue type.
type = TYPE_OBSERV, & ! Observation type.
name = app%name, & ! Name of message queue.
access = MQUEUE_RDONLY) ! Read-only access.

if (dm_is_error(rc)) then
call logger%error('failed to open mqueue /' // trim(app%name) // ': ' // &
Expand Down
12 changes: 6 additions & 6 deletions app/dmfs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ program dmfs

! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%logger, &
node_id = app%node_id, &
source = app%name, &
debug = app%debug, &
ipc = (len_trim(app%logger) > 0), &
verbose = app%verbose)
call logger%configure(name = app%logger, & ! Name of logger process.
node_id = app%node_id, & ! Node id.
source = app%name, & ! Log source.
debug = app%debug, & ! Forward debug messages via IPC.
ipc = (len_trim(app%logger) > 0), & ! Enable IPC.
verbose = app%verbose) ! Print logs to standard error.

! Run main loop.
call dm_signal_register(signal_callback)
Expand Down
20 changes: 10 additions & 10 deletions app/dmgrc.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ program dmgrc

! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%logger, &
node_id = app%node_id, &
source = app%name, &
debug = app%debug, &
ipc = (len_trim(app%logger) > 0), &
verbose = app%verbose)
call logger%configure(name = app%logger, & ! Name of logger process.
node_id = app%node_id, & ! Node id.
source = app%name, & ! Log source.
debug = app%debug, & ! Forward debug messages via IPC.
ipc = (len_trim(app%logger) > 0), & ! Enable IPC.
verbose = app%verbose) ! Print logs to standard error.

init_block: block
! Open observation message queue for reading.
rc = dm_mqueue_open(mqueue = mqueue, &
type = TYPE_OBSERV, &
name = app%name, &
access = MQUEUE_RDONLY)
rc = dm_mqueue_open(mqueue = mqueue, & ! Message queue type.
type = TYPE_OBSERV, & ! Observation type.
name = app%name, & ! Name of message queue.
access = MQUEUE_RDONLY) ! Read-only access.

if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to open mqueue /' // trim(app%name) // ': ' // dm_system_error_message())
Expand Down
60 changes: 20 additions & 40 deletions app/dmimport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,11 @@ integer function import(app) result(rc)

! Check for appropriate database table.
select case (app%type)
case (TYPE_NODE)
has = dm_db_has_table(db, SQL_TABLE_NODES)
case (TYPE_SENSOR)
has = dm_db_has_table(db, SQL_TABLE_SENSORS)
case (TYPE_TARGET)
has = dm_db_has_table(db, SQL_TABLE_TARGETS)
case (TYPE_OBSERV)
has = dm_db_has_table(db, SQL_TABLE_OBSERVS)
case (TYPE_LOG)
has = dm_db_has_table(db, SQL_TABLE_LOGS)
case (TYPE_NODE); has = dm_db_has_table(db, SQL_TABLE_NODES)
case (TYPE_SENSOR); has = dm_db_has_table(db, SQL_TABLE_SENSORS)
case (TYPE_TARGET); has = dm_db_has_table(db, SQL_TABLE_TARGETS)
case (TYPE_OBSERV); has = dm_db_has_table(db, SQL_TABLE_OBSERVS)
case (TYPE_LOG); has = dm_db_has_table(db, SQL_TABLE_LOGS)
end select

if (.not. has) then
Expand Down Expand Up @@ -131,16 +126,11 @@ integer function import(app) result(rc)

! Read record from file.
select case (app%type)
case (TYPE_NODE)
rc = dm_csv_read(node, unit, app%separator, app%quote)
case (TYPE_SENSOR)
rc = dm_csv_read(sensor, unit, app%separator, app%quote)
case (TYPE_TARGET)
rc = dm_csv_read(target, unit, app%separator, app%quote)
case (TYPE_OBSERV)
rc = dm_csv_read(observ, unit, app%separator, app%quote)
case (TYPE_LOG)
rc = dm_csv_read(log, unit, app%separator, app%quote)
case (TYPE_NODE); rc = dm_csv_read(node, unit, app%separator, app%quote)
case (TYPE_SENSOR); rc = dm_csv_read(sensor, unit, app%separator, app%quote)
case (TYPE_TARGET); rc = dm_csv_read(target, unit, app%separator, app%quote)
case (TYPE_OBSERV); rc = dm_csv_read(observ, unit, app%separator, app%quote)
case (TYPE_LOG); rc = dm_csv_read(log, unit, app%separator, app%quote)
end select

! Ignore comments and empty rows.
Expand All @@ -161,16 +151,11 @@ integer function import(app) result(rc)
! Validate record but skip database insert on dry run.
if (app%dry) then
select case (app%type)
case (TYPE_NODE)
valid = dm_node_is_valid(node)
case (TYPE_SENSOR)
valid = dm_sensor_is_valid(sensor)
case (TYPE_TARGET)
valid = dm_target_is_valid(target)
case (TYPE_OBSERV)
valid = dm_observ_is_valid(observ)
case (TYPE_LOG)
valid = dm_log_is_valid(log)
case (TYPE_NODE); valid = dm_node_is_valid(node)
case (TYPE_SENSOR); valid = dm_sensor_is_valid(sensor)
case (TYPE_TARGET); valid = dm_target_is_valid(target)
case (TYPE_OBSERV); valid = dm_observ_is_valid(observ)
case (TYPE_LOG); valid = dm_log_is_valid(log)
end select

if (.not. valid) then
Expand All @@ -184,16 +169,11 @@ integer function import(app) result(rc)

! Validate and insert record.
select case (app%type)
case (TYPE_NODE)
rc = dm_db_insert(db, node)
case (TYPE_SENSOR)
rc = dm_db_insert(db, sensor)
case (TYPE_TARGET)
rc = dm_db_insert(db, target)
case (TYPE_OBSERV)
rc = dm_db_insert(db, observ)
case (TYPE_LOG)
rc = dm_db_insert(db, log)
case (TYPE_NODE); rc = dm_db_insert(db, node)
case (TYPE_SENSOR); rc = dm_db_insert(db, sensor)
case (TYPE_TARGET); rc = dm_db_insert(db, target)
case (TYPE_OBSERV); rc = dm_db_insert(db, observ)
case (TYPE_LOG); rc = dm_db_insert(db, log)
end select

! Handle database result.
Expand Down
2 changes: 1 addition & 1 deletion app/dmlog.f90
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ integer function read_args(app, log) result(rc)
arg_type('target', short='T', type=ARG_TYPE_ID), & ! -T, --target <id>
arg_type('observ', short='O', type=ARG_TYPE_UUID), & ! -O, --observ <uuid>
arg_type('source', short='Z', type=ARG_TYPE_ID, max_len=LOG_SOURCE_LEN), & ! -Z, --source <id>
arg_type('message', short='m', type=ARG_TYPE_STRING, max_len=LOG_MESSAGE_LEN, required=.true.) & ! -m, --message <string>
arg_type('message', short='m', type=ARG_TYPE_STRING, max_len=LOG_MESSAGE_LEN, required=.true.) & ! -m, --message <string>
]

! Read all command-line arguments.
Expand Down
40 changes: 20 additions & 20 deletions app/dmlogger.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ program dmlogger
character(len=*), parameter :: APP_NAME = 'dmlogger'
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 2
integer, parameter :: APP_PATCH = 3

integer, parameter :: APP_DB_NSTEPS = 500 !! Number of steps before database is optimised.
integer, parameter :: APP_DB_TIMEOUT = DB_TIMEOUT_DEFAULT !! SQLite 3 busy timeout in mseconds.

type :: app_type
!! Application settings.
character(len=ID_LEN) :: name = APP_NAME !! Name of logger instance and POSIX semaphore.
character(len=FILE_PATH_LEN) :: config = ' ' !! Path to configuration file.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to SQLite database file.
character(len=NODE_ID_LEN) :: node_id = ' ' !! Node id.
integer :: minlevel = LL_WARNING !! Minimum level for a log to be stored in the database.
logical :: ipc = .false. !! Use POSIX semaphore for process synchronisation.
logical :: verbose = .false. !! Print debug messages to stderr.
character(len=ID_LEN) :: name = APP_NAME !! Name of logger instance and POSIX semaphore.
character(len=FILE_PATH_LEN) :: config = ' ' !! Path to configuration file.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to SQLite database file.
character(len=NODE_ID_LEN) :: node_id = ' ' !! Node id.
integer :: min_level = LL_INFO !! Minimum level for a log to be stored in the database.
logical :: ipc = .false. !! Use POSIX semaphore for process synchronisation.
logical :: verbose = .false. !! Print debug messages to stderr.
end type app_type

class(logger_class), pointer :: logger ! Logger object.
Expand All @@ -45,10 +45,10 @@ program dmlogger
init_block: block
! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%name, & ! Name of global logger.
call logger%configure(name = app%name, & ! Name of global logger.
node_id = app%node_id, & ! Sensor node id.
source = app%name, & ! Application name.
ipc = .false., & ! Don't send logs via IPC.
source = app%name, & ! Application name.
ipc = .false., & ! Don't send logs via IPC.
verbose = app%verbose) ! Prints logs to terminal.

! Open SQLite database.
Expand All @@ -60,10 +60,10 @@ program dmlogger
end if

! Open log message queue for reading.
rc = dm_mqueue_open(mqueue = mqueue, &
type = TYPE_LOG, &
name = app%name, &
access = MQUEUE_RDONLY)
rc = dm_mqueue_open(mqueue = mqueue, & ! Message queue type.
type = TYPE_LOG, & ! Log type.
name = app%name, & ! Name of message queue.
access = MQUEUE_RDONLY) ! Read-only access.

if (dm_is_error(rc)) then
call logger%error('failed to open mqueue /' // trim(app%name) // ': ' // &
Expand Down Expand Up @@ -121,7 +121,7 @@ integer function read_args(app) result(rc)
! Overwrite settings.
call dm_arg_get(args(3), app%database)
call dm_arg_get(args(4), app%node_id)
call dm_arg_get(args(5), app%minlevel)
call dm_arg_get(args(5), app%min_level)
call dm_arg_get(args(6), app%ipc)
call dm_arg_get(args(7), app%verbose)

Expand All @@ -146,7 +146,7 @@ integer function read_args(app) result(rc)
return
end if

if (.not. dm_log_level_is_valid(app%minlevel)) then
if (.not. dm_log_level_is_valid(app%min_level)) then
call dm_error_out(rc, 'invalid log level')
return
end if
Expand All @@ -167,7 +167,7 @@ integer function read_config(app) result(rc)
if (dm_is_ok(rc)) then
call dm_config_get(config, 'database', app%database)
call dm_config_get(config, 'node', app%node_id)
call dm_config_get(config, 'minlevel', app%minlevel)
call dm_config_get(config, 'minlevel', app%min_level)
call dm_config_get(config, 'ipc', app%ipc)
call dm_config_get(config, 'verbose', app%verbose)
end if
Expand Down Expand Up @@ -211,7 +211,7 @@ subroutine run(app, db, mqueue, sem)

call logger%info('started ' // APP_NAME)
call logger%debug('waiting for log on mqueue /' // trim(app%name) // ' (minimum log level is ' // &
trim(LOG_LEVEL_NAMES(app%minlevel)) // ')')
trim(LOG_LEVEL_NAMES(app%min_level)) // ')')
ipc_loop: do
! Blocking read from POSIX message queue.
rc = dm_mqueue_read(mqueue, log)
Expand All @@ -229,7 +229,7 @@ subroutine run(app, db, mqueue, sem)
if (app%verbose) call logger%out(log)

! Skip if log level is lower than minimum level.
if (log%level < app%minlevel) cycle ipc_loop
if (log%level < app%min_level) cycle ipc_loop

! Skip if log already exists.
if (dm_db_has_log(db, log%id)) then
Expand Down
22 changes: 11 additions & 11 deletions app/dmlua.f90
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ program dmlua

! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%logger, &
node_id = app%node_id, &
source = app%name, &
debug = app%debug, &
ipc = (len_trim(app%logger) > 0), &
verbose = app%verbose)
call logger%configure(name = app%logger, & ! Name of logger process.
node_id = app%node_id, & ! Node id.
source = app%name, & ! Log source.
debug = app%debug, & ! Forward debug messages via IPC.
ipc = (len_trim(app%logger) > 0), & ! Enable IPC.
verbose = app%verbose) ! Print logs to standard error.

init_block: block
! Initialise Lua interpreter.
Expand Down Expand Up @@ -81,15 +81,15 @@ program dmlua
rc = dm_lua_open(lua, app%script, eval=.true.)

if (dm_is_error(rc)) then
call logger%error('failed to load Lua script', error=rc)
call logger%error('failed to load Lua script ' // app%script, error=rc)
exit init_block
end if

! Open observation message queue for reading.
rc = dm_mqueue_open(mqueue = mqueue, &
type = TYPE_OBSERV, &
name = app%name, &
access = MQUEUE_RDONLY)
rc = dm_mqueue_open(mqueue = mqueue, & ! Message queue type.
type = TYPE_OBSERV, & ! Observation type.
name = app%name, & ! Name of message queue.
access = MQUEUE_RDONLY) ! Read-only access.

if (dm_is_error(rc)) then
call logger%error('failed to open mqueue /' // trim(app%name) // ': ' // &
Expand Down
12 changes: 6 additions & 6 deletions app/dmpipe.f90
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ program dmpipe

! Initialise logger.
logger => dm_logger_get_default()
call logger%configure(name = app%logger, &
node_id = app%node_id, &
source = app%name, &
debug = app%debug, &
ipc = (len_trim(app%logger) > 0), &
verbose = app%verbose)
call logger%configure(name = app%logger, & ! Name of logger process.
node_id = app%node_id, & ! Node id.
source = app%name, & ! Log source.
debug = app%debug, & ! Forward debug messages via IPC.
ipc = (len_trim(app%logger) > 0), & ! Enable IPC.
verbose = app%verbose) ! Print logs to standard error.

! Run main loop.
call dm_signal_register(signal_callback)
Expand Down
Loading

0 comments on commit b305e56

Please sign in to comment.