Publishing project from SPIN to public
[platform/core/convergence/remote-rsc-svc.git] / src / common / common.cpp
1 /*
2  * Copyright (c) 2015 Samsung 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
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <syslog.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <dlog.h>
28 #include "common.h"
29
30 #ifndef EXTAPI
31 #define EXTAPI __attribute__((visibility("default")))
32 #endif
33
34 #define IOT_SERVER_MSG_LOG_FILE         "/var/log/messages"
35 #define FILE_LENGTH 255
36
37 static int iot_debug_file_fd;
38 static char iot_debug_file_buf[FILE_LENGTH];
39
40 EXTAPI void iot_log(int type , int priority , const char *tag , const char *fmt , ...)
41 {
42         va_list ap;
43         ssize_t len;
44         ssize_t total_sent_size = 0;
45         va_start(ap, fmt);
46
47         switch (type) {
48         case IOT_LOG_PRINT_FILE:
49                 iot_debug_file_fd = open(IOT_SERVER_MSG_LOG_FILE, O_WRONLY|O_CREAT|O_APPEND, 0644);
50                 if (iot_debug_file_fd != -1) {
51                         vsnprintf(iot_debug_file_buf, 255, fmt , ap);
52                         do {
53                                 len = write(iot_debug_file_fd, iot_debug_file_buf, strlen(iot_debug_file_buf));
54                                 if (len >= 0) {
55                                         total_sent_size += len;
56                                 }
57                         } while (total_sent_size < FILE_LENGTH);
58
59                         close(iot_debug_file_fd);
60                 }
61                 break;
62
63         case IOT_LOG_SYSLOG:
64                 int syslog_prio;
65                 switch (priority) {
66                 case IOT_LOG_ERR:
67                         syslog_prio = LOG_ERR|LOG_DAEMON;
68                         break;
69
70                 case IOT_LOG_WARN:
71                         syslog_prio = LOG_WARNING|LOG_DAEMON;
72                         break;
73
74                 case IOT_LOG_DBG:
75                         syslog_prio = LOG_DEBUG|LOG_DAEMON;
76                         break;
77
78                 case IOT_LOG_INFO:
79                         syslog_prio = LOG_INFO|LOG_DAEMON;
80                         break;
81
82                 default:
83                         syslog_prio = priority;
84                         break;
85                 }
86
87                 vsyslog(syslog_prio, fmt, ap);
88                 break;
89
90         case IOT_LOG_DLOG:
91                 if (tag) {
92                         switch (priority) {
93                         case IOT_LOG_ERR:
94                                 SLOG_VA(LOG_ERROR, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
95                                 break;
96
97                         case IOT_LOG_WARN:
98                                 SLOG_VA(LOG_WARN, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
99                                 break;
100
101                         case IOT_LOG_DBG:
102                                 SLOG_VA(LOG_DEBUG, tag ? tag : "NULL", fmt ? fmt : "NULL" , ap);
103                                 break;
104
105                         case IOT_LOG_INFO:
106                                 SLOG_VA(LOG_INFO, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
107                                 break;
108
109                         default:
110                                 break;
111                         }
112                 }
113                 break;
114         default:
115                 break;
116         }
117
118         va_end(ap);
119 }
120
121 bool get_proc_name(pid_t pid, char *process_name)
122 {
123         FILE *fp;
124         char buf[NAME_MAX];
125         char filename[PATH_MAX];
126
127         sprintf(filename, "/proc/%d/stat", pid);
128         fp = fopen(filename, "r");
129
130         if (fp == NULL)
131                 return false;
132
133         if (fscanf(fp, "%*s (%[^)]", buf) < 1) {
134                 fclose(fp);
135                 return false;
136         }
137
138         strncpy(process_name, buf, NAME_MAX-1);
139         process_name[NAME_MAX-1] = '\0';
140         fclose(fp);
141
142         return true;
143 }
144
145 const char* get_client_name(void)
146 {
147         const int pid_string_size = 10;
148         static pid_t pid = -1;
149         static char client_name[NAME_MAX + pid_string_size];
150         char proc_name[NAME_MAX];
151
152         if (pid == -1) {
153                 pid = getpid();
154                 get_proc_name(pid, proc_name);
155                 snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);
156         }
157
158         return client_name;
159 }