AT&T ECOMP Vendor Event Listener library  0.1
evel_logging.c
Go to the documentation of this file.
1 /**************************************************************************/
37 #include <string.h>
38 #include <assert.h>
39 #include <syslog.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42 
43 #include <curl/curl.h>
44 
45 #include "evel.h"
46 
47 
48 /*****************************************************************************/
49 /* Debug settings. Logging is done through macros so these need to be */
50 /* externally visible. */
51 /*****************************************************************************/
53 //static char *syslog_ident = "evel";
54 int debug_indent = 0;
55 
56 /*****************************************************************************/
57 /* Buffers for error strings from this library. */
58 /*****************************************************************************/
59 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
60 
61 
62 /**************************************************************************/
68 void log_initialize(EVEL_LOG_LEVELS level, const char * ident)
69 {
70  assert(level < EVEL_LOG_MAX);
71  assert(ident != NULL);
72 
73  debug_level = level;
74  openlog(ident, LOG_PID, LOG_USER);
75 }
76 
77 /**************************************************************************/
87 const char * evel_error_string(void)
88 {
89  return(evel_err_string);
90 }
91 
92 /***************************************************************************//*
93  * Store the formatted string into the static error string and log the error.
94  *
95  * @param format Error string in standard printf format.
96  * @param ... Variable parameters to be substituted into the format string.
97  *****************************************************************************/
98 void log_error_state(char * format, ...)
99 {
100  va_list largs;
101 
102  assert(format != NULL);
103  va_start(largs, format);
104  vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
105  va_end(largs);
106  EVEL_ERROR("%s", evel_err_string);
107 }
108 
109 
110 /**************************************************************************/
120 void log_debug(EVEL_LOG_LEVELS level, char * format, ...)
121 {
122  va_list largs;
123  int priority;
124  char indent_fmt[1024];
125  char *syslog_fmt = NULL;
126 
127  /***************************************************************************/
128  /* Test assumptions. */
129  /***************************************************************************/
130  assert(format != NULL);
131  assert(level <= EVEL_LOG_MAX);
132 
133  if (level >= debug_level)
134  {
135  if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
136  {
137  /***********************************************************************/
138  /* Just use the format as is. */
139  /***********************************************************************/
140  syslog_fmt = format;
141  }
142  else
143  {
144  /***********************************************************************/
145  /* Combine the format with a preceding number of indent markers. */
146  /***********************************************************************/
147  sprintf(indent_fmt, "%.*s%s",
148  debug_indent,
150  format);
151  syslog_fmt = indent_fmt;
152  }
153 
154  /*************************************************************************/
155  /* Work out the syslog priority value. */
156  /*************************************************************************/
157  switch (level)
158  {
159  case EVEL_LOG_ERROR:
160  priority = LOG_ERR;
161  break;
162 
163  case EVEL_LOG_INFO:
164  priority = LOG_INFO;
165  break;
166 
167  case EVEL_LOG_DEBUG:
168  case EVEL_LOG_SPAMMY:
169  default:
170  priority = LOG_DEBUG;
171  break;
172  }
173 
174  /*************************************************************************/
175  /* Write the log to the file next, which requires the var args list. */
176  /*************************************************************************/
177  va_start(largs, format);
178  vsyslog(priority, syslog_fmt, largs);
179  va_end(largs);
180  }
181 }
#define EVEL_MAX_ERROR_STRING_LEN
Definition: evel.h:102
EVEL_LOG_LEVELS
Logging levels.
Definition: evel.h:88
void log_initialize(EVEL_LOG_LEVELS level, const char *ident)
Initialize logging.
Definition: evel_logging.c:68
Header for EVEL library.
#define EVEL_ERROR(FMT,...)
Definition: evel.h:3624
void log_debug(EVEL_LOG_LEVELS level, char *format,...)
Generate a debug log.
Definition: evel_logging.c:120
void log_error_state(char *format,...)
Definition: evel_logging.c:98
const char * evel_error_string(void)
Descriptive text for library errors.
Definition: evel_logging.c:87
int debug_indent
Definition: evel_logging.c:54
#define INDENT_SEPARATORS
Definition: evel.h:3637
EVEL_LOG_LEVELS debug_level
Definition: evel_logging.c:52