import source from lvm2 2.02.79
[external/device-mapper.git] / daemons / cmirrord / logging.h
1 /*
2  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
3  *
4  * This copyrighted material is made available to anyone wishing to use,
5  * modify, copy, or redistribute it subject to the terms and conditions
6  * of the GNU Lesser General Public License v.2.1.
7  *
8  * You should have received a copy of the GNU Lesser General Public License
9  * along with this program; if not, write to the Free Software Foundation,
10  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
11  */
12
13 #ifndef _LVM_CLOG_LOGGING_H
14 #define _LVM_CLOG_LOGGING_H
15
16 #define _GNU_SOURCE
17 #define _FILE_OFFSET_BITS 64
18
19 #include "configure.h"
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <syslog.h>
23
24 /* SHORT_UUID - print last 8 chars of a string */
25 #define SHORT_UUID(x) (strlen(x) > 8) ? ((x) + (strlen(x) - 8)) : (x)
26
27 extern const char *__rq_types_off_by_one[];
28 #define RQ_TYPE(x) __rq_types_off_by_one[(x) - 1]
29
30 extern int log_tabbing;
31 extern int log_is_open;
32 extern int log_membership_change;
33 extern int log_checkpoint;
34 extern int log_resend_requests;
35
36 #define LOG_OPEN(ident, option, facility) do { \
37                 openlog(ident, option, facility); \
38                 log_is_open = 1;                  \
39         } while (0)
40
41 #define LOG_CLOSE(void) do { \
42                 log_is_open = 0; \
43                 closelog();      \
44         } while (0)
45
46 #define LOG_OUTPUT(level, f, arg...) do {                               \
47                 int __i;                                                \
48                 char __buffer[16];                                      \
49                 FILE *fp = (level > LOG_NOTICE) ? stderr : stdout;      \
50                 if (log_is_open) {                                      \
51                         for (__i = 0; (__i < log_tabbing) && (__i < 15); __i++) \
52                                 __buffer[__i] = '\t';                   \
53                         __buffer[__i] = '\0';                           \
54                         syslog(level, "%s" f "\n", __buffer, ## arg);   \
55                 } else {                                                \
56                         for (__i = 0; __i < log_tabbing; __i++)         \
57                                 fprintf(fp, "\t");                      \
58                         fprintf(fp, f "\n", ## arg);                    \
59                 }                                                       \
60         } while (0)
61
62
63 #ifdef DEBUG
64 #define LOG_DBG(f, arg...) LOG_OUTPUT(LOG_DEBUG, f, ## arg)
65 #else /* DEBUG */
66 #define LOG_DBG(f, arg...)
67 #endif /* DEBUG */
68
69 #define LOG_COND(__X, f, arg...) do {\
70                 if (__X) {           \
71                         LOG_OUTPUT(LOG_NOTICE, f, ## arg); \
72                 } \
73         } while (0)
74 #define LOG_PRINT(f, arg...) LOG_OUTPUT(LOG_NOTICE, f, ## arg)
75 #define LOG_ERROR(f, arg...) LOG_OUTPUT(LOG_ERR, f, ## arg)
76
77 #endif /* _LVM_CLOG_LOGGING_H */