1 /**************************************************************************//**
3 * Wrapper for event logging built on syslog.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 *****************************************************************************/
28 #include <curl/curl.h>
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";
41 /*****************************************************************************/
42 /* Buffers for error strings from this library. */
43 /*****************************************************************************/
44 static char evel_err_string[EVEL_MAX_ERROR_STRING_LEN] = "<NULL>";
47 /**************************************************************************//**
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)
55 assert(level < EVEL_LOG_MAX);
56 assert(ident != NULL);
59 openlog(ident, LOG_PID, LOG_USER);
62 /**************************************************************************//**
63 * Descriptive text for library errors.
65 * Return a text error string that relates to the last failure. May be
66 * "<null>" but will never be NULL.
68 * @returns Text error string.
70 * @note Must not be freed!
71 *****************************************************************************/
72 const char * evel_error_string(void)
74 return(evel_err_string);
77 /***************************************************************************//*
78 * Store the formatted string into the static error string and log the error.
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, ...)
87 assert(format != NULL);
88 va_start(largs, format);
89 vsnprintf(evel_err_string, EVEL_MAX_ERROR_STRING_LEN, format, largs);
91 EVEL_ERROR("%s", evel_err_string);
95 /**************************************************************************//**
96 * Generate a debug log.
98 * Provides an interface to syslog with formatting of the nesting level
99 * so that it's easier to see function entry/exit.
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, ...)
109 char indent_fmt[1024];
110 char *syslog_fmt = NULL;
112 /***************************************************************************/
113 /* Test assumptions. */
114 /***************************************************************************/
115 assert(format != NULL);
116 assert(level <= EVEL_LOG_MAX);
118 if (level >= debug_level)
120 if ((debug_level == EVEL_LOG_INFO) || (debug_indent == 0))
122 /***********************************************************************/
123 /* Just use the format as is. */
124 /***********************************************************************/
129 /***********************************************************************/
130 /* Combine the format with a preceding number of indent markers. */
131 /***********************************************************************/
132 sprintf(indent_fmt, "%.*s%s",
136 syslog_fmt = indent_fmt;
139 /*************************************************************************/
140 /* Work out the syslog priority value. */
141 /*************************************************************************/
153 case EVEL_LOG_SPAMMY:
155 priority = LOG_DEBUG;
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);