5053d97c333373907d029b09605df97e56f2f8fb
[apps/home/starter.git] / lock-mgr / src / lockd-debug.c
1 /*
2  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved 
3  *
4  * This file is part of <starter>
5  * Written by <Seungtaek Chung> <seungtaek.chung@samsung.com>, <Mi-Ju Lee> <miju52.lee@samsung.com>, <Xi Zhichan> <zhichan.xi@samsung.com>
6  *
7  * PROPRIETARY/CONFIDENTIAL
8  *
9  * This software is the confidential and proprietary information of SAMSUNG ELECTRONICS ("Confidential Information").
10  * You shall not disclose such Confidential Information and shall use it only in accordance
11  * with the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
12  * SAMSUNG make no representations or warranties about the suitability of the software,
13  * either express or implied, including but not limited to the implied warranties of merchantability,
14  * fitness for a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as a result of using,
16  * modifying or distributing this software or its derivatives.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <time.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <glib.h>
28
29 #include "lockd-debug.h"
30
31 #define LINEMAX 256
32 #define MAXFILELEN      1048576
33 #define LOGFILE "/tmp/starter.log"
34
35 void lockd_log_t(char *fmt, ...)
36 {
37         va_list ap;
38         FILE *fd = 0;
39         char buf[LINEMAX] = { 0, };
40         char debugString[LINEMAX] = { 0, };
41
42         va_start(ap, fmt);
43         vsnprintf(buf, sizeof(buf), fmt, ap);
44         va_end(ap);
45         int fileLen = 0;
46         struct tm local_t;
47         time_t current_time = 0;
48         bzero((char *)&debugString, LINEMAX);
49         time(&current_time);
50         gmtime_r(&current_time, &local_t);
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                 return;
64         } else {
65                 debugString[len] = '\n';
66         }
67         if ((fd = fopen(LOGFILE, "at+")) == NULL) {
68                 LOCKD_DBG("File fopen fail for writing Pwlock information");
69         } else {
70                 int pid = -1;
71                 if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
72                         LOCKD_DBG
73                             ("File fwrite fail for writing Pwlock information");
74                         fclose(fd);
75                         if ((pid = fork()) < 0) {
76                         } else if (pid == 0) {
77                                 execl("/bin/rm", "rm", "-f", LOGFILE,
78                                       (char *)0);
79                         }
80                 } else {
81                         fseek(fd, 0l, SEEK_END);
82                         fileLen = ftell(fd);
83                         if (fileLen > MAXFILELEN) {
84                                 fclose(fd);
85                                 if ((pid = fork()) < 0) {
86                                         return;
87                                 } else if (pid == 0) {
88                                         execl("/bin/rm", "rm", "-f", LOGFILE,
89                                               (char *)0);
90                                 }
91                         } else
92                                 fclose(fd);
93                 }
94         }
95 }