debug.c revision 9a990aa9f7e8c105e0cfeea8d8cbdc776c2d5d7a
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder Simo Sorce <ssorce@redhat.com>
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder Stephen Gallagher <sgallagh@redhat.com>
b4fbc96e05117839ca409f5f20f97b3ac872d1edTill Mossakowski Copyright (C) 2009 Red Hat
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is free software; you can redistribute it and/or modify
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder it under the terms of the GNU General Public License as published by
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder the Free Software Foundation; either version 3 of the License, or
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (at your option) any later version.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is distributed in the hope that it will be useful,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder GNU General Public License for more details.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder You should have received a copy of the GNU General Public License
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_timestamps = SSSDBG_TIMESTAMP_UNRESOLVED;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_microseconds = SSSDBG_MICROSECONDS_UNRESOLVED;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maedererrno_t set_debug_file_from_fd(const int fd)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "fdopen failed [%d][%s].\n", ret, strerror(ret));
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder "Could not open debug file descriptor [%d]. "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "Debug messages will not be written to the file "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "for this child process [%s][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if ((old_level != 0) && !(old_level & 0x000F))
2353f65833a3da763392f771223250cd50b8d873Christian Maederstatic void debug_fflush(void)
2353f65833a3da763392f771223250cd50b8d873Christian Maederstatic void debug_vprintf(const char *format, va_list ap)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder vfprintf(debug_file ? debug_file : stderr, format, ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic void debug_printf(const char *format, ...)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic void debug_printf(const char *format, ...)
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder /* First, evaluate the message to be sent */
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder /* ENOMEM, just return */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder res = asprintf(&code_file, "CODE_FILE=%s", file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder res = asprintf(&code_line, "CODE_LINE=%ld", line);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* If this log message was sent from a provider,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * track the domain.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Send the log message to journald, specifying the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * source code location and other tracking data.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#endif /* WiTH_JOURNALD */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *format, ...)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* If we are not outputting logs to files, we should be sending them
2353f65833a3da763392f771223250cd50b8d873Christian Maeder * to journald.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * NOTE: on modern systems, this is where stdout/stderr will end up
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * from system services anyway. The only difference here is that we
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * can also provide extra structuring data to make it more easily
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * searchable.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = journal_send(file, line, function, level, format, ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Emergency fallback, send to STDERR */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* get date time without year */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_printf("(%s:%.6ld %d) [%s] [%s] (%#.4x): ",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_printf("(%s %d) [%s] [%s] (%#.4x): ",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maedervoid ldb_debug_messages(void *context, enum ldb_debug_level level,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_fn(__FILE__, __LINE__, "ldb", loglevel, "%s\n", message);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint open_debug_file_ex(const char *filename, FILE **filep, bool want_cloexec)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = asprintf(&logpath, "%s/%s.log", LOG_PATH, log_file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (debug_file && !filep) fclose(debug_file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_EMERG, "Could not open file [%s]. Error: [%d][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (void) fcntl(debug_fd, F_SETFD, flags | FD_CLOEXEC);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return open_debug_file_ex(NULL, NULL, true);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Check for EINTR, which means we should retry
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * because the system call was interrupted by a
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Even if we were unable to close the debug log, we need to make
2353f65833a3da763392f771223250cd50b8d873Christian Maeder * sure that we open up a new one. Log rotation will remove the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * current file, so all debug messages will be disappearing.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * We should write an error to the syslog warning of the resource
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * leak and then proceed with opening the new file.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_ALERT, "Could not close debug file [%s]. [%d][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_ALERT, "Attempting to open new file anyway. "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "Be aware that this is a resource leak\n");