1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
17 /**************************************************************************//**
19 * Wrapper for event logging built on syslog.
21 ****************************************************************************/
29 #include <curl/curl.h>
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";
42 /*****************************************************************************/
43 /* Buffers for error strings from this library. */
44 /*****************************************************************************/
45 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
48 /**************************************************************************//**
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)
56 assert(level < EVEL_LOG_MAX);
57 assert(ident != NULL);
60 openlog(ident, LOG_PID, LOG_USER);
63 /**************************************************************************//**
64 * Descriptive text for library errors.
66 * Return a text error string that relates to the last failure. May be
67 * "<null>" but will never be NULL.
69 * @returns Text error string.
71 * @note Must not be freed!
72 *****************************************************************************/
73 const char * evel_error_string(void)
75 return(evel_err_string);
78 /***************************************************************************//*
79 * Store the formatted string into the static error string and log the error.
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, ...)
88 assert(format != NULL);
89 va_start(largs, format);
90 vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
92 EVEL_ERROR("%s", evel_err_string);
96 /**************************************************************************//**
97 * Generate a debug log.
99 * Provides an interface to syslog with formatting of the nesting level
100 * so that it's easier to see function entry/exit.
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, ...)
110 char indent_fmt[1024];
111 char *syslog_fmt = NULL;
113 /***************************************************************************/
114 /* Test assumptions. */
115 /***************************************************************************/
116 assert(format != NULL);
117 assert(level <= EVEL_LOG_MAX);
119 if (level >= debug_level)
121 if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
123 /***********************************************************************/
124 /* Just use the format as is. */
125 /***********************************************************************/
130 /***********************************************************************/
131 /* Combine the format with a preceding number of indent markers. */
132 /***********************************************************************/
133 sprintf(indent_fmt, "%.*s%s",
137 syslog_fmt = indent_fmt;
140 /*************************************************************************/
141 /* Work out the syslog priority value. */
142 /*************************************************************************/
154 case EVEL_LOG_SPAMMY:
156 priority = LOG_DEBUG;
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);