Greenbone Vulnerability Management Libraries 23.16.0
logging.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
15
16#include "logging.h"
17
18#include "gvm_sentry.h" /* for gvm_sentry_log */
19#include "logging_domain.h"
20
21#include <errno.h> /* for errno */
22#include <libgen.h> /* for dirname */
23#include <stdio.h> /* for fflush, fprintf, stderr */
24#include <stdlib.h> /* for atoi */
25#include <string.h> /* for strcasecmp, strlen, strerror */
26#define SYSLOG_NAMES
27#include <syslog.h> /* for LOG_INFO, facilitynames, closelog, openlog */
28#undef SYSLOG_NAMES
29#include <time.h> /* for localtime, time, time_t */
30#include <unistd.h> /* for getpid */
31
32#undef G_LOG_DOMAIN
36#define G_LOG_DOMAIN "libgvm base"
37
43static gchar *log_tz = NULL;
44
56gchar *
57get_time (gchar *time_fmt)
58{
59 time_t now;
60 struct tm ts;
61 gchar buf[80], *original_tz;
62
63 if (!time_fmt)
64 return NULL;
65
66 if (log_tz)
67 {
68 original_tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
69 setenv ("TZ", log_tz, 1);
70 tzset ();
71 }
72
73 /* Get the current time. */
74 now = time (NULL);
75
76 /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz." */
77 localtime_r (&now, &ts);
78 strftime (buf, sizeof (buf), time_fmt, &ts);
79
80 if (log_tz)
81 {
82 /* Revert to stored TZ. */
83 if (original_tz)
84 {
85 setenv ("TZ", original_tz, 1);
86 g_free (original_tz);
87 tzset ();
88 }
89 else
90 unsetenv ("TZ");
91 }
92
93 return g_strdup_printf ("%s", buf);
94}
95
103static gint
104level_int_from_string (const gchar *level)
105{
106 if (level && strlen (level) > 0)
107 {
108 if (level[0] >= '0' && level[0] <= '9')
109 return atoi (level);
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;
122 }
123 return 0;
124}
125
134static gint
135facility_int_from_string (const gchar *facility)
136{
137 if (facility && strlen (facility) > 0)
138 {
139 int i = 0;
140 while (facilitynames[i].c_name != NULL)
141 {
142 if (g_ascii_strcasecmp (facility, facilitynames[i].c_name) == 0)
143 return facilitynames[i].c_val;
144 i++;
145 }
146 }
147 return LOG_LOCAL0;
148}
149
160GSList *
161load_log_configuration (const gchar *config_file)
162{
163 GKeyFile *key_file;
164 GKeyFileFlags flags;
165 GError *error = NULL;
166 /* key_file *_has_* functions requires this. */
167
168 // FIXME: If a g_* function that takes error fails, then free error.
169
170 /* Groups found in the conf file. */
171 gchar **groups;
172 /* Temp variable to iterate over groups. */
173 gchar **group;
174
175 /* The link list for the structure above and it's tmp helper */
176 GSList *log_domain_list = NULL;
177
178 /* Create a new GKeyFile object and a bitwise list of flags. */
179 key_file = g_key_file_new ();
180 flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
181
182 /* Load the GKeyFile from conf or return. */
183 if (!g_key_file_load_from_file (key_file, config_file, flags, &error))
184 {
185 g_error ("%s: %s", config_file, error->message);
186 }
187
188 /* Get all the groups available. */
189 groups = g_key_file_get_groups (key_file, NULL);
190
191 /* Point to the group head. */
192 group = groups;
193 /* Iterate till we get to the end of the array. */
194 while (*group != NULL)
195 {
196 /* Structure to hold per group settings. */
197 gvm_logging_domain_t *log_domain_entry =
198 gvm_logging_domain_new (g_strdup (*group));
199
200 /* Look for the prepend string. */
201 if (g_key_file_has_key (key_file, *group, "prepend", &error))
202 {
204 log_domain_entry,
205 g_key_file_get_value (key_file, *group, "prepend", &error));
206 }
207
208 /* Look for the log_separator string. */
209 if (g_key_file_has_key (key_file, *group, "separator", &error))
210 {
212 log_domain_entry,
213 g_key_file_get_value (key_file, *group, "separator", &error));
214 }
215
216 /* Look for the prepend time format string. */
217 if (g_key_file_has_key (key_file, *group, "prepend_time_format", &error))
218 {
220 log_domain_entry,
221 g_key_file_get_value (key_file, *group, "prepend_time_format",
222 &error));
223 }
224
225 /* Look for the log file string. */
226 if (g_key_file_has_key (key_file, *group, "file", &error))
227 {
229 log_domain_entry,
230 g_key_file_get_value (key_file, *group, "file", &error));
231 }
232
233 /* Look for the prepend log level string. */
234 if (g_key_file_has_key (key_file, *group, "level", &error))
235 {
236 gchar *level;
237
238 level = g_key_file_get_value (key_file, *group, "level", &error);
239 level = g_strchug (level);
240 gvm_logging_domain_set_default_level (log_domain_entry,
241 level_int_from_string (level));
242 g_free (level);
243 }
244
245 /* Look for the syslog_facility string. */
246 if (g_key_file_has_key (key_file, *group, "syslog_facility", &error))
247 {
249 log_domain_entry,
250 g_key_file_get_value (key_file, *group, "syslog_facility", &error));
251 }
252
253 /* Look for the syslog_ident string. */
254 if (g_key_file_has_key (key_file, *group, "syslog_ident", &error))
255 {
257 log_domain_entry,
258 g_key_file_get_value (key_file, *group, "syslog_ident", &error));
259 }
260 else
261 gvm_logging_domain_set_syslog_ident (log_domain_entry,
262 g_strdup (*group));
263
264 /* Attach the struct to the list. */
265 log_domain_list = g_slist_prepend (log_domain_list, log_domain_entry);
266 group++;
267 }
268 /* Free the groups array. */
269 g_strfreev (groups);
270
271 /* Free the key file. */
272 g_key_file_free (key_file);
273
274 return log_domain_list;
275}
276
282void
283free_log_configuration (GSList *log_domain_list)
284{
285 GSList *log_domain_list_tmp;
286
287 /* Free the struct fields then the struct and then go the next
288 * item in the link list.
289 */
290
291 /* Go to the head of the list. */
292 log_domain_list_tmp = log_domain_list;
293 while (log_domain_list_tmp != NULL)
294 {
295 gvm_logging_domain_t *log_domain_entry;
296
297 /* Get the list data which is an gvm_logging_domain_t struct. */
298 log_domain_entry = log_domain_list_tmp->data;
299
300 /* Free the struct contents. */
301 gvm_logging_domain_free (log_domain_entry);
302
303 /* Go to the next item. */
304 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
305 }
306 /* Free the link list. */
307 g_slist_free (log_domain_list);
308}
309
318void
319gvm_log_silent (const char *log_domain, GLogLevelFlags log_level,
320 const char *message, gpointer gvm_log_config_list)
321{
322 (void) log_domain;
323 (void) log_level;
324 (void) message;
325 (void) gvm_log_config_list;
326 return;
327}
328
329static GMutex *logger_mutex = NULL;
330
334static void
336{
337 if (logger_mutex == NULL)
338 {
339 logger_mutex = g_malloc (sizeof (*logger_mutex));
340 g_mutex_init (logger_mutex);
341 }
342}
343
347void
349{
350 /* Initialize logger lock if not done already. */
352
353 g_mutex_lock (logger_mutex);
354}
355
359void
361{
362 g_mutex_unlock (logger_mutex);
363}
364
365static char *reference = NULL;
366
380void
382{
383 g_free (reference);
384 reference = ref;
385}
386
395char *
397{
398 return (char *) reference;
399}
400
407void
409{
410 g_free (reference);
411 reference = NULL;
412}
413
422void
423gvm_log_func (const char *log_domain, GLogLevelFlags log_level,
424 const char *message, gpointer gvm_log_config_list)
425{
426 gchar *prepend;
427 gchar *prepend_buf;
428 gchar *prepend_tmp;
429 gchar *prepend_tmp1;
430 gchar *tmp;
431 gchar *tmpstr;
432 int messagelen;
433
434 /* For link list operations. */
435 GSList *log_domain_list_tmp;
436 gvm_logging_domain_t *log_domain_entry = NULL;
437
438 /* Channel to log through. */
439 GIOChannel *channel = NULL;
440 GError *error = NULL;
441
442 /* The default parameters to be used. The group '*' will override
443 * these defaults if it's found.
444 */
445 gchar *prepend_format = "%t %s %p - ";
446 gchar *time_format = "%Y-%m-%d %Hh%M.%S %Z";
447 gchar *log_separator = ":";
448 gchar *log_file = "-";
449 GLogLevelFlags default_level = G_LOG_LEVEL_DEBUG;
450 gchar *syslog_facility = "local0";
451 const gchar *syslog_ident = log_domain;
452
453 /* Let's load the default configuration file directives from the
454 * linked list. Scanning the link list twice is inefficient but
455 * leaves the source cleaner.
456 */
457 if (gvm_log_config_list != NULL && log_domain != NULL)
458 {
459 /* Go to the head of the list. */
460 log_domain_list_tmp = (GSList *) gvm_log_config_list;
461
462 while (log_domain_list_tmp != NULL)
463 {
465
466 entry = log_domain_list_tmp->data;
467
468 /* Override defaults if the current linklist group name is '*'. */
469 if (g_ascii_strcasecmp (gvm_logging_domain_get_log_domain (entry),
470 "*")
471 == 0)
472 {
473 /* Get the list data for later use. */
474 log_domain_entry = entry;
475
476 /* Override defaults if the group items are not null. */
477 if (gvm_logging_domain_get_prepend_string (log_domain_entry))
478 prepend_format =
479 gvm_logging_domain_get_prepend_string (log_domain_entry);
480 if (gvm_logging_domain_get_prepend_time_format (log_domain_entry))
481 time_format =
483 if (gvm_logging_domain_get_log_file (log_domain_entry))
484 log_file = gvm_logging_domain_get_log_file (log_domain_entry);
485 if (gvm_logging_domain_get_default_level (log_domain_entry))
486 default_level =
487 *gvm_logging_domain_get_default_level (log_domain_entry);
488 if (gvm_logging_domain_get_log_channel (log_domain_entry))
489 channel = gvm_logging_domain_get_log_channel (log_domain_entry);
490 if (gvm_logging_domain_get_syslog_facility (log_domain_entry))
491 syslog_facility =
493 if (gvm_logging_domain_get_prepend_separator (log_domain_entry))
494 log_separator =
496 break;
497 }
498
499 /* Go to the next item. */
500 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
501 }
502 }
503
504 /* Let's load the configuration file directives if a linked list item for
505 * the log domain group exists.
506 */
507 if (gvm_log_config_list != NULL && log_domain != NULL)
508 {
509 /* Go to the head of the list. */
510 log_domain_list_tmp = (GSList *) gvm_log_config_list;
511
512 while (log_domain_list_tmp != NULL)
513 {
515
516 entry = log_domain_list_tmp->data;
517
518 /* Search for the log domain in the link list. */
519 if (g_ascii_strcasecmp (gvm_logging_domain_get_log_domain (entry),
520 log_domain)
521 == 0)
522 {
523 /* Get the list data which is an gvm_logging_domain_t struct. */
524 log_domain_entry = entry;
525
526 /* Get the struct contents. */
527 if (gvm_logging_domain_get_prepend_string (log_domain_entry))
528 prepend_format =
529 gvm_logging_domain_get_prepend_string (log_domain_entry);
530 if (gvm_logging_domain_get_prepend_time_format (log_domain_entry))
531 time_format =
533 if (gvm_logging_domain_get_log_file (log_domain_entry))
534 log_file = gvm_logging_domain_get_log_file (log_domain_entry);
535 if (gvm_logging_domain_get_default_level (log_domain_entry))
536 default_level =
537 *gvm_logging_domain_get_default_level (log_domain_entry);
538 if (gvm_logging_domain_get_log_channel (log_domain_entry))
539 channel = gvm_logging_domain_get_log_channel (log_domain_entry);
540 if (gvm_logging_domain_get_syslog_facility (log_domain_entry))
541 syslog_facility =
543 if (gvm_logging_domain_get_syslog_ident (log_domain_entry))
544 syslog_ident =
545 gvm_logging_domain_get_syslog_ident (log_domain_entry);
546 if (gvm_logging_domain_get_prepend_separator (log_domain_entry))
547 log_separator =
549 break;
550 }
551
552 /* Go to the next item. */
553 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
554 }
555 }
556
557 /* If the current log entry is less severe than the specified log level,
558 * let's exit.
559 */
560 if (default_level < log_level)
561 return;
562
563 /* Prepend buf is a newly allocated empty string. Makes life easier. */
564 prepend_buf = g_strdup ("");
565
566 /* Make the tmp pointer (for iteration) point to the format string. */
567 tmp = prepend_format;
568
569 while (*tmp != '\0')
570 {
571 /* If the current char is a % and the next one is a p, get the pid. */
572 if ((*tmp == '%') && (*(tmp + 1) == 'p'))
573 {
574 if (reference)
575 {
576 prepend_tmp =
577 g_strdup_printf ("%s%d%s%s", prepend_buf, (int) getpid (),
578 log_separator, reference);
579 }
580 else
581 {
582 /* Use g_strdup, a new string returned. Store it in a tmp var
583 * until we free the old one. */
584 prepend_tmp =
585 g_strdup_printf ("%s%d", prepend_buf, (int) getpid ());
586 }
587 /* Free the old string. */
588 g_free (prepend_buf);
589 /* Point the buf ptr to the new string. */
590 prepend_buf = prepend_tmp;
591 /* Skip over the two chars we've processed '%p'. */
592 tmp += 2;
593 }
594 else if ((*tmp == '%') && (*(tmp + 1) == 't'))
595 {
596 /* Get time returns a newly allocated string.
597 * Store it in a tmp var.
598 */
599 prepend_tmp1 = get_time (time_format);
600 if (!prepend_tmp1)
601 {
602 prepend_tmp1 = g_strdup ("");
603 }
604 /* Use g_strdup. New string returned. Store it in a tmp var until
605 * we free the old one.
606 */
607 prepend_tmp = g_strdup_printf ("%s%s", prepend_buf, prepend_tmp1);
608 /* Free the time tmp var. */
609 g_free (prepend_tmp1);
610 /* Free the old string. */
611 g_free (prepend_buf);
612 /* Point the buf ptr to the new string. */
613 prepend_buf = prepend_tmp;
614 /* Skip over the two chars we've processed '%t.' */
615 tmp += 2;
616 }
617 else if ((*tmp == '%') && (*(tmp + 1) == 's'))
618 {
619 /* Use g_strdup. New string returned. Store it in a tmp var until
620 * we free the old one.
621 */
622 prepend_tmp = g_strdup_printf ("%s%s", prepend_buf, log_separator);
623 /* Free the old string. */
624 g_free (prepend_buf);
625 /* Point the buf ptr to the new string. */
626 prepend_buf = prepend_tmp;
627 /* Skip over the two chars we've processed '%s.' */
628 tmp += 2;
629 }
630 else
631 {
632 /* Jump to the next character. */
633 tmp++;
634 }
635 }
636
637 /* Step through all possible messages prefixing them with an appropriate
638 * tag.
639 */
640 switch (log_level)
641 {
642 case G_LOG_FLAG_RECURSION:
643 prepend = g_strdup_printf ("RECURSION%s%s", log_separator, prepend_buf);
644 break;
645
646 case G_LOG_FLAG_FATAL:
647 prepend = g_strdup_printf ("FATAL%s%s", log_separator, prepend_buf);
648 break;
649
650 case G_LOG_LEVEL_ERROR:
651 prepend = g_strdup_printf ("ERROR%s%s", log_separator, prepend_buf);
652 break;
653
654 case G_LOG_LEVEL_CRITICAL:
655 prepend = g_strdup_printf ("CRITICAL%s%s", log_separator, prepend_buf);
656 break;
657
658 case G_LOG_LEVEL_WARNING:
659 prepend = g_strdup_printf ("WARNING%s%s", log_separator, prepend_buf);
660 break;
661
662 case G_LOG_LEVEL_MESSAGE:
663 prepend = g_strdup_printf ("MESSAGE%s%s", log_separator, prepend_buf);
664 break;
665
666 case G_LOG_LEVEL_INFO:
667 prepend = g_strdup_printf (" INFO%s%s", log_separator, prepend_buf);
668 break;
669
670 case G_LOG_LEVEL_DEBUG:
671 prepend = g_strdup_printf (" DEBUG%s%s", log_separator, prepend_buf);
672 break;
673
674 default:
675 prepend = g_strdup_printf ("UNKNOWN%s%s", log_separator, prepend_buf);
676 break;
677 }
678
679 /* If the current log entry is more severe than the specified log
680 * level, print out the message. In case MESSAGE already ends in a
681 * LF and there is not only the LF, remove the LF to avoid empty
682 * lines in the log.
683 */
684 messagelen = message ? strlen (message) : 0;
685 if (messagelen > 1 && message[messagelen - 1] == '\n')
686 messagelen--;
687 tmpstr = g_strdup_printf ("%s%s%s%s %.*s\n", log_domain ? log_domain : "",
688 log_separator, prepend, log_separator, messagelen,
689 message);
690 g_free (prepend);
691
692 if (log_level <= G_LOG_LEVEL_WARNING)
693 gvm_sentry_log (message);
694
695 gvm_log_lock ();
696 /* Output everything to stderr if logfile is NULL, an empty string or "-". */
697 if (!log_file || g_ascii_strcasecmp (log_file, "-") == 0
698 || !g_strcmp0 (log_file, ""))
699 {
700 fprintf (stderr, "%s", tmpstr);
701 fflush (stderr);
702 }
703 /* Output everything to syslog if logfile is "syslog" */
704 else if (g_ascii_strcasecmp (log_file, "syslog") == 0)
705 {
706 int facility = facility_int_from_string (syslog_facility);
707 int syslog_level = LOG_INFO;
708
709 openlog (syslog_ident, LOG_CONS | LOG_PID | LOG_NDELAY, facility);
710
711 switch (log_level)
712 {
713 case G_LOG_FLAG_FATAL:
714 syslog_level = LOG_ALERT;
715 break;
716 case G_LOG_LEVEL_ERROR:
717 syslog_level = LOG_ERR;
718 break;
719 case G_LOG_LEVEL_CRITICAL:
720 syslog_level = LOG_CRIT;
721 break;
722 case G_LOG_LEVEL_WARNING:
723 syslog_level = LOG_WARNING;
724 break;
725 case G_LOG_LEVEL_MESSAGE:
726 syslog_level = LOG_NOTICE;
727 break;
728 case G_LOG_LEVEL_INFO:
729 syslog_level = LOG_INFO;
730 break;
731 case G_LOG_LEVEL_DEBUG:
732 syslog_level = LOG_DEBUG;
733 break;
734 default:
735 syslog_level = LOG_INFO;
736 break;
737 }
738
739 /* Syslog doesn't support messages longer than 1kb. The overflow data
740 will not be logged or will be shown in the hypervisor console
741 if it runs on a virtual machine. */
742 if (messagelen > 1000)
743 {
744 int pos;
745 char *message_aux, *message_aux2;
746 char buffer[1000];
747
748 message_aux2 = g_strdup (message);
749 message_aux = message_aux2;
750 for (pos = 0; pos <= messagelen; pos = pos + sizeof (buffer) - 1)
751 {
752 memcpy (buffer, message_aux, sizeof (buffer) - 1);
753 buffer[sizeof (buffer) - 1] = '\0';
754 message_aux = &(message_aux[sizeof (buffer) - 1]);
755 syslog (syslog_level, "%s", buffer);
756 }
757 g_free (message_aux2);
758 }
759 else
760 syslog (syslog_level, "%s", message);
761
762 closelog ();
763 }
764 else
765 {
766 /* Open a channel and store it in the struct or
767 * retrieve and use an already existing channel.
768 */
769 if (channel == NULL)
770 {
771 channel = g_io_channel_new_file (log_file, "a", &error);
772 if (!channel)
773 {
774 gchar *log = g_strdup (log_file);
775 gchar *dir = dirname (log);
776
777 /* Check error. In case of the directory does not exist, it will
778 * be handle below. In other case a message is printed to the
779 * stderr since the channel is still not created/accessible.
780 */
781 if (error->code != G_FILE_ERROR_NOENT)
782 fprintf (stderr, "Can not open '%s' logfile: %s\n", log_file,
783 error->message);
784 g_error_free (error);
785
786 /* Ensure directory exists. */
787 if (g_mkdir_with_parents (dir, 0755)) /* "rwxr-xr-x" */
788 {
789 g_warning ("Failed to create log file directory %s: %s", dir,
790 strerror (errno));
791 g_free (log);
792 g_free (tmpstr);
793 g_free (prepend_buf);
794 return;
795 }
796 g_free (log);
797
798 /* Try again. */
799 error = NULL;
800 channel = g_io_channel_new_file (log_file, "a", &error);
801 if (!channel)
802 {
803 g_error ("Can not open '%s' logfile: %s", log_file,
804 error->message);
805 }
806 }
807
808 /* Store it in the struct for later use. */
809 if (log_domain_entry != NULL)
810 gvm_logging_domain_set_log_channel (log_domain_entry, channel);
811 }
812 g_io_channel_write_chars (channel, (const gchar *) tmpstr, -1, NULL,
813 &error);
814 g_io_channel_flush (channel, NULL);
815 }
817 g_free (tmpstr);
818 g_free (prepend_buf);
819}
820
832void
833log_func_for_gnutls (int level, const char *message)
834{
835 g_log ("x gnutls", G_LOG_LEVEL_INFO, "tls(%d): %s", level, message);
836}
837
847static int
849{
850 GIOChannel *channel = NULL;
851 const gchar *log_file;
852
853 log_file = gvm_logging_domain_get_log_file (log_domain_entry);
854
855 // No log file was specified, log file is empty or set to "-" then
856 // stderr will be used as default later on. See gvm_log_func.
857 if (!log_file || g_ascii_strcasecmp (log_file, "-") == 0
858 || !g_strcmp0 (log_file, ""))
859 return 0;
860
861 // If syslog is used we do not need to check the log file permissions.
862 if (g_ascii_strcasecmp (log_file, "syslog") == 0)
863 return 0;
864
865 channel = g_io_channel_new_file (log_file, "a", NULL);
866 if (!channel)
867 {
868 gchar *log = g_strdup (log_file);
869 gchar *dir = dirname (log);
870
871 /* Ensure directory exists. */
872 if (g_mkdir_with_parents (dir, 0755)) /* "rwxr-xr-x" */
873 {
874 g_free (log);
875 return -1;
876 }
877 g_free (log);
878
879 /* Try again. */
880 channel = g_io_channel_new_file (log_file, "a", NULL);
881 if (!channel)
882 return -1;
883 }
884 g_io_channel_shutdown (channel, TRUE, NULL);
885 g_io_channel_unref (channel);
886 return 0;
887}
888
897void
898set_log_tz (const gchar *tz)
899{
900 g_free (log_tz);
901 log_tz = tz ? g_strdup (tz) : NULL;
902}
903
904static int
905setup_log_handlers_internal (GSList *gvm_log_config_list, GLogFunc log_func,
906 GLogFunc default_log_func,
907 GLogFunc default_domain_log_func)
908{
909 GSList *log_domain_list_tmp;
910 int err;
911 int ret = 0;
912
913 if (gvm_log_config_list != NULL)
914 {
915 /* Go to the head of the list. */
916 log_domain_list_tmp = (GSList *) gvm_log_config_list;
917
918 while (log_domain_list_tmp != NULL)
919 {
920 gvm_logging_domain_t *log_domain_entry;
921
922 /* Get the list data which is an gvm_logging_domain_t struct. */
923 log_domain_entry = log_domain_list_tmp->data;
924
925 err = check_log_file (log_domain_entry);
926 if (err)
927 {
928 ret = -1;
929 /* Go to the next item. */
930 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
931 continue;
932 }
933
934 if (g_ascii_strcasecmp (
935 gvm_logging_domain_get_log_domain (log_domain_entry), "*"))
936 {
937 g_log_set_handler (
938 gvm_logging_domain_get_log_domain (log_domain_entry),
939 (GLogLevelFlags) (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO
940 | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING
941 | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR
942 | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
943 (GLogFunc) log_func, gvm_log_config_list);
944 }
945 else
946 {
947 g_log_set_default_handler (default_log_func, gvm_log_config_list);
948 }
949
950 /* Go to the next item. */
951 log_domain_list_tmp = g_slist_next (log_domain_list_tmp);
952 }
953 }
954 g_log_set_handler (
955 "",
956 (GLogLevelFlags) (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE
957 | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL
958 | G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL
959 | G_LOG_FLAG_RECURSION),
960 default_domain_log_func, gvm_log_config_list);
961
962 return ret;
963}
964
975int
976setup_log_handlers (GSList *gvm_log_config_list)
977{
978 return setup_log_handlers_internal (gvm_log_config_list, gvm_log_func,
980}
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:423
void free_log_reference(void)
Free the log reference object.
Definition logging.c:408
char * get_log_reference(void)
Get the log reference object.
Definition logging.c:396
void set_log_reference(char *ref)
Set the log reference object.
Definition logging.c:381
static int check_log_file(gvm_logging_domain_t *log_domain_entry)
Check permissions of log file and log file directory.
Definition logging.c:848
void log_func_for_gnutls(int level, const char *message)
This function logs debug messages from gnutls.
Definition logging.c:833
void free_log_configuration(GSList *log_domain_list)
Frees all resources loaded by the config loader.
Definition logging.c:283
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:360
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:348
static GMutex * logger_mutex
Definition logging.c:329
void set_log_tz(const gchar *tz)
Set the log timezone.
Definition logging.c:898
static void gvm_log_lock_init(void)
Initialize logger_mutex mutex if it was not done before.
Definition logging.c:335
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:365
int setup_log_handlers(GSList *gvm_log_config_list)
Sets up routing of logdomains to log handlers.
Definition logging.c:976
void gvm_log_silent(const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer gvm_log_config_list)
Returns immediately.
Definition logging.c:319
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:905
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.
Logging stores the parameters loaded from a log configuration.