forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ml
67 lines (61 loc) · 2.07 KB
/
plugin.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(*
* (c) 2004-2010 Anastasia Gornostaeva
*)
open Hooks
exception Error of string
let pluginlist :
((string * ((string * (string * string) list) list -> unit)) list) ref
= ref []
let add_plugin name proc =
pluginlist := (name, proc) :: !pluginlist
let loadfile name path opts =
let file = path ^ (if Dynlink.is_native then ".cmxs" else ".cmo") in
try
Dynlink.loadfile file;
log#info "Plugin %s (%s) is successfully loaded" name file;
true
with
| Dynlink.Error (Dynlink.File_not_found str) -> (
if Dynlink.is_native then (
log#error "Unable to load plugin %s (%s): %s"
name path (Dynlink.error_message (Dynlink.File_not_found str));
false
) else
let file = path ^ ".cma" in
try
Dynlink.loadfile file;
log#info "Plugin %s %s is successfully loaded" name file;
true
with Dynlink.Error err ->
log#error "Unable to load plugin %s (%s): %s"
name path (Dynlink.error_message err);
false
)
| Sys_error msg ->
log#error "Unable to load plugin %s (%s): %s" name path msg;
false
let load_plugins plugins =
List.iter
(fun (name, path, opts) ->
log#info "Loading plugin %s" name;
if List.mem_assoc name !pluginlist then (
log#info "Plugin %s is already loaded" name;
try
let plugin = List.assoc name !pluginlist in
plugin opts
with exn ->
log#error "Plugin %s failed: %s" name
(Printexc.to_string exn)
)
else
match path with
| None -> log#warning "Plugin %s is not found" name
| Some vpath ->
if loadfile name vpath opts then
try
let plugin = List.assoc name !pluginlist in
plugin opts
with exn ->
log#error "Plugin %s failed: %s" name
(Printexc.to_string exn)
) plugins