Logo AND Algorithmique Numérique Distribuée

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