1 /*
2 * Copyright (c) 2008, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <stdio.h>
33 #include <log/logd.h>
34 #include <ctype.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <stdlib.h>
38 #include <cutils/sockets.h>
39 #include <unistd.h>
40
41 /*
42 * Note: also accepts 0-9 priorities
43 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
44 */
filterCharToPri(char c)45 static android_LogPriority filterCharToPri (char c)
46 {
47 android_LogPriority pri;
48
49 c = tolower(c);
50
51 if (c >= '0' && c <= '9') {
52 if (c >= ('0'+ANDROID_LOG_SILENT)) {
53 pri = ANDROID_LOG_VERBOSE;
54 } else {
55 pri = (android_LogPriority)(c - '0');
56 }
57 } else if (c == 'v') {
58 pri = ANDROID_LOG_VERBOSE;
59 } else if (c == 'd') {
60 pri = ANDROID_LOG_DEBUG;
61 } else if (c == 'i') {
62 pri = ANDROID_LOG_INFO;
63 } else if (c == 'w') {
64 pri = ANDROID_LOG_WARN;
65 } else if (c == 'e') {
66 pri = ANDROID_LOG_ERROR;
67 } else if (c == 'f') {
68 pri = ANDROID_LOG_FATAL;
69 } else if (c == 's') {
70 pri = ANDROID_LOG_SILENT;
71 } else if (c == '*') {
72 pri = ANDROID_LOG_DEFAULT;
73 } else {
74 pri = ANDROID_LOG_UNKNOWN;
75 }
76
77 return pri;
78 }
79
usage(const char * s)80 static int usage(const char *s)
81 {
82 fprintf(stderr, "USAGE: %s [-p priorityChar] [-t tag] message\n", s);
83
84 fprintf(stderr, "\tpriorityChar should be one of:\n"
85 "\t\tv,d,i,w,e\n");
86 exit(-1);
87 }
88
89
log_main(int argc,char * argv[])90 int log_main(int argc, char *argv[])
91 {
92 android_LogPriority priority;
93 const char *tag = "log";
94 char buffer[4096];
95 int i;
96
97 priority = ANDROID_LOG_INFO;
98
99 for (;;) {
100 int ret;
101
102 ret = getopt(argc, argv, "t:p:h");
103
104 if (ret < 0) {
105 break;
106 }
107
108 switch(ret) {
109 case 't':
110 tag = optarg;
111 break;
112
113 case 'p':
114 priority = filterCharToPri(optarg[0]);
115 if (priority == ANDROID_LOG_UNKNOWN) {
116 usage(argv[0]);
117 }
118 break;
119
120 case 'h':
121 usage(argv[0]);
122 break;
123 }
124 }
125
126 if (optind == argc) {
127 usage(argv[0]);
128 }
129
130 buffer[0] = '\0';
131
132 for (i = optind ; i < argc ; i++) {
133 strlcat(buffer, argv[i], sizeof(buffer)-1);
134 strlcat(buffer, " ", sizeof(buffer)-1);
135 }
136
137 if(buffer[0] == 0) {
138 usage(argv[0]);
139 }
140
141 __android_log_print(priority, tag, "%s", buffer);
142
143 return 0;
144 }
145
146