Skip to content

Commit eb94546

Browse files
nvlsianpunashif
authored andcommitted
logging/log_backend_fs: support any directory
Added procedure which is able to create nested log director. Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent 6b18e69 commit eb94546

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

subsys/logging/log_backend_fs.c

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int allocate_new_file(struct fs_file_t *file);
3131
static int del_oldest_log(void);
3232
static int get_log_file_id(struct fs_dirent *ent);
3333

34-
static int check_log_dir_available(void)
34+
static int check_log_volumen_available(void)
3535
{
3636
int index = 0;
3737
char const *name;
@@ -42,7 +42,7 @@ static int check_log_dir_available(void)
4242
if (rc == 0) {
4343
if (strncmp(CONFIG_LOG_BACKEND_FS_DIR,
4444
name,
45-
strlen(CONFIG_LOG_BACKEND_FS_DIR))
45+
strlen(name))
4646
== 0) {
4747
return 0;
4848
}
@@ -52,6 +52,57 @@ static int check_log_dir_available(void)
5252
return -ENOENT;
5353
}
5454

55+
static int create_log_dir(const char *path)
56+
{
57+
const char *next;
58+
const char *last = path + (strlen(path) - 1);
59+
char w_path[MAX_PATH_LEN];
60+
int rc, len;
61+
struct fs_dir_t dir;
62+
63+
fs_dir_t_init(&dir);
64+
65+
/* the fist directory name is the mount point*/
66+
/* the firs path's letter might be meaningless `/`, let's skip it */
67+
next = strchr(path + 1, '/');
68+
if (!next) {
69+
return 0;
70+
}
71+
72+
while (1) {
73+
next++;
74+
if (next > last) {
75+
return 0;
76+
}
77+
next = strchr(next, '/');
78+
if (!next) {
79+
next = last;
80+
len = last - path + 1;
81+
} else {
82+
len = next - path;
83+
}
84+
85+
memcpy(w_path, path, len);
86+
w_path[len] = 0;
87+
88+
rc = fs_opendir(&dir, w_path);
89+
if (rc) {
90+
/* assume directory doesn't exist */
91+
rc = fs_mkdir(w_path);
92+
if (rc) {
93+
break;
94+
}
95+
}
96+
rc = fs_closedir(&dir);
97+
if (rc) {
98+
break;
99+
}
100+
}
101+
102+
return rc;
103+
104+
}
105+
55106
static int check_log_file_exist(int num)
56107
{
57108
struct fs_dir_t dir;
@@ -93,10 +144,13 @@ int write_log_to_file(uint8_t *data, size_t length, void *ctx)
93144
struct fs_file_t *f = &file;
94145

95146
if (backend_state == BACKEND_FS_NOT_INITIALIZED) {
96-
if (check_log_dir_available()) {
147+
if (check_log_volumen_available()) {
97148
return length;
98149
}
99-
rc = allocate_new_file(&file);
150+
rc = create_log_dir(CONFIG_LOG_BACKEND_FS_DIR);
151+
if (!rc) {
152+
rc = allocate_new_file(&file);
153+
}
100154
backend_state = (rc ? BACKEND_FS_CORRUPTED : BACKEND_FS_OK);
101155
}
102156

tests/subsys/logging/log_backend_fs/prj.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ CONFIG_FS_LOG_LEVEL_OFF=y
1717
# fs_dirent structures are big.
1818
CONFIG_MAIN_STACK_SIZE=2048
1919
CONFIG_ZTEST_STACKSIZE=4096
20-
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2304
20+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
2121
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096

tests/subsys/logging/log_backend_fs/src/log_fs_test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ static void test_wipe_fs_logs(void)
5959
fs_file_t_init(&file);
6060

6161
rc = fs_opendir(&dir, CONFIG_LOG_BACKEND_FS_DIR);
62-
zassert_equal(rc, 0, "Can not open directory.");
62+
if (rc) {
63+
/* log directory might not exist jet */
64+
return;
65+
}
6366

6467
/* Iterate over logging directory. */
6568
while (1) {

0 commit comments

Comments
 (0)