errno_t journal_send(const char *file, long line, const char *function, int level, const char *format, va_list ap) { errno_t ret; int res; char *message = NULL; char *code_file = NULL; char *code_line = NULL; const char *domain; /* First, evaluate the message to be sent */ ret = vasprintf(&message, format, ap); if (ret == -1) { /* ENOMEM, just return */ return ENOMEM; } res = asprintf(&code_file, "CODE_FILE=%s", file); if (res == -1) { ret = ENOMEM; goto journal_done; } res = asprintf(&code_line, "CODE_LINE=%ld", line); if (res == -1) { ret = ENOMEM; goto journal_done; } /* If this log message was sent from a provider, * track the domain. */ domain = getenv(SSS_DOM_ENV); if (domain == NULL) { domain = ""; } /* Send the log message to journald, specifying the * source code location and other tracking data. */ res = sd_journal_send_with_location( code_file, code_line, function, "MESSAGE=%s", message, "PRIORITY=%i", LOG_DEBUG, "SSSD_DOMAIN=%s", domain, "SSSD_PRG_NAME=%s", debug_prg_name, "SSSD_DEBUG_LEVEL=%x", level, NULL); ret = -res; journal_done: free(code_line); free(code_file); free(message); return ret; }
SOL_API void sol_log_print_function_journal(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args) { #ifdef PLATFORM_SYSTEMD char *code_file = NULL; char *code_line = NULL; char *msg = NULL; int r, sd_level = _sol_log_level_to_syslog(message_level); r = asprintf(&code_file, "CODE_FILE=%s", file); if (r == -1) fprintf(stderr, "ERR: asprintf() CODE_FILE=%s failed\n", file); r = asprintf(&code_line, "CODE_LINE=%d", line); if (r == -1) fprintf(stderr, "ERR: asprintf() CODE_LINE=%d failed\n", line); r = vasprintf(&msg, format, args); if (r == -1) fprintf(stderr, "ERR: asprintf() %s failed\n", format); sd_journal_send_with_location(code_file, code_line, function, "PRIORITY=%i", sd_level, "MESSAGE=%s", msg ? msg : format, #ifdef PTHREAD "THREAD=%" PRIu64, (uint64_t)(uintptr_t)pthread_self(), #endif NULL); free(code_file); free(code_line); free(msg); #else static bool once = false; if (!once) { once = true; fputs("ERROR: systemd support not compiled in, using syslog.\n", stderr); } sol_log_print_function_syslog(data, domain, message_level, file, function, line, format, args); #endif }