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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
caja-debug-log.c: Ring buffer for logging debug messages
Copyright (C) 2006 Novell, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
Author: Federico Mena-Quintero <federico@novell.com>
*/
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "caja-debug-log.h"
#include "caja-file.h"
#include <src/glibcompat.h> /* for g_list_free_full */
#define DEFAULT_RING_BUFFER_NUM_LINES 1000
#define KEY_FILE_GROUP "debug log"
#define KEY_FILE_DOMAINS_KEY "enable domains"
#define KEY_FILE_MAX_LINES_KEY "max lines"
#define MAX_URI_COUNT 20
static GStaticMutex log_mutex = G_STATIC_MUTEX_INIT;
static GHashTable *domains_hash;
static char **ring_buffer;
static int ring_buffer_next_index;
static int ring_buffer_num_lines;
static int ring_buffer_max_lines = DEFAULT_RING_BUFFER_NUM_LINES;
static GSList *milestones_head;
static GSList *milestones_tail;
static void
lock (void)
{
g_static_mutex_lock (&log_mutex);
}
static void
unlock (void)
{
g_static_mutex_unlock (&log_mutex);
}
void
caja_debug_log (gboolean is_milestone, const char *domain, const char *format, ...)
{
va_list args;
va_start (args, format);
caja_debug_logv (is_milestone, domain, NULL, format, args);
va_end (args);
}
static gboolean
is_domain_enabled (const char *domain)
{
/* User actions are always logged */
if (strcmp (domain, CAJA_DEBUG_LOG_DOMAIN_USER) == 0)
return TRUE;
if (!domains_hash)
return FALSE;
return (g_hash_table_lookup (domains_hash, domain) != NULL);
}
static void
ensure_ring (void)
{
if (ring_buffer)
return;
ring_buffer = g_new0 (char *, ring_buffer_max_lines);
ring_buffer_next_index = 0;
ring_buffer_num_lines = 0;
}
static void
add_to_ring (char *str)
{
ensure_ring ();
g_assert (str != NULL);
if (ring_buffer_num_lines == ring_buffer_max_lines)
{
/* We have an overlap, and the ring_buffer_next_index points to
* the "first" item. Free it to make room for the new item.
*/
g_assert (ring_buffer[ring_buffer_next_index] != NULL);
g_free (ring_buffer[ring_buffer_next_index]);
}
else
ring_buffer_num_lines++;
g_assert (ring_buffer_num_lines <= ring_buffer_max_lines);
ring_buffer[ring_buffer_next_index] = str;
ring_buffer_next_index++;
if (ring_buffer_next_index == ring_buffer_max_lines)
{
ring_buffer_next_index = 0;
g_assert (ring_buffer_num_lines == ring_buffer_max_lines);
}
}
static void
add_to_milestones (const char *str)
{
char *str_copy;
str_copy = g_strdup (str);
if (milestones_tail)
{
milestones_tail = g_slist_append (milestones_tail, str_copy);
milestones_tail = milestones_tail->next;
}
else
{
milestones_head = milestones_tail = g_slist_append (NULL, str_copy);
}
g_assert (milestones_head != NULL && milestones_tail != NULL);
}
void
caja_debug_logv (gboolean is_milestone, const char *domain, const GList *uris, const char *format, va_list args)
{
char *str;
char *debug_str;
struct timeval tv;
struct tm tm;
lock ();
if (!(is_milestone || is_domain_enabled (domain)))
goto out;
str = g_strdup_vprintf (format, args);
gettimeofday (&tv, NULL);
tm = *localtime (&tv.tv_sec);
debug_str = g_strdup_printf ("%p %04d/%02d/%02d %02d:%02d:%02d.%04d (%s): %s",
g_thread_self (),
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
(int) (tv.tv_usec / 100),
domain,
str);
g_free (str);
if (uris)
{
int debug_str_len;
int uris_len;
const GList *l;
char *new_str;
char *p;
int count;
uris_len = 0;
count = 0;
for (l = uris; l; l = l->next)
{
const char *uri;
uri = l->data;
uris_len += strlen (uri) + 2; /* plus 2 for a tab and the newline */
if (count++ > MAX_URI_COUNT)
{
uris_len += 4; /* "...\n" */
break;
}
}
debug_str_len = strlen (debug_str);
new_str = g_new (char, debug_str_len + 1 + uris_len + 1); /* plus 1 for newline & zero */
p = g_stpcpy (new_str, debug_str);
*p++ = '\n';
count = 0;
for (l = uris; l; l = l->next)
{
const char *uri;
uri = l->data;
*p++ = '\t';
p = g_stpcpy (p, uri);
if (l->next)
*p++ = '\n';
if (count++ > MAX_URI_COUNT)
{
p = g_stpcpy (p, "...\n");
break;
}
}
g_free (debug_str);
debug_str = new_str;
}
add_to_ring (debug_str);
if (is_milestone)
add_to_milestones (debug_str);
out:
unlock ();
}
void
caja_debug_log_with_uri_list (gboolean is_milestone, const char *domain, const GList *uris,
const char *format, ...)
{
va_list args;
va_start (args, format);
caja_debug_logv (is_milestone, domain, uris, format, args);
va_end (args);
}
void
caja_debug_log_with_file_list (gboolean is_milestone, const char *domain, GList *files,
const char *format, ...)
{
va_list args;
GList *uris;
GList *l;
int count;
/* Avoid conversion if debugging disabled */
if (!(is_milestone ||
caja_debug_log_is_domain_enabled (domain)))
{
return;
}
uris = NULL;
count = 0;
for (l = files; l; l = l->next)
{
CajaFile *file;
char *uri;
file = CAJA_FILE (l->data);
uri = caja_file_get_uri (file);
if (caja_file_is_gone (file))
{
char *new_uri;
/* Hack: this will create an invalid URI, but it's for
* display purposes only.
*/
new_uri = g_strconcat (uri ? uri : "", " (gone)", NULL);
g_free (uri);
uri = new_uri;
}
uris = g_list_prepend (uris, uri);
/* Avoid doing to much work, more than MAX_URI_COUNT uris
won't be shown anyway */
if (count++ > MAX_URI_COUNT + 1)
{
break;
}
}
uris = g_list_reverse (uris);
va_start (args, format);
caja_debug_logv (is_milestone, domain, uris, format, args);
va_end (args);
g_list_free_full (uris, g_free);
}
gboolean
caja_debug_log_load_configuration (const char *filename, GError **error)
{
GKeyFile *key_file;
char **strings;
gsize num_strings;
int num;
GError *my_error;
g_assert (filename != NULL);
g_assert (error == NULL || *error == NULL);
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, error))
{
g_key_file_free (key_file);
return FALSE;
}
/* Domains */
my_error = NULL;
strings = g_key_file_get_string_list (key_file, KEY_FILE_GROUP, KEY_FILE_DOMAINS_KEY, &num_strings, &my_error);
if (my_error)
g_error_free (my_error);
else
{
int i;
for (i = 0; i < num_strings; i++)
strings[i] = g_strstrip (strings[i]);
caja_debug_log_enable_domains ((const char **) strings, num_strings);
g_strfreev (strings);
}
/* Number of lines */
my_error = NULL;
num = g_key_file_get_integer (key_file, KEY_FILE_GROUP, KEY_FILE_MAX_LINES_KEY, &my_error);
if (my_error)
g_error_free (my_error);
else
caja_debug_log_set_max_lines (num);
g_key_file_free (key_file);
return TRUE;
}
void
caja_debug_log_enable_domains (const char **domains, int n_domains)
{
int i;
g_assert (domains != NULL);
g_assert (n_domains >= 0);
lock ();
if (!domains_hash)
domains_hash = g_hash_table_new (g_str_hash, g_str_equal);
for (i = 0; i < n_domains; i++)
{
g_assert (domains[i] != NULL);
if (strcmp (domains[i], CAJA_DEBUG_LOG_DOMAIN_USER) == 0)
continue; /* user actions are always enabled */
if (g_hash_table_lookup (domains_hash, domains[i]) == NULL)
{
char *domain;
domain = g_strdup (domains[i]);
g_hash_table_insert (domains_hash, domain, domain);
}
}
unlock ();
}
void
caja_debug_log_disable_domains (const char **domains, int n_domains)
{
int i;
g_assert (domains != NULL);
g_assert (n_domains >= 0);
lock ();
if (domains_hash)
{
for (i = 0; i < n_domains; i++)
{
char *domain;
g_assert (domains[i] != NULL);
if (strcmp (domains[i], CAJA_DEBUG_LOG_DOMAIN_USER) == 0)
continue; /* user actions are always enabled */
domain = g_hash_table_lookup (domains_hash, domains[i]);
if (domain)
{
g_hash_table_remove (domains_hash, domain);
g_free (domain);
}
}
} /* else, there is nothing to disable */
unlock ();
}
gboolean
caja_debug_log_is_domain_enabled (const char *domain)
{
gboolean retval;
g_assert (domain != NULL);
lock ();
retval = is_domain_enabled (domain);
unlock ();
return retval;
}
struct domains_dump_closure
{
char **domains;
int num_domains;
};
static void
domains_foreach_dump_cb (gpointer key, gpointer value, gpointer data)
{
struct domains_dump_closure *closure;
char *domain;
closure = data;
domain = key;
closure->domains[closure->num_domains] = domain;
closure->num_domains++;
}
static GKeyFile *
make_key_file_from_configuration (void)
{
GKeyFile *key_file;
struct domains_dump_closure closure;
int num_domains;
key_file = g_key_file_new ();
/* domains */
if (domains_hash)
{
num_domains = g_hash_table_size (domains_hash);
if (num_domains != 0)
{
closure.domains = g_new (char *, num_domains);
closure.num_domains = 0;
g_hash_table_foreach (domains_hash, domains_foreach_dump_cb, &closure);
g_assert (num_domains == closure.num_domains);
g_key_file_set_string_list (key_file, KEY_FILE_GROUP, KEY_FILE_DOMAINS_KEY,
(const gchar * const *) closure.domains, closure.num_domains);
g_free (closure.domains);
}
}
/* max lines */
g_key_file_set_integer (key_file, KEY_FILE_GROUP, KEY_FILE_MAX_LINES_KEY, ring_buffer_max_lines);
return key_file;
}
static gboolean
write_string (const char *filename, FILE *file, const char *str, GError **error)
{
if (fputs (str, file) == EOF)
{
int saved_errno;
saved_errno = errno;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (saved_errno),
"error when writing to log file %s", filename);
return FALSE;
}
return TRUE;
}
static gboolean
dump_configuration (const char *filename, FILE *file, GError **error)
{
GKeyFile *key_file;
char *data;
gsize length;
gboolean success;
if (!write_string (filename, file,
"\n\n"
"This configuration for the debug log can be re-created\n"
"by putting the following in ~/caja-debug-log.conf\n"
"(use ';' to separate domain names):\n\n",
error))
{
return FALSE;
}
success = FALSE;
key_file = make_key_file_from_configuration ();
data = g_key_file_to_data (key_file, &length, error);
if (!data)
goto out;
if (!write_string (filename, file, data, error))
{
goto out;
}
success = TRUE;
out:
g_key_file_free (key_file);
return success;
}
static gboolean
dump_milestones (const char *filename, FILE *file, GError **error)
{
GSList *l;
if (!write_string (filename, file, "===== BEGIN MILESTONES =====\n", error))
return FALSE;
for (l = milestones_head; l; l = l->next)
{
const char *str;
str = l->data;
if (!(write_string (filename, file, str, error)
&& write_string (filename, file, "\n", error)))
return FALSE;
}
if (!write_string (filename, file, "===== END MILESTONES =====\n", error))
return FALSE;
return TRUE;
}
static gboolean
dump_ring_buffer (const char *filename, FILE *file, GError **error)
{
int start_index;
int i;
if (!write_string (filename, file, "===== BEGIN RING BUFFER =====\n", error))
return FALSE;
if (ring_buffer_num_lines == ring_buffer_max_lines)
start_index = ring_buffer_next_index;
else
start_index = 0;
for (i = 0; i < ring_buffer_num_lines; i++)
{
int idx;
idx = (start_index + i) % ring_buffer_max_lines;
if (!(write_string (filename, file, ring_buffer[idx], error)
&& write_string (filename, file, "\n", error)))
{
return FALSE;
}
}
if (!write_string (filename, file, "===== END RING BUFFER =====\n", error))
return FALSE;
return TRUE;
}
gboolean
caja_debug_log_dump (const char *filename, GError **error)
{
FILE *file;
gboolean success;
g_assert (error == NULL || *error == NULL);
lock ();
success = FALSE;
file = fopen (filename, "w");
if (!file)
{
int saved_errno;
saved_errno = errno;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (saved_errno),
"could not open log file %s", filename);
goto out;
}
if (!(dump_milestones (filename, file, error)
&& dump_ring_buffer (filename, file, error)
&& dump_configuration (filename, file, error)))
{
goto do_close;
}
success = TRUE;
do_close:
if (fclose (file) != 0)
{
int saved_errno;
saved_errno = errno;
if (error && *error)
{
g_error_free (*error);
*error = NULL;
}
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (saved_errno),
"error when closing log file %s", filename);
success = FALSE;
}
out:
unlock ();
return success;
}
void
caja_debug_log_set_max_lines (int num_lines)
{
char **new_buffer;
int lines_to_copy;
g_assert (num_lines > 0);
lock ();
if (num_lines == ring_buffer_max_lines)
goto out;
new_buffer = g_new0 (char *, num_lines);
lines_to_copy = MIN (num_lines, ring_buffer_num_lines);
if (ring_buffer)
{
int start_index;
int i;
if (ring_buffer_num_lines == ring_buffer_max_lines)
start_index = (ring_buffer_next_index + ring_buffer_max_lines - lines_to_copy) % ring_buffer_max_lines;
else
start_index = ring_buffer_num_lines - lines_to_copy;
g_assert (start_index >= 0 && start_index < ring_buffer_max_lines);
for (i = 0; i < lines_to_copy; i++)
{
int idx;
idx = (start_index + i) % ring_buffer_max_lines;
new_buffer[i] = ring_buffer[idx];
ring_buffer[idx] = NULL;
}
for (i = 0; i < ring_buffer_max_lines; i++)
g_free (ring_buffer[i]);
g_free (ring_buffer);
}
ring_buffer = new_buffer;
ring_buffer_next_index = lines_to_copy;
ring_buffer_num_lines = lines_to_copy;
ring_buffer_max_lines = num_lines;
out:
unlock ();
}
int
caja_debug_log_get_max_lines (void)
{
int retval;
lock ();
retval = ring_buffer_max_lines;
unlock ();
return retval;
}
void
caja_debug_log_clear (void)
{
int i;
lock ();
if (!ring_buffer)
goto out;
for (i = 0; i < ring_buffer_max_lines; i++)
{
g_free (ring_buffer[i]);
ring_buffer[i] = NULL;
}
ring_buffer_next_index = 0;
ring_buffer_num_lines = 0;
out:
unlock ();
}
|