Send output to systemd-journal from C++ program
Asked Answered
I

2

7

I have some Python programs that I'd like to convert to C++11 code as a way of learning the language. The target platform is Debian, kernel version >=4.2 (i.e. systemd).

In Python I can log debugging/logging information to the system log using the syslog module. In C I would be able to #include <syslog.h>. But, from this article I understand that for the current C++ implementation there is no such implementation (anymore). If the program is a service (or daemon?) this would apparently happen automagically although I don't gather how the message priority would be distinguished.

I've Googled around but can't really find a solution to my problem: How do I send information to the system log from within a C++11 program?

EDIT: (throws hands in the air and gives up ;-) ) Since there doesn't seem to be a native solution to my problem and I'm still in the starting phase of learning C++, I now consider to use C i.s.o. C++ since C natively supports syslog with syslog.h.

Interdictory answered 3/11, 2015 at 17:9 Comment(2)
This looks like you were looking for a reason to not learn C++ and learn C instead.Falconet
ANSI C doesn't "natively" support syslog, systemd-journal, nor a host of other APIs. it is what is called a "system api", and this particular system api exposes "C bindings". those C bindings are then used by literally every language that wishes to use syslog. this is the case for multiple platforms not just linux since, decades ago, system APIs conformed around the use of C stack semantics and linker logic (it was the dominant system programming language for several decades, arguably still is.) so, any "native" solution (python, c++, other) still uses the underlying system api and C header.Shamanism
H
8

You can also send log data to journald via the sd_journal_send(3) or sd_journal_print(3) c-functions.

http://0pointer.de/blog/projects/journal-submit.html gives more info and examples on this topic.

Hydrophobia answered 31/5, 2016 at 13:9 Comment(0)
H
1

I would use SysLogAppender of Log4Cplus. But the question and therefore the answer are opinion-based etc etc

Hyetography answered 3/11, 2015 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.