Exemple #1
0
static int journal_previous (lua_State *L) {
	sd_journal *j = check_journal(L, 1);
	int err = sd_journal_previous(j);
	if (err < 0) return handle_error(L, -err);
	lua_pushboolean(L, err);
	return 1;
}
static void test_skip(void (*setup)(void))
{
    char t[] = "/tmp/journal-skip-XXXXXX";
    sd_journal *j;
    int r;

    assert_se(mkdtemp(t));
    assert_se(chdir(t) >= 0);

    setup();

    /* Seek to head, iterate down.
     */
    assert_ret(sd_journal_open_directory(&j, t, 0));
    assert_ret(sd_journal_seek_head(j));
    assert_ret(sd_journal_next(j));
    test_check_numbers_down(j, 4);
    sd_journal_close(j);

    /* Seek to tail, iterate up.
     */
    assert_ret(sd_journal_open_directory(&j, t, 0));
    assert_ret(sd_journal_seek_tail(j));
    assert_ret(sd_journal_previous(j));
    test_check_numbers_up(j, 4);
    sd_journal_close(j);

    /* Seek to tail, skip to head, iterate down.
     */
    assert_ret(sd_journal_open_directory(&j, t, 0));
    assert_ret(sd_journal_seek_tail(j));
    assert_ret(r = sd_journal_previous_skip(j, 4));
    assert_se(r == 4);
    test_check_numbers_down(j, 4);
    sd_journal_close(j);

    /* Seek to head, skip to tail, iterate up.
     */
    assert_ret(sd_journal_open_directory(&j, t, 0));
    assert_ret(sd_journal_seek_head(j));
    assert_ret(r = sd_journal_next_skip(j, 4));
    assert_se(r == 4);
    test_check_numbers_up(j, 4);
    sd_journal_close(j);

    log_info("Done...");

    if (arg_keep)
        log_info("Not removing %s", t);
    else {
        journal_directory_vacuum(".", 3000000, 0, 0, NULL);

        assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
    }

    puts("------------------------------------------------------------");
}
static void test_check_numbers_up (sd_journal *j, int count) {
        for (int i = count; i >= 1; i--) {
                int r;
                test_check_number(j, i);
                assert_ret(r = sd_journal_previous(j));
                if (i == 1)
                        assert_se(r == 0);
                else
                        assert_se(r == 1);
        }

}
Exemple #4
0
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;
}