Skip to content

Commit 941a7ac

Browse files
committed
refactor(storage): log partition id for logs
since the storage module is now used with 2 partitions Signed-off-by: Cyril Fougeray <cyril.fougeray@toolsforhumanity.com>
1 parent 42c2e4c commit 941a7ac

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

lib/storage/storage.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ init_area(struct storage_area_s *area)
120120
area->rd_idx = area->wr_idx;
121121
}
122122

123-
LOG_DBG("Storage area initialized: rd: 0x%04lx, wr: 0x%04lx", area->rd_idx,
124-
area->wr_idx);
123+
LOG_DBG("Storage area initialized (partition %u): rd: 0x%04lx, wr: 0x%04lx",
124+
fa->fa_id, area->rd_idx, area->wr_idx);
125125

126126
return RET_SUCCESS;
127127
}
@@ -184,9 +184,10 @@ storage_peek(struct storage_area_s *area, char *buffer, size_t *size)
184184
reset_area(area);
185185
// print log after area reset to keep it in flash in case remote not
186186
// accessible
187-
LOG_ERR("Area in invalid state has been reset (flash read ret %d); rd: "
188-
"0x%04x, wr: 0x%04x",
189-
flash_read_ret, rd_idx, wr_idx);
187+
LOG_ERR(
188+
"Area in invalid state has been reset (partition %u, flash read "
189+
"ret %d); rd: 0x%04x, wr: 0x%04x",
190+
area->fa->fa_id, flash_read_ret, rd_idx, wr_idx);
190191
err_code = RET_ERROR_INVALID_STATE;
191192
}
192193

@@ -223,7 +224,8 @@ storage_free(struct storage_area_s *area)
223224
ret = flash_area_write(area->fa, (off_t)area->rd_idx, (const void *)&header,
224225
sizeof(header));
225226
if (ret) {
226-
LOG_ERR("Unable to invalidate record: %d", ret);
227+
LOG_ERR("Unable to invalidate record (partition %u): %d",
228+
area->fa->fa_id, ret);
227229
ret = RET_ERROR_INTERNAL;
228230
goto exit;
229231
}
@@ -237,13 +239,16 @@ storage_free(struct storage_area_s *area)
237239
area->rd_idx = (off_t)(area->rd_idx + sizeof(storage_header_t) +
238240
record_size + padding);
239241

240-
LOG_DBG("New record freed, size: %u, rd off: 0x%x, wr off: 0x%x",
241-
record_size, (uint32_t)area->rd_idx, (uint32_t)area->wr_idx);
242+
LOG_DBG("New record freed (partition %u), size: %u, rd off: 0x%x, wr off: "
243+
"0x%x",
244+
area->fa->fa_id, record_size, (uint32_t)area->rd_idx,
245+
(uint32_t)area->wr_idx);
242246

243247
if (area->rd_idx >= area->wr_idx) {
244248
size_t space_left_bytes = area->fa->fa_size - (size_t)area->wr_idx;
245249
if (space_left_bytes < MINIMUM_EMPTY_SPACE) {
246-
LOG_INF("%u bytes left, erasing", space_left_bytes);
250+
LOG_INF("partition %u: %u bytes left, erasing", area->fa->fa_id,
251+
space_left_bytes);
247252
reset_area(area);
248253
}
249254
}
@@ -340,7 +345,8 @@ storage_push(struct storage_area_s *area, char *record, size_t size)
340345
ret = flash_area_read(area->fa, (off_t)(area->wr_idx + sizeof(header)),
341346
(void *)record, size_to_write_trunc);
342347
if (ret) {
343-
LOG_ERR("Unable to read back record after write: %d", ret);
348+
LOG_ERR("Unable to read back record after write (partition %u): %d",
349+
area->fa->fa_id, ret);
344350
}
345351

346352
if (size % FLASH_WRITE_BLOCK_SIZE) {
@@ -354,7 +360,9 @@ storage_push(struct storage_area_s *area, char *record, size_t size)
354360
uint16_t rb_crc = crc16_ccitt(0xffff, record, size);
355361
if (header.crc16 != rb_crc) {
356362
reset_area(area);
357-
LOG_ERR("Invalid CRC16 read after record has been written");
363+
LOG_ERR("Invalid CRC16 read after record has been written "
364+
"(partition %u)",
365+
area->fa->fa_id);
358366

359367
ret = RET_ERROR_INVALID_STATE;
360368
goto exit;
@@ -373,8 +381,10 @@ storage_push(struct storage_area_s *area, char *record, size_t size)
373381
// push write index with padding included
374382
area->wr_idx = (off_t)(area->wr_idx + (sizeof(header) + size_in_flash));
375383

376-
LOG_DBG("New record written, size: %u, rd off: 0x%x, wr off: 0x%x", size,
377-
(uint32_t)area->rd_idx, (uint32_t)area->wr_idx);
384+
LOG_DBG("New record written (partition %u), size: %u, rd off: 0x%x, wr "
385+
"off: 0x%x",
386+
area->fa->fa_id, size, (uint32_t)area->rd_idx,
387+
(uint32_t)area->wr_idx);
378388
exit:
379389
k_sem_give(&sem_storage);
380390

@@ -454,10 +464,12 @@ storage_init(struct storage_area_s *area, uint8_t partition_id)
454464

455465
ret = init_area(area);
456466
if (ret != RET_SUCCESS) {
457-
LOG_WRN("Unable to find valid records, erasing area");
467+
LOG_WRN("Partition %u: unable to find valid records, erasing area",
468+
partition_id);
458469
ret = flash_area_erase(area->fa, 0, area->fa->fa_size);
459470
if (ret) {
460-
LOG_ERR("Unable to erase flash area: %d", ret);
471+
LOG_ERR("Unable to erase flash area (partition %u): %d",
472+
partition_id, ret);
461473
ret = RET_ERROR_INTERNAL;
462474
goto exit;
463475
}

0 commit comments

Comments
 (0)