File: log_domain.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (381 lines) | stat: -rw-r--r-- 11,223 bytes parent folder | download | duplicates (2)
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
370
371
372
373
374
375
376
377
378
379
380
381
/*
   (c) Copyright 2012-2013  DirectFB integrated media GmbH
   (c) Copyright 2001-2013  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Shimokawa <andi@directfb.org>,
              Marek Pikarski <mass@directfb.org>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/



#include <config.h>

#include <direct/list.h>
#include <direct/log.h>
#include <direct/log_domain.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/print.h>
#include <direct/thread.h>
#include <direct/util.h>


#if DIRECT_BUILD_TEXT

typedef struct {
     DirectLink             link;

     char                  *name;
     DirectLogDomainConfig  config;
} LogDomainEntry;

/**********************************************************************************************************************/

static DirectMutex   domains_lock;
static unsigned int  domains_age;
static DirectLink   *domains;      // FIXME: use hash table like in direct/result.c

void
__D_log_domain_init()
{
     domains_age = 1;

     direct_mutex_init( &domains_lock );
}

void
__D_log_domain_deinit()
{
     direct_mutex_deinit( &domains_lock );
}

/**********************************************************************************************************************/

__dfb_no_instrument_function__
static __inline__ LogDomainEntry *
lookup_domain( const char *name, bool sub );

static __inline__ LogDomainEntry *
lookup_domain( const char *name, bool sub )
{
     LogDomainEntry *entry;

     direct_list_foreach (entry, domains) {
          if (! direct_strcasecmp( entry->name, name ))
               return entry;
     }

     /*
      * If the domain being registered contains a slash, but didn't exactly match an entry
      * in directfbrc, check to see if the domain is descended from an entry in directfbrc
      * (e.g. 'ui/field/messages' matches 'ui' or 'ui/field')
      */
     if (sub && strchr(name, '/')) {
          int passed_name_len = strlen( name );

          direct_list_foreach (entry, domains) {
               int entry_len = strlen( entry->name );
               if ((passed_name_len > entry_len) &&
                   (name[entry_len] == '/') &&
                   (! direct_strncasecmp( entry->name, name, entry_len))) {
                    return entry;
               }
          }
     }

     return NULL;
}

__dfb_no_instrument_function__
static DirectLogLevel
check_domain( DirectLogDomain *domain );

static DirectLogLevel
check_domain( DirectLogDomain *domain )
{
     if (direct_config->log_none)
          return DIRECT_LOG_NONE;

     if (direct_config->log_all)
          return DIRECT_LOG_ALL;

     if (domain->age != domains_age) {
          LogDomainEntry *entry;

          if (direct_mutex_lock( &domains_lock ))
               return DIRECT_LOG_ALL;

          entry = lookup_domain( domain->name, true );

          domain->age = domains_age;

          if (entry) {
               domain->registered = true;
               domain->config     = entry->config;
          }
          else {
               domain->config.log   = direct_config->log;
               domain->config.level = direct_config->log_level;
          }

          direct_mutex_unlock( &domains_lock );
     }

     return domain->config.level;
}

/**********************************************************************************************************************/

void
direct_log_domain_configure( const char *name, const DirectLogDomainConfig *config )
{
     LogDomainEntry *entry;

     if (direct_mutex_lock( &domains_lock ))
          return;

     entry = lookup_domain( name, false );
     if (!entry) {
          direct_mutex_unlock( &domains_lock );

          // FIXME: use sliced dynamic or static block, hash table
          entry = (LogDomainEntry*) direct_calloc( 1, sizeof(LogDomainEntry) );
          if (!entry) {
               D_WARN( "out of memory" );
               return;
          }

          entry->name = direct_strdup( name );
          if (!entry->name) {
               D_WARN( "out of memory" );
               direct_free( entry );
               return;
          }

          if (direct_mutex_lock( &domains_lock )) {
               direct_free( entry->name );
               direct_free( entry );
               return;
          }

          direct_list_prepend( &domains, &entry->link );
     }

     entry->config = *config;

     if (! ++domains_age)
          domains_age++;

     direct_mutex_unlock( &domains_lock );
}
  
bool
direct_log_domain_check( DirectLogDomain *domain )
{
     return check_domain( domain ) >= DIRECT_LOG_DEBUG;
}

bool
direct_log_domain_check_level( DirectLogDomain *domain,
                               DirectLogLevel   level )
{
     return check_domain( domain ) >= level;
}

/**********************************************************************************************************************/


/* FIXME: merge following */


__dfb_no_instrument_function__
DirectResult
direct_log_domain_vprintf( DirectLogDomain *domain,
                           DirectLogLevel   level,
                           const char      *format,
                           va_list          ap )
{
     if (check_domain( domain ) >= level) {
          char          buf[200];
          char         *ptr = buf;
          long long     micros = direct_clock_get_time( DIRECT_CLOCK_MONOTONIC );
          long long     millis = micros / 1000LL;
          DirectThread *thread = direct_thread_self();
          int           indent = direct_trace_debug_indent() * 4;
          int           len;

          /* Prepare user message. */
#ifdef __GNUC__
          va_list ap2;

          va_copy( ap2, ap );
          len = direct_vsnprintf( buf, sizeof(buf), format, ap2 );
          va_end( ap2 );
#else
          len = direct_vsnprintf( buf, sizeof(buf), format, ap );
#endif
          if (len < 0)
               return DR_FAILURE;

          if (len >= sizeof(buf)) {
               ptr = direct_malloc( len+1 );
               if (!ptr)
                    return DR_NOLOCALMEMORY;

               len = direct_vsnprintf( ptr, len+1, format, ap );
               if (len < 0) {
                    direct_free( ptr );
                    return DR_FAILURE;
               }
          }

          /* Wrap around when too high */
          indent &= 0x7f;

          /* Fill up domain name column after the colon, prepending remaining space (excl. ': ') to indent. */
          indent += (domain->name_len < 34 ? 34 : 50) - domain->name_len - 2;

          /* Print full message. */
          direct_log_printf( domain->config.log,
                             "(-) [%-16.16s %3lld.%03lld,%03lld] (%5d) %s: %*s%s",
                             thread ? thread->name : "  NO NAME",
                             millis / 1000LL, millis % 1000LL, micros % 1000LL,
                             /*thread ? thread->tid :*/ direct_gettid(), domain->name, indent, "", ptr );

          direct_log_flush( domain->config.log, false );

          if (ptr != buf)
               direct_free( ptr );
     }
     else
          direct_log_debug_delay( false );

     return DR_OK;
}

__dfb_no_instrument_function__
DirectResult
direct_log_domain_log( DirectLogDomain *domain,
                       DirectLogLevel   level,
                       const char      *func,
                       const char      *file,
                       int              line,
                       const char      *format, ... )
{
     if (check_domain( domain ) >= level) {
          char          buf[200];
          char         *ptr = buf;
          long long     micros = direct_clock_get_time( DIRECT_CLOCK_MONOTONIC );
          long long     millis = micros / 1000LL;
          DirectThread *thread = direct_thread_self();
          int           indent = direct_trace_debug_indent() * 4;
          int           len;
          va_list       ap;

          /* Prepare user message. */
          va_start( ap, format );
          len = direct_vsnprintf( buf, sizeof(buf), format, ap );
          va_end( ap );

          if (len < 0)
               return DR_FAILURE;

          if (len >= sizeof(buf)) {
               ptr = direct_malloc( len+1 );
               if (!ptr)
                    return DR_NOLOCALMEMORY;

               va_start( ap, format );
               len = direct_vsnprintf( ptr, len+1, format, ap );
               va_end( ap );

               if (len < 0) {
                    direct_free( ptr );
                    return DR_FAILURE;
               }
          }

          /* Wrap around when too high */
          indent &= 0x7f;

          /* Fill up domain name column after the colon, prepending remaining space (excl. ': ') to indent. */
          indent += (domain->name_len < 27 ? 27 : 42) - domain->name_len - 2;

          /* Print full message. */
          direct_log_printf( domain->config.log,
                             "(%c) [%-16.16s %3lld.%03lld,%03lld] (%5d) %s: %*s%s",
                             level > DIRECT_LOG_INFO    ? '-' :
                             level > DIRECT_LOG_WARNING ? '*' :
                             level > DIRECT_LOG_ERROR   ? '#' :
                             level > DIRECT_LOG_NONE    ? '!' : ' ',
                             thread ? thread->name : "  NO NAME",
                             millis / 1000LL, millis % 1000LL, micros % 1000LL,
                             thread ? thread->tid : direct_gettid(), domain->name, indent, "", ptr );

          direct_log_flush( domain->config.log, false );

          if (ptr != buf)
               direct_free( ptr );
     }
     else
          direct_log_debug_delay( false );

     return DR_OK;
}

#else

void
__D_log_domain_init()
{
}

void
__D_log_domain_deinit()
{
}

void
direct_log_domain_vprintf( DirectLogDomain *domain,
                           DirectLogLevel   level,
                           const char      *format,
                           va_list          ap )
{
}

void
direct_log_domain_configure( const char                  *name,
                             const DirectLogDomainConfig *config )
{
}
  
bool
direct_log_domain_check( DirectLogDomain *domain )
{
     return false;
}

#endif /* DIRECT_BUILD_TEXT */