summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eina_cxx/eina_log.hh
blob: c287b7bd2d9c84bed4eca62598917e322d97ca68 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EFL_EINA_LOG_HH
#define EFL_EINA_LOG_HH

#include <sstream>

/**
 * @addtogroup Eina_Cxx_Tools_Group
 *
 * @{
 */

namespace efl { namespace eina {

/**
 * @defgroup Eina_Cxx_Log_Group Log
 * @ingroup Eina_Cxx_Tools_Group
 *
 * @{
 */

/**
 * Types to represent each log level.
 *
 * @{
 */
namespace log_level {

/**
 * Type for representing a critical log level.
 */
struct critical_t { static constexpr ::Eina_Log_Level value = ::EINA_LOG_LEVEL_CRITICAL; };
/** Critical log level */
critical_t const critical = {};

/**
 * Type for representing an error log level.
 */
struct error_t { static constexpr ::Eina_Log_Level value = ::EINA_LOG_LEVEL_ERR; };
/** Error log level */
error_t const error = {};

/**
 * Type for representing an information log level.
 */
struct info_t { static constexpr ::Eina_Log_Level value = ::EINA_LOG_LEVEL_INFO; };
/** Information log level */
info_t const info = {};

/**
 * Type for representing a debug log level.
 */
struct debug_t { static constexpr ::Eina_Log_Level value = ::EINA_LOG_LEVEL_DBG; };
/** Debug log level */
debug_t const debug = {};

/**
 * Type for representing a warning log level.
 */
struct warn_t { static constexpr ::Eina_Log_Level value = ::EINA_LOG_LEVEL_WARN; };
/** Warning log level */
warn_t const warning = {};

}
/**
 * @}
 */


/**
 * Base implementation for log domains.
 */
template <typename D>
struct _domain_base
{
  /**
   * @brief Set the domain log level based on the given log level type.
   *
   * @{
   */
  void set_level(log_level::critical_t l) { set_level(l.value); }
  void set_level(log_level::error_t l) { set_level(l.value); }
  void set_level(log_level::info_t l) { set_level(l.value); }
  void set_level(log_level::debug_t l) { set_level(l.value); }
  void set_level(log_level::warn_t l) { set_level(l.value); }
  /**
   * @}
   */

  /**
   * @brief Set the domain log level to the level specified by the given identifier.
   * @param l Eina native identifier to a log level.
   */
  void set_level( ::Eina_Log_Level l)
  {
    ::eina_log_domain_registered_level_set(static_cast<D&>(*this).domain_raw(), l);
  }

  /**
   * @brief Get the domain log level.
   * @return Eina native identifier representing the current log level of the domain.
   */
  ::Eina_Log_Level get_level() const
  {
    return static_cast< ::Eina_Log_Level>
      (::eina_log_domain_registered_level_get(static_cast<D const&>(*this).domain_raw()));
  }
};

/**
 * @internal
 */
struct global_domain : _domain_base<global_domain>
{
  int domain_raw() const { return EINA_LOG_DOMAIN_GLOBAL; }
};

/**
 * General purpose log domain.
 * It is always registered and available everywhere.
 */
struct global_domain const global_domain = {};

/**
 * @internal
 */
struct default_domain : _domain_base<default_domain>
{
  int domain_raw() const { return EINA_LOG_DOMAIN_DEFAULT; }
};

/**
 * Default log domain.
 * If the macro @c EINA_LOG_DOMAIN_DEFAULT is not defined to anything
 * different it will be equivalent to @c global_domain.
 */
struct default_domain const default_domain = {};

/**
 * Class for creating log domains. It register a new domain upon
 * construction and unregister it upon destruction, following the RAII
 * programming idiom.
 */
struct log_domain : _domain_base<log_domain>
{
  /**
   * @brief Creates a new log domain.
   * @param name Name of the domain.
   * @param color Color of the domain name.
   */
  log_domain(char const* name, char const* color = EINA_COLOR_GREEN)
    : _domain( (::eina_init(), ::eina_log_domain_register(name, color)) )
  {
  }

  /**
   * @brief Unregister the domain.
   */
  ~log_domain()
  {
    ::eina_log_domain_unregister(_domain);
    ::eina_shutdown();
  }
  int domain_raw() const { return _domain; }
private:
  /**
   * @internal
   *
   * Member variable that holds the domain identifier.
   */
  int _domain;
};

/**
 * @internal
 */
inline void _log(std::stringstream const& stream, int domain, ::Eina_Log_Level level
                 , const char* file, const char* function, int line)
{
  ::eina_log_print(domain, level, file, function, line
                   , "%s", stream.str().c_str());
}

/**
 * @def EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 *
 * Logs a message with level %p LEVEL on the domain %p DOMAIN.
 *
 * It works like a STL output stream and should be used with the left
 * shift operator. Example:
 * \code{.py}
 * EINA_CXX_DOM_LOG(my_domain, my_log_level) << "My log message.";
 * \endcode
 */
#define EINA_CXX_DOM_LOG(DOMAIN, LEVEL)                                 \
    for( bool run = ::eina_log_domain_level_check((DOMAIN), LEVEL); run;) \
      for(std::stringstream stream; run ;                               \
          ::efl::eina::_log(std::move(stream), (DOMAIN), LEVEL          \
                            , __FILE__, __FUNCTION__, __LINE__), run = false) \
        stream

/**
 * @def EINA_CXX_DOM_LOG_CRIT(DOMAIN)
 *
 * Logs a message with level %c EINA_LOG_LEVEL_CRITICAL on the domain
 * %p DOMAIN.
 *
 * It is a short for EINA_CXX_DOM_LOG(DOMAIN, ::EINA_LOG_LEVEL_CRITICAL).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_DOM_LOG_CRIT(DOMAIN)                     \
    EINA_CXX_DOM_LOG(DOMAIN.domain_raw(), ::EINA_LOG_LEVEL_CRITICAL)

/**
 * @def EINA_CXX_DOM_LOG_ERR(DOMAIN)
 *
 * Logs a message with level %c EINA_LOG_LEVEL_ERR on the domain
 * %p DOMAIN.
 *
 * It is a short for EINA_CXX_DOM_LOG(DOMAIN, ::EINA_LOG_LEVEL_ERR).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_DOM_LOG_ERR(DOMAIN)                      \
    EINA_CXX_DOM_LOG(DOMAIN.domain_raw(), ::EINA_LOG_LEVEL_ERR)

/**
 * @def EINA_CXX_DOM_LOG_INFO(DOMAIN)
 *
 * Logs a message with level %c EINA_LOG_LEVEL_INFO on the domain
 * %p DOMAIN.
 *
 * It is a short for EINA_CXX_DOM_LOG(DOMAIN, ::EINA_LOG_LEVEL_INFO).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_DOM_LOG_INFO(DOMAIN)                      \
    EINA_CXX_DOM_LOG(DOMAIN.domain_raw(), ::EINA_LOG_LEVEL_INFO)

/**
 * @def EINA_CXX_DOM_LOG_DBG(DOMAIN)
 *
 * Logs a message with level %c EINA_LOG_LEVEL_DBG on the domain
 * %p DOMAIN.
 *
 * It is a short for EINA_CXX_DOM_LOG(DOMAIN, ::EINA_LOG_LEVEL_DBG).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_DOM_LOG_DBG(DOMAIN)                      \
    EINA_CXX_DOM_LOG(DOMAIN.domain_raw(), ::EINA_LOG_LEVEL_DBG)

/**
 * @def EINA_CXX_DOM_LOG_WARN(DOMAIN)
 *
 * Logs a message with level %c EINA_LOG_LEVEL_WARN on the domain
 * %p DOMAIN.
 *
 * It is a short for EINA_CXX_DOM_LOG(DOMAIN, ::EINA_LOG_LEVEL_WARN).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_DOM_LOG_WARN(DOMAIN)                      \
    EINA_CXX_DOM_LOG(DOMAIN.domain_raw(), ::EINA_LOG_LEVEL_WARN)

/**
 * @def EINA_CXX_LOG(LEVEL)
 *
 * Logs a message with level %p LEVEL on the default domain
 * %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, LEVEL).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG(LEVEL)                               \
    EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, LEVEL)

/**
 * @def EINA_CXX_LOG_CRIT()
 *
 * Logs a message with level %c EINA_LOG_LEVEL_CRITICAL on the default
 * domain %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, ::EINA_LOG_LEVEL_CRITICAL).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG_CRIT()                 \
    EINA_CXX_LOG(EINA_LOG_LEVEL_CRITICAL)

/**
 * @def EINA_CXX_LOG_ERR()
 *
 * Logs a message with level %c EINA_LOG_LEVEL_ERR on the default
 * domain %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, ::EINA_LOG_LEVEL_ERR).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG_ERR()                  \
    EINA_CXX_LOG(EINA_LOG_LEVEL_ERR)

/**
 * @def EINA_CXX_LOG_INFO()
 *
 * Logs a message with level %c EINA_LOG_LEVEL_INFO on the default
 * domain %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, ::EINA_LOG_LEVEL_INFO).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG_INFO()                 \
    EINA_CXX_LOG(EINA_LOG_LEVEL_INFO)

/**
 * @def EINA_CXX_LOG_DBG()
 *
 * Logs a message with level %c EINA_LOG_LEVEL_DBG on the default
 * domain %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, ::EINA_LOG_LEVEL_DBG).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG_DBG()                  \
    EINA_CXX_LOG(EINA_LOG_LEVEL_DBG)

/**
 * @def EINA_CXX_LOG_WARN()
 *
 * Logs a message with level %c EINA_LOG_LEVEL_WARN on the default
 * domain %c EINA_LOG_DOMAIN_DEFAULT.
 *
 * It is a short for EINA_CXX_DOM_LOG(EINA_LOG_DOMAIN_DEFAULT, ::EINA_LOG_LEVEL_WARN).
 *
 * @see EINA_CXX_DOM_LOG(DOMAIN, LEVEL)
 */
#define EINA_CXX_LOG_WARN()                 \
    EINA_CXX_LOG(EINA_LOG_LEVEL_WARN)

/**
 * @}
 */

} }

/**
 * @}
 */

#endif