You are not logged in.
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
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
Nice, Thank You.
Offline
Please remember to mark your thread [SOLVED] (edit the title of your first post).
Offline