2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/types.h>
31 #define EXTAPI __attribute__((visibility("default")))
34 #define IOT_SERVER_MSG_LOG_FILE "/var/log/messages"
35 #define FILE_LENGTH 255
37 static int iot_debug_file_fd;
38 static char iot_debug_file_buf[FILE_LENGTH];
40 EXTAPI void iot_log(int type , int priority , const char *tag , const char *fmt , ...)
44 ssize_t total_sent_size = 0;
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);
53 len = write(iot_debug_file_fd, iot_debug_file_buf, strlen(iot_debug_file_buf));
55 total_sent_size += len;
57 } while (total_sent_size < FILE_LENGTH);
59 close(iot_debug_file_fd);
67 syslog_prio = LOG_ERR|LOG_DAEMON;
71 syslog_prio = LOG_WARNING|LOG_DAEMON;
75 syslog_prio = LOG_DEBUG|LOG_DAEMON;
79 syslog_prio = LOG_INFO|LOG_DAEMON;
83 syslog_prio = priority;
87 vsyslog(syslog_prio, fmt, ap);
94 SLOG_VA(LOG_ERROR, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
98 SLOG_VA(LOG_WARN, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
102 SLOG_VA(LOG_DEBUG, tag ? tag : "NULL", fmt ? fmt : "NULL" , ap);
106 SLOG_VA(LOG_INFO, tag ? tag : "NULL" , fmt ? fmt : "NULL" , ap);
121 bool get_proc_name(pid_t pid, char *process_name)
125 char filename[PATH_MAX];
127 sprintf(filename, "/proc/%d/stat", pid);
128 fp = fopen(filename, "r");
133 if (fscanf(fp, "%*s (%[^)]", buf) < 1) {
138 strncpy(process_name, buf, NAME_MAX-1);
139 process_name[NAME_MAX-1] = '\0';
145 const char* get_client_name(void)
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];
154 get_proc_name(pid, proc_name);
155 snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);