2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 static int nixio_openlog(lua_State *L) {
25 int option = 0, facility = LOG_USER;
27 const char *ident = luaL_optstring(L, 1, "nixio");
28 const int j = lua_gettop(L);
29 for (int i=2; i<=j; i++) {
30 const char *flag = luaL_checkstring(L, i);
31 if (!strcmp(flag, "cons")) {
33 } else if (!strcmp(flag, "nowait")) {
35 } else if (!strcmp(flag, "pid")) {
37 } else if (!strcmp(flag, "perror")) {
39 } else if (!strcmp(flag, "ndelay")) {
41 } else if (!strcmp(flag, "odelay")) {
44 return luaL_argerror(L, i,
45 "supported values: cons, nowait, pid, perror, ndelay, odelay");
49 openlog(ident, option, facility);
53 static int nixio_closelog(lua_State *L) {
58 static int nixio__syslogmasg(lua_State *L, int dolog) {
61 const char *flag = luaL_checkstring(L, 1);
62 if (!strcmp(flag, "emerg")) {
64 } else if (!strcmp(flag, "alert")) {
66 } else if (!strcmp(flag, "crit")) {
68 } else if (!strcmp(flag, "err")) {
70 } else if (!strcmp(flag, "warning")) {
71 priority = LOG_WARNING;
72 } else if (!strcmp(flag, "notice")) {
73 priority = LOG_NOTICE;
74 } else if (!strcmp(flag, "info")) {
76 } else if (!strcmp(flag, "debug")) {
79 return luaL_argerror(L, 1, "supported values: emerg, alert, crit, err, "
80 "warning, notice, info, debug");
84 const char *msg = luaL_checkstring(L, 2);
85 syslog(priority, msg);
87 setlogmask(LOG_UPTO(priority));
92 static int nixio_setlogmask(lua_State *L) {
93 return nixio__syslogmasg(L, 0);
96 static int nixio_syslog(lua_State *L) {
97 return nixio__syslogmasg(L, 1);
101 static const luaL_reg R[] = {
102 {"openlog", nixio_openlog},
103 {"syslog", nixio_syslog},
104 {"setlogmask", nixio_setlogmask},
105 {"closelog", nixio_closelog},
109 void nixio_open_syslog(lua_State *L) {
110 luaL_register(L, NULL, R);