Commit eb945462 authored by Andrzej Puzdrowski's avatar Andrzej Puzdrowski Committed by Anas Nashif
Browse files

logging/log_backend_fs: support any directory



Added procedure which is able to create nested log director.

Signed-off-by: default avatarAndrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
parent 6b18e699
Loading
Loading
Loading
Loading
+58 −4
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ static int allocate_new_file(struct fs_file_t *file);
static int del_oldest_log(void);
static int get_log_file_id(struct fs_dirent *ent);

static int check_log_dir_available(void)
static int check_log_volumen_available(void)
{
	int index = 0;
	char const *name;
@@ -42,7 +42,7 @@ static int check_log_dir_available(void)
		if (rc == 0) {
			if (strncmp(CONFIG_LOG_BACKEND_FS_DIR,
				    name,
				    strlen(CONFIG_LOG_BACKEND_FS_DIR))
				    strlen(name))
			    == 0) {
				return 0;
			}
@@ -52,6 +52,57 @@ static int check_log_dir_available(void)
	return -ENOENT;
}

static int create_log_dir(const char *path)
{
	const char *next;
	const char *last = path + (strlen(path) - 1);
	char w_path[MAX_PATH_LEN];
	int rc, len;
	struct fs_dir_t dir;

	fs_dir_t_init(&dir);

	/* the fist directory name is the mount point*/
	/* the firs path's letter might be meaningless `/`, let's skip it */
	next = strchr(path + 1, '/');
	if (!next) {
		return 0;
	}

	while (1) {
		next++;
		if (next > last) {
			return 0;
		}
		next = strchr(next, '/');
		if (!next) {
			next = last;
			len = last - path + 1;
		} else {
			len = next - path;
		}

		memcpy(w_path, path, len);
		w_path[len] = 0;

		rc = fs_opendir(&dir, w_path);
		if (rc) {
			/* assume directory doesn't exist */
			rc = fs_mkdir(w_path);
			if (rc) {
				break;
			}
		}
		rc = fs_closedir(&dir);
		if (rc) {
			break;
		}
	}

	return rc;

}

static int check_log_file_exist(int num)
{
	struct fs_dir_t dir;
@@ -93,10 +144,13 @@ int write_log_to_file(uint8_t *data, size_t length, void *ctx)
	struct fs_file_t *f = &file;

	if (backend_state == BACKEND_FS_NOT_INITIALIZED) {
		if (check_log_dir_available()) {
		if (check_log_volumen_available()) {
			return length;
		}
		rc = create_log_dir(CONFIG_LOG_BACKEND_FS_DIR);
		if (!rc) {
			rc = allocate_new_file(&file);
		}
		backend_state = (rc ? BACKEND_FS_CORRUPTED : BACKEND_FS_OK);
	}

+1 −1
Original line number Diff line number Diff line
@@ -17,5 +17,5 @@ CONFIG_FS_LOG_LEVEL_OFF=y
# fs_dirent structures are big.
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_ZTEST_STACKSIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2304
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096
+4 −1
Original line number Diff line number Diff line
@@ -59,7 +59,10 @@ static void test_wipe_fs_logs(void)
	fs_file_t_init(&file);

	rc = fs_opendir(&dir, CONFIG_LOG_BACKEND_FS_DIR);
	zassert_equal(rc, 0, "Can not open directory.");
	if (rc) {
		/* log directory might not exist jet */
		return;
	}

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