e4e6923e3d396ce7e34f7edfaecfbe7993713f2c
[platform/core/system/upgrade.git] / src / upgrade-apply-deltafs / engine / fota_log.c
1 /*
2  * upgrade-apply-deltafs
3  *
4  * Copyright (c) 2017 - 2022 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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28
29 #define DEF_MAX_LOG_SIZE                (2*1024*1024)
30 #define MAX_FILE_PATH                   512
31
32 long curr_offset = 0;
33 long next_offset = 0;
34 long backup_offset = 0;
35 long max_logfile_size = DEF_MAX_LOG_SIZE;
36
37 /*-----------------------------------------------------------------------------
38   log_printf
39  ----------------------------------------------------------------------------*/
40 int log_printf(FILE* log_fp, char* format_str, ...)
41 {
42         int ret = 0;
43         char log_str[4096];
44         char backup_ch;
45         int len;
46         va_list list;
47
48         va_start(list, format_str);
49         vsnprintf(log_str, sizeof(log_str), format_str, list);
50         va_end(list);
51
52         len = strlen(log_str);
53         next_offset = curr_offset + len;
54
55         if (next_offset <= max_logfile_size) {
56                 if (fprintf(log_fp, "%s", log_str) < 0) {
57                         ret = -1;
58                         goto exit;
59                 }
60                 curr_offset = next_offset;
61                 if (curr_offset == max_logfile_size) {
62                         rewind(log_fp);
63                         curr_offset = 0;
64                 }
65         } else {
66                 backup_offset = max_logfile_size - curr_offset;
67                 backup_ch = log_str[backup_offset];
68                 log_str[backup_offset] = 0x00;
69                 if (fprintf(log_fp, "%s", log_str) < 0) {
70                         ret = -1;
71                         goto exit;
72                 }
73                 rewind(log_fp);
74                 log_str[backup_offset] = backup_ch;
75                 if (fprintf(log_fp, "%s", log_str+backup_offset) < 0) {
76                         ret = -1;
77                         goto exit;
78                 }
79                 curr_offset = next_offset - max_logfile_size;
80         }
81
82 exit:
83         return ret;
84 }