FOSSology  4.4.0
Open Source License Compliance by Open Source Software
logging.h
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 #ifndef LOGGING_H_INCLUDE
12 #define LOGGING_H_INCLUDE
13 
14 /* std library includes */
15 #include <errno.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <string.h>
21 
22 /* other libraries */
23 #include <pthread.h>
24 #include <libpq-fe.h>
25 #include <glib.h>
26 
27 /* ************************************************************************** */
28 /* **** Log file structures ************************************************* */
29 /* ************************************************************************** */
30 
34 typedef struct
35 {
36  gchar* log_name;
37  gchar* pro_name;
38  pid_t pro_pid;
39  FILE* log_file;
40 } log_t;
41 
42 // global log file
43 extern log_t* main_log;
44 
45 #define SCHE_PRONAME "scheduler"
46 
47 /* ************************************************************************** */
48 /* **** ERROR MACROS ******************************************************** */
49 /* ************************************************************************** */
50 
51 /*
52  * The following macro definitions are meant to act as their own statement in
53  * the C language. To accomplish this, they needed to not only be able to be used
54  * in the situation of an "if" statement with no body, but also require that
55  * they are followed by a ";".
56  *
57  * To do this the "do {} while(0)" loop is used, the loop will not appear in
58  * result flow control since it does not modify the flow of control, but it is
59  * a single statement that requires a ";" at the end to be syntactically correct
60  */
61 
63 #define FATAL(...) do { \
64  lprintf(main_log, "FATAL %s.%d: ", __FILE__, __LINE__); \
65  lprintf(main_log, __VA_ARGS__); \
66  lprintf(main_log, "\n"); \
67  lprintf(main_log, "FATAL errno is: %s\n", strerror(errno)); \
68  exit(-2); } while(0)
69 
71 #define THREAD_FATAL(file, ...) do { \
72  con_printf(file, "THREAD_FATAL %s.%d: ", __FILE__, __LINE__); \
73  con_printf(file, __VA_ARGS__); \
74  con_printf(file, "\n"); \
75  con_printf(file, "THREAD_FATAL errno is: %s\n", strerror(errno)); \
76  g_thread_exit(NULL); } while(0)
77 
79 #define ERROR(...) do { \
80  lprintf(main_log, "ERROR %s.%d: ", __FILE__, __LINE__); \
81  lprintf(main_log, __VA_ARGS__); \
82  lprintf(main_log, "\n"); } while(0)
83 
85 #define PQ_ERROR(pg_r, ...) { \
86  lprintf(main_log, "ERROR %s.%d: ", __FILE__, __LINE__); \
87  lprintf(main_log, __VA_ARGS__); \
88  lprintf(main_log, "\n"); \
89  lprintf(main_log, "ERROR postgresql error: %s\n", PQresultErrorMessage(pg_r)); } \
90  SafePQclear(pg_r)
91 
93 #define TEST_NOTIFY verbose > 0
94 #define NOTIFY(...) if(TEST_NOTIFY) do { \
95  lprintf(main_log, "NOTE: "); \
96  lprintf(main_log, __VA_ARGS__); \
97  lprintf(main_log, "\n"); } while(0)
98 
100 #define TEST_WARNING verbose > 1
101 #define WARNING(...) if(TEST_WARNING) do { \
102  lprintf(main_log, "WARNING %s.%d: ", __FILE__, __LINE__); \
103  lprintf(main_log, __VA_ARGS__); \
104  lprintf(main_log, "\n"); } while(0)
105 
106 /* verbose macros, if changing from greater than scheme to bit mask, just */
107 /* change these the the TVERBOSE# macro when a test of verbose is needed, */
108 /* this happens when printing from another thread. The other verbose */
109 /* macro makes the syntax better everywhere else */
110 #define TVERB_JOB (verbose & 0x8)
111 #define TVERB_AGENT (verbose & 0x10)
112 #define TVERB_SCHED (verbose & 0x20)
113 #define TVERB_EVENT (verbose & 0x40)
114 #define TVERB_INTER (verbose & 0x80)
115 #define TVERB_DATAB (verbose & 0x100)
116 #define TVERB_HOST (verbose & 0x200)
117 #define TVERB_SPECIAL (verbose & 0x400)
118 #define V_JOB(...) if(TVERB_JOB) log_printf(__VA_ARGS__)
119 #define V_AGENT(...) if(TVERB_AGENT) log_printf(__VA_ARGS__)
120 #define V_SCHED(...) if(TVERB_SCHED) log_printf(__VA_ARGS__)
121 #define V_EVENT(...) if(TVERB_EVENT) log_printf(__VA_ARGS__)
122 #define V_INTERFACE(...) if(TVERB_INTER) con_printf(main_log, __VA_ARGS__)
123 #define V_DATABASE(...) if(TVERB_DATAB) log_printf(__VA_ARGS__)
124 #define V_HOST(...) if(TVERB_HOST) log_printf(__VA_ARGS__)
125 #define V_SPECIAL(...) if(TVERB_SPECIAL) log_printf(__VA_ARGS__)
126 
127 /* ************************************************************************** */
128 /* **** logging functions *************************************************** */
129 /* ************************************************************************** */
130 
131 log_t* log_new(gchar* log_name, gchar* pro_name, pid_t pro_pid);
132 log_t* log_new_FILE(FILE* log_file, gchar* log_name, gchar* pro_name, pid_t pro_pid);
133 void log_destroy(log_t* log);
134 
135 int lprintf (log_t* log, const char* fmt, ...);
136 int clprintf(log_t* log, char* s_name, uint16_t s_line, const char* fmt, ...);
137 int vlprintf(log_t* log, const char* fmt, va_list args);
138 
139 #define log_printf(...) lprintf(main_log, __VA_ARGS__)
140 #define con_printf(log, ...) clprintf(log, __FILE__, __LINE__, __VA_ARGS__)
141 
142 #endif /* LOGGING_H_INCLUDE */
log_t * main_log
Definition: logging.c:33
int lprintf(log_t *log, const char *fmt,...)
Main logging function.
Definition: logging.c:180
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:126
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:213
int clprintf(log_t *log, char *s_name, uint16_t s_line, const char *fmt,...)
Definition: logging.c:270
log_t * log_new(gchar *log_name, gchar *pro_name, pid_t pro_pid)
Creates a new log.
Definition: logging.c:81
void log_destroy(log_t *log)
Free memory associated with the log file.
Definition: logging.c:150
Definition: logging.h:35
gchar * log_name
The name of the log file that will be printed to.
Definition: logging.h:36
gchar * pro_name
What should be printed as the process name.
Definition: logging.h:37
FILE * log_file
The log file itself.
Definition: logging.h:39
pid_t pro_pid
The pid of the process.
Definition: logging.h:38
char * s_name
Sample source file name.
Definition: testEvent.c:27
uint16_t s_line
Sample source line number.
Definition: testEvent.c:28