summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/logging.c
blob: 4c6bc2ed73e36695172bfe39fdbf8e2d6ff17a1d (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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
  Copyright (c) 2006-2010 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

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

  GlusterFS 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
  Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>

#include "xlator.h"
#include "logging.h"
#include "defaults.h"

#ifdef GF_LINUX_HOST_OS
#include <syslog.h>
#endif

#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif


static pthread_mutex_t  logfile_mutex;
static char            *filename = NULL;
static uint8_t          logrotate = 0;
static FILE            *logfile = NULL;
static gf_loglevel_t    loglevel = GF_LOG_INFO;
static int              gf_log_syslog = 1;

char                    gf_log_xl_log_set;
gf_loglevel_t           gf_log_loglevel; /* extern'd */
FILE                   *gf_log_logfile;

static char            *cmd_log_filename = NULL;
static FILE            *cmdlogfile = NULL;

void
gf_log_logrotate (int signum)
{
        logrotate = 1;
}

void
gf_log_enable_syslog (void)
{
        gf_log_syslog = 1;
}

void
gf_log_disable_syslog (void)
{
        gf_log_syslog = 0;
}

gf_loglevel_t
gf_log_get_loglevel (void)
{
        return loglevel;
}

void
gf_log_set_loglevel (gf_loglevel_t level)
{
        gf_log_loglevel = loglevel = level;
}


gf_loglevel_t
gf_log_get_xl_loglevel (void *this)
{
        xlator_t *xl = this;
        if (!xl)
                return 0;
        return xl->loglevel;
}

void
gf_log_set_xl_loglevel (void *this, gf_loglevel_t level)
{
        xlator_t *xl = this;
        if (!xl)
                return;
        gf_log_xl_log_set = 1;
        xl->loglevel = level;
}

void
gf_log_fini (void)
{
        pthread_mutex_destroy (&logfile_mutex);
}


void
gf_log_globals_init (void)
{
        pthread_mutex_init (&logfile_mutex, NULL);

#ifdef GF_LINUX_HOST_OS
        /* For the 'syslog' output. one can grep 'GlusterFS' in syslog
           for serious logs */
        openlog ("GlusterFS", LOG_PID, LOG_DAEMON);
#endif
}

int
gf_log_init (const char *file)
{
        if (!file){
                fprintf (stderr, "gf_log_init: no filename specified\n");
                return -1;
        }

        filename = gf_strdup (file);
        if (!filename) {
                fprintf (stderr, "gf_log_init: strdup error\n");
                return -1;
        }

        logfile = fopen (file, "a");
        if (!logfile){
                fprintf (stderr,
                         "gf_log_init: failed to open logfile \"%s\" (%s)\n",
                         file,
                         strerror (errno));
                return -1;
        }

        gf_log_logfile = logfile;

        return 0;
}


/*
 * Initialize logging to a central server.
 * If successful, log messages will be written both to
 * the local file and to the remote server.
 */

static int __central_log_enabled = 0;

struct _msg_queue {
        struct list_head msgs;
};

struct _log_msg {
        const char *msg;
        struct list_head queue;
};


void
gf_log_lock (void)
{
        pthread_mutex_lock (&logfile_mutex);
}


void
gf_log_unlock (void)
{
        pthread_mutex_unlock (&logfile_mutex);
}


void
gf_log_cleanup (void)
{
        pthread_mutex_destroy (&logfile_mutex);
}

int
_gf_log_nomem (const char *domain, const char *file,
               const char *function, int line, gf_loglevel_t level,
               size_t size)
{
        const char     *basename        = NULL;
        struct tm      *tm              = NULL;
        xlator_t       *this            = NULL;
        struct timeval  tv              = {0,};
        int             ret             = 0;
        gf_loglevel_t   xlator_loglevel = 0;
        char            msg[8092];
        char            timestr[256];
        char            callstr[4096];

        this = THIS;

        xlator_loglevel = this->loglevel;
        if (xlator_loglevel == 0)
                xlator_loglevel = loglevel;

        if (level > xlator_loglevel)
                goto out;

        static char *level_strings[] = {"",  /* NONE */
                                        "M", /* EMERGENCY */
                                        "A", /* ALERT */
                                        "C", /* CRITICAL */
                                        "E", /* ERROR */
                                        "W", /* WARNING */
                                        "N", /* NOTICE */
                                        "I", /* INFO/NORMAL */
                                        "D", /* DEBUG */
                                        "T", /* TRACE */
                                        ""};

        if (!domain || !file || !function) {
                fprintf (stderr,
                         "logging: %s:%s():%d: invalid argument\n",
                         __FILE__, __PRETTY_FUNCTION__, __LINE__);
                return -1;
        }

#if HAVE_BACKTRACE
        /* Print 'calling function' */
        do {
                void *array[5];
                char **callingfn = NULL;
                size_t bt_size = 0;

                bt_size = backtrace (array, 5);
                if (bt_size)
                        callingfn = backtrace_symbols (&array[2], bt_size-2);
                if (!callingfn)
                        break;

                if (bt_size == 5)
                        snprintf (callstr, 4096, "(-->%s (-->%s (-->%s)))",
                                  callingfn[2], callingfn[1], callingfn[0]);
                if (bt_size == 4)
                        snprintf (callstr, 4096, "(-->%s (-->%s))",
                                  callingfn[1], callingfn[0]);
                if (bt_size == 3)
                        snprintf (callstr, 4096, "(-->%s)", callingfn[0]);

                free (callingfn);
        } while (0);
#endif /* HAVE_BACKTRACE */

        ret = gettimeofday (&tv, NULL);
        if (-1 == ret)
                goto out;

        tm    = localtime (&tv.tv_sec);

        pthread_mutex_lock (&logfile_mutex);
        {
                strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
                snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
                          ".%"GF_PRI_SUSECONDS, tv.tv_usec);

                basename = strrchr (file, '/');
                if (basename)
                        basename++;
                else
                        basename = file;

                ret = sprintf (msg, "[%s] %s [%s:%d:%s] %s %s: no memory "
                               "available for size (%"GF_PRI_SIZET")",
                               timestr, level_strings[level],
                               basename, line, function, callstr,
                               domain, size);
                if (-1 == ret) {
                        goto unlock;
                }

                if (logfile) {
                        fprintf (logfile, "%s\n", msg);
                        fflush (logfile);
                } else {
                        fprintf (stderr, "%s\n", msg);
                }

#ifdef GF_LINUX_HOST_OS
                /* We want only serious log in 'syslog', not our debug
                   and trace logs */
                if (gf_log_syslog && level && (level <= GF_LOG_ERROR))
                        syslog ((level-1), "%s\n", msg);
#endif
        }

unlock:
        pthread_mutex_unlock (&logfile_mutex);
out:
        return ret;
 }

int
_gf_log_callingfn (const char *domain, const char *file, const char *function,
                   int line, gf_loglevel_t level, const char *fmt, ...)
{
        const char     *basename        = NULL;
        struct tm      *tm              = NULL;
        xlator_t       *this            = NULL;
        char           *str1            = NULL;
        char           *str2            = NULL;
        char           *msg             = NULL;
        char            timestr[256]    = {0,};
        char            callstr[4096]   = {0,};
        struct timeval  tv              = {0,};
        size_t          len             = 0;
        int             ret             = 0;
        gf_loglevel_t   xlator_loglevel = 0;
        va_list         ap;

        this = THIS;

        xlator_loglevel = this->loglevel;
        if (xlator_loglevel == 0)
                xlator_loglevel = loglevel;

        if (level > xlator_loglevel)
                goto out;

        static char *level_strings[] = {"",  /* NONE */
                                        "M", /* EMERGENCY */
                                        "A", /* ALERT */
                                        "C", /* CRITICAL */
                                        "E", /* ERROR */
                                        "W", /* WARNING */
                                        "N", /* NOTICE */
                                        "I", /* INFO/NORMAL */
                                        "D", /* DEBUG */
                                        "T", /* TRACE */
                                        ""};

        if (!domain || !file || !function || !fmt) {
                fprintf (stderr,
                         "logging: %s:%s():%d: invalid argument\n",
                         __FILE__, __PRETTY_FUNCTION__, __LINE__);
                return -1;
        }

#if HAVE_BACKTRACE
        /* Print 'calling function' */
        do {
                void *array[5];
                char **callingfn = NULL;
                size_t size = 0;

                size = backtrace (array, 5);
                if (size)
                        callingfn = backtrace_symbols (&array[2], size-2);
                if (!callingfn)
                        break;

                if (size == 5)
                        snprintf (callstr, 4096, "(-->%s (-->%s (-->%s)))",
                                  callingfn[2], callingfn[1], callingfn[0]);
                if (size == 4)
                        snprintf (callstr, 4096, "(-->%s (-->%s))",
                                  callingfn[1], callingfn[0]);
                if (size == 3)
                        snprintf (callstr, 4096, "(-->%s)", callingfn[0]);

                free (callingfn);
        } while (0);
#endif /* HAVE_BACKTRACE */

        ret = gettimeofday (&tv, NULL);
        if (-1 == ret)
                goto out;

        tm    = localtime (&tv.tv_sec);

        pthread_mutex_lock (&logfile_mutex);
        {
                va_start (ap, fmt);

                strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
                snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
                          ".%"GF_PRI_SUSECONDS, tv.tv_usec);

                basename = strrchr (file, '/');
                if (basename)
                        basename++;
                else
                        basename = file;

                ret = gf_asprintf (&str1, "[%s] %s [%s:%d:%s] %s %s: ",
                                   timestr, level_strings[level],
                                   basename, line, function, callstr,
                                   domain);
                if (-1 == ret) {
                        goto unlock;
                }

                ret = vasprintf (&str2, fmt, ap);
                if (-1 == ret) {
                        goto unlock;
                }

                va_end (ap);

                len = strlen (str1);
                msg = GF_MALLOC (len + strlen (str2) + 1, gf_common_mt_char);

                strcpy (msg, str1);
                strcpy (msg + len, str2);

                if (logfile) {
                        fprintf (logfile, "%s\n", msg);
                        fflush (logfile);
                } else {
                        fprintf (stderr, "%s\n", msg);
                }

#ifdef GF_LINUX_HOST_OS
                /* We want only serious log in 'syslog', not our debug
                   and trace logs */
                if (gf_log_syslog && level && (level <= GF_LOG_CRITICAL))
                        syslog ((level-1), "%s\n", msg);
#endif
        }

unlock:
        pthread_mutex_unlock (&logfile_mutex);

        if (msg) {
                GF_FREE (msg);
        }

        if (str1)
                GF_FREE (str1);

        if (str2)
                FREE (str2);

out:
        return ret;
}

int
_gf_log (const char *domain, const char *file, const char *function, int line,
         gf_loglevel_t level, const char *fmt, ...)
{
        const char  *basename = NULL;
        FILE        *new_logfile = NULL;
        va_list      ap;
        struct tm   *tm = NULL;
        char         timestr[256];
        struct timeval tv = {0,};

        char        *str1 = NULL;
        char        *str2 = NULL;
        char        *msg  = NULL;
        size_t       len  = 0;
        int          ret  = 0;
        xlator_t    *this = NULL;
        gf_loglevel_t xlator_loglevel = 0;

        this = THIS;

        xlator_loglevel = this->loglevel;
        if (xlator_loglevel == 0)
                xlator_loglevel = loglevel;

        if (level > xlator_loglevel)
                goto out;

        static char *level_strings[] = {"",  /* NONE */
                                        "M", /* EMERGENCY */
                                        "A", /* ALERT */
                                        "C", /* CRITICAL */
                                        "E", /* ERROR */
                                        "W", /* WARNING */
                                        "N", /* NOTICE */
                                        "I", /* INFO/NORMAL */
                                        "D", /* DEBUG */
                                        "T", /* TRACE */
                                        ""};

        if (!domain || !file || !function || !fmt) {
                fprintf (stderr,
                         "logging: %s:%s():%d: invalid argument\n",
                         __FILE__, __PRETTY_FUNCTION__, __LINE__);
                return -1;
        }


        if (logrotate) {
                logrotate = 0;

                new_logfile = fopen (filename, "a");
                if (!new_logfile) {
                        gf_log ("logrotate", GF_LOG_CRITICAL,
                                "failed to open logfile %s (%s)",
                                filename, strerror (errno));
                        goto log;
                }

                if (logfile)
                        fclose (logfile);

                gf_log_logfile = logfile = new_logfile;
        }

log:
        ret = gettimeofday (&tv, NULL);
        if (-1 == ret)
                goto out;

        tm    = localtime (&tv.tv_sec);

        pthread_mutex_lock (&logfile_mutex);
        {
                va_start (ap, fmt);

                strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
                snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
                          ".%"GF_PRI_SUSECONDS, tv.tv_usec);

                basename = strrchr (file, '/');
                if (basename)
                        basename++;
                else
                        basename = file;

                ret = gf_asprintf (&str1, "[%s] %s [%s:%d:%s] %s: ",
                                   timestr, level_strings[level],
                                   basename, line, function,
                                   domain);
                if (-1 == ret) {
                        goto unlock;
                }

                ret = vasprintf (&str2, fmt, ap);
                if (-1 == ret) {
                        goto unlock;
                }

                va_end (ap);

                len = strlen (str1);
                msg = GF_MALLOC (len + strlen (str2) + 1, gf_common_mt_char);

                strcpy (msg, str1);
                strcpy (msg + len, str2);

                if (logfile) {
                        fprintf (logfile, "%s\n", msg);
                        fflush (logfile);
                } else {
                        fprintf (stderr, "%s\n", msg);
                }

#ifdef GF_LINUX_HOST_OS
                /* We want only serious log in 'syslog', not our debug
                   and trace logs */
                if (gf_log_syslog && level && (level <= GF_LOG_CRITICAL))
                        syslog ((level-1), "%s\n", msg);
#endif
        }

unlock:
        pthread_mutex_unlock (&logfile_mutex);

        if (msg) {
                if ((ret != -1) && __central_log_enabled &&
                    ((glusterfs_central_log_flag_get ()) == 0)) {

                        glusterfs_central_log_flag_set ();
                        {
                                //gf_log_central (msg);
                        }
                        glusterfs_central_log_flag_unset ();
                }
                GF_FREE (msg);
        }

        if (str1)
                GF_FREE (str1);

        if (str2)
                FREE (str2);

out:
        return (0);
}


struct _client_log {
        char *identifier;
        FILE *file;
        struct list_head list;
};

struct _client_log *client_logs = NULL;


static void
client_log_init (struct _client_log *cl, char *identifier)
{
        int   ret = 0;
        char *path = NULL;

        cl->identifier = identifier;

        ret = gf_asprintf (&path, "%s.client-%s", filename, identifier);
        if (-1 == ret) {
                return;
        }
        cl->file = fopen (path, "a");
        GF_FREE (path);

        INIT_LIST_HEAD (&cl->list);
}


static FILE *
__logfile_for_client (char *identifier)
{
        struct _client_log *client = NULL;

        if (!client_logs) {
                client = GF_CALLOC (1, sizeof (*client),
                                    gf_common_mt_client_log);
                if (!client)
                        return NULL;

                client_log_init (client, identifier);

                client_logs = client;
        }

        list_for_each_entry (client, &client_logs->list, list) {
                if (!strcmp (client->identifier, identifier))
                        break;
        }

        if (!client) {
                client = GF_CALLOC (1, sizeof (*client),
                                    gf_common_mt_client_log);
                if (!client)
                        return NULL;

                client_log_init (client, identifier);

                list_add_tail (&client->list, &client_logs->list);
        }

        return client->file;
}


int
gf_log_from_client (const char *msg, char *identifier)
{
        FILE *client_log = NULL;

        client_log = __logfile_for_client (identifier);

        fprintf (client_log, "%s\n", msg);
        fflush (client_log);

        return 0;
}

int
gf_cmd_log_init (const char *filename)
{
        if (!filename){
                gf_log ("glusterd", GF_LOG_CRITICAL, "gf_cmd_log_init: no "
                        "filename specified\n");
                return -1;
        }

        cmd_log_filename = gf_strdup (filename);
        if (!cmd_log_filename) {
                gf_log ("glusterd", GF_LOG_CRITICAL, "gf_cmd_log_init: strdup"
                        " error\n");
                return -1;
        }

        cmdlogfile = fopen (cmd_log_filename, "a");
        if (!cmdlogfile){
                gf_log ("glusterd", GF_LOG_CRITICAL,
                        "gf_cmd_log_init: failed to open logfile \"%s\" "
                        "(%s)\n", cmd_log_filename, strerror (errno));
                return -1;
        }
        return 0;
}

int
gf_cmd_log (const char *domain, const char *fmt, ...)
{
        va_list      ap;
        struct tm   *tm = NULL;
        char         timestr[256];
        struct timeval tv = {0,};
        char        *str1 = NULL;
        char        *str2 = NULL;
        char        *msg  = NULL;
        size_t       len  = 0;
        int          ret  = 0;

        if (!cmdlogfile)
                return -1;


        if (!domain || !fmt) {
                gf_log ("glusterd", GF_LOG_TRACE,
                        "logging: invalid argument\n");
                return -1;
        }

        ret = gettimeofday (&tv, NULL);
        if (ret == -1)
                goto out;

        tm = localtime (&tv.tv_sec);

        va_start (ap, fmt);
        strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
        snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
                  ".%"GF_PRI_SUSECONDS, tv.tv_usec);

        ret = gf_asprintf (&str1, "[%s] %s : ",
                           timestr, domain);
        if (ret == -1) {
                goto out;
        }

        ret = vasprintf (&str2, fmt, ap);
        if (ret == -1) {
                goto out;
        }

        va_end (ap);

        len = strlen (str1);
        msg = GF_MALLOC (len + strlen (str2) + 1, gf_common_mt_char);

        strcpy (msg, str1);
        strcpy (msg + len, str2);

        fprintf (cmdlogfile, "%s\n", msg);
        fflush (cmdlogfile);

out:
        if (msg) {
                GF_FREE (msg);
        }

        if (str1)
                GF_FREE (str1);

        if (str2)
                FREE (str2);

        return (0);
}