Code sync
[apps/native/starter.git] / 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
18
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <fcntl.h>
22 #include <time.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <glib.h>
27
28 #include "lockd-debug.h"
29
30 #define LINEMAX 256
31 #define MAXFILELEN      1048576 /* 32000 */
32 #define LOGFILE "/tmp/starter.log"
33
34 void lockd_log_t(char *fmt, ...)
35 {
36         va_list ap;
37         FILE *fd = 0;
38         char buf[LINEMAX] = { 0, };
39         char debugString[LINEMAX] = { 0, };
40
41         va_start(ap, fmt);
42         vsnprintf(buf, sizeof(buf), fmt, ap);
43         va_end(ap);
44         int fileLen = 0;
45         struct tm local_t;
46         time_t current_time = 0;
47         bzero((char *)&debugString, LINEMAX);
48         time(&current_time);
49         /* local_t = gmtime(&current_time); */
50         gmtime_r(&current_time, &local_t);      /* for prevent 53555 */
51         int len = snprintf(debugString, sizeof(debugString),
52                            "[%d-%02d-%02d, %02d:%02d:%02d]: ",
53                            local_t.tm_year + 1900, local_t.tm_mon + 1,
54                            local_t.tm_mday, local_t.tm_hour, local_t.tm_min,
55                            local_t.tm_sec);
56         if (len == -1) {
57                 return;
58         } else {
59                 debugString[len] = '\0';
60         }
61         len = g_strlcat(debugString, buf, LINEMAX);
62         if (len >= LINEMAX) {
63                 /* TODO:ERROR handling */
64                 return;
65         } else {
66                 debugString[len] = '\n';
67         }
68         /* FIXME this is for permission.. later we should fix and remove this... */
69         /* system("chmod 666 "LOGFILE); */
70         if ((fd = fopen(LOGFILE, "at+")) == NULL) {
71                 LOCKD_DBG("File fopen fail for writing Pwlock information");
72         } else {
73                 int pid = -1;
74                 if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
75                         LOCKD_DBG
76                             ("File fwrite fail for writing Pwlock information");
77                         fclose(fd);
78                         if ((pid = fork()) < 0) {
79                         } else if (pid == 0) {
80                                 execl("/bin/rm", "rm", "-f", LOGFILE,
81                                       (char *)0);
82                         }
83                         /* system("rm -rf "LOGFILE);  */
84                 } else {
85                         fseek(fd, 0l, SEEK_END);
86                         fileLen = ftell(fd);
87                         if (fileLen > MAXFILELEN) {
88                                 fclose(fd);
89                                 if ((pid = fork()) < 0) {
90                                         return;
91                                 } else if (pid == 0) {
92                                         execl("/bin/rm", "rm", "-f", LOGFILE,
93                                               (char *)0);
94                                 }
95                                 /* system("rm -rf "LOGFILE); */
96                         } else
97                                 fclose(fd);
98                 }
99         }
100 }