Introduce log write buffering
[platform/core/system/dlog.git] / include / log_file.h
1 /*
2  * DLOG
3  * Copyright (c) 2005-2008, The Android Open Source Project
4  * Copyright (c) 2012-2020 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifndef _LOG_FILE_H
20 #define _LOG_FILE_H
21
22 #include <logprint.h>
23 #include <queued_entry.h>
24
25 /* Logfile rotation */
26 #define DEFAULT_ROTATE_SIZE_KB 1024
27 #define DEFAULT_ROTATE_NUM_FILES 3
28
29 #define BtoKiB(x)       ((x) >> 10)
30
31 struct log_file;
32
33 struct log_file {
34         char *path;
35         int fd;
36         int should_close;
37         int size;
38         size_t rotate_size_kbytes;
39         size_t max_rotated;
40         struct log_format format;
41         bool isatty;
42         bool colors_auto;
43         struct log_write_buffer buffer;
44         int32_t prev_sec;  /* previously written entry's timestamp, seconds */
45         int32_t prev_nsec; /* previously written timestamp, nanoseconds */
46 };
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 void logfile_init(struct log_file *l_file);
53 bool logfile_init_buffer(struct log_file *l_file, size_t buf_size);
54 void logfile_move(struct log_file *to, struct log_file *from);
55 void logfile_free(struct log_file *l_file);
56 void logfile_set_fd(struct log_file *l_file, int fd, int should_close);
57 int logfile_set_path(struct log_file *l_file, const char *path);
58 int logfile_open(struct log_file *l_file);
59 int logfile_rotate_needed(struct log_file *l_file);
60 void logfile_do_rotate(struct log_file *file);
61 int logfile_flush(struct log_file *file);
62 int logfile_write_with_rotation(const dlogutil_entry_s *e, struct log_file *file, dlogutil_sorting_order_e sort_by);
63
64 #ifdef __cplusplus
65 }
66 #endif
67
68 #endif /* _LOG_FILE_H */