loggify main.
[profile/ivi/eina.git] / src / lib / eina_main.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "eina_config.h"
26 #include "eina_private.h"
27 #include "eina_types.h"
28 #include "eina_main.h"
29 #include "eina_error.h"
30 #include "eina_log.h"
31 #include "eina_hash.h"
32 #include "eina_stringshare.h"
33 #include "eina_list.h"
34 #include "eina_array.h"
35 #include "eina_counter.h"
36 #include "eina_benchmark.h"
37 #include "eina_magic.h"
38 #include "eina_rectangle.h"
39 #include "eina_safety_checks.h"
40
41 /*============================================================================*
42  *                                  Local                                     *
43  *============================================================================*/
44
45 /**
46  * @cond LOCAL
47  */
48
49 static int _eina_main_count = 0;
50 static int _eina_log_dom = -1;
51 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
52 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
53
54 /**
55  * @endcond
56  */
57
58 /*============================================================================*
59  *                                 Global                                     *
60  *============================================================================*/
61
62 /*============================================================================*
63  *                                   API                                      *
64  *============================================================================*/
65
66 /**
67  * @addtogroup Eina_Main_Group Main
68  *
69  * @brief These functions provide general initialisation and shut down
70  * functions.
71  *
72  * @{
73  */
74
75 /**
76  * @brief Initialize the Eina library.
77  *
78  * @return 1 or greater on success, 0 on error.
79  *
80  * This function sets up all the eina modules. It returns 0 on
81  * failure (that is, when one of the module fails to initialize),
82  * otherwise it returns the number of times it has already been
83  * called. The list of initialisation functions that are called are
84  * (in that order):
85  *
86  * @li eina_log_init()
87  * @li eina_error_init()
88  * @li eina_safety_checks_init()
89  * @li eina_hash_init()
90  * @li eina_stringshare_init()
91  * @li eina_list_init()
92  * @li eina_array_init()
93  * @li eina_counter_init()
94  * @li eina_benchmark_init()
95  * @li eina_magic_string_init()
96  * @li eina_rectangle_init()
97  *
98  * When Eina is not used anymore, call eina_shutdown() to shut down
99  * the Eina library.
100  */
101 EAPI int
102 eina_init(void)
103 {
104    if (_eina_main_count) goto finish_init;
105
106    if (!eina_log_init())
107      {
108         fprintf(stderr, "Could not initialize eina logging system.\n");
109         return 0;
110      }
111
112    _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
113    if (_eina_log_dom < 0)
114      {
115         EINA_LOG_ERR("Could not register log domain: eina");
116         eina_log_shutdown();
117         return 0;
118      }
119
120    if (!eina_error_init())
121      {
122         ERR("Could not initialize eina error module.");
123         goto eina_init_error;
124      }
125
126    if (!eina_safety_checks_init())
127      {
128         ERR("Could not initialize eina safety checks module.");
129         goto safety_checks_init_error;
130      }
131
132    if (!eina_hash_init())
133      {
134         ERR("Could not initialize eina hash module.");
135         goto hash_init_error;
136      }
137    if (!eina_stringshare_init())
138      {
139         ERR("Could not initialize eina stringshare module.");
140         goto stringshare_init_error;
141      }
142    if (!eina_list_init())
143      {
144         ERR("Could not initialize eina list module.");
145         goto list_init_error;
146      }
147    if (!eina_array_init())
148      {
149         ERR("Could not initialize eina array module.");
150         goto array_init_error;
151      }
152    if (!eina_counter_init())
153      {
154         ERR("Could not initialize eina counter module.");
155         goto counter_init_error;
156      }
157    if (!eina_benchmark_init())
158      {
159         ERR("Could not initialize eina benchmark module.");
160         goto benchmark_init_error;
161      }
162    if (!eina_magic_string_init())
163      {
164         ERR("Could not initialize eina magic string module.");
165         goto magic_string_init_error;
166      }
167    if (!eina_rectangle_init())
168      {
169         ERR("Could not initialize eina rectangle module.");
170         goto rectangle_init_error;
171      }
172
173  finish_init:
174    return ++_eina_main_count;
175
176  rectangle_init_error:
177    eina_magic_string_shutdown();
178  magic_string_init_error:
179    eina_benchmark_shutdown();
180  benchmark_init_error:
181    eina_counter_shutdown();
182  counter_init_error:
183    eina_array_shutdown();
184  array_init_error:
185    eina_list_shutdown();
186  list_init_error:
187    eina_stringshare_shutdown();
188  stringshare_init_error:
189    eina_hash_shutdown();
190  hash_init_error:
191    eina_safety_checks_shutdown();
192  safety_checks_init_error:
193    eina_error_shutdown();
194  eina_init_error:
195    _eina_log_dom = -1;
196    eina_log_shutdown();
197
198    return 0;
199 }
200
201 /**
202  * @brief Shut down the Eina library.
203  *
204  * @return 0 when all the modules is completely shut down, 1 or
205  * greater otherwise.
206  *
207  * This function shuts down the Eina library. It returns 0 when it has
208  * been called the same number of times than eina_init(). In that case
209  * it shut down all the Eina modules. The list of shut down functions
210  * that are called are (in that order):
211  *
212  * @li eina_rectangle_shutdown()
213  * @li eina_magic_string_shutdown()
214  * @li eina_benchmark_shutdown()
215  * @li eina_counter_shutdown()
216  * @li eina_array_shutdown()
217  * @li eina_list_shutdown()
218  * @li eina_stringshare_shutdown()
219  * @li eina_hash_shutdown()
220  * @li eina_safety_checks_shutdown()
221  * @li eina_error_shutdown()
222  * @li eina_log_shutdown()
223  *
224  * Once this function succeeds (that is, @c 0 is returned), you must
225  * not call any of the Eina function anymore. You must call
226  * eina_init() again to use the Eina functions again.
227  */
228 EAPI int
229 eina_shutdown(void)
230 {
231    if (_eina_main_count != 1) goto finish_shutdown;
232
233    eina_rectangle_shutdown();
234    eina_magic_string_shutdown();
235    eina_benchmark_shutdown();
236    eina_counter_shutdown();
237    eina_array_shutdown();
238    eina_list_shutdown();
239    eina_stringshare_shutdown();
240    eina_hash_shutdown();
241    eina_safety_checks_shutdown();
242    eina_error_shutdown();
243    eina_log_domain_unregister(_eina_log_dom);
244    _eina_log_dom = -1;
245    eina_log_shutdown();
246
247  finish_shutdown:
248    return --_eina_main_count;
249 }
250
251 /**
252  * @}
253  */