int main(int argc, char *argv[]) { JournalFile *one, *two, *three; char t[] = "/tmp/journal-stream-XXXXXX"; unsigned i; _cleanup_journal_close_ sd_journal *j = NULL; char *z; const void *data; size_t l; log_set_max_level(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &one) == 0); assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &two) == 0); assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &three) == 0); for (i = 0; i < N_ENTRIES; i++) { char *p, *q; dual_timestamp ts; struct iovec iovec[2]; dual_timestamp_get(&ts); assert_se(asprintf(&p, "NUMBER=%u", i) >= 0); iovec[0].iov_base = p; iovec[0].iov_len = strlen(p); assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0); iovec[1].iov_base = q; iovec[1].iov_len = strlen(q); if (i % 10 == 0) assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0); else { if (i % 3 == 0) assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0); assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0); } free(p); free(q); } journal_file_close(one); journal_file_close(two); journal_file_close(three); assert_se(sd_journal_open_directory(&j, t, 0) >= 0); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); SD_JOURNAL_FOREACH_BACKWARDS(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } SD_JOURNAL_FOREACH(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } sd_journal_flush_matches(j); verify_contents(j, 1); printf("NEXT TEST\n"); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 5); printf("NEXT TEST\n"); sd_journal_flush_matches(j); assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 0); assert_se(sd_journal_query_unique(j, "NUMBER") >= 0); SD_JOURNAL_FOREACH_UNIQUE(j, data, l) printf("%.*s\n", (int) l, (const char*) data); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); return 0; }
static int journal_flush_matches (lua_State *L) { sd_journal *j = check_journal(L, 1); sd_journal_flush_matches(j); lua_pushboolean(L, 1); return 1; }
QStringList UnitModel::getLastJrnlEntries(QString unit) const { QString match1, match2; int r, jflags; QStringList reply; const void *data; size_t length; uint64_t time; sd_journal *journal; if (!userBus.isEmpty()) { match1 = QString("USER_UNIT=" + unit); jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_CURRENT_USER); } else { match1 = QString("_SYSTEMD_UNIT=" + unit); match2 = QString("UNIT=" + unit); jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM); } r = sd_journal_open(&journal, jflags); if (r != 0) { qDebug() << "Failed to open journal"; return reply; } sd_journal_flush_matches(journal); r = sd_journal_add_match(journal, match1.toUtf8(), 0); if (r != 0) return reply; if (!match2.isEmpty()) { sd_journal_add_disjunction(journal); r = sd_journal_add_match(journal, match2.toUtf8(), 0); if (r != 0) return reply; } r = sd_journal_seek_tail(journal); if (r != 0) return reply; // Fetch the last 5 entries for (int i = 0; i < 5; ++i) { r = sd_journal_previous(journal); if (r == 1) { QString line; // Get the date and time r = sd_journal_get_realtime_usec(journal, &time); if (r == 0) { QDateTime date; date.setMSecsSinceEpoch(time/1000); line.append(date.toString("yyyy.MM.dd hh:mm")); } // Color messages according to priority r = sd_journal_get_data(journal, "PRIORITY", &data, &length); if (r == 0) { int prio = QString::fromUtf8((const char *)data, length).section('=',1).toInt(); if (prio <= 3) line.append("<span style='color:tomato;'>"); else if (prio == 4) line.append("<span style='color:khaki;'>"); else line.append("<span style='color:palegreen;'>"); } // Get the message itself r = sd_journal_get_data(journal, "MESSAGE", &data, &length); if (r == 0) { line.append(": " + QString::fromUtf8((const char *)data, length).section('=',1) + "</span>"); if (line.length() > 195) line = QString(line.left(195) + "..." + "</span>"); reply << line; } } else // previous failed, no more entries return reply; } sd_journal_close(journal); return reply; }