1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #if defined(linux) && !defined(__ANDROID__)
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <syslog.h>
21
22 #include "os_log.h"
23 #include "os_env.h"
24
25 #define LINE_SZ 1024
26
27 class SyslogWrapper
28 {
29 private:
30 // avoid any unwanted function
31 SyslogWrapper(const SyslogWrapper &);
32 SyslogWrapper &operator=(const SyslogWrapper &);
33 public:
34 SyslogWrapper();
35 ~SyslogWrapper();
36 };
37
38 static SyslogWrapper syslog_wrapper;
39
SyslogWrapper()40 SyslogWrapper::SyslogWrapper()
41 {
42 int option = LOG_PID | LOG_CONS;
43 RK_U32 syslog_perror = 0;
44
45 os_get_env_u32("mpp_syslog_perror", &syslog_perror, 0);
46 if (syslog_perror)
47 option |= LOG_PERROR;
48
49 openlog("mpp", option, LOG_USER);
50 }
51
~SyslogWrapper()52 SyslogWrapper::~SyslogWrapper()
53 {
54 closelog();
55 }
56
os_log_trace(const char * tag,const char * msg,va_list list)57 void os_log_trace(const char* tag, const char* msg, va_list list)
58 {
59 char line[LINE_SZ] = {0};
60 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
61 vsyslog(LOG_NOTICE, line, list);
62 }
63
os_log_debug(const char * tag,const char * msg,va_list list)64 void os_log_debug(const char* tag, const char* msg, va_list list)
65 {
66 char line[LINE_SZ] = {0};
67 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
68 vsyslog(LOG_DEBUG, line, list);
69 }
70
os_log_info(const char * tag,const char * msg,va_list list)71 void os_log_info(const char* tag, const char* msg, va_list list)
72 {
73 char line[LINE_SZ] = {0};
74 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
75 vsyslog(LOG_INFO, line, list);
76 }
77
os_log_warn(const char * tag,const char * msg,va_list list)78 void os_log_warn(const char* tag, const char* msg, va_list list)
79 {
80 char line[LINE_SZ] = {0};
81 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
82 vsyslog(LOG_WARNING, line, list);
83 }
84
os_log_error(const char * tag,const char * msg,va_list list)85 void os_log_error(const char* tag, const char* msg, va_list list)
86 {
87 char line[LINE_SZ] = {0};
88 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
89 vsyslog(LOG_ERR, line, list);
90 }
91
os_log_fatal(const char * tag,const char * msg,va_list list)92 void os_log_fatal(const char* tag, const char* msg, va_list list)
93 {
94 char line[LINE_SZ] = {0};
95 snprintf(line, sizeof(line) - 1, "%s: %s", tag, msg);
96 vsyslog(LOG_CRIT, line, list);
97 }
98
99 #endif
100