1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 /**************************************************************************//**
20 * Wrapper for event logging built on syslog.
22 ****************************************************************************/
30 #include <curl/curl.h>
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";
43 /*****************************************************************************/
44 /* Buffers for error strings from this library. */
45 /*****************************************************************************/
46 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
49 /**************************************************************************//**
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)
57 assert(level < EVEL_LOG_MAX);
58 assert(ident != NULL);
61 openlog(ident, LOG_PID, LOG_USER);
64 /**************************************************************************//**
65 * Descriptive text for library errors.
67 * Return a text error string that relates to the last failure. May be
68 * "<null>" but will never be NULL.
70 * @returns Text error string.
72 * @note Must not be freed!
73 *****************************************************************************/
74 const char * evel_error_string(void)
76 return(evel_err_string);
79 /***************************************************************************//*
80 * Store the formatted string into the static error string and log the error.
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, ...)
89 assert(format != NULL);
90 va_start(largs, format);
91 vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
93 EVEL_ERROR("%s", evel_err_string);
97 /**************************************************************************//**
98 * Generate a debug log.
100 * Provides an interface to syslog with formatting of the nesting level
101 * so that it's easier to see function entry/exit.
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, ...)
111 char indent_fmt[1024];
112 char *syslog_fmt = NULL;
114 /***************************************************************************/
115 /* Test assumptions. */
116 /***************************************************************************/
117 assert(format != NULL);
118 assert(level <= EVEL_LOG_MAX);
120 if (level >= debug_level)
122 if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
124 /***********************************************************************/
125 /* Just use the format as is. */
126 /***********************************************************************/
131 /***********************************************************************/
132 /* Combine the format with a preceding number of indent markers. */
133 /***********************************************************************/
134 sprintf(indent_fmt, "%.*s%s",
138 syslog_fmt = indent_fmt;
141 /*************************************************************************/
142 /* Work out the syslog priority value. */
143 /*************************************************************************/
155 case EVEL_LOG_SPAMMY:
157 priority = LOG_DEBUG;
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);