version update for systemd
[apps/native/starter.git] / lock-mgr / src / lockd-debug.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include <stdio.h>
18 #include <stdarg.h>
19 #include <fcntl.h>
20 #include <time.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <glib.h>
25
26 #include "lockd-debug.h"
27
28 #define LINEMAX 256
29 #define MAXFILELEN      1048576
30 #define LOGFILE "/tmp/starter.log"
31
32 void lockd_log_t(char *fmt, ...)
33 {
34         va_list ap;
35         FILE *fd = 0;
36         char buf[LINEMAX] = { 0, };
37         char debugString[LINEMAX] = { 0, };
38
39         va_start(ap, fmt);
40         vsnprintf(buf, sizeof(buf), fmt, ap);
41         va_end(ap);
42         int fileLen = 0;
43         struct tm local_t;
44         time_t current_time = 0;
45         bzero((char *)&debugString, LINEMAX);
46         time(&current_time);
47         gmtime_r(&current_time, &local_t);
48         int len = snprintf(debugString, sizeof(debugString),
49                            "[%d-%02d-%02d, %02d:%02d:%02d]: ",
50                            local_t.tm_year + 1900, local_t.tm_mon + 1,
51                            local_t.tm_mday, local_t.tm_hour, local_t.tm_min,
52                            local_t.tm_sec);
53         if (len == -1) {
54                 return;
55         } else {
56                 debugString[len] = '\0';
57         }
58         len = g_strlcat(debugString, buf, LINEMAX);
59         if (len >= LINEMAX) {
60                 return;
61         } else {
62                 debugString[len] = '\n';
63         }
64         if ((fd = fopen(LOGFILE, "at+")) == NULL) {
65                 LOCKD_DBG("File fopen fail for writing Pwlock information");
66         } else {
67                 int pid = -1;
68                 if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
69                         LOCKD_DBG
70                             ("File fwrite fail for writing Pwlock information");
71                         fclose(fd);
72                         if ((pid = fork()) < 0) {
73                         } else if (pid == 0) {
74                                 execl("/bin/rm", "rm", "-f", LOGFILE,
75                                       (char *)0);
76                         }
77                 } else {
78                         fseek(fd, 0l, SEEK_END);
79                         fileLen = ftell(fd);
80                         if (fileLen > MAXFILELEN) {
81                                 fclose(fd);
82                                 if ((pid = fork()) < 0) {
83                                         return;
84                                 } else if (pid == 0) {
85                                         execl("/bin/rm", "rm", "-f", LOGFILE,
86                                               (char *)0);
87                                 }
88                         } else
89                                 fclose(fd);
90                 }
91         }
92 }