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