1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi/native_api.h"
17 #include <cerrno>
18 #include <cstdarg>
19 #include <syslog.h>
20 #define PARAM_0 0
21 #define ONEVAL (-1)
22 #define ONEVALS 1
23 #define SUCCESS 1
24 #define FAIL (-1)
25 #define NO_ERR 0
26 #define ERRON_0 0
27 
Openlog(napi_env env, napi_callback_info info)28 static napi_value Openlog(napi_env env, napi_callback_info info)
29 {
30     errno = NO_ERR;
31     const char param[] = "MyMsgMARK";
32     openlog(param, LOG_PID, NO_ERR);
33     int ret = FAIL;
34     if (errno == NO_ERR) {
35         ret = SUCCESS;
36     }
37     napi_value result = nullptr;
38     napi_create_int32(env, ret, &result);
39     return result;
40 }
41 
Setlogmask(napi_env env, napi_callback_info info)42 static napi_value Setlogmask(napi_env env, napi_callback_info info)
43 {
44     int result = setlogmask(PARAM_0);
45 
46     if (result != setlogmask(result)) {
47         napi_value result;
48         napi_create_int32(env, ONEVAL, &result);
49         return result;
50     } else {
51         napi_value result;
52         napi_create_int32(env, PARAM_0, &result);
53         return result;
54     }
55 }
56 
Syslog(napi_env env, napi_callback_info info)57 static napi_value Syslog(napi_env env, napi_callback_info info)
58 {
59     errno = ERRON_0;
60     int priority = LOG_USER;
61     const char param[] = "MyMsgMARK";
62     char message[] = "Hello, syslog!";
63     openlog(param, LOG_PID, NO_ERR);
64     syslog(priority, "%s", message);
65     int ret = ONEVAL;
66     if (errno == PARAM_0) {
67         ret = ONEVALS;
68     }
69     napi_value result = nullptr;
70     napi_create_int32(env, ret, &result);
71     return result;
72 }
73 
74 EXTERN_C_START
Init(napi_env env, napi_value exports)75 static napi_value Init(napi_env env, napi_value exports)
76 {
77     napi_property_descriptor desc[] = {
78         {"setlogmask", nullptr, Setlogmask, nullptr, nullptr, nullptr, napi_default, nullptr},
79         {"openlog", nullptr, Openlog, nullptr, nullptr, nullptr, napi_default, nullptr},
80         {"syslog", nullptr, Syslog, nullptr, nullptr, nullptr, napi_default, nullptr}};
81     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
82     return exports;
83 }
84 
85 EXTERN_C_END
86 
87 static napi_module demoModule = {
88     .nm_version = 1,
89     .nm_flags = 0,
90     .nm_filename = nullptr,
91     .nm_register_func = Init,
92     .nm_modname = "syslog",
93     .nm_priv = ((void *)0),
94     .reserved = {0},
95 };
96 
RegisterModule(void)97 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
98