a214ca272fd664a72b035b4e0d62c95dd4c17432
[apps/home/quickpanel.git] / test / quickpanel_debug_util.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <common.h>
29 #include <glib.h>
30
31 #define LINEMAX 256
32 #define MAXFILELEN      1048576 /* 32000 */
33 #define LOGFILE "/tmp/quickpanel.log"
34
35 void debug_printf(char *fmt, ...) {
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]: ", local_t.tm_year + 1900,
53                         local_t.tm_mon + 1, local_t.tm_mday, local_t.tm_hour,
54                         local_t.tm_min, local_t.tm_sec);
55         if (len == -1) {
56                 return;
57         } else {
58                 debugString[len] = '\0';
59         }
60         len = g_strlcat(debugString, buf, LINEMAX);
61         if (len >= LINEMAX) {
62                 /* TODO:ERROR handling */
63                 return;
64         } else {
65                 debugString[len] = '\n';
66         }
67         /* FIXME this is for permission.. later we should fix and remove this... */
68         /* system("chmod 666 "LOGFILE); */
69         if ((fd = fopen(LOGFILE, "at+")) == NULL) {
70                 DBG("File fopen fail for writing Pwlock information");
71         } else {
72                 int pid = -1;
73                 if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
74                         DBG("File fwrite fail for writing Pwlock information");
75                         fclose(fd);
76                         if ((pid = fork()) < 0) {
77                         } else if (pid == 0) {
78                                 execl("/bin/rm", "rm", "-f", LOGFILE, (char *) 0);
79                         }
80                         /* system("rm -rf "LOGFILE);  */
81                 } else {
82                         fseek(fd, 0l, SEEK_END);
83                         fileLen = ftell(fd);
84                         if (fileLen > MAXFILELEN) {
85                                 fclose(fd);
86                                 if ((pid = fork()) < 0) {
87                                         return;
88                                 } else if (pid == 0) {
89                                         execl("/bin/rm", "rm", "-f", LOGFILE, (char *) 0);
90                                 }
91                                 /* system("rm -rf "LOGFILE); */
92                         } else
93                                 fclose(fd);
94                 }
95         }
96 }