From 0b5c70550a3f9c62478682d27e9203ef313298c0 Mon Sep 17 00:00:00 2001 From: ge Date: Sat, 3 Jan 2026 15:10:39 +0300 Subject: [PATCH] init --- .editorconfig | 8 +++++ .gitattributes | 8 +++++ .gitignore | 24 +++++++++++++ README.md | 18 ++++++++++ UNLICENSE | 22 ++++++++++++ examples/log.v | 12 +++++++ syslog.c.v | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ v.mod | 7 ++++ 8 files changed, 193 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 UNLICENSE create mode 100644 examples/log.v create mode 100644 syslog.c.v create mode 100644 v.mod diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..01072ca --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.v] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9a98968 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +* text=auto eol=lf +*.bat eol=crlf + +*.v linguist-language=V +*.vv linguist-language=V +*.vsh linguist-language=V +v.mod linguist-language=V +.vdocignore linguist-language=ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a00ab5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Binaries for programs and plugins +main +syslog +*.exe +*.exe~ +*.so +*.dylib +*.dll + +# Ignore binary output folders +bin/ + +# Ignore common editor/system specific metadata +.DS_Store +.idea/ +.vscode/ +*.iml + +# ENV +.env + +# vweb and database +*.db +*.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..87b2fd4 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Write Messages into System Journal + +`syslog` module is a thin wrapper around syslog C API. Read the +[syslog(3)](https://www.man7.org/linux/man-pages/man3/syslog.3.html) +manual page for details. + +Basic usage: + +```v +import syslog + +mut log := syslog.open() +defer { + log.close() +} + +log.send(.info, 'Hello, World!') +``` diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..c91541e --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,22 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and +successors. We intend this dedication to be an overt act of relinquishment in +perpetuity of all present and future rights to this software under copyright +law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/examples/log.v b/examples/log.v new file mode 100644 index 0000000..c54f83b --- /dev/null +++ b/examples/log.v @@ -0,0 +1,12 @@ +import syslog + +fn main() { + mut log := syslog.open(ident: 'helloworld') + defer { + log.close() + } + log.send(.info, 'hello from V!') + log.send(.err, 'error message from V!') + + eprintln('On Linux run `journalctl -t helloworld` to see the result.') +} diff --git a/syslog.c.v b/syslog.c.v new file mode 100644 index 0000000..cd39bcf --- /dev/null +++ b/syslog.c.v @@ -0,0 +1,94 @@ +module syslog + +#include + +fn C.openlog(&char, i32, i32) +fn C.syslog(i32, &char, ...voidptr) +fn C.closelog() + +/* +Values for option +*/ + +pub const log_cons = C.LOG_CONS +pub const log_ndelay = C.LOG_NDELAY +pub const log_nowait = C.LOG_NOWAIT +pub const log_odelay = C.LOG_ODELAY +pub const log_perror = C.LOG_PERROR +pub const log_pid = C.LOG_PID + +/* +Values for facility +*/ + +pub const log_auth = C.LOG_AUTH +pub const log_authpriv = C.LOG_AUTHPRIV +pub const log_cron = C.LOG_CRON +pub const log_daemon = C.LOG_DAEMON +pub const log_ftp = C.LOG_FTP +pub const log_kern = C.LOG_KERN +pub const log_local0 = C.LOG_LOCAL0 +pub const log_local1 = C.LOG_LOCAL1 +pub const log_local2 = C.LOG_LOCAL2 +pub const log_local3 = C.LOG_LOCAL3 +pub const log_local4 = C.LOG_LOCAL4 +pub const log_local5 = C.LOG_LOCAL5 +pub const log_local6 = C.LOG_LOCAL6 +pub const log_local7 = C.LOG_LOCAL7 +pub const log_lpr = C.LOG_LPR +pub const log_mail = C.LOG_MAIL +pub const log_news = C.LOG_NEWS +pub const log_syslog = C.LOG_SYSLOG +pub const log_user = C.LOG_USER +pub const log_uucp = C.LOG_UUCP + +/* +Values for level +*/ + +pub enum Level { + emerg = C.LOG_EMERG + alert = C.LOG_ALERT + crit = C.LOG_CRIT + err = C.LOG_ERR + warn = C.LOG_WARNING + notice = C.LOG_NOTICE + info = C.LOG_INFO + debug = C.LOG_DEBUG +} + +@[params] +pub struct OpenParams { +pub: + ident string + option int + facility int = log_user +} + +// open opens a system log for program. See also openlog(3). +pub fn open(p OpenParams) Syslog { + C.openlog(&char(p.ident.str), i32(p.option), i32(p.facility)) + return Syslog{ + OpenParams: p + is_opened: true + } +} + +struct Syslog { + OpenParams +pub mut: + is_opened bool +} + +// send sends a message of level `lvl` to system journal. See also syslog(3). +pub fn (s Syslog) send(lvl Level, msg string) { + C.syslog(i32(lvl), c'%s', &char(msg.str)) +} + +// close closes the system journal. See also closelog(3). +pub fn (mut s Syslog) close() { + if s.is_opened { + C.closelog() + s.is_opened = false + } +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..43b2945 --- /dev/null +++ b/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'syslog' + description: 'Write messages into system journal' + version: '0.1.0' + license: 'Unlicense' + dependencies: [] +}