You are not logged in.

#1 2019-12-05 02:55:53

Rob.2019
Member
Registered: 2019-10-12
Posts: 7

How can I implement this code? Is there another way to read .journal?

Greetings,

I am trying to read in any journalctl file, I tried to run the example code at the bottom. I am thinking I may need some add on c libraries or I need to add some compile flags??

I compiled as: gcc readLog1.c -o readLog1

Got these errors :
undefined reference to `sd_journal_open'
undefined reference to `sd_journal_seek_head'
undefined reference to `sd_journal_get_data'
undefined reference to `sd_journal_next'
undefined reference to `sd_journal_close'

#include <stdio.h>
#include <string.h>
#include <systemd/sd-journal.h>

int main(int argc, char *argv[]) {
  int r;
  sd_journal *j;
  r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
  if (r < 0) {
    fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
    return 1;
  }
  SD_JOURNAL_FOREACH(j) {
    const char *d;
    size_t l;

    r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
    if (r < 0) {
      fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
      continue;
    }

    printf("%.*s\n", (int) l, d);
  }
  sd_journal_close(j);
  return 0;
}

Offline

#2 2019-12-05 03:39:54

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,771

Re: How can I implement this code? Is there another way to read .journal?

You did not link the library.  Not sure what the name of the library for sd_jounral is, but you need a -l in your compile directive for it.

Edit:  It is libsystemd, so compile it with: gcc readLog1.c -o readLog1 -lsystemd


ewaller@odin/home/ewaller[130] % cat test.c
#include <stdio.h>
#include <string.h>
#include <systemd/sd-journal.h>

int main(int argc, char *argv[]) {
  int r;
  sd_journal *j;
  r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
  if (r < 0) {
    fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
    return 1;
  }
  SD_JOURNAL_FOREACH(j) {
    const char *d;
    size_t l;

    r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
    if (r < 0) {
      fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
      continue;
    }

    printf("%.*s\n", (int) l, d);
  }
  sd_journal_close(j);
  return 0;
}
ewaller@odin/home/ewaller % gcc test.c -o readLog1 -lsystemd
ewaller@odin/home/ewaller % ./readLog1  | head
MESSAGE=microcode: microcode updated early to revision 0x27, date = 2019-02-26
MESSAGE=Linux version 5.1.9-arch1-1-ARCH (builduser@heftig-25959) (gcc version 8.3.0 (GCC)) #1 SMP PREEMPT Tue Jun 11 16:18:09 UTC 2019
MESSAGE=Command line: initrd=\intel-ucode.img initrd=\initramfs-linux.img root=/dev/sda6 rw acpi_backlight=native
MESSAGE=KERNEL supported cpus:
MESSAGE=  Intel GenuineIntel
MESSAGE=  AMD AuthenticAMD
MESSAGE=  Hygon HygonGenuine
MESSAGE=  Centaur CentaurHauls
MESSAGE=x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
MESSAGE=x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
ewaller@odin/home/ewaller % 

Last edited by ewaller (2019-12-05 03:55:31)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2019-12-07 01:00:22

Rob.2019
Member
Registered: 2019-10-12
Posts: 7

Re: How can I implement this code? Is there another way to read .journal?

Nice, Thank You.

Offline

#4 2019-12-07 14:20:14

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,645

Re: How can I implement this code? Is there another way to read .journal?

Please remember to mark your thread [SOLVED] (edit the title of your first post).

Offline

Board footer

Powered by FluxBB