Git Product home page Git Product logo

ocaml-systemd's Introduction

ocaml-systemd Build Status

OCaml library allowing interaction with systemd and journald

Journald Usage

open Journald

let () = journal_send_message Priority.INFO "Hello";

	 (* compile and link its compilation units with option -g to get CODE_FILE and CODE_LINE entries*)
	 journal_send_message_loc Priority.INFO "Hello with location";
	 (* compile and link its compilation units with option -g *)
	 journal_send ["CUSTOM_FIELD", "CUSTOM_VALUE"];
	 journal_send_loc ["CUSTOM_FIELD", "CUSTOM_VALUE with location"];

Performance

To get callstack info (required for the implicit CODE_FILE and CODE_LINE entries) you need to build your compilation units with debugging information.

Getting the callstack at runtime also results in an performance overhead, so there a 2 groups of journal functions:

Journal functions with location info

val journal_send_message_loc : Priority.t -> string -> unit
val journal_send_loc : (string * string) list -> unit

Journal functions without location info

val journal_send : (string * string) list -> unit
val journal_send_message : Priority.t -> string -> unit

Socket activation

No need for forking the process, binding/listening the sockets.

Also support for systemd watchdog functionality.

Complete Example: Lwt echo server using socket activation

open Daemon
open Daemon.State
open Lwt
open Lwt_unix
open Journald

let echo_server conn_fd =
  let in_channel = Lwt_io.of_fd ~mode:Lwt_io.input conn_fd in
  let out_channel = Lwt_io.of_fd ~mode:Lwt_io.output conn_fd in
  finalize (fun () -> Lwt_stream.iter_s (Lwt_io.write_line out_channel) (Lwt_io.read_lines in_channel))
	   (fun () -> close conn_fd)

let rec accept fd =
  Lwt_unix.accept (Lwt_unix.of_unix_file_descr fd)
  >>= (fun (conn_fd, _) ->
       async (fun _ -> echo_server conn_fd);
       accept fd)

let _ =
  (* Notify systemd software watchdog every second *)
  Lwt_engine.on_timer 1.0 true (fun _ -> notify Watchdog|>ignore);
  notify Ready |> ignore;
  match listen_fds () with
  | [] -> journal_send_message Priority.CRIT "No file descriptors passed by the system manager"
  | fd::_ -> journal_send_message Priority.INFO "socket activation succeded";
	     accept fd |> Lwt_main.run

ocaml-systemd's People

Contributors

gaborigloi avatar jonludlam avatar juergenhoetzel avatar psafont avatar reynir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ocaml-systemd's Issues

dune

Could you please switch to dune?

You could provide a way to explicitly pass locations

Using the calltrace information is a fragile way to get location information -- in particular it requires programs to be compiled with -g. You should consider also providing an API that allows user to pass a location explicitly (and I would argue that the short name on the "path of least effort" should be this function, with eg. journal_send_implicit for the callstack-inspecting version).

Locations can be passed manually after having been inserted automatically by a syntactic processing pass (eg. one could think of a [%located journal_send] foo bar extension).

As of 4.02, OCaml provides the (admittedly under-documented, and of debatable elegance) __LOC__, __FILE__ and __LINE__ macros. so you could just encourage users to consistently write journal_send __LOC__ ... without the syntactic sugar.

Note that beside removing the requirement to consistently use -g, this gives user more control on the reported location. In particular, if (as a library user) you define auxiliary functions to perform the logging, you may want to report the location of the caller function instead, and letting user explicitly manipulate location as first-class values gives them the flexibility to decide this.

Could you tag the current state?

It seems that the current state is quite stable. Could you tag or release it? We are currently downloading the HEAD of branch master but this is of course not a stable reference. Of course, we could clone this repository and add the tag ourselves but we prefer if you had a release that we could refer to.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.