Hello.
I'm new to the language, and I have problem with writing very simple program using fsmonitor.
I wrote this code:
os
import FSMonitor # needed for FSMonitor class
import AsyncDispatch # needed for Dispatcher
# this is callback handler for dispatcher
proc monitorEvent(m: FSMonitor; ev: MonitorEvent) =
echo (ev)
var monitor = FSMonitor()
monitor.add("/home/user1/")
var dispatcher = newDispatcher ()
#dispatcher.register(monitor, monitorEvent)
echo("end of program")
Everything compile just fine but i got runtime error
~/Data/nim/nim-0.10.2/bin/nim c -r --verbosity:0 --warnings:off nimtestq.nim config/nim.cfg(17, 83) Hint: line too long [LineTooLong] lib/pure/asyncdispatch.nim(1254, 6) Hint: Processing recvLine as an async proc. [User] nimtestq.nim(12, 4) Hint: 'dispatcher' is declared but not used [XDeclaredButNotUsed] nimtestq.nim(6, 5) Hint: 'nimtestq.monitorEvent(m: FSMonitor, ev: MonitorEvent)' is declared but not used [XDeclaredButNotUsed] Traceback (most recent call last) nimtestq.nim(10) nimtestq fsmonitor.nim(99) add os.nim(277) raiseOSError Error: unhandled exception: Invalid argument [OSError] Error: execution of an external program failed
According to documentation proc add(monitor: FSMonitor; target: string; filters = {MonitorAll}): cint {. discardable, raises: [OSError], tags: [].} Target is string and i suppose it should be path do directory. What i am doing wrong?
If I uncomment line #dispatcher.register(monitor, monitorEvent) then i got compilation error
~/Data/nim/nim-0.10.2/bin/nim c -r --verbosity:0 --warnings:off nimtestq.nim config/nim.cfg(17, 83) Hint: line too long [LineTooLong] lib/pure/asyncdispatch.nim(1254, 6) Hint: Processing recvLine as an async proc. [User] nimtestq.nim(13, 10) Error: type mismatch: got (PDispatcher, FSMonitor, proc (FSMonitor, MonitorEvent){.gcsafe, locks: 0.}) but expected one of: fsmonitor.register(d: Dispatcher, monitor: FSMonitor, handleEvent: proc (FSMonitor, MonitorEvent){.closure.}) asyncdispatch.register(fd: TAsyncFD)
In this case i don't know how to convert type PDispatcher to Dispatcher type?
I couldn't find some working example how to use FSMonitor in nim. According documentation fsmonitor is experimental. Is there some way to get working this simple kind of program in nim?
Im using nim 'nim-0.10.2' and running on ubuntu
uname -a Linux pcx 3.13.0-51-generic #84-Ubuntu SMP Wed Apr 15 12:08:34 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.2 LTS Release: 14.04 Codename: trusty
Thank you for all advices.
You have to use var monitor = newMonitor() to properly initialize the monitor. See the source code at the bottom of the module for an example usage: https://github.com/Araq/Nim/blob/devel/lib/pure/fsmonitor.nim#L201-L217
I would recommend updating to Nim 0.11.0 as it fixes many bugs (or wait for 0.11.2 which should come soon).
Yet another good example of why we need the ability to restrict instantiation of types to within modules (i.e. 'constructors')
var monitor = FSMonitor()
should yield a compiler error if it happens outside of the fsmonitor module.
Thank you. I do not understand well why last loop is so crucial, but now everything works perfectly fine :) I will put my working sample:
import os
import FSMonitor # needed for FSMonitor class
import asyncio
# this is callback handler for dispatcher
proc monitorEvent(m: FSMonitor; ev: MonitorEvent) =
echo (ev)
var monitor = newMonitor()
echo monitor.add("/tmp")
var dispatcher = newDispatcher ()
dispatcher.register(monitor, monitorEvent)
while true:
if not dispatcher.poll(): break
Apparently there's now a wrapper for libfswatch:
https://github.com/FedericoCeratto/nim-fswatch
While no help to you, maybe it's interesting for someone else reading this. I ported over fsmonitor to modern asyncdispatch in February, since I quickly had to hack together an online event display. But it only supports linux, same as the old fsmonitor.
https://github.com/Vindaar/fsmonitor2
However, looking at the File System Event API for OSX, adding support doesn't seem all that complicated:
I don't have a Mac, so attempting that would be a pain. Sounds like a fun weekend project for someone to attempt though. :)