36#define G_LOG_DOMAIN "libgvm base"
61 gchar buf[80], *original_tz;
68 original_tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
77 localtime_r (&now, &ts);
78 strftime (buf,
sizeof (buf), time_fmt, &ts);
85 setenv (
"TZ", original_tz, 1);
93 return g_strdup_printf (
"%s", buf);
106 if (level && strlen (level) > 0)
108 if (level[0] >=
'0' && level[0] <=
'9')
110 if (strcasecmp (level,
"critical") == 0)
111 return G_LOG_LEVEL_CRITICAL;
112 if (strcasecmp (level,
"debug") == 0)
113 return G_LOG_LEVEL_DEBUG;
114 if (strcasecmp (level,
"error") == 0)
115 return G_LOG_LEVEL_ERROR;
116 if (strcasecmp (level,
"info") == 0)
117 return G_LOG_LEVEL_INFO;
118 if (strcasecmp (level,
"message") == 0)
119 return G_LOG_LEVEL_MESSAGE;
120 if (strcasecmp (level,
"warning") == 0)
121 return G_LOG_LEVEL_WARNING;
137 if (facility && strlen (facility) > 0)
140 while (facilitynames[i].c_name != NULL)
142 if (g_ascii_strcasecmp (facility, facilitynames[i].c_name) == 0)
143 return facilitynames[i].c_val;
165 GError *error = NULL;
176 GSList *log_domain_list = NULL;
179 key_file = g_key_file_new ();
180 flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
183 if (!g_key_file_load_from_file (key_file, config_file, flags, &error))
185 g_error (
"%s: %s", config_file, error->message);
189 groups = g_key_file_get_groups (key_file, NULL);
194 while (*group != NULL)
201 if (g_key_file_has_key (key_file, *group,
"prepend", &error))
205 g_key_file_get_value (key_file, *group,
"prepend", &error));
209 if (g_key_file_has_key (key_file, *group,
"separator", &error))
213 g_key_file_get_value (key_file, *group,
"separator", &error));
217 if (g_key_file_has_key (key_file, *group,
"prepend_time_format", &error))
221 g_key_file_get_value (key_file, *group,
"prepend_time_format",
226 if (g_key_file_has_key (key_file, *group,
"file", &error))
230 g_key_file_get_value (key_file, *group,
"file", &error));
234 if (g_key_file_has_key (key_file, *group,
"level", &error))
238 level = g_key_file_get_value (key_file, *group,
"level", &error);
239 level = g_strchug (level);
246 if (g_key_file_has_key (key_file, *group,
"syslog_facility", &error))
250 g_key_file_get_value (key_file, *group,
"syslog_facility", &error));
254 g_strdup (
"local0"));
257 if (g_key_file_has_key (key_file, *group,
"syslog_ident", &error))
261 g_key_file_get_value (key_file, *group,
"syslog_ident", &error));
268 log_domain_list = g_slist_prepend (log_domain_list, log_domain_entry);
275 g_key_file_free (key_file);
277 return log_domain_list;
288 GSList *log_domain_list_tmp;
295 log_domain_list_tmp = log_domain_list;
296 while (log_domain_list_tmp != NULL)
301 log_domain_entry = log_domain_list_tmp->data;
307 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
310 g_slist_free (log_domain_list);
323 const char *message, gpointer gvm_log_config_list)
328 (void) gvm_log_config_list;
427 const char *message, gpointer gvm_log_config_list)
438 GSList *log_domain_list_tmp;
442 GIOChannel *channel = NULL;
443 GError *error = NULL;
448 gchar *prepend_format =
"%t %s %p - ";
449 gchar *time_format =
"%Y-%m-%d %Hh%M.%S %Z";
450 gchar *log_separator =
":";
451 gchar *log_file =
"-";
452 GLogLevelFlags default_level = G_LOG_LEVEL_DEBUG;
453 gchar *syslog_facility =
"local0";
454 gchar *syslog_ident = NULL;
460 if (gvm_log_config_list != NULL && log_domain != NULL)
463 log_domain_list_tmp = (GSList *) gvm_log_config_list;
465 while (log_domain_list_tmp != NULL)
469 entry = log_domain_list_tmp->data;
477 log_domain_entry = entry;
503 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
510 if (gvm_log_config_list != NULL && log_domain != NULL)
513 log_domain_list_tmp = (GSList *) gvm_log_config_list;
515 while (log_domain_list_tmp != NULL)
519 entry = log_domain_list_tmp->data;
527 log_domain_entry = entry;
556 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
563 if (default_level < log_level)
567 prepend_buf = g_strdup (
"");
570 tmp = prepend_format;
575 if ((*tmp ==
'%') && (*(tmp + 1) ==
'p'))
580 g_strdup_printf (
"%s%d%s%s", prepend_buf, (
int) getpid (),
588 g_strdup_printf (
"%s%d", prepend_buf, (
int) getpid ());
591 g_free (prepend_buf);
593 prepend_buf = prepend_tmp;
597 else if ((*tmp ==
'%') && (*(tmp + 1) ==
't'))
602 prepend_tmp1 =
get_time (time_format);
605 prepend_tmp1 = g_strdup (
"");
610 prepend_tmp = g_strdup_printf (
"%s%s", prepend_buf, prepend_tmp1);
612 g_free (prepend_tmp1);
614 g_free (prepend_buf);
616 prepend_buf = prepend_tmp;
620 else if ((*tmp ==
'%') && (*(tmp + 1) ==
's'))
625 prepend_tmp = g_strdup_printf (
"%s%s", prepend_buf, log_separator);
627 g_free (prepend_buf);
629 prepend_buf = prepend_tmp;
645 case G_LOG_FLAG_RECURSION:
646 prepend = g_strdup_printf (
"RECURSION%s%s", log_separator, prepend_buf);
649 case G_LOG_FLAG_FATAL:
650 prepend = g_strdup_printf (
"FATAL%s%s", log_separator, prepend_buf);
653 case G_LOG_LEVEL_ERROR:
654 prepend = g_strdup_printf (
"ERROR%s%s", log_separator, prepend_buf);
657 case G_LOG_LEVEL_CRITICAL:
658 prepend = g_strdup_printf (
"CRITICAL%s%s", log_separator, prepend_buf);
661 case G_LOG_LEVEL_WARNING:
662 prepend = g_strdup_printf (
"WARNING%s%s", log_separator, prepend_buf);
665 case G_LOG_LEVEL_MESSAGE:
666 prepend = g_strdup_printf (
"MESSAGE%s%s", log_separator, prepend_buf);
669 case G_LOG_LEVEL_INFO:
670 prepend = g_strdup_printf (
" INFO%s%s", log_separator, prepend_buf);
673 case G_LOG_LEVEL_DEBUG:
674 prepend = g_strdup_printf (
" DEBUG%s%s", log_separator, prepend_buf);
678 prepend = g_strdup_printf (
"UNKNOWN%s%s", log_separator, prepend_buf);
687 messagelen = message ? strlen (message) : 0;
688 if (messagelen > 1 && message[messagelen - 1] ==
'\n')
690 tmpstr = g_strdup_printf (
"%s%s%s%s %.*s\n", log_domain ? log_domain :
"",
691 log_separator, prepend, log_separator, messagelen,
695 if (log_level <= G_LOG_LEVEL_WARNING)
700 if (!log_file || g_ascii_strcasecmp (log_file,
"-") == 0
701 || !g_strcmp0 (log_file,
""))
703 fprintf (stderr,
"%s", tmpstr);
707 else if (g_ascii_strcasecmp (log_file,
"syslog") == 0)
710 int syslog_level = LOG_INFO;
712 openlog (syslog_ident, LOG_CONS | LOG_PID | LOG_NDELAY, facility);
716 case G_LOG_FLAG_FATAL:
717 syslog_level = LOG_ALERT;
719 case G_LOG_LEVEL_ERROR:
720 syslog_level = LOG_ERR;
722 case G_LOG_LEVEL_CRITICAL:
723 syslog_level = LOG_CRIT;
725 case G_LOG_LEVEL_WARNING:
726 syslog_level = LOG_WARNING;
728 case G_LOG_LEVEL_MESSAGE:
729 syslog_level = LOG_NOTICE;
731 case G_LOG_LEVEL_INFO:
732 syslog_level = LOG_INFO;
734 case G_LOG_LEVEL_DEBUG:
735 syslog_level = LOG_DEBUG;
738 syslog_level = LOG_INFO;
745 if (messagelen > 1000)
748 char *message_aux, *message_aux2;
751 message_aux2 = g_strdup (message);
752 message_aux = message_aux2;
753 for (pos = 0; pos <= messagelen; pos = pos +
sizeof (buffer) - 1)
755 memcpy (buffer, message_aux,
sizeof (buffer) - 1);
756 buffer[
sizeof (buffer) - 1] =
'\0';
757 message_aux = &(message_aux[
sizeof (buffer) - 1]);
758 syslog (syslog_level,
"%s", buffer);
760 g_free (message_aux2);
763 syslog (syslog_level,
"%s", message);
774 channel = g_io_channel_new_file (log_file,
"a", &error);
777 gchar *log = g_strdup (log_file);
778 gchar *dir = dirname (log);
784 if (error->code != G_FILE_ERROR_NOENT)
785 fprintf (stderr,
"Can not open '%s' logfile: %s\n", log_file,
787 g_error_free (error);
790 if (g_mkdir_with_parents (dir, 0755))
792 g_warning (
"Failed to create log file directory %s: %s", dir,
796 g_free (prepend_buf);
803 channel = g_io_channel_new_file (log_file,
"a", &error);
806 g_error (
"Can not open '%s' logfile: %s", log_file,
812 if (log_domain_entry != NULL)
815 g_io_channel_write_chars (channel, (
const gchar *) tmpstr, -1, NULL,
817 g_io_channel_flush (channel, NULL);
821 g_free (prepend_buf);
838 g_log (
"x gnutls", G_LOG_LEVEL_INFO,
"tls(%d): %s", level, message);
853 GIOChannel *channel = NULL;
854 const gchar *log_file;
860 if (!log_file || g_ascii_strcasecmp (log_file,
"-") == 0
861 || !g_strcmp0 (log_file,
""))
865 if (g_ascii_strcasecmp (log_file,
"syslog") == 0)
868 channel = g_io_channel_new_file (log_file,
"a", NULL);
871 gchar *log = g_strdup (log_file);
872 gchar *dir = dirname (log);
875 if (g_mkdir_with_parents (dir, 0755))
883 channel = g_io_channel_new_file (log_file,
"a", NULL);
887 g_io_channel_shutdown (channel, TRUE, NULL);
888 g_io_channel_unref (channel);
904 log_tz = tz ? g_strdup (tz) : NULL;
909 GLogFunc default_log_func,
910 GLogFunc default_domain_log_func)
912 GSList *log_domain_list_tmp;
916 if (gvm_log_config_list != NULL)
919 log_domain_list_tmp = (GSList *) gvm_log_config_list;
921 while (log_domain_list_tmp != NULL)
926 log_domain_entry = log_domain_list_tmp->data;
933 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
937 if (g_ascii_strcasecmp (
942 (GLogLevelFlags) (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO
943 | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING
944 | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR
945 | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
946 (GLogFunc) log_func, gvm_log_config_list);
950 g_log_set_default_handler (default_log_func, gvm_log_config_list);
954 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
959 (GLogLevelFlags) (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE
960 | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL
961 | G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL
962 | G_LOG_FLAG_RECURSION),
963 default_domain_log_func, gvm_log_config_list);
void gvm_sentry_log(const char *message)
Send a message to Sentry server if it was initialized.
Definition gvm_sentry.c:97
Implementation of sentry methods.
void gvm_log_func(const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer gvm_log_config_list)
Creates the formatted string and outputs it to the log destination.
Definition logging.c:426
void free_log_reference(void)
Free the log reference object.
Definition logging.c:411
char * get_log_reference(void)
Get the log reference object.
Definition logging.c:399
void set_log_reference(char *ref)
Set the log reference object.
Definition logging.c:384
static int check_log_file(gvm_logging_domain_t *log_domain_entry)
Check permissions of log file and log file directory.
Definition logging.c:851
void log_func_for_gnutls(int level, const char *message)
This function logs debug messages from gnutls.
Definition logging.c:836
void free_log_configuration(GSList *log_domain_list)
Frees all resources loaded by the config loader.
Definition logging.c:286
gchar * get_time(gchar *time_fmt)
Returns time as specified in time_fmt strftime format.
Definition logging.c:57
static gint level_int_from_string(const gchar *level)
Return the integer corresponding to a log level string.
Definition logging.c:104
GSList * load_log_configuration(const gchar *config_file)
Loads parameters from a config file into a linked list.
Definition logging.c:161
void gvm_log_unlock(void)
Unlock logger_mutex.
Definition logging.c:363
static gchar * log_tz
Timezone to use for logs.
Definition logging.c:43
void gvm_log_lock(void)
Try to lock logger_mutex.
Definition logging.c:351
static GMutex * logger_mutex
Definition logging.c:332
void set_log_tz(const gchar *tz)
Set the log timezone.
Definition logging.c:901
static void gvm_log_lock_init(void)
Initialize logger_mutex mutex if it was not done before.
Definition logging.c:338
static gint facility_int_from_string(const gchar *facility)
Return the integer corresponding to a syslog facility string.
Definition logging.c:135
static char * reference
Definition logging.c:368
int setup_log_handlers(GSList *gvm_log_config_list)
Sets up routing of logdomains to log handlers.
Definition logging.c:979
void gvm_log_silent(const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer gvm_log_config_list)
Returns immediately.
Definition logging.c:322
static int setup_log_handlers_internal(GSList *gvm_log_config_list, GLogFunc log_func, GLogFunc default_log_func, GLogFunc default_domain_log_func)
Definition logging.c:908
Implementation of logging methods.
void gvm_logging_domain_set_prepend_separator(gvm_logging_domain_t *log_domain, gchar *prepend_separator)
Sets the prepend separator for the logging domain.
Definition logging_domain.c:341
GIOChannel * gvm_logging_domain_get_log_channel(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:359
void gvm_logging_domain_set_log_file(gvm_logging_domain_t *log_domain, gchar *log_file)
Sets the log file for the logging domain.
Definition logging_domain.c:137
void gvm_logging_domain_set_syslog_ident(gvm_logging_domain_t *log_domain, gchar *syslog_ident)
Sets the syslog ident for the logging domain.
Definition logging_domain.c:307
void gvm_logging_domain_set_prepend_time_format(gvm_logging_domain_t *log_domain, gchar *prepend_time_format)
Sets the prepend time format for the logging domain.
Definition logging_domain.c:206
void gvm_logging_domain_set_syslog_facility(gvm_logging_domain_t *log_domain, gchar *syslog_facility)
Sets the syslog facility for the logging domain.
Definition logging_domain.c:274
GLogLevelFlags * gvm_logging_domain_get_default_level(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:224
gvm_logging_domain_t * gvm_logging_domain_new(gchar *log_domain)
Function to initialize logging instance.
Definition logging_domain.c:40
gchar * gvm_logging_domain_get_prepend_time_format(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:189
gchar * gvm_logging_domain_get_syslog_ident(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:292
void gvm_logging_domain_set_log_channel(gvm_logging_domain_t *log_domain, GIOChannel *log_channel)
Sets the log channel for the logging domain.
Definition logging_domain.c:374
void gvm_logging_domain_free(gvm_logging_domain_t *log_domain)
Frees the resources associated with the given logging domain.
Definition logging_domain.c:72
gchar * gvm_logging_domain_get_log_file(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:121
void gvm_logging_domain_set_prepend_string(gvm_logging_domain_t *log_domain, gchar *prepend_string)
Sets the preprend string for the logging domain.
Definition logging_domain.c:171
gchar * gvm_logging_domain_get_log_domain(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:105
void gvm_logging_domain_set_default_level(gvm_logging_domain_t *log_domain, GLogLevelFlags default_level)
Sets the default log level for the logging domain.
Definition logging_domain.c:239
gchar * gvm_logging_domain_get_prepend_separator(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:325
gchar * gvm_logging_domain_get_prepend_string(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:155
gchar * gvm_logging_domain_get_syslog_facility(gvm_logging_domain_t *log_domain)
Definition logging_domain.c:258
Implementation of logging domain handling.
struct gvm_logging_domain gvm_logging_domain_t
Definition logging_domain.h:15