Git init
[framework/multimedia/libmm-log.git] / src / logmanager_viewer.c
1 /*
2  * libmm-log
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <memory.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <string.h>
32
33 #define FIFO_FILE "/tmp/logman.fifo"
34
35 #define OP_USAGE -1
36 #define OP_VIEWER 1
37 #define OP_RELOAD 2
38 #define OP_DUMP 3
39
40 void dump_logmanager(void);
41 int reload_logmanager(void);
42 static int get_options(int argc, char* argv[]);
43 static int usage(int argc, char* argv[]);
44 static void run_viewer(void);
45
46 static struct sigaction sigint_action;  /* Backup pointer of SIGINT handler */
47 static int fd;
48
49 void interrupt_signal(int sig)
50 {
51     close(fd);
52     remove(FIFO_FILE);
53
54     sigaction(SIGINT, &sigint_action, NULL);
55     raise(sig);
56     exit(0);
57 }
58
59 int main(int argc, char **argv)
60 {
61     switch(get_options(argc,argv))
62     {
63     case OP_VIEWER:
64         run_viewer();
65         break;
66     case OP_RELOAD:
67         reload_logmanager();
68         break;
69     case OP_DUMP:
70         dump_logmanager();
71         break;
72     default:
73         break;
74     }
75     return 0;
76 }
77
78 static int get_options(int argc, char* argv[])
79 {
80     int ch = 0;
81     int operation = OP_USAGE;
82
83     if(argc != 2)
84         return usage(argc, argv);
85
86     while((ch = getopt(argc, argv, "vrd")) != EOF)
87     {
88         switch(ch)
89         {
90         case 'v':
91             operation = OP_VIEWER;
92             break;
93         case 'r':
94             operation = OP_RELOAD;
95             break;
96         case 'd':
97             operation = OP_DUMP;
98             break;
99         default:
100             return usage(argc, argv);
101         }
102     }
103     if(optind != argc)
104         return usage(argc, argv);
105
106     return operation;
107 }
108
109 static int usage(int argc, char* argv[])
110 {
111     fprintf(stderr, "Usage : %s option\n", argv[0]);
112     fprintf(stderr, "[OPTIONS]\n");
113     fprintf(stderr, "  -v : Run logviewer\n");
114     fprintf(stderr, "  -r : Reload configuration\n");
115     fprintf(stderr, "  -d : Dump current log settings\n");
116     return OP_USAGE;
117 }
118
119 static void run_viewer(void)
120 {
121     char readbuf[4097];
122     int byte_read = 0;
123     mode_t pre_mask;
124     struct sigaction action;
125
126     action.sa_handler = interrupt_signal;
127     action.sa_flags = 0;
128     sigemptyset(&action.sa_mask);
129
130     sigaction(SIGINT, &action, &sigint_action);
131
132     pre_mask = umask(0);
133     if (mknod(FIFO_FILE, S_IFIFO|0666, 0) == -1)
134     {
135         perror ("mknod failed\n");
136     }
137     umask(pre_mask);
138
139     fd = open(FIFO_FILE, O_RDONLY|O_NONBLOCK);
140
141     if (fd == -1)
142     {
143         perror("PIPE CREATE\n");
144     }
145     else
146     {
147         memset(readbuf, 0, 4097);
148         while(1)
149         {
150             byte_read = read(fd,readbuf, 4096);
151             if(byte_read > 0 && byte_read <= 4096 && readbuf[0] != '\0')
152             {
153                 readbuf[byte_read] = '\0';
154                 printf("%s", readbuf);
155                 memset(readbuf, 0, 4097);
156             }
157             else
158             {
159                 usleep(1);
160             }
161         }
162     }
163 }
164