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