pmm  1.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
pmm_log.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008-2010 Robert Higgins
3  Author: Robert Higgins <robert.higgins@ucd.ie>
4 
5  This file is part of PMM.
6 
7  PMM is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  PMM is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with PMM. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef PMM_LOG_H_
21 #define PMM_LOG_H_
22 /*!
23  * @file pmm_log.h
24  *
25  * @brief logging macros
26  *
27  * Header file for logging macros. Inspired by GridSolve/NetSolve
28  */
29 
30 
31 #if HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <stdio.h> // for fprintf
36 #include <time.h> // for time
37 #include <sys/types.h> // for getpid
38 #include <unistd.h> // for getpid
39 #include <string.h> // for strcmp
40 
41 /* If debugging/output macros are not yet defined, define them now */
42 
43 
44 // SWITCHPRINTF macro takes one of these parameters as an argument to describe
45 // where to print a message (log, debug, error).
46 #ifdef HAVE_ISO_VARARGS
47 #define PMM_DBG "0" //!< defines debug print stream for SWITCHPRINTF(OUTPUT,...)
48 #define PMM_LOG "1" //!< defines log print stream for SWITCHPRINTF(OUTPUT,...)
49 #define PMM_ERR "2" //!< defines error print stream for SWITCHPRINTF(OUTPUT,...)
50 #else
51 // If we do not have VARARGS a call to SWITCHPRINTF(PMM_DBG, "asdasd\n") must
52 // be macroed to printf("","asdasd") so we define the descriptors in such a way
53 #define PMM_DBG ""
54 #define PMM_LOG ""
55 #define PMM_ERR ""
56 #endif
57 
58 
59 /*!
60  * \def DBGPRINTF(...)
61  *
62  * Prints a debug message to \a stderr stream including timestamp, calling
63  * file, line and function, and the formatted string passed as argument (same
64  * as printf). If debugging is not enabled no message will be emitted.
65  *
66  */
67 #ifndef DBGPRINTF
68 # ifdef ENABLE_DEBUG
69 # ifdef HAVE_ISO_VARARGS
70 # define DBGPRINTF(...) do { time_t clock; struct tm now;\
71  clock = time(0); localtime_r(&clock, &now); \
72  fprintf(stderr,"[%02d/%02d/%04d %02d:%02d:%02d] [%s:%d] [%s] %d DBG: ",\
73  now.tm_mon+1,now.tm_mday,now.tm_year+1900,\
74  now.tm_hour,now.tm_min,now.tm_sec,\
75  __FILE__,__LINE__,__FUNCNAME__, (int)getpid());\
76  fprintf(stderr, __VA_ARGS__); \
77  fflush(stderr); } while (0)
78 # else /* does not know ISO style varargs */
79 # define DBGPRINTF printf("%s: %d [] %d DBG: ", __FILE__ , __LINE__ , -1 )+printf
80 # endif /* #ifdef HAVE_ISO_VARARGS */
81 # else /* dont want debugging */
82 # ifdef HAVE_ISO_VARARGS
83 # define DBGPRINTF(...)
84 # else /* does not know ISO style varargs */
85 static void DBGPRINTF(/*@unused@*/ const char *format, ...) {}
86 # endif /* #ifdef HAVE_ISO_VARARGS */
87 # endif /* #ifdef ENABLE_DEBUG */
88 #endif /* #ifndef DBGPRINTF */
89 
90 
91 /*!
92  * \def ERRPRINTF(...)
93  *
94  * prints an error message to \a stderr stream including timestamp, calling
95  * file, line and function, and the formatted string passed as argument (same
96  * as printf)
97  */
98 #ifndef ERRPRINTF
99 # ifdef HAVE_ISO_VARARGS
100 # define ERRPRINTF(...) do { time_t clock; struct tm now;\
101  clock = time(0); localtime_r(&clock, &now); \
102  fprintf(stderr,"[%02d/%02d/%04d %02d:%02d:%02d] [%s:%d] [%s] %d ERR: ",\
103  now.tm_mon+1,now.tm_mday,now.tm_year+1900,\
104  now.tm_hour,now.tm_min,now.tm_sec,\
105  __FILE__,__LINE__,__FUNCNAME__, (int)getpid()); \
106  fprintf(stderr, __VA_ARGS__); \
107  fflush(stderr); } while (0)
108 # else
109 # define ERRPRINTF printf
110 # endif /* ifdef HAVE_ISO_VARARGS */
111 #endif /* ERRPRINTF */
112 
113 
114 /*!
115  * \def LOGPRINTF(...)
116  *
117  * prints a logging message to \a stderr stream including timestamp, calling
118  * file, line and function, and the formatted string passed as argument (same
119  * as printf)
120  */
121 //TODO change this to stdout ?
122 #ifndef LOGPRINTF
123 # ifdef HAVE_ISO_VARARGS
124 # ifdef PMM_DEBUG
125 # define LOGPRINTF(...) do { time_t clock; struct tm now;\
126  clock = time(0); localtime_r(&clock, &now); \
127  fprintf(stderr,"[%02d/%02d/%04d %02d:%02d:%02d] [%s:%d] [%s] %d LOG: ",\
128  now.tm_mon+1,now.tm_mday,now.tm_year+1900,\
129  now.tm_hour,now.tm_min,now.tm_sec,\
130  __FILE__,__LINE__,__FUNCNAME__,\
131  (int)getpid());\
132  fprintf(stderr, __VA_ARGS__); \
133  fflush(stderr); } while (0)
134 # else
135 # define LOGPRINTF(...) do { time_t clock; struct tm now;\
136  clock = time(0); localtime_r(&clock, &now); \
137  fprintf(stderr,"[%02d/%02d/%04d %02d:%02d:%02d] [%s] %d LOG: ",\
138  now.tm_mon+1,now.tm_mday,now.tm_year+1900,\
139  now.tm_hour,now.tm_min,now.tm_sec,__FUNCNAME__,\
140  (int)getpid());\
141  fprintf(stderr, __VA_ARGS__); \
142  fflush(stderr); } while (0)
143 # endif
144 # else
145 # define LOGPRINTF printf
146 # endif /* ifdef HAVE_ISO_VARARGS */
147 #endif /* LOGPRINTF */
148 
149 /*!
150  * \def SWITCHPRINTF(OUTPUT,...)
151  *
152  * calls LOGPRINTF/ERRPRINTF/DBGPRINTF depending on an argument \a OUTPUT
153  * which may by one of PMM_LOG, PMM_ERR, PMM_DBG
154  */
155 #ifndef SWITCHPRINTF
156 # ifdef HAVE_ISO_VARARGS
157 # define SWITCHPRINTF(OUTPUT,...) do { \
158  if(strcmp(OUTPUT,PMM_LOG)==0) {\
159  LOGPRINTF(__VA_ARGS__);\
160  } else if(strcmp(OUTPUT, PMM_ERR)==0) {\
161  ERRPRINTF(__VA_ARGS__);\
162  } else if(strcmp(OUTPUT,PMM_DBG)==0) {\
163  DBGPRINTF(__VA_ARGS__);\
164  } else {\
165  LOGPRINTF(__VA_ARGS__);\
166  }\
167  } while (0)
168 # else
169 # define SWITCHPRINTF printf
170 # endif /* HAVE_ISO_VARARGS */
171 #endif /* SWITCHPRINTF */
172 
173 #endif /*PMM_LOG_H_*/