Add eina_log module into the main module and global header.
[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_types.h"
26 #include "eina_main.h"
27 #include "eina_error.h"
28 #include "eina_log.h"
29 #include "eina_hash.h"
30 #include "eina_stringshare.h"
31 #include "eina_list.h"
32 #include "eina_array.h"
33 #include "eina_counter.h"
34 #include "eina_benchmark.h"
35 #include "eina_magic.h"
36 #include "eina_rectangle.h"
37
38 /*============================================================================*
39  *                                  Local                                     *
40  *============================================================================*/
41
42 /**
43  * @cond LOCAL
44  */
45
46 static int _eina_main_count = 0;
47
48 /**
49  * @endcond
50  */
51
52 /*============================================================================*
53  *                                 Global                                     *
54  *============================================================================*/
55
56 /*============================================================================*
57  *                                   API                                      *
58  *============================================================================*/
59
60 /**
61  * @addtogroup Eina_Main_Group Main
62  *
63  * @brief These functions provide general initialisation and shut down
64  * functions.
65  *
66  * @{
67  */
68
69 /**
70  * @brief Initialize the Eina library.
71  *
72  * @return 1 or greater on success, 0 on error.
73  *
74  * This function sets up all the eina modules. It returns 0 on
75  * failure (that is, when one of the module fails to initialize),
76  * otherwise it returns the number of times it has already been
77  * called. The list of initialisation functions that are called are
78  * (in that order):
79  *
80  * @li eina_error_init()
81  * @li eina_hash_init()
82  * @li eina_stringshare_init()
83  * @li eina_list_init()
84  * @li eina_array_init()
85  * @li eina_counter_init()
86  * @li eina_benchmark_init()
87  * @li eina_magic_string_init()
88  * @li eina_rectangle_init()
89  *
90  * When Eina is not used anymore, call eina_shutdown() to shut down
91  * the Eina library.
92  */
93 EAPI int
94 eina_init(void)
95 {
96    if (_eina_main_count) goto finish_init;
97
98    if (!eina_error_init())
99      {
100         fprintf(stderr, "Could not initialize eina error module.\n");
101         return 0;
102      }
103
104    if (!eina_log_init())
105      {
106         fprintf(stderr, "Could not initialize eina log module.\n");
107         goto log_init_error;
108      }
109
110    if (!eina_hash_init())
111      {
112         EINA_ERROR_PERR("Could not initialize eina hash module.\n");
113         goto hash_init_error;
114      }
115    if (!eina_stringshare_init())
116      {
117         EINA_ERROR_PERR("Could not initialize eina stringshare module.\n");
118         goto stringshare_init_error;
119      }
120    if (!eina_list_init())
121      {
122         EINA_ERROR_PERR("Could not initialize eina list module.\n");
123         goto list_init_error;
124      }
125    if (!eina_array_init())
126      {
127         EINA_ERROR_PERR("Could not initialize eina array module.\n");
128         goto array_init_error;
129      }
130    if (!eina_counter_init())
131      {
132         EINA_ERROR_PERR("Could not initialize eina counter module.\n");
133         goto counter_init_error;
134      }
135    if (!eina_benchmark_init())
136      {
137         EINA_ERROR_PERR("Could not initialize eina benchmark module.\n");
138         goto benchmark_init_error;
139      }
140    if (!eina_magic_string_init())
141      {
142         EINA_ERROR_PERR("Could not initialize eina magic string module.\n");
143         goto magic_string_init_error;
144      }
145    if (!eina_rectangle_init())
146      {
147         EINA_ERROR_PERR("Could not initialize eina rectangle module.\n");
148         goto rectangle_init_error;
149      }
150
151  finish_init:
152    return ++_eina_main_count;
153
154  rectangle_init_error:
155    eina_magic_string_shutdown();
156  magic_string_init_error:
157    eina_benchmark_shutdown();
158  benchmark_init_error:
159    eina_counter_shutdown();
160  counter_init_error:
161    eina_array_shutdown();
162  array_init_error:
163    eina_list_shutdown();
164  list_init_error:
165    eina_stringshare_shutdown();
166  stringshare_init_error:
167    eina_hash_shutdown();
168  hash_init_error:
169    eina_log_shutdown();
170  log_init_error:
171    eina_error_shutdown();
172
173    return 0;
174 }
175
176 /**
177  * @brief Shut down the Eina library.
178  *
179  * @return 0 when all the modules is completely shut down, 1 or
180  * greater otherwise.
181  *
182  * This function shuts down the Eina library. It returns 0 when it has
183  * been called the same number of times than eina_init(). In that case
184  * it shut down all the Eina modules. The list of shut down functions
185  * that are called are (in that order):
186  *
187  * @li eina_rectangle_init()
188  * @li eina_magic_string_init()
189  * @li eina_benchmark_init()
190  * @li eina_counter_init()
191  * @li eina_array_init()
192  * @li eina_list_init()
193  * @li eina_stringshare_init()
194  * @li eina_hash_init()
195  * @li eina_error_init()
196  *
197  * Once this function succeeds (that is, @c 0 is returned), you must
198  * not call any of the Eina function anymore. You must call
199  * eina_init() again to use the Eina functions again.
200  */
201 EAPI int
202 eina_shutdown(void)
203 {
204    if (_eina_main_count != 1) goto finish_shutdown;
205
206    eina_rectangle_shutdown();
207    eina_magic_string_shutdown();
208    eina_benchmark_shutdown();
209    eina_counter_shutdown();
210    eina_array_shutdown();
211    eina_list_shutdown();
212    eina_stringshare_shutdown();
213    eina_hash_shutdown();
214    eina_log_shutdown();
215    eina_error_shutdown();
216
217  finish_shutdown:
218    return --_eina_main_count;
219 }
220
221 /**
222  * @}
223  */