Git init
[framework/system/dlog.git] / include / logprint.h
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
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 #ifndef _LOGPRINT_H
18 #define _LOGPRINT_H
19
20 #include <time.h>
21 #include <pthread.h>
22
23 #include <logger.h>
24 #include <dlog.h>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 typedef enum {
31     FORMAT_OFF = 0,
32     FORMAT_BRIEF,
33     FORMAT_PROCESS,
34     FORMAT_TAG,
35     FORMAT_THREAD,
36     FORMAT_RAW,
37     FORMAT_TIME,
38     FORMAT_THREADTIME,
39     FORMAT_LONG,
40 } log_print_format;
41
42 typedef struct log_format_t log_format;
43
44 typedef struct log_entry_t {
45     time_t tv_sec;
46     long tv_nsec;
47     log_priority priority;
48     pid_t pid;
49     pthread_t tid;
50     const char * tag;
51     size_t messageLen;
52     const char * message;
53 } log_entry;
54
55 log_format *log_format_new();
56
57 void log_format_free(log_format *p_format);
58
59 void log_set_print_format(log_format *p_format, 
60         log_print_format format);
61
62 /**
63  * Returns FORMAT_OFF on invalid string
64  */
65 log_print_format log_format_from_string(const char *s);
66
67 /** 
68  * filterExpression: a single filter expression
69  * eg "AT:d"
70  *
71  * returns 0 on success and -1 on invalid expression
72  *
73  * Assumes single threaded execution
74  *
75  */
76
77 int log_add_filter_rule(log_format *p_format, 
78         const char *filterExpression);
79
80
81 /** 
82  * filterString: a whitespace-separated set of filter expressions 
83  * eg "AT:d *:i"
84  *
85  * returns 0 on success and -1 on invalid expression
86  *
87  * Assumes single threaded execution
88  *
89  */
90
91 int log_add_filter_string(log_format *p_format,
92         const char *filterString);
93
94
95 /** 
96  * returns 1 if this log line should be printed based on its priority
97  * and tag, and 0 if it should not
98  */
99 int log_should_print_line (
100         log_format *p_format, const char *tag, log_priority pri);
101
102
103 /**
104  * Splits a wire-format buffer into an log_entry
105  * entry allocated by caller. Pointers will point directly into buf
106  *
107  * Returns 0 on success and -1 on invalid wire format (entry will be
108  * in unspecified state)
109  */
110 int log_process_log_buffer(struct logger_entry *buf,
111                                  log_entry *entry);
112
113 /**
114  * Formats a log message into a buffer
115  *
116  * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
117  * If return value != defaultBuffer, caller must call free()
118  * Returns NULL on malloc error
119  */
120
121 char *log_format_log_line (    
122     log_format *p_format,
123     char *defaultBuffer,
124     size_t defaultBufferSize,
125     const log_entry *p_line,
126     size_t *p_outLength);
127
128
129 /**
130  * Either print or do not print log line, based on filter
131  *
132  * Assumes single threaded execution
133  *
134  */
135 int log_print_log_line(
136     log_format *p_format,
137     int fd,
138     const log_entry *entry);
139
140
141
142 #ifdef __cplusplus
143 }
144 #endif
145
146 #endif /*_LOGPRINT_H*/