Rework LuCI build system
[project/luci.git] / libs / luci-lib-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
22 #ifndef __WINNT__
23 #include <syslog.h>
24
25
26 static int nixio_openlog(lua_State *L) {
27         int option = 0, facility = LOG_USER;
28
29         const char *ident = luaL_optstring(L, 1, "nixio");
30         const int j = lua_gettop(L);
31         for (int i=2; i<=j; i++) {
32                 const char *flag = luaL_checkstring(L, i);
33                 if (!strcmp(flag, "cons")) {
34                         option |= LOG_CONS;
35                 } else if (!strcmp(flag, "nowait")) {
36                         option |= LOG_NOWAIT;
37                 } else if (!strcmp(flag, "pid")) {
38                         option |= LOG_PID;
39                 } else if (!strcmp(flag, "perror")) {
40 #ifdef LOG_PERROR
41                         option |= LOG_PERROR;
42 #endif
43                 } else if (!strcmp(flag, "ndelay")) {
44                         option |= LOG_NDELAY;
45                 } else if (!strcmp(flag, "odelay")) {
46                         option |= LOG_ODELAY;
47                 } else {
48                         return luaL_argerror(L, i,
49                                 "supported values: cons, nowait, pid, perror, ndelay, odelay");
50                 }
51         }
52
53         openlog(ident, option, facility);
54         return 0;
55 }
56
57 static int nixio_closelog(lua_State *L) {
58         closelog();
59         return 0;
60 }
61
62 static int nixio__syslogmask(lua_State *L, int dolog) {
63         int priority;
64
65         const char *flag = luaL_checkstring(L, 1);
66         if (!strcmp(flag, "emerg")) {
67                 priority = LOG_EMERG;
68         } else if (!strcmp(flag, "alert")) {
69                 priority = LOG_ALERT;
70         } else if (!strcmp(flag, "crit")) {
71                 priority = LOG_CRIT;
72         } else if (!strcmp(flag, "err")) {
73                 priority = LOG_ERR;
74         } else if (!strcmp(flag, "warning")) {
75                 priority = LOG_WARNING;
76         } else if (!strcmp(flag, "notice")) {
77                 priority = LOG_NOTICE;
78         } else if (!strcmp(flag, "info")) {
79                 priority = LOG_INFO;
80         } else if (!strcmp(flag, "debug")) {
81                 priority = LOG_DEBUG;
82         } else {
83                 return luaL_argerror(L, 1, "supported values: emerg, alert, crit, err, "
84                                 "warning, notice, info, debug");
85         }
86
87         if (dolog) {
88                 const char *msg = luaL_checkstring(L, 2);
89                 syslog(priority, "%s", msg);
90         } else {
91                 setlogmask(LOG_UPTO(priority));
92         }
93         return 0;
94 }
95
96 static int nixio_setlogmask(lua_State *L) {
97         return nixio__syslogmask(L, 0);
98 }
99
100 static int nixio_syslog(lua_State *L) {
101         return nixio__syslogmask(L, 1);
102 }
103
104 /* module table */
105 static const luaL_reg R[] = {
106         {"openlog",             nixio_openlog},
107         {"syslog",              nixio_syslog},
108         {"setlogmask",  nixio_setlogmask},
109         {"closelog",    nixio_closelog},
110         {NULL,                  NULL}
111 };
112
113 void nixio_open_syslog(lua_State *L) {
114         luaL_register(L, NULL, R);
115 }
116
117 #else /* __WINNT__ */
118
119 void nixio_open_syslog(lua_State *L) {
120 }
121
122 #endif /* __WINNT__ */