FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
logging.h
Go to the documentation of this file.
1 /* **************************************************************
2 Copyright (C) 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 ************************************************************** */
22 #ifndef LOGGING_H_INCLUDE
23 #define LOGGING_H_INCLUDE
24 
25 /* std library includes */
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 
33 /* other libraries */
34 #include <pthread.h>
35 #include <libpq-fe.h>
36 #include <glib.h>
37 
38 /* ************************************************************************** */
39 /* **** Log file structures ************************************************* */
40 /* ************************************************************************** */
41 
45 typedef struct
46 {
47  gchar* log_name;
48  gchar* pro_name;
49  pid_t pro_pid;
50  FILE* log_file;
51 } log_t;
52 
53 // global log file
54 extern log_t* main_log;
55 
56 #define SCHE_PRONAME "scheduler"
57 
58 /* ************************************************************************** */
59 /* **** ERROR MACROS ******************************************************** */
60 /* ************************************************************************** */
61 
62 /*
63  * The following macro definitions are meant to act as their own statement in
64  * the C language. To accomplish this, they needed to not only be able to be used
65  * in the situation of an "if" statement with no body, but also require that
66  * they are followed by a ";".
67  *
68  * To do this the "do {} while(0)" loop is used, the loop will not appear in
69  * result flow control since it does not modify the flow of control, but it is
70  * a single statement that requires a ";" at the end to be syntactically correct
71  */
72 
74 #define FATAL(...) do { \
75  lprintf(main_log, "FATAL %s.%d: ", __FILE__, __LINE__); \
76  lprintf(main_log, __VA_ARGS__); \
77  lprintf(main_log, "\n"); \
78  lprintf(main_log, "FATAL errno is: %s\n", strerror(errno)); \
79  exit(-2); } while(0)
80 
82 #define THREAD_FATAL(file, ...) do { \
83  con_printf(file, "THREAD_FATAL %s.%d: ", __FILE__, __LINE__); \
84  con_printf(file, __VA_ARGS__); \
85  con_printf(file, "\n"); \
86  con_printf(file, "THREAD_FATAL errno is: %s\n", strerror(errno)); \
87  g_thread_exit(NULL); } while(0)
88 
90 #define ERROR(...) do { \
91  lprintf(main_log, "ERROR %s.%d: ", __FILE__, __LINE__); \
92  lprintf(main_log, __VA_ARGS__); \
93  lprintf(main_log, "\n"); } while(0)
94 
96 #define PQ_ERROR(pg_r, ...) { \
97  lprintf(main_log, "ERROR %s.%d: ", __FILE__, __LINE__); \
98  lprintf(main_log, __VA_ARGS__); \
99  lprintf(main_log, "\n"); \
100  lprintf(main_log, "ERROR postgresql error: %s\n", PQresultErrorMessage(pg_r)); } \
101  SafePQclear(pg_r)
102 
104 #define TEST_NOTIFY verbose > 0
105 #define NOTIFY(...) if(TEST_NOTIFY) do { \
106  lprintf(main_log, "NOTE: "); \
107  lprintf(main_log, __VA_ARGS__); \
108  lprintf(main_log, "\n"); } while(0)
109 
111 #define TEST_WARNING verbose > 1
112 #define WARNING(...) if(TEST_WARNING) do { \
113  lprintf(main_log, "WARNING %s.%d: ", __FILE__, __LINE__); \
114  lprintf(main_log, __VA_ARGS__); \
115  lprintf(main_log, "\n"); } while(0)
116 
117 /* verbose macros, if changing from greater than scheme to bit mask, just */
118 /* change these the the TVERBOSE# macro when a test of verbose is needed, */
119 /* this happens when printing from another thread. The other verbose */
120 /* macro makes the syntax better everywhere else */
121 #define TVERB_JOB (verbose & 0x8)
122 #define TVERB_AGENT (verbose & 0x10)
123 #define TVERB_SCHED (verbose & 0x20)
124 #define TVERB_EVENT (verbose & 0x40)
125 #define TVERB_INTER (verbose & 0x80)
126 #define TVERB_DATAB (verbose & 0x100)
127 #define TVERB_HOST (verbose & 0x200)
128 #define TVERB_SPECIAL (verbose & 0x400)
129 #define V_JOB(...) if(TVERB_JOB) log_printf(__VA_ARGS__)
130 #define V_AGENT(...) if(TVERB_AGENT) log_printf(__VA_ARGS__)
131 #define V_SCHED(...) if(TVERB_SCHED) log_printf(__VA_ARGS__)
132 #define V_EVENT(...) if(TVERB_EVENT) log_printf(__VA_ARGS__)
133 #define V_INTERFACE(...) if(TVERB_INTER) con_printf(main_log, __VA_ARGS__)
134 #define V_DATABASE(...) if(TVERB_DATAB) log_printf(__VA_ARGS__)
135 #define V_HOST(...) if(TVERB_HOST) log_printf(__VA_ARGS__)
136 #define V_SPECIAL(...) if(TVERB_SPECIAL) log_printf(__VA_ARGS__)
137 
138 /* ************************************************************************** */
139 /* **** logging functions *************************************************** */
140 /* ************************************************************************** */
141 
142 log_t* log_new(gchar* log_name, gchar* pro_name, pid_t pro_pid);
143 log_t* log_new_FILE(FILE* log_file, gchar* log_name, gchar* pro_name, pid_t pro_pid);
144 void log_destroy(log_t* log);
145 
146 int lprintf (log_t* log, const char* fmt, ...);
147 int clprintf(log_t* log, char* s_name, uint16_t s_line, const char* fmt, ...);
148 int vlprintf(log_t* log, const char* fmt, va_list args);
149 
150 #define log_printf(...) lprintf(main_log, __VA_ARGS__)
151 #define con_printf(log, ...) clprintf(log, __FILE__, __LINE__, __VA_ARGS__)
152 
153 #endif /* LOGGING_H_INCLUDE */
Definition: logging.h:45
FILE * log_file
The log file itself.
Definition: logging.h:50
gchar * pro_name
What should be printed as the process name.
Definition: logging.h:48
log_t * main_log
Definition: logging.c:44
int lprintf(log_t *log, const char *fmt,...)
Main logging function.
Definition: logging.c:191
uint16_t s_line
Sample source line number.
Definition: testEvent.c:39
int vlprintf(log_t *log, const char *fmt, va_list args)
The provides the same functionality for lprintf as vprintf does for printf.
Definition: logging.c:224
int clprintf(log_t *log, char *s_name, uint16_t s_line, const char *fmt,...)
Definition: logging.c:281
gchar * log_name
The name of the log file that will be printed to.
Definition: logging.h:47
pid_t pro_pid
The pid of the process.
Definition: logging.h:49
void log_destroy(log_t *log)
Free memory associated with the log file.
Definition: logging.c:161
log_t * log_new_FILE(FILE *log_file, gchar *log_name, gchar *pro_name, pid_t pro_pid)
Creates a log file structure based on an already created FILE*.
Definition: logging.c:137
log_t * log_new(gchar *log_name, gchar *pro_name, pid_t pro_pid)
Creates a new log.
Definition: logging.c:92
char * s_name
Sample source file name.
Definition: testEvent.c:38