026983544d82e4cc4366c7b594eaa472dff453a9
[platform/adaptation/tm1/sensor-hal-tm1.git] / src / lib / sensor_logs.cpp
1 /*
2  * libsensord-share
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
20 /*TODO: clean up header files*/
21 #include <syslog.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <dlog.h>
28 #include "sensor_logs.h"
29
30 #ifndef EXTAPI
31 #define EXTAPI __attribute__((visibility("default")))
32 #endif
33
34 #define SF_SERVER_MSG_LOG_FILE          "/var/log/messages"
35 #define FILE_LENGTH 255
36
37 static int sf_debug_file_fd;
38 static char sf_debug_file_buf[FILE_LENGTH];
39
40 EXTAPI void sf_log(int type , int priority , const char *tag , const char *fmt , ...)
41 {
42         va_list ap;
43         va_start(ap, fmt);
44
45         switch (type) {
46                 case SF_LOG_PRINT_FILE:
47                         sf_debug_file_fd = open(SF_SERVER_MSG_LOG_FILE, O_WRONLY|O_CREAT|O_APPEND, 0644);
48                         if (sf_debug_file_fd != -1) {
49                                 vsnprintf(sf_debug_file_buf,255, fmt , ap );
50                                 int total_bytes_written = 0;
51                                 while (total_bytes_written < (int) strlen(sf_debug_file_buf)){
52                                         int bytes_written = write(sf_debug_file_fd, (sf_debug_file_buf + total_bytes_written), strlen(sf_debug_file_buf) - total_bytes_written);
53                                         if (bytes_written == -1)
54                                                 break;
55                                         total_bytes_written = total_bytes_written + bytes_written;
56                                 }
57                                 close(sf_debug_file_fd);
58                         }
59                         break;
60
61                 case SF_LOG_SYSLOG:
62                         int syslog_prio;
63                         switch (priority) {
64                                 case SF_LOG_ERR:
65                                         syslog_prio = LOG_ERR|LOG_DAEMON;
66                                         break;
67                                 case SF_LOG_WARN:
68                                         syslog_prio = LOG_WARNING|LOG_DAEMON;
69                                         break;
70
71                                 case SF_LOG_DBG:
72                                         syslog_prio = LOG_DEBUG|LOG_DAEMON;
73                                         break;
74
75                                 case SF_LOG_INFO:
76                                         syslog_prio = LOG_INFO|LOG_DAEMON;
77                                         break;
78
79                                 default:
80                                         syslog_prio = priority;
81                                         break;
82                         }
83
84                         vsyslog(syslog_prio, fmt, ap);
85                         break;
86
87                 case SF_LOG_DLOG:
88                         if (tag) {
89                                 switch (priority) {
90                                         case SF_LOG_ERR:
91                                                 SLOG_VA(LOG_ERROR, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
92                                                 break;
93
94                                         case SF_LOG_WARN:
95                                                 SLOG_VA(LOG_WARN, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
96                                                 break;
97
98                                         case SF_LOG_DBG:
99                                                 SLOG_VA(LOG_DEBUG, tag ? tag : "NULL", fmt ? fmt : "NULL" , ap);
100                                                 break;
101
102                                         case SF_LOG_INFO:
103                                                 SLOG_VA(LOG_INFO, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
104                                                 break;
105                                 }
106                         }
107                         break;
108         }
109
110         va_end(ap);
111 }