Initial OpenECOMP Demo commit
[demo.git] / vnfs / VES / code / evel_library / evel_logging.c
1 /**************************************************************************//**
2  * @file
3  * Wrapper for event logging built on syslog.
4  *
5  * License
6  * -------
7  *
8  * Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:  This product includes
20  *    software developed by the AT&T.
21  * 4. Neither the name of AT&T nor the names of its contributors may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
29  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************/
36
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 /*****************************************************************************/
52 EVEL_LOG_LEVELS debug_level = EVEL_LOG_DEBUG;
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 /**************************************************************************//**
63  * Initialize logging
64  *
65  * @param[in] level  The debugging level - one of ::EVEL_LOG_LEVELS.
66  * @param[in] ident  The identifier for our logs.
67  *****************************************************************************/
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 /**************************************************************************//**
78  * Descriptive text for library errors.
79  *
80  * Return a text error string that relates to the last failure.  May be
81  * "<null>" but will never be NULL.
82  *
83  * @returns   Text error string.
84  *
85  * @note      Must not be freed!
86  *****************************************************************************/
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 /**************************************************************************//**
111  *  Generate a debug log.
112  *
113  *  Provides an interface to syslog with formatting of the nesting level
114  *  so that it's easier to see function entry/exit.
115  *
116  *  @param[in]  level   The debug level - see ::EVEL_LOG_LEVELS.
117  *  @param[in]  format  The output formatting in printf style.
118  *  @param[in]  ...     Variable arguments as specified in the format string.
119  *****************************************************************************/
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,
149               INDENT_SEPARATORS,
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 }