Changing notification level of ticker window(100->150)
[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.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 <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
26 #ifndef LOGFILE
27 #define LOGFILE         DATADIR"/quickpanel.log"
28 #endif
29
30 #define MAXSIZE (1 << 17)
31
32 static char buf[512];
33
34 void debug_printf(const char *msg, ...)
35 {
36         int fd;
37         va_list arg_list;
38         int len;
39         struct tm ts;
40         time_t ctime;
41         int status;
42         struct stat buffer;
43
44         /* Set time */
45         ctime = time(NULL);
46         localtime_r(&ctime, &ts);
47
48         snprintf(buf, 64, "[%04d/%02d/%02d %02d:%02d:%02d] ",
49                  ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
50                  ts.tm_hour, ts.tm_min, ts.tm_sec);
51         len = strlen(buf);
52
53         va_start(arg_list, msg);
54
55         fd = open(LOGFILE, O_WRONLY | O_CREAT | O_APPEND,
56                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
57         if (fd == -1) {
58                 fprintf(stderr, msg, arg_list);
59                 return;
60         }
61
62         status = fstat(fd, &buffer);
63         if (!status && (buffer.st_size > MAXSIZE)) {
64                 ftruncate(fd, 0);
65         }
66
67         len = vsnprintf(&buf[len], 511, msg, arg_list);
68         /* fix for flawfinder warnings: check string length */
69         if (len < 0) {
70                 return;
71         }
72
73         write(fd, buf, strlen(buf));
74
75         close(fd);
76         va_end(arg_list);
77 }