Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Save some space ;)
[simgrid.git] / src / xbt / module.c
1 /* $Id$ */
2
3 /* module handling                                                          */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/error.h"
14 #include "xbt/dynar.h"
15 #include "xbt/config.h"
16
17 #include "gras/process.h" /* FIXME: bad loop */
18
19 #include "xbt/module.h" /* this module */
20
21 #include "xbt_modinter.h"  /* prototype of other module's init/exit in XBT */
22 #include "gras_modinter.h" /* same in GRAS */
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module,xbt, "module handling");
25
26 static int xbt_running_process = 0;
27
28 struct xbt_module_ {
29   xbt_dynar_t *deps;
30   xbt_cfg_t *cfg;
31   int ref;
32   xbt_module_new_fct_t new;
33   xbt_module_finalize_fct_t finalize;
34 };
35
36 void 
37 xbt_init(int *argc, char **argv) {
38   static short int first_run = 1;
39   if(first_run)
40     xbt_init_defaultlog(argc, argv, NULL);
41   first_run = 0;
42 }
43
44 /**
45  * xbt_init_defaultlog:
46  * @argc:
47  * @argv:
48  *
49  * Initialize the gras mecanisms.
50  */
51 void
52 xbt_init_defaultlog(int *argc,char **argv, const char *defaultlog) {
53   int i,j;
54   char *opt;
55   int found=0;
56
57   INFO0("Initialize GRAS");
58   
59   /** Set logs and init log submodule */
60   for (i=1; i<*argc; i++) {
61     if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
62       found = 1;
63       opt=strchr(argv[i],'=');
64       opt++;
65       xbt_log_control_set(opt);
66       DEBUG1("Did apply '%s' as log setting",opt);
67       /*remove this from argv*/
68       for (j=i+1; j<*argc; j++) {
69         argv[j-1] = argv[j];
70       } 
71       argv[j-1] = NULL;
72       (*argc)--;
73       i--; /* compensate effect of next loop incrementation */
74     }
75   }
76   if (!found && defaultlog) {
77      xbt_log_control_set(defaultlog);
78   }
79    
80   gras_process_init(); /* calls procdata_init, which calls dynar_new */
81   /** init other submodules */
82   if (xbt_running_process++ == 0) {
83     gras_msg_init();
84     gras_trp_init();
85     gras_datadesc_init();
86   }
87 }
88
89 /**
90  * xbt_exit:
91  *
92  * Finalize the gras mecanisms.
93  */
94 void 
95 xbt_exit(){
96   INFO0("Exiting GRAS");
97   gras_process_exit();
98   if (--xbt_running_process == 0) {
99     gras_msg_exit();
100     gras_trp_exit();
101     gras_datadesc_exit();
102   }
103   xbt_log_exit();
104   DEBUG0("Exited GRAS");
105 }