Cleanup code and correct License
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / evel_logging.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17 /**************************************************************************//**
18  * @file
19  * Wrapper for event logging built on syslog.
20  *
21  ****************************************************************************/
22
23 #include <string.h>
24 #include <assert.h>
25 #include <syslog.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28
29 #include <curl/curl.h>
30
31 #include "evel.h"
32
33
34 /*****************************************************************************/
35 /* Debug settings.  Logging is done through macros so these need to be       */
36 /* externally visible.                                                       */
37 /*****************************************************************************/
38 EVEL_LOG_LEVELS debug_level = EVEL_LOG_DEBUG;
39 //static char *syslog_ident = "evel";
40 int debug_indent = 0;
41
42 /*****************************************************************************/
43 /* Buffers for error strings from this library.                              */
44 /*****************************************************************************/
45 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
46
47
48 /**************************************************************************//**
49  * Initialize logging
50  *
51  * @param[in] level  The debugging level - one of ::EVEL_LOG_LEVELS.
52  * @param[in] ident  The identifier for our logs.
53  *****************************************************************************/
54 void log_initialize(EVEL_LOG_LEVELS level, const char * ident)
55 {
56   assert(level < EVEL_LOG_MAX);
57   assert(ident != NULL);
58
59   debug_level = level;
60   openlog(ident, LOG_PID, LOG_USER);
61 }
62
63 /**************************************************************************//**
64  * Descriptive text for library errors.
65  *
66  * Return a text error string that relates to the last failure.  May be
67  * "<null>" but will never be NULL.
68  *
69  * @returns   Text error string.
70  *
71  * @note      Must not be freed!
72  *****************************************************************************/
73 const char * evel_error_string(void)
74 {
75   return(evel_err_string);
76 }
77
78 /***************************************************************************//*
79  * Store the formatted string into the static error string and log the error.
80  *
81  * @param format  Error string in standard printf format.
82  * @param ...     Variable parameters to be substituted into the format string.
83  *****************************************************************************/
84 void log_error_state(char * format, ...)
85 {
86   va_list largs;
87
88   assert(format != NULL);
89   va_start(largs, format);
90   vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
91   va_end(largs);
92   EVEL_ERROR("%s", evel_err_string);
93 }
94
95
96 /**************************************************************************//**
97  *  Generate a debug log.
98  *
99  *  Provides an interface to syslog with formatting of the nesting level
100  *  so that it's easier to see function entry/exit.
101  *
102  *  @param[in]  level   The debug level - see ::EVEL_LOG_LEVELS.
103  *  @param[in]  format  The output formatting in printf style.
104  *  @param[in]  ...     Variable arguments as specified in the format string.
105  *****************************************************************************/
106 void log_debug(EVEL_LOG_LEVELS level, char * format, ...)
107 {
108   va_list largs;
109   int priority;
110   char indent_fmt[1024];
111   char *syslog_fmt = NULL;
112
113   /***************************************************************************/
114   /* Test assumptions.                                                       */
115   /***************************************************************************/
116   assert(format != NULL);
117   assert(level <= EVEL_LOG_MAX);
118
119   if (level >= debug_level)
120   {
121     if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
122     {
123       /***********************************************************************/
124       /* Just use the format as is.                                          */
125       /***********************************************************************/
126       syslog_fmt = format;
127     }
128     else
129     {
130       /***********************************************************************/
131       /* Combine the format with a preceding number of indent markers.       */
132       /***********************************************************************/
133       sprintf(indent_fmt, "%.*s%s",
134               debug_indent,
135               INDENT_SEPARATORS,
136               format);
137       syslog_fmt = indent_fmt;
138     }
139
140     /*************************************************************************/
141     /* Work out the syslog priority value.                                   */
142     /*************************************************************************/
143     switch (level)
144     {
145     case EVEL_LOG_ERROR:
146       priority = LOG_ERR;
147       break;
148
149     case EVEL_LOG_INFO:
150       priority = LOG_INFO;
151       break;
152
153     case EVEL_LOG_DEBUG:
154     case EVEL_LOG_SPAMMY:
155     default:
156       priority = LOG_DEBUG;
157       break;
158     }
159
160     /*************************************************************************/
161     /* Write the log to the file next, which requires the var args list.     */
162     /*************************************************************************/
163     va_start(largs, format);
164     vsyslog(priority, syslog_fmt, largs);
165     va_end(largs);
166   }
167 }