sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / shared / 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 #include <fcntl.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <dlog.h>
27 #include <sensor_common.h>
28 #include "sensor_logs.h"
29
30 #define PATH_MAX 256
31
32 #if defined(_DEBUG)
33 bool get_proc_name(pid_t pid, char *process_name)
34 {
35         FILE *fp;
36         char buf[NAME_MAX];
37         char filename[PATH_MAX];
38
39         sprintf(filename, "/proc/%d/stat", pid);
40         fp = fopen(filename, "r");
41
42         if (fp == NULL)
43                 return false;
44
45         if (fscanf(fp, "%*s (%[^)]", buf) < 1) {
46                 fclose(fp);
47                 return false;
48         }
49
50         strncpy(process_name, buf, NAME_MAX-1);
51         process_name[NAME_MAX-1] = '\0';
52         fclose(fp);
53
54         return true;
55 }
56 #else
57 bool get_proc_name(pid_t pid, char *process_name)
58 {
59         char buf[NAME_MAX];
60
61         if (snprintf(buf, sizeof(buf), "%d process", pid) < 1) {
62                 return false;
63         }
64
65         strncpy(process_name, buf, NAME_MAX-1);
66         process_name[NAME_MAX-1] = '\0';
67
68         return true;
69 }
70 #endif
71
72 const char* get_client_name(void)
73 {
74         const int pid_string_size = 10;
75         static pid_t pid = -1;
76         static char client_name[NAME_MAX + pid_string_size];
77
78         char proc_name[NAME_MAX];
79
80         if (pid == -1)
81         {
82                 pid = getpid();
83                 get_proc_name(pid, proc_name);
84                 snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);
85         }
86
87         return client_name;
88 }