558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik/*
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik Authors:
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik Simo Sorce <ssorce@redhat.com>
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik Copyright (C) 2009 Red Hat
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik This program is free software; you can redistribute it and/or modify
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik it under the terms of the GNU General Public License as published by
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik the Free Software Foundation; either version 3 of the License, or
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik (at your option) any later version.
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik This program is distributed in the hope that it will be useful,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik but WITHOUT ANY WARRANTY; without even the implied warranty of
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik GNU General Public License for more details.
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik You should have received a copy of the GNU General Public License
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik along with this program. If not, see <http://www.gnu.org/licenses/>.
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik*/
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#ifndef __SSSD_DEBUG_H__
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define __SSSD_DEBUG_H__
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#include "config.h"
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSS_ATTRIBUTE_PRINTF(a1, a2) __attribute__((format (printf, a1, a2)))
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#else
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSS_ATTRIBUTE_PRINTF(a1, a2)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#endif
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
7c30eade4ae794ed809845f2ef70dda849b6e7c9Lukas Slebodnik#define APPEND_LINE_FEED 0x1
7c30eade4ae794ed809845f2ef70dda849b6e7c9Lukas Slebodnik
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnikenum sss_logger_t {
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik STDERR_LOGGER = 0,
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik FILES_LOGGER,
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik#ifdef WITH_JOURNALD
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik JOURNALD_LOGGER,
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik#endif
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik};
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnikextern const char *sss_logger_str[];
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern const char *debug_prg_name;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern int debug_level;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern int debug_timestamps;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern int debug_microseconds;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern int debug_to_file;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern int debug_to_stderr;
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnikextern enum sss_logger_t sss_logger;
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikextern const char *debug_log_file;
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnikvoid sss_set_logger(const char *logger);
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikvoid sss_vdebug_fn(const char *file,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik long line,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik const char *function,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik int level,
7c30eade4ae794ed809845f2ef70dda849b6e7c9Lukas Slebodnik int flags,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik const char *format,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik va_list ap);
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikvoid sss_debug_fn(const char *file,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik long line,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik const char *function,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik int level,
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik const char *format, ...) SSS_ATTRIBUTE_PRINTF(5, 6);
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikint debug_convert_old_level(int old_level);
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikerrno_t set_debug_file_from_fd(const int fd);
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnikint get_fd_from_debug_file(void);
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSS_DOM_ENV "_SSS_DOM"
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_FATAL_FAILURE 0x0010 /* level 0 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_CRIT_FAILURE 0x0020 /* level 1 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_OP_FAILURE 0x0040 /* level 2 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_MINOR_FAILURE 0x0080 /* level 3 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_CONF_SETTINGS 0x0100 /* level 4 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_FUNC_DATA 0x0200 /* level 5 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TRACE_FUNC 0x0400 /* level 6 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TRACE_LIBS 0x1000 /* level 7 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TRACE_INTERNAL 0x2000 /* level 8 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TRACE_ALL 0x4000 /* level 9 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_BE_FO 0x8000 /* level 9 */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_IMPORTANT_INFO SSSDBG_OP_FAILURE
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_INVALID -1
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_UNRESOLVED 0
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_MASK_ALL 0xFFF0 /* enable all debug levels */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_DEFAULT SSSDBG_FATAL_FAILURE
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TIMESTAMP_UNRESOLVED -1
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_TIMESTAMP_DEFAULT 1
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_MICROSECONDS_UNRESOLVED -1
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSDBG_MICROSECONDS_DEFAULT 0
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik#define SSSD_LOGGER_OPTS \
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik {"logger", '\0', POPT_ARG_STRING, &opt_logger, 0, \
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik _("Set logger"), "stderr|files|journald"},
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik
09e3f0af96cecb94be69084c025f0355b3111993Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define SSSD_DEBUG_OPTS \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik {"debug-level", 'd', POPT_ARG_INT, &debug_level, 0, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik _("Debug level"), NULL}, \
18a47bcc463c866de6b6b0327e6d85ceb1e0f7d6Lukas Slebodnik {"debug-to-files", 'f', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &debug_to_file, 0, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik _("Send the debug output to files instead of stderr"), NULL }, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik {"debug-to-stderr", 0, POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &debug_to_stderr, 0, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik _("Send the debug output to stderr directly."), NULL }, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik {"debug-timestamps", 0, POPT_ARG_INT, &debug_timestamps, 0, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik _("Add debug timestamps"), NULL}, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik {"debug-microseconds", 0, POPT_ARG_INT, &debug_microseconds, 0, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik _("Show timestamps with microseconds"), NULL},
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik/** \def DEBUG(level, format, ...)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \brief macro to generate debug messages
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \param level the debug level, please use one of the SSSDBG_* macros
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \param format the debug message format string, should result in a
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik newline-terminated message
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \param ... the debug message format arguments
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik*/
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define DEBUG(level, format, ...) do { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik int __debug_macro_level = level; \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik if (DEBUG_IS_SET(__debug_macro_level)) { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik sss_debug_fn(__FILE__, __LINE__, __FUNCTION__, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik __debug_macro_level, \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik format, ##__VA_ARGS__); \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik } \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik} while (0)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik/** \def DEBUG_IS_SET(level)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \brief checks whether level is set in debug_level
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik \param level the debug level, please use one of the SSSDBG*_ macros
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik*/
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define DEBUG_IS_SET(level) (debug_level & (level) || \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik (debug_level == SSSDBG_UNRESOLVED && \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik (level & (SSSDBG_FATAL_FAILURE | \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik SSSDBG_CRIT_FAILURE))))
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define DEBUG_INIT(dbg_lvl) do { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik if (dbg_lvl != SSSDBG_INVALID) { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik debug_level = debug_convert_old_level(dbg_lvl); \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik } else { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik debug_level = SSSDBG_UNRESOLVED; \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik } \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik\
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik talloc_set_log_fn(talloc_log_fn); \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik} while (0)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik/* CLI tools shall debug to stderr even when SSSD was compiled with journald
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik * support
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik */
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define DEBUG_CLI_INIT(dbg_lvl) do { \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik DEBUG_INIT(dbg_lvl); \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik debug_to_stderr = 1; \
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik} while (0)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define PRINT(fmt, ...) fprintf(stdout, gettext(fmt), ##__VA_ARGS__)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#define ERROR(fmt, ...) fprintf(stderr, gettext(fmt), ##__VA_ARGS__)
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik
558ec7d717735bb16c210c675c2cc5bee1da4576Lukas Slebodnik#endif /* __SSSD_DEBUG_H__ */