nixio: solaris does not recognize LOG_PERROR
[project/luci.git] / libs / nixio / src / syslog.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "nixio.h"
20 #include <string.h>
21 #include <syslog.h>
22
23
24 static int nixio_openlog(lua_State *L) {
25         int option = 0, facility = LOG_USER;
26
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")) {
32                         option |= LOG_CONS;
33                 } else if (!strcmp(flag, "nowait")) {
34                         option |= LOG_NOWAIT;
35                 } else if (!strcmp(flag, "pid")) {
36                         option |= LOG_PID;
37                 } else if (!strcmp(flag, "perror")) {
38 #ifdef LOG_PERROR
39                         option |= LOG_PERROR;
40 #endif
41                 } else if (!strcmp(flag, "ndelay")) {
42                         option |= LOG_NDELAY;
43                 } else if (!strcmp(flag, "odelay")) {
44                         option |= LOG_ODELAY;
45                 } else {
46                         return luaL_argerror(L, i,
47                                 "supported values: cons, nowait, pid, perror, ndelay, odelay");
48                 }
49         }
50
51         openlog(ident, option, facility);
52         return 0;
53 }
54
55 static int nixio_closelog(lua_State *L) {
56         closelog();
57         return 0;
58 }
59
60 static int nixio__syslogmasg(lua_State *L, int dolog) {
61         int priority;
62
63         const char *flag = luaL_checkstring(L, 1);
64         if (!strcmp(flag, "emerg")) {
65                 priority = LOG_EMERG;
66         } else if (!strcmp(flag, "alert")) {
67                 priority = LOG_ALERT;
68         } else if (!strcmp(flag, "crit")) {
69                 priority = LOG_CRIT;
70         } else if (!strcmp(flag, "err")) {
71                 priority = LOG_ERR;
72         } else if (!strcmp(flag, "warning")) {
73                 priority = LOG_WARNING;
74         } else if (!strcmp(flag, "notice")) {
75                 priority = LOG_NOTICE;
76         } else if (!strcmp(flag, "info")) {
77                 priority = LOG_INFO;
78         } else if (!strcmp(flag, "debug")) {
79                 priority = LOG_DEBUG;
80         } else {
81                 return luaL_argerror(L, 1, "supported values: emerg, alert, crit, err, "
82                                 "warning, notice, info, debug");
83         }
84
85         if (dolog) {
86                 const char *msg = luaL_checkstring(L, 2);
87                 syslog(priority, "%s", msg);
88         } else {
89                 setlogmask(LOG_UPTO(priority));
90         }
91         return 0;
92 }
93
94 static int nixio_setlogmask(lua_State *L) {
95         return nixio__syslogmasg(L, 0);
96 }
97
98 static int nixio_syslog(lua_State *L) {
99         return nixio__syslogmasg(L, 1);
100 }
101
102 /* module table */
103 static const luaL_reg R[] = {
104         {"openlog",             nixio_openlog},
105         {"syslog",              nixio_syslog},
106         {"setlogmask",  nixio_setlogmask},
107         {"closelog",    nixio_closelog},
108         {NULL,                  NULL}
109 };
110
111 void nixio_open_syslog(lua_State *L) {
112         luaL_register(L, NULL, R);
113 }