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