@@ -17,6 +17,16 @@ namespace chat
1717namespace meshtastic
1818{
1919
20+ #ifndef NODE_STORE_LOG_ENABLE
21+ #define NODE_STORE_LOG_ENABLE 0
22+ #endif
23+
24+ #if NODE_STORE_LOG_ENABLE
25+ #define NODE_STORE_LOG (...) Serial.printf(__VA_ARGS__)
26+ #else
27+ #define NODE_STORE_LOG (...)
28+ #endif
29+
2030namespace
2131{
2232uint32_t crc32 (const uint8_t * data, size_t len)
@@ -46,31 +56,31 @@ void logNvsStats(const char* tag, const char* ns)
4656 esp_err_t err = nvs_get_stats (nullptr , &stats);
4757 if (err == ESP_OK)
4858 {
49- Serial. printf (" [NodeStore] NVS stats(%s): used=%u free=%u total=%u namespaces=%u\n " ,
50- tag ? tag : " ?" ,
51- static_cast <unsigned >(stats.used_entries ),
52- static_cast <unsigned >(stats.free_entries ),
53- static_cast <unsigned >(stats.total_entries ),
54- static_cast <unsigned >(stats.namespace_count ));
59+ NODE_STORE_LOG (" [NodeStore] NVS stats(%s): used=%u free=%u total=%u namespaces=%u\n " ,
60+ tag ? tag : " ?" ,
61+ static_cast <unsigned >(stats.used_entries ),
62+ static_cast <unsigned >(stats.free_entries ),
63+ static_cast <unsigned >(stats.total_entries ),
64+ static_cast <unsigned >(stats.namespace_count ));
5565 }
5666 else
5767 {
58- Serial. printf (" [NodeStore] NVS stats(%s) err=%s\n " ,
59- tag ? tag : " ?" ,
60- esp_err_to_name (err));
68+ NODE_STORE_LOG (" [NodeStore] NVS stats(%s) err=%s\n " ,
69+ tag ? tag : " ?" ,
70+ esp_err_to_name (err));
6171 }
6272 if (ns && ns[0 ])
6373 {
6474 nvs_handle_t handle;
6575 err = nvs_open (ns, NVS_READONLY, &handle);
6676 if (err == ESP_OK)
6777 {
68- Serial. printf (" [NodeStore] NVS open ns=%s ok\n " , ns);
78+ NODE_STORE_LOG (" [NodeStore] NVS open ns=%s ok\n " , ns);
6979 nvs_close (handle);
7080 }
7181 else
7282 {
73- Serial. printf (" [NodeStore] NVS open ns=%s err=%s\n " , ns, esp_err_to_name (err));
83+ NODE_STORE_LOG (" [NodeStore] NVS open ns=%s err=%s\n " , ns, esp_err_to_name (err));
7484 }
7585 }
7686}
@@ -97,18 +107,18 @@ void NodeStore::begin()
97107 }
98108
99109 entries_.clear ();
100- Serial. printf (" [NodeStore] loaded=0\n " );
110+ NODE_STORE_LOG (" [NodeStore] loaded=0\n " );
101111}
102112
103113void NodeStore::upsert (uint32_t node_id, const char * short_name, const char * long_name,
104114 uint32_t now_secs, float snr, float rssi, uint8_t protocol, uint8_t role,
105115 uint8_t hops_away)
106116{
107- Serial. printf (" [NodeStore] upsert node=%08lX ts=%lu snr=%.1f rssi=%.1f\n " ,
108- (unsigned long )node_id,
109- (unsigned long )now_secs,
110- snr,
111- rssi);
117+ NODE_STORE_LOG (" [NodeStore] upsert node=%08lX ts=%lu snr=%.1f rssi=%.1f\n " ,
118+ (unsigned long )node_id,
119+ (unsigned long )now_secs,
120+ snr,
121+ rssi);
112122 // find existing
113123 for (auto & e : entries_)
114124 {
@@ -220,16 +230,16 @@ void NodeStore::save()
220230 {
221231 if (saveToSd ())
222232 {
223- Serial. printf (" [NodeStore] save ok (SD) count=%u\n " ,
224- static_cast <unsigned >(entries_.size ()));
233+ NODE_STORE_LOG (" [NodeStore] save ok (SD) count=%u\n " ,
234+ static_cast <unsigned >(entries_.size ()));
225235 return ;
226236 }
227237 }
228238
229239 Preferences prefs;
230240 if (!prefs.begin (kPersistNodesNs , false ))
231241 {
232- Serial. printf (" [NodeStore] save failed ns=%s\n " , kPersistNodesNs );
242+ NODE_STORE_LOG (" [NodeStore] save failed ns=%s\n " , kPersistNodesNs );
233243 logNvsStats (" save-open" , kPersistNodesNs );
234244 return ;
235245 }
@@ -265,20 +275,20 @@ void NodeStore::save()
265275 prefs.putUInt (kPersistNodesKeyCrc , crc);
266276 if (written != expected)
267277 {
268- Serial. printf (" [NodeStore] save failed wrote=%u expected=%u\n " ,
269- static_cast <unsigned >(written),
270- static_cast <unsigned >(expected));
278+ NODE_STORE_LOG (" [NodeStore] save failed wrote=%u expected=%u\n " ,
279+ static_cast <unsigned >(written),
280+ static_cast <unsigned >(expected));
271281 logNvsStats (" save-write" , kPersistNodesNs );
272282 }
273283 else
274284 {
275285 size_t verify_len = prefs.getBytesLength (kPersistNodesKey );
276286 uint8_t verify_ver = prefs.getUChar (kPersistNodesKeyVer , 0 );
277287 uint32_t verify_crc = prefs.getUInt (kPersistNodesKeyCrc , 0 );
278- Serial. printf (" [NodeStore] save ok len=%u ver=%u crc=%08lX\n " ,
279- static_cast <unsigned >(verify_len),
280- static_cast <unsigned >(verify_ver),
281- static_cast <unsigned long >(verify_crc));
288+ NODE_STORE_LOG (" [NodeStore] save ok len=%u ver=%u crc=%08lX\n " ,
289+ static_cast <unsigned >(verify_len),
290+ static_cast <unsigned >(verify_ver),
291+ static_cast <unsigned long >(verify_crc));
282292 }
283293 }
284294 else
@@ -288,7 +298,7 @@ void NodeStore::save()
288298 prefs.remove (kPersistNodesKeyCrc );
289299 }
290300 prefs.end ();
291- Serial. printf (" [NodeStore] saved=%u\n " , static_cast <unsigned >(entries_.size ()));
301+ NODE_STORE_LOG (" [NodeStore] saved=%u\n " , static_cast <unsigned >(entries_.size ()));
292302}
293303
294304void NodeStore::maybeSave ()
@@ -334,22 +344,22 @@ bool NodeStore::loadFromNvs()
334344 Preferences prefs;
335345 if (!prefs.begin (kPersistNodesNs , false ))
336346 {
337- Serial. printf (" [NodeStore] begin failed ns=%s\n " , kPersistNodesNs );
347+ NODE_STORE_LOG (" [NodeStore] begin failed ns=%s\n " , kPersistNodesNs );
338348 logNvsStats (" begin" , kPersistNodesNs );
339349 return false ;
340350 }
341351 size_t len = prefs.getBytesLength (kPersistNodesKey );
342352 uint8_t ver = prefs.getUChar (kPersistNodesKeyVer , 0 );
343353 bool has_crc = prefs.isKey (kPersistNodesKeyCrc );
344354 uint32_t stored_crc = has_crc ? prefs.getUInt (kPersistNodesKeyCrc , 0 ) : 0 ;
345- Serial. printf (" [NodeStore] blob len=%u ver=%u crc=%08lX has_crc=%u\n " ,
346- static_cast <unsigned >(len),
347- static_cast <unsigned >(ver),
348- static_cast <unsigned long >(stored_crc),
349- has_crc ? 1U : 0U );
355+ NODE_STORE_LOG (" [NodeStore] blob len=%u ver=%u crc=%08lX has_crc=%u\n " ,
356+ static_cast <unsigned >(len),
357+ static_cast <unsigned >(ver),
358+ static_cast <unsigned long >(stored_crc),
359+ has_crc ? 1U : 0U );
350360 if (len == 0 && has_crc)
351361 {
352- Serial. printf (" [NodeStore] stale meta detected, clearing ver/crc\n " );
362+ NODE_STORE_LOG (" [NodeStore] stale meta detected, clearing ver/crc\n " );
353363 prefs.remove (kPersistNodesKeyVer );
354364 prefs.remove (kPersistNodesKeyCrc );
355365 }
@@ -372,16 +382,16 @@ bool NodeStore::loadFromNvs()
372382 if (!has_crc)
373383 {
374384 prefs.end ();
375- Serial. printf (" [NodeStore] missing crc\n " );
385+ NODE_STORE_LOG (" [NodeStore] missing crc\n " );
376386 clear ();
377387 return false ;
378388 }
379389 if (ver != kPersistVersion )
380390 {
381391 prefs.end ();
382- Serial. printf (" [NodeStore] version mismatch stored=%u expected=%u\n " ,
383- static_cast <unsigned >(ver),
384- static_cast <unsigned >(kPersistVersion ));
392+ NODE_STORE_LOG (" [NodeStore] version mismatch stored=%u expected=%u\n " ,
393+ static_cast <unsigned >(ver),
394+ static_cast <unsigned >(kPersistVersion ));
385395 clear ();
386396 return false ;
387397 }
@@ -390,9 +400,9 @@ bool NodeStore::loadFromNvs()
390400 if (calc_crc != stored_crc)
391401 {
392402 prefs.end ();
393- Serial. printf (" [NodeStore] crc mismatch stored=%08lX calc=%08lX\n " ,
394- static_cast <unsigned long >(stored_crc),
395- static_cast <unsigned long >(calc_crc));
403+ NODE_STORE_LOG (" [NodeStore] crc mismatch stored=%08lX calc=%08lX\n " ,
404+ static_cast <unsigned long >(stored_crc),
405+ static_cast <unsigned long >(calc_crc));
396406 clear ();
397407 return false ;
398408 }
@@ -417,13 +427,13 @@ bool NodeStore::loadFromNvs()
417427 }
418428 else if (len > 0 )
419429 {
420- Serial. printf (" [NodeStore] invalid blob size=%u\n " , static_cast <unsigned >(len));
430+ NODE_STORE_LOG (" [NodeStore] invalid blob size=%u\n " , static_cast <unsigned >(len));
421431 prefs.end ();
422432 clear ();
423433 return false ;
424434 }
425435 prefs.end ();
426- Serial. printf (" [NodeStore] loaded=%u\n " , static_cast <unsigned >(entries_.size ()));
436+ NODE_STORE_LOG (" [NodeStore] loaded=%u\n " , static_cast <unsigned >(entries_.size ()));
427437 return true ;
428438}
429439
@@ -491,7 +501,7 @@ bool NodeStore::loadFromSd()
491501 dst.role = src.role ;
492502 entries_.push_back (dst);
493503 }
494- Serial. printf (" [NodeStore] loaded=%u (SD)\n " , static_cast <unsigned >(entries_.size ()));
504+ NODE_STORE_LOG (" [NodeStore] loaded=%u (SD)\n " , static_cast <unsigned >(entries_.size ()));
495505 return true ;
496506}
497507
0 commit comments