| 1 |
/* sysprof-system-logs.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#include <glib/gi18n.h> |
| 26 |
#include <glib/gstdio.h> |
| 27 |
|
| 28 |
#include "sysprof-system-logs.h" |
| 29 |
#include "sysprof-instrument-private.h" |
| 30 |
#include "sysprof-recording-private.h" |
| 31 |
|
| 32 |
#if HAVE_LIBSYSTEMD |
| 33 |
# include "sysprof-journald-source.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
struct _SysprofSystemLogs |
| 37 |
{ |
| 38 |
SysprofInstrument parent_instance; |
| 39 |
}; |
| 40 |
|
| 41 |
struct _SysprofSystemLogsClass |
| 42 |
{ |
| 43 |
SysprofInstrumentClass parent_class; |
| 44 |
}; |
| 45 |
|
| 46 |
G_DEFINE_FINAL_TYPE (SysprofSystemLogs, sysprof_system_logs, SYSPROF_TYPE_INSTRUMENT) |
| 47 |
|
| 48 |
#if HAVE_LIBSYSTEMD |
| 49 |
static char * |
| 50 |
journal_get_data (sd_journal *journal, |
| 51 |
const char *field) |
| 52 |
{ |
| 53 |
gconstpointer data = NULL; |
| 54 |
const char *endptr; |
| 55 |
const char *eq; |
| 56 |
gsize length; |
| 57 |
int ret; |
| 58 |
|
| 59 |
g_assert (journal != NULL); |
| 60 |
g_assert (field != NULL); |
| 61 |
|
| 62 |
if ((ret = sd_journal_get_data (journal, field, &data, &length)) < 0) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
endptr = (const char *)data + length; |
| 66 |
if (!(eq = memchr (data, '=', length))) |
| 67 |
return NULL; |
| 68 |
|
| 69 |
eq++; |
| 70 |
|
| 71 |
return g_strndup (eq, endptr - eq); |
| 72 |
} |
| 73 |
|
| 74 |
static gboolean |
| 75 |
sysprof_system_logs_callback (SysprofCaptureWriter *writer, |
| 76 |
sd_journal *journal) |
| 77 |
{ |
| 78 |
g_autofree char *message = NULL; |
| 79 |
g_autofree char *priority = NULL; |
| 80 |
sd_id128_t boot_id; |
| 81 |
guint64 usec; |
| 82 |
int severity = 0; |
| 83 |
|
| 84 |
g_assert (journal != NULL); |
| 85 |
g_assert (writer != NULL); |
| 86 |
|
| 87 |
if (sd_journal_get_monotonic_usec (journal, &usec, &boot_id) != 0 || |
| 88 |
!(message = journal_get_data (journal, "MESSAGE"))) |
| 89 |
return G_SOURCE_CONTINUE; |
| 90 |
|
| 91 |
priority = journal_get_data (journal, "PRIORITY"); |
| 92 |
|
| 93 |
if (priority != NULL) |
| 94 |
{ |
| 95 |
switch (priority[0]) |
| 96 |
{ |
| 97 |
case '0': |
| 98 |
case '1': |
| 99 |
severity = G_LOG_LEVEL_ERROR; |
| 100 |
break; |
| 101 |
|
| 102 |
case '2': |
| 103 |
severity = G_LOG_LEVEL_CRITICAL; |
| 104 |
break; |
| 105 |
|
| 106 |
case '3': |
| 107 |
case '4': |
| 108 |
severity = G_LOG_LEVEL_WARNING; |
| 109 |
break; |
| 110 |
|
| 111 |
default: |
| 112 |
case '5': |
| 113 |
severity = G_LOG_LEVEL_MESSAGE; |
| 114 |
break; |
| 115 |
|
| 116 |
case '6': |
| 117 |
severity = G_LOG_LEVEL_INFO; |
| 118 |
break; |
| 119 |
|
| 120 |
case '7': |
| 121 |
severity = G_LOG_LEVEL_DEBUG; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
sysprof_capture_writer_add_log (writer, usec*1000, -1, -1, severity, "System Log", message); |
| 127 |
|
| 128 |
return G_SOURCE_CONTINUE; |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
#if HAVE_LIBSYSTEMD |
| 133 |
static DexFuture * |
| 134 |
sysprof_system_logs_record_finished (DexFuture *future, |
| 135 |
gpointer user_data) |
| 136 |
{ |
| 137 |
GSource *source = user_data; |
| 138 |
g_source_destroy (source); |
| 139 |
return dex_future_new_for_boolean (TRUE); |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
static DexFuture * |
| 144 |
sysprof_system_logs_record (SysprofInstrument *instrument, |
| 145 |
SysprofRecording *recording, |
| 146 |
GCancellable *cancellable) |
| 147 |
{ |
| 148 |
#if HAVE_LIBSYSTEMD |
| 149 |
SysprofCaptureWriter *writer; |
| 150 |
GSource *source; |
| 151 |
|
| 152 |
g_assert (SYSPROF_IS_SYSTEM_LOGS (instrument)); |
| 153 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 154 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 155 |
|
| 156 |
writer = _sysprof_recording_writer (recording); |
| 157 |
|
| 158 |
source = sysprof_journald_source_new ((SysprofJournaldSourceFunc)sysprof_system_logs_callback, |
| 159 |
sysprof_capture_writer_ref (writer), |
| 160 |
(GDestroyNotify)sysprof_capture_writer_unref); |
| 161 |
g_source_set_static_name (source, "[sysprof-journald]"); |
| 162 |
g_source_set_priority (source, G_PRIORITY_LOW); |
| 163 |
g_source_attach (source, NULL); |
| 164 |
|
| 165 |
return dex_future_finally (dex_cancellable_new_from_cancellable (cancellable), |
| 166 |
sysprof_system_logs_record_finished, |
| 167 |
source, |
| 168 |
(GDestroyNotify)g_source_unref); |
| 169 |
#else |
| 170 |
_sysprof_recording_diagnostic (recording, |
| 171 |
_("System Logs"), |
| 172 |
_("Recording system logs is not supported on your platform.")); |
| 173 |
return dex_future_new_for_boolean (TRUE); |
| 174 |
#endif |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
sysprof_system_logs_class_init (SysprofSystemLogsClass *klass) |
| 179 |
{ |
| 180 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 181 |
|
| 182 |
instrument_class->record = sysprof_system_logs_record; |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
sysprof_system_logs_init (SysprofSystemLogs *self) |
| 187 |
{ |
| 188 |
} |
| 189 |
|
| 190 |
SysprofInstrument * |
| 191 |
sysprof_system_logs_new (void) |
| 192 |
{ |
| 193 |
return g_object_new (SYSPROF_TYPE_SYSTEM_LOGS, NULL); |
| 194 |
} |