1 /**************************************************************************//**
3 * Wrapper for event logging built on syslog.
8 * Copyright(c) <2016>, AT&T Intellectual Property. All other rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
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.
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 *****************************************************************************/
43 #include <curl/curl.h>
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";
56 /*****************************************************************************/
57 /* Buffers for error strings from this library. */
58 /*****************************************************************************/
59 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
62 /**************************************************************************//**
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)
70 assert(level < EVEL_LOG_MAX);
71 assert(ident != NULL);
74 openlog(ident, LOG_PID, LOG_USER);
77 /**************************************************************************//**
78 * Descriptive text for library errors.
80 * Return a text error string that relates to the last failure. May be
81 * "<null>" but will never be NULL.
83 * @returns Text error string.
85 * @note Must not be freed!
86 *****************************************************************************/
87 const char * evel_error_string(void)
89 return(evel_err_string);
92 /***************************************************************************//*
93 * Store the formatted string into the static error string and log the error.
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, ...)
102 assert(format != NULL);
103 va_start(largs, format);
104 vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
106 EVEL_ERROR("%s", evel_err_string);
110 /**************************************************************************//**
111 * Generate a debug log.
113 * Provides an interface to syslog with formatting of the nesting level
114 * so that it's easier to see function entry/exit.
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, ...)
124 char indent_fmt[1024];
125 char *syslog_fmt = NULL;
127 /***************************************************************************/
128 /* Test assumptions. */
129 /***************************************************************************/
130 assert(format != NULL);
131 assert(level <= EVEL_LOG_MAX);
133 if (level >= debug_level)
135 if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
137 /***********************************************************************/
138 /* Just use the format as is. */
139 /***********************************************************************/
144 /***********************************************************************/
145 /* Combine the format with a preceding number of indent markers. */
146 /***********************************************************************/
147 sprintf(indent_fmt, "%.*s%s",
151 syslog_fmt = indent_fmt;
154 /*************************************************************************/
155 /* Work out the syslog priority value. */
156 /*************************************************************************/
168 case EVEL_LOG_SPAMMY:
170 priority = LOG_DEBUG;
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);