1 /*
2  * Copyright (C) 2003-2015 FreeIPMI Core Team
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #ifndef IPMI_TRACE_H
20 #define IPMI_TRACE_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif /* HAVE_CONFIG_H */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef STDC_HEADERS
29 #include <string.h>
30 #endif /* STDC_HEADERS */
31 #include <errno.h>
32 #ifdef WITH_ENCRYPTION
33 #include <gpg-error.h>
34 #endif /* !WITH_ENCRYPTION */
35 
36 #include "freeipmi/fiid/fiid.h"
37 
38 #include "ipmi-fiid-util.h"
39 
40 #define ERR_WRAPPER_STR_MAX_LEN 4096
41 
42 #if defined (IPMI_TRACE)
43 #define TRACE_MSG_OUT(__msgtracestr, __msgtracenum)     \
44   do {                                                  \
45     fprintf (stderr,                                    \
46              "%s: %d: %s: error '%s' (%d)\n",           \
47              __FILE__, __LINE__, __FUNCTION__,          \
48              __msgtracestr, __msgtracenum);             \
49     fflush (stderr);                                    \
50   } while (0)
51 
52 #define TRACE_ERRNO_OUT(__errno_orig)                                   \
53   do {                                                                  \
54     extern int errno;                                                   \
55     int __save_errno = __errno_orig;                                    \
56     char __errnostr[ERR_WRAPPER_STR_MAX_LEN + 1];                       \
57     memset (__errnostr, '\0', ERR_WRAPPER_STR_MAX_LEN + 1);             \
58     strerror_r (__save_errno, __errnostr, ERR_WRAPPER_STR_MAX_LEN);     \
59     fprintf (stderr,                                                    \
60              "%s: %d: %s: errno '%s' (%d)\n",                           \
61              __FILE__, __LINE__, __FUNCTION__,                          \
62              __errnostr, __save_errno);                                 \
63     fflush (stderr);                                                    \
64     __errno_orig = __save_errno;                                        \
65   } while (0)
66 
67 #ifdef WITH_ENCRYPTION
68 #define TRACE_GCRYPT_OUT(__error_orig)                                  \
69   do {                                                                  \
70     char __errorstr[ERR_WRAPPER_STR_MAX_LEN + 1];                       \
71     memset (__errorstr, '\0', ERR_WRAPPER_STR_MAX_LEN + 1);             \
72     gpg_strerror_r (__error_orig, __errorstr, ERR_WRAPPER_STR_MAX_LEN); \
73     fprintf (stderr,                                                    \
74              "%s: %d: %s: gcrypt error '%s' (%d)\n",                    \
75              __FILE__, __LINE__, __FUNCTION__,                          \
76              __errorstr, __error_orig);                                 \
77     fflush (stderr);                                                    \
78   } while (0)
79 #else /* !WITH_ENCRYPTION */
80 #define TRACE_GCRYPT_OUT(__error_orig)
81 #endif /* !WITH_ENCRYPTION */
82 
83 #else /* !IPMI_TRACE */
84 #define TRACE_MSG_OUT(__msgtracestr, __msgtracenum)
85 #define TRACE_ERRNO_OUT(__errno_orig)
86 #define TRACE_GCRYPT_OUT(__error_orig)
87 #endif /* !IPMI_TRACE */
88 
89 #define ERR_TRACE(__str, __num)                 \
90   do {                                          \
91     TRACE_MSG_OUT (__str, __num);               \
92   } while (0)
93 
94 #define SET_ERRNO(__errno)                      \
95   do {                                          \
96     errno = (__errno);                          \
97     TRACE_ERRNO_OUT (errno);                    \
98   } while (0)
99 
100 #define ERRNO_TRACE(__errno)                    \
101   do {                                          \
102     TRACE_ERRNO_OUT (__errno);                  \
103   } while (0)
104 
105 #define FIID_OBJECT_ERROR_TO_ERRNO(__obj)                                   \
106   do {                                                                      \
107     set_errno_by_fiid_object ((__obj));                                     \
108     TRACE_MSG_OUT (fiid_obj_errormsg ((__obj)), fiid_obj_errnum ((__obj))); \
109   } while (0)
110 
111 #define FIID_ITERATOR_ERROR_TO_ERRNO(__iter)                                            \
112   do {                                                                                  \
113     set_errno_by_fiid_iterator ((__iter));                                              \
114     TRACE_MSG_OUT (fiid_iterator_errormsg ((__iter)), fiid_iterator_errnum ((__iter))); \
115   } while (0)
116 
117 #define ERR_GCRYPT_TRACE(__error) \
118   do {                            \
119     TRACE_GCRYPT_OUT (__error);   \
120   } while (0)
121 
122 #endif /* IPMI_TRACE_H */
123